- contract TradeOffer(assetRequested: Asset,
- amountRequested: Amount,9 y6 W) H- W7 {' I' E7 H
- seller: Program,
- cancelKey: PublicKey) locks offered {
- clause trade() requires payment: amountRequested of assetRequested {
- lock payment with seller
- unlock offered
- }8 h0 E* ^/ _3 W$ k1 }3 b% U
- clause cancel(sellerSig: Signature) {, b- G$ L* A X7 e& F* ~6 C5 \
- verify checkTxSig(cancelKey, sellerSig)- L* K! g& F" U+ }, g+ P
- unlock offered* \% F9 ?% x, [. m& }
- }" O# e+ C3 ~9 \& u( v3 q' Z" V: p
- }
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
锁定合约
第一步:调用create-account-receiver 生成 control_program* y6 h9 \8 c; f! y
以下是相关代码片段:
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");# C2 B4 K/ s* F1 C8 h2 Q
第二步调用list-pubkeys 获取 pubkey
, o% t3 d6 @5 e# z
7 C* h" a3 q& v. t# w
以下是相关代码片段:7 [) P$ B0 |4 N8 A
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
3 F; w7 u3 H2 q! b
以下是相关代码片段:3 G- H6 X3 r. Q* x% O
- JSONObject param=new JSONObject();
- JSONArray agrs=new JSONArray();
- //合约的四个参数值7 E$ M9 T7 G5 ^( w; I- j+ R
- JSONObject assetParam=new JSONObject();( z) |6 w3 q) z" [$ W1 ?' u
- assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");4 \, E# S4 w9 |. s6 ?
- agrs.put(assetParam);/ _1 T8 P0 }( [1 A' A; p' e
- JSONObject amountParam=new JSONObject();
- amountParam.put("integer",200000000l);( [! t, W, D1 q- C# u
- agrs.put(amountParam);
- JSONObject programParam=new JSONObject(); s. W; m& D1 P& K! r8 y
- programParam.put("string",control_program);
- agrs.put(programParam);; L$ v; ?- Y5 W; X$ s
- JSONObject publicKeyParam=new JSONObject();
- publicKeyParam.put("string",pubkey);5 V1 e) V5 W; d% G# v. h4 O6 u
- agrs.put(publicKeyParam);
- param.put("agrs",agrs);
- param.put("contract","contract TradeOffer(assetRequested: Asset, amountRequested: Amount, seller: Program, cancelKey: PublicKey) locks offered { clause trade() requires payment: amountRequested of assetRequested { lock payment with seller unlock offered } clause cancel(sellerSig: Signature) { verify checkTxSig(cancelKey, sellerSig) unlock offered } }");
- //调用编译合约
- sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
第四步:将program 传入build-transaction接口去build一个交易的到data
/ e3 P! }( j- Y! M2 ]
以下是相关代码片段:
- param=new JSONObject();
- agrs=new JSONArray();
- JSONObject spendAccount=new JSONObject();, G4 p( h" K& T, _* z; m7 d3 k T
- spendAccount.put("account_id","0H757LPD00A02");' e" d0 [$ j5 c H0 @
- spendAccount.put("amount",9909099090000l);
- spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
- spendAccount.put("type","spend_account");) s) Y8 G3 E. D4 D, |& L
- agrs.put(spendAccount);7 d. ^& R/ W9 Q/ C3 M- n
- JSONObject controlAccount=new JSONObject();6 X$ L- N3 p) i; i1 N3 K
- controlAccount.put("control_program",program); H1 v, ]8 A1 Y
- controlAccount.put("amount",9909099090000l);
- controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
- controlAccount.put("type","control_program");
- agrs.put(controlAccount);$ n3 ^8 A' n% ?/ Y& P# j
- JSONObject spendAccount2=new JSONObject();; A0 N- S# I4 G) ^0 \+ x
- spendAccount2.put("account_id","0H757LPD00A02");# i2 n; l9 j7 n* ]
- spendAccount2.put("amount",6000000l);2 y1 N/ U/ ^1 q* }. q; \: {
- spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
- spendAccount2.put("type","spend_account");
- agrs.put(spendAccount2);, A3 l7 v; h% j/ f( O1 e- c
- param.put("actions",agrs);! ]1 O3 q' F+ @# u& K
- param.put("ttl",0);$ V0 K0 [2 e, t! J/ _
- sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
7 N2 ~+ @' a, v& c' u& w
以下是相关代码片段:7 @+ g, g; m$ Z6 }0 W* F: i9 X6 x
param=new JSONObject();5 \; s# V9 H5 Q# m+ D% r6 ^
param.put("password","xxx");7 b7 [ d3 }( |! h" m
param.put("transaction",data);( v. c6 M, n$ z+ j% C. f
sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
第六步:调用submit-transactions提交交易
以下是相关代码片段:
param=new JSONObject();
param.put("raw_transaction",raw_transaction);
sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
解锁/取消合约
首先需要decode出生成合约时候的参数7 m5 W' Q& z8 M4 V# S
调用list-unspent-outputs 获取生成的合约信息获取program# p- H# _* Y5 [# c! y- J6 ]
以下是相关代码片段:
param=new JSONObject();& }5 s, O) I+ N% ?
param.put("id",outputid);
param.put("smart_contract",true);, k! v, z7 V! B
sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");7 {+ ~! d; D. ]6 E" V( G+ Y g1 E
调用decode-program 传入获取生成的合约参数信息 k) \- d3 J0 S1 T
以下是相关代码片段:
param=new JSONObject();
param.put("program",program);5 k# l6 k& d! J$ _
sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");5 u( u& b1 L( R# E/ K
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
取消合约的构造参数如下:6 n3 j" [6 G: I) j# d
# r) {) _ q7 Y7 g8 e" v
- spendAccountUnspentOutput = arguments: [{
- type: 'raw_tx_signature',
- // 生成合约第二步的pubkeylist 详情
- raw_data: {
- derivation_path: pubkeylist.pubkey_infos[0].derivation_path,: \4 y% Y; m1 C: I0 u
- xpub: pubkeylist.root_xpub3 O0 b. k# ]0 Q# y
- }
- }, {
- type: 'data',
- raw_data: {
- // 参数偏移量 在一个合约里是固定的
- value: '13000000'! K, @! J8 C/ D# Z5 d
- }3 F! v- M: x! v# r" l) T, U# K) R
- }],
- output_id: output_id,6 f! c6 `; s/ X& q+ ]; p8 H/ K
- type: 'spend_account_unspent_output'% e- ?( \6 |) S6 @
- }( C) C- c+ O) k3 `, O; g
- const controlAction = {; t2 U9 r/ S" B, m
- type: 'control_program',, b7 ]* r( {9 f. S2 g* _
- amount: 100000000,1 E2 P" B% a! n2 R2 r5 Y
- asset_id: asset_id,
- control_program:control_program
- }
- const gasAction = {& z T9 s j. `) a' Q# r& G
- type: 'spend_account',: `0 y2 V) d! ~
- account_id:account_id,# s4 @) [# i n6 C' o% w' m
- asset_alias: 'BTM',) z. _4 S$ r- C$ m
- amount: 50000000
- }
执行合约的参数构造如下:& }! f& X0 v2 }* z2 }/ u+ l) U
$ ^2 w# E5 _9 e
- const spendAccountUnspentOutput = {
- arguments: [{
- type: 'data',
- raw_data: {0 \! E+ G# [) C! o, T5 d2 p
- // 执行合约输入资产偏移量 B1 v2 w& h8 C r/ r) Y, [6 F" j
- value: '00000000'
- }
- }],
- output_id: output_id, Q/ y) n4 `2 b7 w- k y
- type: 'spend_account_unspent_output'3 ~$ b I8 O* L+ y
- }
- // 合约执行提供的资产5 j9 l* Q* A% ]6 F9 q
- const issueControlAction = {
- control_program: control_program,
- amount: 100000000,
- asset_id: asset_id,. r5 S" f7 G/ ?7 G9 ]' T3 V3 B
- type: 'control_program'
- }( F6 F, o6 f( a9 [
- // 合约执行提供的资产
- const issueSpendAction = {
- account_id: account_id,7 w0 o2 b( {, h; Q
- amount: 100000000,* V* X( S" V% x" V, A
- asset_id: asset_id,0 _5 V8 X5 i5 R/ d
- type: 'spend_account'( {) P; @$ u5 j5 W- I: [0 g
- }
- // 旷工费8 ]1 K3 c/ j8 B1 B6 }
- const gasAction = {
- type: 'spend_account',; e- z: d$ p% ?" _1 a5 o/ `
- account_id: account_id,
- asset_alias: 'BTM',; M: L2 D, H& V, O
- amount: 50000000
- }
- // 合约执行获得资产对象
- const controlAction = {' ]% C# o. ~: l
- type: 'control_program',6 l) V; t$ A( d( Q$ q) b8 p
- amount: 100000000,
- asset_id: asset_id,
- control_program: compileData.control_program
- }
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档2 ^$ \( F2 u+ k% C. A' `7 }
备注- D& A8 z+ h+ N2 F, J7 p
调用比原基于okhttp接口javautil 如下: s+ _2 t9 k% n/ b! r; n
- public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {) o* H1 A3 _" s; L
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");: _- b: g1 g/ _2 l4 ]
- RequestBody body = RequestBody.create(mediaType, bodyStr);
- Request request = new Request.Builder()5 E& M) @4 w, \( U9 [! z
- .url(bytomApiserverUrl+"/"+method)6 ]) e: U2 C: {6 j4 W# b+ Z
- .post(body)4 d! p& f( @% ~+ |8 S0 E) F* ?
- .addHeader("cache-control", "no-cache")
- .addHeader("Connection", "close")6 d" g9 @+ O0 E# }& C6 p0 r
- .build();
- if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
- }else {: v3 ]7 l# I$ h z* M
- byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
- String authHeader = "Basic " + new String(encodedAuth);: Q$ C1 g% [3 u- s" U
- request = new Request.Builder()
- .url(bytomApiserverUrl+"/"+method)
- .post(body)7 Z4 c' e- D6 v9 K" p! g9 D
- .addHeader("authorization", authHeader)
- .addHeader("cache-control", "no-cache")- ^% B. S5 T: V
- .addHeader("Connection", "close")- \4 W$ X- \5 r5 {) x1 x' j, Q
- .build();8 e9 t! n( j. D/ U k6 e
- }
- Response response = client.newCall(request).execute(); G ]+ F7 c% K6 |) n, {
- return response.body().string();+ M- j7 e% H+ v# \ w6 Y
- }
+ S2 G, V" f5 ?# ?+ ~ [
( L7 n# O7 v, o8 Z5 a
比原项目仓库:
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom