- contract TradeOffer(assetRequested: Asset,( ], R5 `4 h8 v& x
- amountRequested: Amount,
- seller: Program,% U; c D" `) \5 Q
- cancelKey: PublicKey) locks offered {! k& [; u7 ~8 L2 p4 ~. \
- clause trade() requires payment: amountRequested of assetRequested {
- lock payment with seller" X8 Q2 a [$ {, X$ m
- unlock offered
- }: g, \8 |7 i+ v1 u; `
- clause cancel(sellerSig: Signature) {
- verify checkTxSig(cancelKey, sellerSig). _8 C5 i3 n! F' e
- unlock offered
- }
- }
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档" R1 Q* c8 K, w* E/ R. B0 U
锁定合约- y8 A2 r! _: L0 _8 M7 p1 Q
第一步:调用create-account-receiver 生成 control_program
以下是相关代码片段:/ L8 X- d) p6 B |0 }
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");" ~- B) Z: N2 y# o" D& g
第二步调用list-pubkeys 获取 pubkey
, l, i/ P6 Q' O% k# H! y" `8 _& l
4 B; k4 {& b$ x
以下是相关代码片段:; r p# z9 [5 L- l* W
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
* J& }2 n0 E, H( Y1 O$ O. b
6 S$ N0 t- \& O% w0 f4 ?7 \; `/ f
以下是相关代码片段:9 j) |" W8 ~! X! v; K
- JSONObject param=new JSONObject();
- JSONArray agrs=new JSONArray();9 j# K4 G' }/ H8 \" p
- //合约的四个参数值
- JSONObject assetParam=new JSONObject();
- assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
- agrs.put(assetParam);
- JSONObject amountParam=new JSONObject();
- amountParam.put("integer",200000000l);
- agrs.put(amountParam);
- JSONObject programParam=new JSONObject();
- programParam.put("string",control_program);
- agrs.put(programParam);
- JSONObject publicKeyParam=new JSONObject();
- publicKeyParam.put("string",pubkey);: t3 ]2 W7 W l. d
- agrs.put(publicKeyParam);/ n T w! n5 _8 }& t0 o
- param.put("agrs",agrs);& C8 V; `* v" H+ s; h( g
- 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& t9 V5 G5 o( M- t1 |% ~5 R
0 o7 k$ K+ V1 O' O+ Z Y3 i! D9 l, }
以下是相关代码片段:2 }: {* D' j4 u O
- param=new JSONObject();- |9 K4 E' h4 q1 `+ y! C0 p
- agrs=new JSONArray();7 n- U0 f, S8 T$ p$ @% N% }
- JSONObject spendAccount=new JSONObject();
- spendAccount.put("account_id","0H757LPD00A02");
- spendAccount.put("amount",9909099090000l);& P* z: A" `% q" S
- spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
- spendAccount.put("type","spend_account");, d* L# w4 [3 [
- agrs.put(spendAccount);
- JSONObject controlAccount=new JSONObject();+ @5 z. f' i1 Z' f2 z- Q+ e$ S! z
- controlAccount.put("control_program",program);# L! u% n% M$ j' e' B+ ]+ k7 x
- controlAccount.put("amount",9909099090000l);
- controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");8 B- V- V. `" z
- controlAccount.put("type","control_program");
- agrs.put(controlAccount);
- JSONObject spendAccount2=new JSONObject();( g7 }+ f/ {! N' E, ~3 b
- spendAccount2.put("account_id","0H757LPD00A02");
- spendAccount2.put("amount",6000000l);
- spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
- spendAccount2.put("type","spend_account");- g5 m5 b- |4 M4 u
- agrs.put(spendAccount2);4 T& b+ ~% g" g5 \4 l6 F+ i2 T
- param.put("actions",agrs); r. _/ k# {4 s6 F% R" P7 B
- param.put("ttl",0);
- sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
' G9 N8 P% p# L* C6 l1 I% Q9 @
以下是相关代码片段:
param=new JSONObject();
param.put("password","xxx");
param.put("transaction",data);) D* a: x) ~( R3 M+ z$ p* H
sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
第六步:调用submit-transactions提交交易6 H3 T4 U1 [: J, j
- j9 l- Y- _, I0 x' j
以下是相关代码片段:7 b! j4 ?1 ?+ ]% M' }' ~: Z) \% X
param=new JSONObject();
param.put("raw_transaction",raw_transaction);/ ~5 I( M" z. ?) G# _
sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
解锁/取消合约
首先需要decode出生成合约时候的参数
调用list-unspent-outputs 获取生成的合约信息获取program
$ T; J: H2 A- \3 t3 u' t" ?1 i
以下是相关代码片段:
param=new JSONObject();
param.put("id",outputid);3 J8 P* g) T; {6 i: N$ {
param.put("smart_contract",true);, b0 k$ s4 Q, q" \" G, @. m
sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");3 c" Q6 K) c0 E
调用decode-program 传入获取生成的合约参数信息
以下是相关代码片段:
param=new JSONObject();
param.put("program",program);
sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)/ `0 }1 K) C$ C# e! V- s
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可6 _+ a! W+ v0 K0 m7 c
取消合约的构造参数如下:8 T4 v1 z% J/ A: {
- spendAccountUnspentOutput = arguments: [{
- type: 'raw_tx_signature',; m/ Y, z* t1 }
- // 生成合约第二步的pubkeylist 详情
- raw_data: {
- derivation_path: pubkeylist.pubkey_infos[0].derivation_path,! z& Y* I, P# x4 u2 T' e
- xpub: pubkeylist.root_xpub
- }* Z6 W* f9 g# _- [6 v& X1 o
- }, {/ `; b- K* o/ G# Y, N! u% z
- type: 'data',5 w' [5 e/ Y% P+ e( K
- raw_data: {; B2 o- g# q5 p% Q2 j
- // 参数偏移量 在一个合约里是固定的 0 a" t: y( `% l
- value: '13000000'0 g: R. N( J; v7 J4 J
- }
- }],; d4 @5 z, Y8 O. ?- h2 Z' c( _
- output_id: output_id,) [9 D+ F$ }7 @" @! s
- type: 'spend_account_unspent_output'
- }
- const controlAction = {
- type: 'control_program',9 s: [! v% ~& D2 K1 z7 f& Z+ U
- amount: 100000000,
- asset_id: asset_id,7 R+ @: T0 Y, G- B* v9 V
- control_program:control_program
- }. V3 A2 r" J0 G6 _, X
- const gasAction = {6 w) K$ e" s" r3 U+ R# w
- type: 'spend_account',: K- Q9 X% N8 D9 @9 Q
- account_id:account_id,) i! v4 z4 N; s( \. j
- asset_alias: 'BTM',2 t* v6 x9 M# @) z4 f
- amount: 50000000
- }
执行合约的参数构造如下:
- const spendAccountUnspentOutput = {" e: E9 m# J% o
- arguments: [{
- type: 'data',
- raw_data: {
- // 执行合约输入资产偏移量9 O9 {4 ]6 h* t. g) ?8 ?; x/ R
- value: '00000000'
- }
- }],
- output_id: output_id,
- type: 'spend_account_unspent_output'
- }
- // 合约执行提供的资产
- const issueControlAction = {! Z/ m/ s4 s5 Y: R4 R+ B" m$ r1 O+ u7 m1 R
- control_program: control_program,4 x( R/ C2 E+ C* [8 ]
- amount: 100000000,2 I' j; C* {1 b7 j" m% @5 B
- asset_id: asset_id,1 T# L/ n" u# h( Q0 F( z
- type: 'control_program'6 z+ @3 N+ j0 \* D
- }
- // 合约执行提供的资产! d7 B& L$ }# H4 d
- const issueSpendAction = {& Z, W: n3 {1 K
- account_id: account_id,
- amount: 100000000,6 Y8 [+ v6 H. P, _/ h; A
- asset_id: asset_id,
- type: 'spend_account'7 g' W! o) Q% i# O+ l( k3 p* k6 C
- }/ f$ `. |3 D, a: @1 B+ s
- // 旷工费4 h3 E* o0 ]# r! Y( r E
- const gasAction = {6 \/ G! a: w$ U
- type: 'spend_account',0 H& x z3 L c
- account_id: account_id,2 [7 B& x7 R: S' E! Y" ?# |( n5 H- X
- asset_alias: 'BTM',9 c7 J; _7 A g5 R
- amount: 50000000
- }9 b" R, r1 R( c! S O$ |5 y
- // 合约执行获得资产对象
- const controlAction = {
- type: 'control_program',' ^- [! I( f2 u
- amount: 100000000,7 r0 a+ c6 L, u5 S3 @' T
- asset_id: asset_id,6 g. t5 _6 Z# N3 `
- control_program: compileData.control_program
- }
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档
备注# T- r1 C6 R; ?6 v
调用比原基于okhttp接口javautil 如下:1 X$ j5 v) j7 }
- public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {8 h5 S4 d5 Y& |) M
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, bodyStr);
- Request request = new Request.Builder(); j0 c. }. f: t ~, }
- .url(bytomApiserverUrl+"/"+method)
- .post(body)6 c5 ]! {5 w: [* q* g) f Q
- .addHeader("cache-control", "no-cache")
- .addHeader("Connection", "close")
- .build();: A7 e/ @0 j0 P0 Q0 D2 J4 L
- if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
- }else {! c* n" E7 ?2 W/ P$ H3 F4 ?' Q
- byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));9 _% I: ]* }/ {
- String authHeader = "Basic " + new String(encodedAuth);
- request = new Request.Builder()
- .url(bytomApiserverUrl+"/"+method)
- .post(body)/ g$ f) t8 t4 T5 t3 }6 ~
- .addHeader("authorization", authHeader)4 Z0 N! `0 D$ @9 w+ C' x, H% k
- .addHeader("cache-control", "no-cache") g) x* n3 Z0 ]8 [
- .addHeader("Connection", "close")
- .build();
- }; H* N. l2 m( H9 s6 d; m5 }. R
- Response response = client.newCall(request).execute();
- return response.body().string();- j. s% I$ E2 z; t+ x8 a
- }
# j5 R5 ]: h3 a: u
比原项目仓库:$ H! X: Y! ]4 _# Y: R# u" c8 g0 k4 @0 V
Github地址:https://github.com/Bytom/bytom" x/ _! n! {( U- W7 Y2 q1 n/ {
Gitee地址:https://gitee.com/BytomBlockchain/bytom