Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:+ n/ T$ V8 T! [# W: D  Q' `
  1. contract TradeOffer(assetRequested: Asset,
    . O# m  p7 T6 \* {& ^" [6 ~
  2.                     amountRequested: Amount,9 y6 W) H- W7 {' I' E7 H
  3.                     seller: Program,
    . ?9 S6 r0 K7 F) v
  4.                     cancelKey: PublicKey) locks offered {
    2 c0 I- b. \4 A
  5.   clause trade() requires payment: amountRequested of assetRequested {
    ) e8 H# g  y- f* B& f. m; Y6 W
  6.     lock payment with seller
    : p* e' l. a5 p. ~  U5 B+ c5 O
  7.     unlock offered
    4 n  y4 c  \9 E' g% F
  8.   }8 h0 E* ^/ _3 W$ k1 }3 b% U
  9.   clause cancel(sellerSig: Signature) {, b- G$ L* A  X7 e& F* ~6 C5 \
  10.     verify checkTxSig(cancelKey, sellerSig)- L* K! g& F" U+ }, g+ P
  11.     unlock offered* \% F9 ?% x, [. m& }
  12.   }" O# e+ C3 ~9 \& u( v3 q' Z" V: p
  13. }
复制代码

+ {) n4 q% H& d) s: S5 V$ w导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
% s& I3 v; a$ e. n- k; x锁定合约
1 W: T0 y8 E8 R! P+ f第一步:调用create-account-receiver 生成 control_program* y6 h9 \8 c; f! y

. M+ R' r9 T4 T0 b+ @以下是相关代码片段:
- b4 X% \: ]. y% l' F) N" c0 ]. {sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");# C2 B4 K/ s* F1 C8 h2 Q
第二步调用list-pubkeys 获取 pubkey
0 h4 e5 q" }0 Q, w) q" K4 A, 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","");
) z: G( ]% P) h6 N第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
' L0 J' x. z3 m' s* O
3 E% ~/ f( G/ I# m4 a3 F; w7 u3 H2 q! b
以下是相关代码片段:3 G- H6 X3 r. Q* x% O
           
  1. JSONObject param=new JSONObject();
    . h/ `6 h/ [* D/ ^  F6 _" ]
  2.             JSONArray agrs=new JSONArray();
    7 N4 r! `& Q0 Q! v
  3.             //合约的四个参数值7 E$ M9 T7 G5 ^( w; I- j+ R
  4.             JSONObject assetParam=new JSONObject();( z) |6 w3 q) z" [$ W1 ?' u
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");4 \, E# S4 w9 |. s6 ?
  6.             agrs.put(assetParam);/ _1 T8 P0 }( [1 A' A; p' e
  7.             JSONObject amountParam=new JSONObject();
    & t  L. U: [9 T. |! K' X
  8.             amountParam.put("integer",200000000l);( [! t, W, D1 q- C# u
  9.             agrs.put(amountParam);
    3 f* R8 h% p) d; t
  10.             JSONObject programParam=new JSONObject();  s. W; m& D1 P& K! r8 y
  11.             programParam.put("string",control_program);
    2 G8 o' ?6 T* t* ?+ ^! ?9 j6 Y
  12.             agrs.put(programParam);; L$ v; ?- Y5 W; X$ s
  13.             JSONObject publicKeyParam=new JSONObject();
    * w0 F9 J6 ?( `6 i+ X9 a% j1 ?; h. f
  14.             publicKeyParam.put("string",pubkey);5 V1 e) V5 W; d% G# v. h4 O6 u
  15.             agrs.put(publicKeyParam);
    ' Z/ |6 J! y: Z8 Q; W
  16.             param.put("agrs",agrs);
    4 D; E1 `1 l5 ?
  17.             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 } }");
    / n4 ]% J4 p% D1 V6 x7 T' R
  18.             //调用编译合约
    - r. [3 \4 s( n8 j$ E# @2 W* R
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码

# Z9 ?1 }) x" A/ L$ h4 t4 N第四步:将program 传入build-transaction接口去build一个交易的到data
$ X4 P8 _+ r& X8 S
' x/ D$ t. x2 b/ g8 v/ e3 P! }( j- Y! M2 ]
以下是相关代码片段:
& c% R( B2 \  R% v! c0 K           
  1. param=new JSONObject();
    4 a5 j3 P$ \% @9 p
  2.             agrs=new JSONArray();
    # W+ n  b, s$ X2 _4 r% W# Z
  3.             JSONObject spendAccount=new JSONObject();, G4 p( h" K& T, _* z; m7 d3 k  T
  4.             spendAccount.put("account_id","0H757LPD00A02");' e" d0 [$ j5 c  H0 @
  5.             spendAccount.put("amount",9909099090000l);
    $ F/ T: {- _2 z
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    " _+ P7 \; x9 {# B9 S# U
  7.             spendAccount.put("type","spend_account");) s) Y8 G3 E. D4 D, |& L
  8.             agrs.put(spendAccount);7 d. ^& R/ W9 Q/ C3 M- n
  9.             JSONObject controlAccount=new JSONObject();6 X$ L- N3 p) i; i1 N3 K
  10.             controlAccount.put("control_program",program);  H1 v, ]8 A1 Y
  11.             controlAccount.put("amount",9909099090000l);
    % c5 G" M: x  R
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    - C) F# s. p- x& M! \5 M" f
  13.             controlAccount.put("type","control_program");
    3 ^' l7 ~, a% \. q* V
  14.             agrs.put(controlAccount);$ n3 ^8 A' n% ?/ Y& P# j
  15.             JSONObject spendAccount2=new JSONObject();; A0 N- S# I4 G) ^0 \+ x
  16.             spendAccount2.put("account_id","0H757LPD00A02");# i2 n; l9 j7 n* ]
  17.             spendAccount2.put("amount",6000000l);2 y1 N/ U/ ^1 q* }. q; \: {
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    % ]! R0 K( V3 G! ?3 p: S! I) P! z
  19.             spendAccount2.put("type","spend_account");
    + p" A7 a: w; C$ A  J, `0 X
  20.             agrs.put(spendAccount2);, A3 l7 v; h% j/ f( O1 e- c
  21.             param.put("actions",agrs);! ]1 O3 q' F+ @# u& K
  22.             param.put("ttl",0);$ V0 K0 [2 e, t! J/ _
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

7 Z' C2 p5 n" X. W& ?. W8 d第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
9 V, b( ]4 p; r  p  B
2 }/ ^: y1 N$ }" Z' w; a1 \* _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","");
0 b# w" `3 A+ M6 w0 O7 @5 z! Q) p第六步:调用submit-transactions提交交易
9 F+ v% K4 ]# `+ Z) i" I
0 h( ^! ~- @3 y6 V" G以下是相关代码片段:
5 Q- u) w& W$ S            param=new JSONObject();
' {6 C3 _# }- L  d! k            param.put("raw_transaction",raw_transaction);
/ [4 R% `$ P: n' s7 |5 f1 b: o5 S            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
1 I1 v* x/ k$ }解锁/取消合约
; c# @1 G0 y- Z( c( O! x2 ]3 e首先需要decode出生成合约时候的参数7 m5 W' Q& z8 M4 V# S
调用list-unspent-outputs 获取生成的合约信息获取program# p- H# _* Y5 [# c! y- J6 ]

* `$ L+ G6 E, A4 v* u. c$ ]以下是相关代码片段:
2 a& b+ E' L6 I9 H) I* uparam=new JSONObject();& }5 s, O) I+ N% ?
        param.put("id",outputid);
$ F7 d% z3 |) n& X        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

6 m' L" ^) ]$ L以下是相关代码片段:
" y* c' E( |' v$ qparam=new JSONObject();
2 k. ^6 C  m$ o% B+ U        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出来的为值是逆序的(后续会有文章详细介绍)
* M  H0 |; [  A+ J解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
2 p+ h) G, \1 X, {7 w+ ]取消合约的构造参数如下:6 n3 j" [6 G: I) j# d
# r) {) _  q7 Y7 g8 e" v
            
  1.   spendAccountUnspentOutput = arguments: [{
    9 q* p8 z, K* D& H4 q' C
  2.                   type: 'raw_tx_signature',
    ' b7 n6 e$ r7 d: z/ z+ |
  3.                   // 生成合约第二步的pubkeylist 详情
    ; U; j( Q5 Y. L/ e- f! B" Y
  4.                   raw_data: {
    # r+ ]8 t6 u9 {1 D
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,: \4 y% Y; m1 C: I0 u
  6.                     xpub: pubkeylist.root_xpub3 O0 b. k# ]0 Q# y
  7.                   }
    , v( _- y8 L$ P6 e0 ~/ Y
  8.                 }, {
    2 g6 J! i$ c) A* [
  9.                   type: 'data',
    6 s; k7 P4 X7 u0 u2 L* q
  10.                   raw_data: {
    , D% b, X2 T/ Y9 R
  11.                     // 参数偏移量 在一个合约里是固定的
    ( S4 L. b+ p% ]& I% [& j
  12.                     value: '13000000'! K, @! J8 C/ D# Z5 d
  13.                   }3 F! v- M: x! v# r" l) T, U# K) R
  14.                 }],
    " Q* {% t7 ~* ~$ V4 s! r
  15.                 output_id: output_id,6 f! c6 `; s/ X& q+ ]; p8 H/ K
  16.                 type: 'spend_account_unspent_output'% e- ?( \6 |) S6 @
  17.               }( C) C- c+ O) k3 `, O; g
  18.               const controlAction = {; t2 U9 r/ S" B, m
  19.                 type: 'control_program',, b7 ]* r( {9 f. S2 g* _
  20.                 amount: 100000000,1 E2 P" B% a! n2 R2 r5 Y
  21.                 asset_id: asset_id,
    1 w. v7 O: S6 x8 e  j6 L( U
  22.                 control_program:control_program
    * y8 ?& g9 t; O8 W5 c+ s2 {
  23.               }
    ' h0 R8 r* |. G- ^- s. K' q: v
  24.               const gasAction = {& z  T9 s  j. `) a' Q# r& G
  25.                 type: 'spend_account',: `0 y2 V) d! ~
  26.                 account_id:account_id,# s4 @) [# i  n6 C' o% w' m
  27.                 asset_alias: 'BTM',) z. _4 S$ r- C$ m
  28.                 amount: 50000000
    1 o, G# i2 E+ Q$ r6 R
  29.               }
复制代码

. F, `( |1 W( @执行合约的参数构造如下:& }! f& X0 v2 }* z2 }/ u+ l) U
$ ^2 w# E5 _9 e
         
  1. const spendAccountUnspentOutput = {
    + P0 o; }% F3 b; J
  2.                 arguments: [{
    9 R1 _& F: h/ q, a( M; g# t- v: g- v
  3.                   type: 'data',
    * P$ F4 d( f: J/ ^. n
  4.                   raw_data: {0 \! E+ G# [) C! o, T5 d2 p
  5.                     // 执行合约输入资产偏移量  B1 v2 w& h8 C  r/ r) Y, [6 F" j
  6.                     value: '00000000'
    9 S! y. e9 T2 a
  7.                   }
    % e# e, o8 d" o# O' F
  8.                 }],
    ! f% n$ N/ X. a8 Y& X
  9.                 output_id: output_id,  Q/ y) n4 `2 b7 w- k  y
  10.                 type: 'spend_account_unspent_output'3 ~$ b  I8 O* L+ y
  11.               }
    8 b1 A$ L2 @) A9 T
  12.               // 合约执行提供的资产5 j9 l* Q* A% ]6 F9 q
  13.               const issueControlAction = {
    ' {8 f2 }' E6 I1 a* M# ]9 O( a
  14.                 control_program: control_program,
    - |. g4 F; s+ S! u# Y# X. J$ I% Q
  15.                 amount:  100000000,
    4 c+ Y1 C. ^6 i4 j* D: C- A+ V8 V
  16.                 asset_id: asset_id,. r5 S" f7 G/ ?7 G9 ]' T3 V3 B
  17.                 type: 'control_program'
    & g/ N$ @. L2 j7 s1 ]
  18.               }( F6 F, o6 f( a9 [
  19.               // 合约执行提供的资产
    5 n, r0 m7 f! B4 Z+ M. {/ q# O
  20.               const issueSpendAction = {
    + p0 L' V  q2 q# O* r. k2 _
  21.                 account_id: account_id,7 w0 o2 b( {, h; Q
  22.                 amount: 100000000,* V* X( S" V% x" V, A
  23.                 asset_id: asset_id,0 _5 V8 X5 i5 R/ d
  24.                 type: 'spend_account'( {) P; @$ u5 j5 W- I: [0 g
  25.               }
      B7 m2 O# w8 [( v$ U
  26.               // 旷工费8 ]1 K3 c/ j8 B1 B6 }
  27.               const gasAction = {
    & ?  d$ Q4 j, w+ S5 E$ ?. v( j" Q
  28.                 type: 'spend_account',; e- z: d$ p% ?" _1 a5 o/ `
  29.                 account_id: account_id,
    . N6 m: M. N' @2 x; _. J
  30.                 asset_alias: 'BTM',; M: L2 D, H& V, O
  31.                 amount: 50000000
    6 c7 o) ^# m2 W7 w: _6 f  }3 E
  32.               }
    0 Q4 R9 ~! ]* F; B
  33.               // 合约执行获得资产对象
    5 F0 ^0 J; d) s
  34.               const controlAction = {' ]% C# o. ~: l
  35.                 type: 'control_program',6 l) V; t$ A( d( Q$ q) b8 p
  36.                 amount:  100000000,
    * H6 U- \, {4 e
  37.                 asset_id: asset_id,
    : f8 {0 ?  J: h6 T
  38.                 control_program: compileData.control_program
    ) S) {" L4 B8 h) `# n/ i
  39.               }
复制代码

( W) Y1 Y! z& S0 D5 l% Obuild 操作其实就是指定输入输出的过程,详情请查看 官方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
  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {) o* H1 A3 _" s; L
  2.         OkHttpClient client = new OkHttpClient();
    # \  {' |$ M2 N0 U
  3.         MediaType mediaType = MediaType.parse("application/json");: _- b: g1 g/ _2 l4 ]
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);
    : O$ g/ F8 B9 W. G) L
  5.         Request request = new Request.Builder()5 E& M) @4 w, \( U9 [! z
  6.                 .url(bytomApiserverUrl+"/"+method)6 ]) e: U2 C: {6 j4 W# b+ Z
  7.                 .post(body)4 d! p& f( @% ~+ |8 S0 E) F* ?
  8.                 .addHeader("cache-control", "no-cache")
    4 ~& }  E' [3 L6 {/ i# z/ z
  9.                 .addHeader("Connection", "close")6 d" g9 @+ O0 E# }& C6 p0 r
  10.                 .build();
    1 W5 G( N2 j( V1 e0 Y/ M
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
    # ]8 O$ B7 R$ S, X& @; ]
  12.         }else {: v3 ]7 l# I$ h  z* M
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
    % |7 T5 Q% G* h3 A8 k" b
  14.             String authHeader = "Basic " + new String(encodedAuth);: Q$ C1 g% [3 u- s" U
  15.             request = new Request.Builder()
    " Q( g  _  W  M% N8 J) @
  16.                     .url(bytomApiserverUrl+"/"+method)
    ! l0 o* v- y6 X: X! d
  17.                     .post(body)7 Z4 c' e- D6 v9 K" p! g9 D
  18.                     .addHeader("authorization", authHeader)
    % V$ |  W) \1 i. |2 J) e7 r
  19.                     .addHeader("cache-control", "no-cache")- ^% B. S5 T: V
  20.                     .addHeader("Connection", "close")- \4 W$ X- \5 r5 {) x1 x' j, Q
  21.                     .build();8 e9 t! n( j. D/ U  k6 e
  22.         }
    1 b) ], m* S1 D0 z! {/ I% F, s
  23.         Response response = client.newCall(request).execute();  G  ]+ F7 c% K6 |) n, {
  24.         return response.body().string();+ M- j7 e% H+ v# \  w6 Y
  25.     }
复制代码

! p: b6 |# D$ H+ S$ }/ l3 J+ S2 G, V" f5 ?# ?+ ~  [
( L7 n# O7 v, o8 Z5 a
比原项目仓库:
1 M7 _" ]8 G: v4 L8 }) FGithub地址:https://github.com/Bytom/bytom
2 ]7 L7 V, u0 wGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

凤翩翩求其凰翩d 小学生
  • 粉丝

    0

  • 关注

    2

  • 主题

    1