- contract TradeOffer(assetRequested: Asset,
- amountRequested: Amount,
- seller: Program,4 P! v2 ]) D8 {. J3 T4 Y9 H
- cancelKey: PublicKey) locks offered {* X/ i& A% s( M& ?, P! G
- clause trade() requires payment: amountRequested of assetRequested {" M# F6 `" t' e9 B" w7 F
- lock payment with seller
- unlock offered6 V; v# E. P8 C- m3 Q
- }$ _& V0 O, J) \5 r1 P7 j: w
- clause cancel(sellerSig: Signature) {
- verify checkTxSig(cancelKey, sellerSig)
- unlock offered
- }# I8 h, g7 Q% n# @' M
- }
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档( N& i, `1 o+ {) F: L
锁定合约
第一步:调用create-account-receiver 生成 control_program
以下是相关代码片段:
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888",""); Y7 P% p; D9 n" @: V: Q2 c
第二步调用list-pubkeys 获取 pubkey
. ]9 `. C+ Y z; g+ J" M: y
以下是相关代码片段:
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");" Q4 }2 x! R& v) l: i2 U7 c P
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序- S6 {- G1 e" \0 @3 J5 l( T
以下是相关代码片段:
- JSONObject param=new JSONObject();
- JSONArray agrs=new JSONArray();
- //合约的四个参数值
- JSONObject assetParam=new JSONObject();
- assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
- agrs.put(assetParam);" X2 L J# I9 k% D
- JSONObject amountParam=new JSONObject();
- amountParam.put("integer",200000000l);
- agrs.put(amountParam);
- JSONObject programParam=new JSONObject();& i2 X/ S, p% [ A
- programParam.put("string",control_program);& s: N, ?& e+ d" F
- agrs.put(programParam);
- JSONObject publicKeyParam=new JSONObject();
- publicKeyParam.put("string",pubkey);
- agrs.put(publicKeyParam);
- param.put("agrs",agrs);8 ?* v8 A% ^+ n1 Y8 R% u
- 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 } }");
- //调用编译合约/ g, |/ c; L# P& K7 i# m
- sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
第四步:将program 传入build-transaction接口去build一个交易的到data/ Y9 a1 [( o; f% |5 m
; t$ b% p$ n. s+ H0 j+ D/ r* q0 {
以下是相关代码片段:( G1 q; E: {! z7 W/ G( H
- param=new JSONObject(); q8 l1 Z5 P+ |; I" i% V, }
- agrs=new JSONArray();
- JSONObject spendAccount=new JSONObject();# U0 }6 M: D6 ?8 n3 x5 Q
- spendAccount.put("account_id","0H757LPD00A02");& s0 L! C4 Y9 o9 ~
- spendAccount.put("amount",9909099090000l);% I3 _: o4 p/ k1 l5 w! g. [- C
- spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
- spendAccount.put("type","spend_account");7 ]1 t% n0 E* U' [0 e
- agrs.put(spendAccount);
- JSONObject controlAccount=new JSONObject();" R- ?0 ]4 o3 d1 U. x& z: n; L
- controlAccount.put("control_program",program);
- controlAccount.put("amount",9909099090000l);
- controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");. V) x; z/ c! k- m4 e4 }, u
- controlAccount.put("type","control_program");
- agrs.put(controlAccount);
- JSONObject spendAccount2=new JSONObject();
- spendAccount2.put("account_id","0H757LPD00A02");
- spendAccount2.put("amount",6000000l);1 A3 X: D. [; Z& u4 m ~% ]/ U
- spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
- spendAccount2.put("type","spend_account");0 c9 R2 ^' {. E; F; l' y
- agrs.put(spendAccount2);
- param.put("actions",agrs);7 C- H1 ^4 {% m0 x' R0 b9 F
- param.put("ttl",0);
- sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
7 @9 N x, F9 u+ n
) o$ q( X) n6 k' U: ~2 o- g4 r2 G
以下是相关代码片段:& k- E8 k$ h9 M. E# m
param=new JSONObject();
param.put("password","xxx");# ]) | P8 n9 l i. M
param.put("transaction",data);6 V: O" ]1 Z" b9 r# q% A; ?
sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
第六步:调用submit-transactions提交交易& F% S7 \# D* R& L+ h% H' X: o
以下是相关代码片段:
param=new JSONObject();
param.put("raw_transaction",raw_transaction); R7 u' }+ G% C. L- n
sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
解锁/取消合约! d/ O* C1 H1 W, g8 |& X
首先需要decode出生成合约时候的参数
调用list-unspent-outputs 获取生成的合约信息获取program. h* k- U' r3 T+ L- ~ }
以下是相关代码片段:9 S8 N. h5 Z; g) V5 M
param=new JSONObject();/ u1 ]1 O$ t1 y7 b4 z0 ~' x
param.put("id",outputid);) G0 l: _5 A5 i& q, b
param.put("smart_contract",true);$ J1 \1 c+ E9 S4 s4 \! q
sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");
调用decode-program 传入获取生成的合约参数信息( Y" |- w1 B9 f' `* }9 w
以下是相关代码片段:; q7 O6 O0 m. s- \
param=new JSONObject();
param.put("program",program);
sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可 |3 M* d8 w: \, M# G$ R' V0 ]4 A
取消合约的构造参数如下:% Z0 l" o6 W3 e# V, w
- spendAccountUnspentOutput = arguments: [{
- type: 'raw_tx_signature',! W% v/ l. v6 Z3 v9 w# y
- // 生成合约第二步的pubkeylist 详情
- raw_data: {
- derivation_path: pubkeylist.pubkey_infos[0].derivation_path,
- xpub: pubkeylist.root_xpub7 C& b7 N0 y5 d- U2 x
- }& b, Y2 U: l0 g* v, j( s& E$ ?
- }, {
- type: 'data',
- raw_data: {
- // 参数偏移量 在一个合约里是固定的
- value: '13000000'/ `* _* k5 p+ D; q7 |$ A
- }
- }],, C$ C0 p+ T# x2 r2 Z# p
- output_id: output_id,
- type: 'spend_account_unspent_output'
- }- ?, x, a3 ~# `4 `7 e* {! j
- const controlAction = {
- type: 'control_program',+ P7 [, P) F5 ? i# S) ^9 Q6 S
- amount: 100000000,
- asset_id: asset_id,3 u+ @) L9 o+ n9 \5 f1 J
- control_program:control_program
- }6 ~; F9 P( y" p
- const gasAction = {
- type: 'spend_account',' O8 L4 \$ ]+ W! G3 X# x! W
- account_id:account_id,3 z9 K3 b8 z, N+ Y0 s
- asset_alias: 'BTM',
- amount: 50000000
- }
执行合约的参数构造如下:' B4 P, y8 r: X8 K6 J3 f
- const spendAccountUnspentOutput = {& X( J4 q) \# m1 [6 K
- arguments: [{
- type: 'data',2 L# L) N6 f' g: E( M
- raw_data: {
- // 执行合约输入资产偏移量/ N7 s. \+ e. O. S6 T5 m/ b# ^6 K
- value: '00000000'
- }1 @2 N5 l3 m% k+ _; P& e
- }],8 R j2 g$ v- f
- output_id: output_id,
- type: 'spend_account_unspent_output'- N) N" d( X( b$ E$ g
- }
- // 合约执行提供的资产) v% j- v- f0 a8 A
- const issueControlAction = {
- control_program: control_program,
- amount: 100000000, B [, o& X0 }* `- i
- asset_id: asset_id,( R; P( m0 n; Y( }
- type: 'control_program'
- }
- // 合约执行提供的资产
- const issueSpendAction = {# R4 Y: K0 K) N
- account_id: account_id,
- amount: 100000000,+ M( B a" q& u5 i
- asset_id: asset_id,0 K0 ^# e0 U. s+ g$ a1 s
- type: 'spend_account'* f4 L2 M7 B$ a
- }( r3 l: d1 A8 T% U
- // 旷工费! @# _* o+ ^) N( w8 a
- const gasAction = {( Z# b$ ?3 n6 M$ H: K. n/ q- T
- type: 'spend_account',
- account_id: account_id,
- asset_alias: 'BTM',
- amount: 500000008 U% B- I5 Z: `$ P1 v! B: Y
- }
- // 合约执行获得资产对象
- const controlAction = {
- type: 'control_program',% Y) z0 z: [/ [3 G/ A6 o
- amount: 100000000,, T6 J& r5 h. ~/ N4 c% H
- asset_id: asset_id,0 z1 d( T1 I, W0 K
- control_program: compileData.control_program
- }
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档9 W# ]9 V6 n, W. e$ h6 S- S- g
备注
调用比原基于okhttp接口javautil 如下:) p: O: V. r4 o9 s
- public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");! q/ p0 V1 R* P% P% ~+ z; z* Y: |( T
- RequestBody body = RequestBody.create(mediaType, bodyStr);
- Request request = new Request.Builder()0 o' h J8 o. L" ^4 Q
- .url(bytomApiserverUrl+"/"+method)5 D. O. m+ z! g
- .post(body)
- .addHeader("cache-control", "no-cache"). I' \7 O5 f& m5 ]! I; t, v7 w5 t
- .addHeader("Connection", "close")
- .build();: J0 a) o8 `+ Q6 _
- if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){6 ` K: O0 ~2 g, {4 v
- }else {
- byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));' v' f2 w {7 {% v5 A6 V
- String authHeader = "Basic " + new String(encodedAuth);
- request = new Request.Builder()
- .url(bytomApiserverUrl+"/"+method)4 X0 X O3 {/ I" b# B* h
- .post(body)( R, }% |4 ?0 e* p# w$ q! X
- .addHeader("authorization", authHeader)
- .addHeader("cache-control", "no-cache")4 V# c: \3 F4 J
- .addHeader("Connection", "close")$ l( @: _; @. { c
- .build();
- }4 C# y! l3 @* R. B
- Response response = client.newCall(request).execute();
- return response.body().string();+ B8 p6 G* ]; @5 s! [' y
- }
5 Z0 H2 b6 G7 }
, i' M7 h% l# \. b h
比原项目仓库:; O1 K/ O" _8 F
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom