- contract TradeOffer(assetRequested: Asset,
- amountRequested: Amount,5 E6 ], a. m( W% d5 e) j
- seller: Program,
- cancelKey: PublicKey) locks offered {
- clause trade() requires payment: amountRequested of assetRequested {
- lock payment with seller/ e! L7 T2 _; o( n) k
- unlock offered
- }
- clause cancel(sellerSig: Signature) {& R4 m8 m% G0 f6 @3 [9 J) D1 v; t _/ N
- verify checkTxSig(cancelKey, sellerSig); J+ Z7 f+ l9 {8 K) y
- unlock offered
- }7 C; Y% @+ f9 `$ F" ^/ g( p5 ]
- }
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
锁定合约0 J% P# x2 L- w
第一步:调用create-account-receiver 生成 control_program
# {+ V& v2 @/ U7 }
以下是相关代码片段:
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");
第二步调用list-pubkeys 获取 pubkey1 |, y+ A( i' ~9 S) W
以下是相关代码片段:0 ?$ J7 u; p7 F. L6 w
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序( D2 n* A1 q4 @' O1 {0 e
1 O4 N0 {; t$ D
以下是相关代码片段:2 `9 [, N2 j+ D# ]: _# U, ^7 j7 r
- JSONObject param=new JSONObject();
- JSONArray agrs=new JSONArray();* T9 k% F1 a8 C4 F. x8 ~
- //合约的四个参数值, r9 E5 O, g$ Q: q
- JSONObject assetParam=new JSONObject();9 R& [2 \9 I! _1 `- j
- assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");/ u, {7 y0 ]: G( [
- agrs.put(assetParam);
- JSONObject amountParam=new JSONObject();6 a7 d6 t" e5 M1 t
- amountParam.put("integer",200000000l);
- agrs.put(amountParam);
- JSONObject programParam=new JSONObject();
- programParam.put("string",control_program);6 N3 a, `, Y7 s, N# b( s* b
- agrs.put(programParam);* @' Q( D2 o9 J6 |
- JSONObject publicKeyParam=new JSONObject();. X2 X: O- J1 ^# G {* m
- publicKeyParam.put("string",pubkey);
- agrs.put(publicKeyParam);1 W9 K) `% h* }, x4 M
- 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 } }");# R7 H1 X' {0 |, [ y2 P2 Z
- //调用编译合约
- sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
第四步:将program 传入build-transaction接口去build一个交易的到data3 D; q+ o* Q3 P. d
+ A7 O8 }+ z% d
以下是相关代码片段:& @+ h- g5 Z* h7 J4 }
- param=new JSONObject();( C3 ~1 J0 J0 v% \9 N& C
- agrs=new JSONArray();
- JSONObject spendAccount=new JSONObject();% P& K- z: \& N/ G
- spendAccount.put("account_id","0H757LPD00A02");
- spendAccount.put("amount",9909099090000l);
- spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
- spendAccount.put("type","spend_account");
- agrs.put(spendAccount);
- JSONObject controlAccount=new JSONObject();
- controlAccount.put("control_program",program);
- controlAccount.put("amount",9909099090000l);. }0 e$ g' v. A# s( C
- controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");1 h+ O4 c C0 Z$ x$ u
- controlAccount.put("type","control_program");
- agrs.put(controlAccount);/ n' r8 R Y/ V6 U$ ~) d+ g+ G+ F
- JSONObject spendAccount2=new JSONObject();4 N. m" j3 U! y% O1 X
- spendAccount2.put("account_id","0H757LPD00A02");
- spendAccount2.put("amount",6000000l);
- spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
- spendAccount2.put("type","spend_account");8 G7 L/ i: }& G1 O' L
- agrs.put(spendAccount2);( N' r/ } P7 t: _. {' q- s
- param.put("actions",agrs);
- param.put("ttl",0);5 }& c4 K! ^5 E; r0 Q5 C
- sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction* D5 P" l6 ?+ z& U- a& s% ]2 ]# E
! v6 |* P/ U& m
/ Z( B; B# j7 J4 G5 d; F
以下是相关代码片段:% q, u6 @7 m- {" J. B8 o2 O
param=new JSONObject();, P% {0 [: {0 A- Z" g
param.put("password","xxx");
param.put("transaction",data);& _( U/ Q2 K: u: Q: O0 b
sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
第六步:调用submit-transactions提交交易# c, _6 ?( s3 C1 p$ V1 }/ [; Q& L# g* |
" ?: _6 |+ l3 Q# W# `
以下是相关代码片段:
param=new JSONObject();
param.put("raw_transaction",raw_transaction);
sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
解锁/取消合约
首先需要decode出生成合约时候的参数6 O6 U0 r* q+ ^8 k/ {* q% G( N
调用list-unspent-outputs 获取生成的合约信息获取program
以下是相关代码片段:
param=new JSONObject();( y, h1 L5 G$ | @/ U2 |
param.put("id",outputid);
param.put("smart_contract",true);
sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");' H; ~* \ ^* {/ T- \
调用decode-program 传入获取生成的合约参数信息 L8 b8 k: c" @9 h& s
以下是相关代码片段:
param=new JSONObject();
param.put("program",program);
sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可" _8 Y0 v& V; `* U. A% `+ N2 K
取消合约的构造参数如下:; t3 _1 \0 x( Q4 v" |
- spendAccountUnspentOutput = arguments: [{3 |- K1 h7 c( c; v$ n( `
- type: 'raw_tx_signature',3 b2 D; P6 S+ T5 x( d
- // 生成合约第二步的pubkeylist 详情' K4 ~& ?( O, T4 i8 V7 \5 [3 k4 {
- raw_data: {
- derivation_path: pubkeylist.pubkey_infos[0].derivation_path,2 e/ A$ E4 q2 Y' V6 Y: v, p2 }
- xpub: pubkeylist.root_xpub
- }
- }, {6 d2 t7 ]7 B7 ^$ y' k+ ^' L2 n; h' I5 @
- type: 'data',
- raw_data: {
- // 参数偏移量 在一个合约里是固定的 ) \7 h4 W' v; Y6 w; U& i$ @8 P& O2 U
- value: '13000000'& |$ I x& Q# Z: |
- }
- }],
- output_id: output_id,- Y! O, Z4 B2 T2 n2 F2 b. O; ?
- type: 'spend_account_unspent_output'
- }
- const controlAction = {9 K; J7 O1 K! n7 p0 ?
- type: 'control_program',
- amount: 100000000,5 r+ [ s4 f+ H& S$ Q0 |- W: |
- asset_id: asset_id,% t3 ~' l0 u% l" q+ _# `2 R
- control_program:control_program
- }+ S* `7 u4 ?& X6 y: G
- const gasAction = {
- type: 'spend_account',6 ~" c+ {* N+ a
- account_id:account_id,0 G0 A) \9 d+ p$ g: V% f! i
- asset_alias: 'BTM',
- amount: 50000000
- }
执行合约的参数构造如下:
- const spendAccountUnspentOutput = {
- arguments: [{
- type: 'data',2 A8 u/ s1 W, z7 w2 T, q+ K
- raw_data: {# [! b; m6 v% X L" O- g, k, Q
- // 执行合约输入资产偏移量( E8 w; a# m4 m
- value: '00000000'
- }& _+ {5 e; F' J% P
- }],: w, l5 M5 z5 U/ D' G4 S
- output_id: output_id,
- type: 'spend_account_unspent_output'
- }
- // 合约执行提供的资产/ O ?/ F7 F, A7 `' ?! R# m+ L
- const issueControlAction = {6 O2 a+ i/ }* T7 p6 \! \, P
- control_program: control_program,
- amount: 100000000,1 M: d% w7 ]1 b9 `
- asset_id: asset_id,, q5 M" w; ^! N8 [ l4 s
- type: 'control_program'
- }* I# R4 e% V" d0 h- C. V M% O
- // 合约执行提供的资产. Q; _$ F3 O/ p4 `3 f! c5 i2 R1 Y
- const issueSpendAction = {
- account_id: account_id,: p" f. }8 {. e7 R; P+ \
- amount: 100000000,
- asset_id: asset_id,
- type: 'spend_account'
- }% i m7 Z- ^8 }: s6 y* N! X
- // 旷工费7 K6 h* o) V7 m
- const gasAction = {
- type: 'spend_account',
- account_id: account_id,
- asset_alias: 'BTM',1 F" f; S; i e4 I" [3 ^( _
- amount: 50000000, ^/ C2 f' o( G4 D% i8 y
- }
- // 合约执行获得资产对象! c1 T- D$ J9 b$ c `9 e7 ~
- const controlAction = {
- type: 'control_program',
- amount: 100000000,
- asset_id: asset_id,
- control_program: compileData.control_program
- }
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档6 s; T4 ]2 `& M B% b o
备注
调用比原基于okhttp接口javautil 如下:
- public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, bodyStr);1 i% |1 M& u' {" a/ R: p
- Request request = new Request.Builder()
- .url(bytomApiserverUrl+"/"+method)
- .post(body)
- .addHeader("cache-control", "no-cache")
- .addHeader("Connection", "close")
- .build();
- if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){. {' [7 w6 g0 N7 }% o) E# |
- }else {
- byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));9 a9 ]- Y' k) `* C. }. R' M
- String authHeader = "Basic " + new String(encodedAuth); x$ F" ?6 U4 N* @
- request = new Request.Builder()
- .url(bytomApiserverUrl+"/"+method)
- .post(body)' g7 `! H% n+ s, @
- .addHeader("authorization", authHeader)
- .addHeader("cache-control", "no-cache")
- .addHeader("Connection", "close")2 r5 x) u$ a3 g0 M; u, }
- .build();7 |( a ~! J7 i# @% ?" _
- }# D0 E! f- M8 `9 z) y6 |1 g- y8 D
- Response response = client.newCall(request).execute();# C. m/ g$ W0 u
- return response.body().string();
- }
9 L. k8 L- E6 Q) W
比原项目仓库:
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom