Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
# b+ ~" N$ c  _2 ?
  1. contract TradeOffer(assetRequested: Asset,1 n* W# ?! L/ f, e9 B2 n9 h' c& L
  2.                     amountRequested: Amount,  b: {  ^8 {! I0 Z
  3.                     seller: Program,5 K/ b5 ~% {' I
  4.                     cancelKey: PublicKey) locks offered {
    " Z( T# L. r. M# [; {9 J7 D
  5.   clause trade() requires payment: amountRequested of assetRequested {0 e/ o" ?, u/ }
  6.     lock payment with seller
    ) Y! A& o, i* M
  7.     unlock offered
    $ b: q  ?% T7 Q# T! E0 O
  8.   }
    , c8 d7 t# c: H8 r" b5 {
  9.   clause cancel(sellerSig: Signature) {
    $ q9 k  t. K  l& E: s" N
  10.     verify checkTxSig(cancelKey, sellerSig), ^) h8 l3 E* F. b; {- h; S
  11.     unlock offered. q* a; @9 C& L" C& a% G2 m
  12.   }
    ' x' x! r& P- z) U( o3 ~7 ]6 X
  13. }
复制代码

+ k# X  }$ `/ J" y, {# H导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档4 Y! G% Q1 o, Z9 {9 x' l
锁定合约& Z* M4 g: X8 x: U/ q+ T/ t
第一步:调用create-account-receiver 生成 control_program
  h$ Q! P/ B- C, z8 ]+ U6 R4 ] . o1 E$ t( k2 ?; `1 U0 l+ H
以下是相关代码片段:4 n8 U1 j) R3 s4 k- `% N
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");* A2 f' `7 C) e6 L; O5 D
第二步调用list-pubkeys 获取 pubkey
; O$ p( A( S; o* ]/ v
# X5 Z) t5 e3 O0 M& k8 \/ m! A' z& g; E! l; J! P& n
以下是相关代码片段:
) m/ _& ~* v6 R3 SsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");. X3 E4 [! w3 D$ s" \5 D/ X' }
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
/ Q6 s$ w* g3 F$ m6 _$ a* C, G, r4 O. z" B5 W* K

! U$ s+ O7 i; I, P# p以下是相关代码片段:
5 s+ J, _) C' }' {" D- z3 e           
  1. JSONObject param=new JSONObject();' ~! d4 S1 r8 z0 _
  2.             JSONArray agrs=new JSONArray();
    ! U* ?. a9 @) |
  3.             //合约的四个参数值
    : ^& J+ d( |3 I+ H
  4.             JSONObject assetParam=new JSONObject();
    - a6 p. u) j6 w3 {7 [" \. q
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
    ) N. i$ T1 \: m" a/ s2 A
  6.             agrs.put(assetParam);
    0 [. Z/ F4 \  M1 X2 \2 e
  7.             JSONObject amountParam=new JSONObject();5 s$ {! `& O! W" C  O9 }
  8.             amountParam.put("integer",200000000l);
    3 Y* g; R, U, b# Y) N
  9.             agrs.put(amountParam);
    : q7 C% a7 H( v$ E; y  s
  10.             JSONObject programParam=new JSONObject();7 s/ H& ]* }1 {/ w/ C
  11.             programParam.put("string",control_program);
    # h: _# i/ D8 q0 B% T+ P
  12.             agrs.put(programParam);
      c2 B1 F' W2 J* i1 l
  13.             JSONObject publicKeyParam=new JSONObject();& P  K  a; Y4 K
  14.             publicKeyParam.put("string",pubkey);+ z5 A  p# O! ^' N7 q) D
  15.             agrs.put(publicKeyParam);
    * M7 }  K" e2 `8 A- h3 x
  16.             param.put("agrs",agrs);
    0 _& K  k' Z8 d. e+ y- B
  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 } }");
    ! Y) A) b* [! y  v6 Y  {
  18.             //调用编译合约
      L% `2 C) G/ d# E; l
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码

( W$ E, C- |; a, f: \5 p第四步:将program 传入build-transaction接口去build一个交易的到data2 c+ @) r- X4 `" @+ L
, C5 ]& `' y. t& x  N

$ K8 X* F7 e2 d  ?3 s以下是相关代码片段:+ x2 S8 l' n$ ?( w
           
  1. param=new JSONObject();
    ( M. q) M+ L( X8 E8 u
  2.             agrs=new JSONArray();
    7 t0 E+ p  J1 ^( S/ J$ V, R8 e
  3.             JSONObject spendAccount=new JSONObject();8 G# S( w4 Z$ `8 H4 Y$ @" q- b
  4.             spendAccount.put("account_id","0H757LPD00A02");
    ) l& c1 }1 Z9 x& L" e9 Q$ T
  5.             spendAccount.put("amount",9909099090000l);" R* m. ~! z1 V# M+ ]3 h5 }7 c9 e6 h, ?% s
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");5 C4 E2 ~2 [) K( I6 R
  7.             spendAccount.put("type","spend_account");4 z6 |, B% O/ D& s) q, i
  8.             agrs.put(spendAccount);# Z( q& h! Q! f+ @" V" ^: M3 j8 U
  9.             JSONObject controlAccount=new JSONObject();
    3 D. l1 l6 N# ?4 t$ A$ S
  10.             controlAccount.put("control_program",program);  K* I( T& K2 O9 k4 |' ?8 r* I, {
  11.             controlAccount.put("amount",9909099090000l);+ N9 q* u4 S/ v5 S
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    & J, n/ R$ B" F' t0 F& m& }
  13.             controlAccount.put("type","control_program");# ~* r# ^+ t+ R* _6 W9 F: L; A
  14.             agrs.put(controlAccount);9 t5 M% {/ n4 p3 R$ I: z
  15.             JSONObject spendAccount2=new JSONObject();
    9 h8 [2 L- g+ Q
  16.             spendAccount2.put("account_id","0H757LPD00A02");; f+ G' D+ ?! j+ `
  17.             spendAccount2.put("amount",6000000l);& m( S' ~* w2 B1 ^
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    & B/ y( Q, @" K+ ~
  19.             spendAccount2.put("type","spend_account");
      N% k4 W7 u- \. r
  20.             agrs.put(spendAccount2);. q# U6 \. B. _6 ^9 Z' D
  21.             param.put("actions",agrs);7 F3 \" s/ b8 C, K! w) }1 I1 `2 v
  22.             param.put("ttl",0);6 {* |7 j/ ^8 d$ t' @
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码
+ l8 l! z- [/ [1 A
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction% n$ T5 ]1 P9 x* |8 L! K' k5 _

  L+ Y5 }' P" Q& x
- n# r; h) y, |  V, v( X4 h6 H( H以下是相关代码片段:
8 ^" j9 y0 @/ U            param=new JSONObject();
3 L" L9 t9 B$ }! X! f& K9 Y! e            param.put("password","xxx");" @5 V7 J& z$ M6 ]6 ~
            param.put("transaction",data);
$ u6 T$ h# P1 [% k: [            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
/ i( Z0 O+ P* V' @2 q; |第六步:调用submit-transactions提交交易6 c6 H5 g% i& j, x8 z
( ?  N1 y; B  T
以下是相关代码片段:
( U" {  n3 ?! w1 e7 y8 l            param=new JSONObject();1 A* E% b' N+ n2 I7 }4 p( t. \
            param.put("raw_transaction",raw_transaction);. b1 A$ {5 V1 W* F
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
. ~8 @6 d! p* K! ]" g解锁/取消合约  t: J3 U% O/ n( L& W3 x
首先需要decode出生成合约时候的参数
/ Z3 x3 m6 L& N) y5 ?5 ^调用list-unspent-outputs 获取生成的合约信息获取program
- w& b3 m& V& ]* c8 C* z6 l0 K! m& |  [8 p
以下是相关代码片段:
: H2 v* I( L% |- t& k7 o/ C9 c+ Hparam=new JSONObject();
* k2 t" M; {# D# b        param.put("id",outputid);
1 O" f, x1 v- z  e2 ^' S7 h# o/ r' J& y        param.put("smart_contract",true);
/ }9 }/ E0 u, F8 i) @8 q; O" V        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");
. @2 ~+ W  I: B( Y( `调用decode-program 传入获取生成的合约参数信息+ F: T6 |& h2 F

5 v/ E$ q) C% r6 L以下是相关代码片段:% y5 g; h' F9 X
param=new JSONObject();- U0 f9 J: D3 `" B3 d4 ^
        param.put("program",program);: S- p& I6 v5 D6 X
        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");/ P1 z+ i8 ~2 q4 h2 B" X
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
0 m1 T" f/ M" G- _% d解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
; d3 r) O% T; U* y' i0 F& I3 q取消合约的构造参数如下:! v' n) Z/ _2 l6 i! ^. J
, ?% c( c; H7 i
            
  1.   spendAccountUnspentOutput = arguments: [{
    / s9 R9 V! B& k) ]3 {& o
  2.                   type: 'raw_tx_signature',
    ! ?/ I0 W3 e1 h% m% q4 ^
  3.                   // 生成合约第二步的pubkeylist 详情
    5 N) l' v% H3 [4 W7 l
  4.                   raw_data: {
    # [+ o2 n- O) I3 L5 E8 p# A
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,( ~0 T. \/ f1 E& a" m7 f1 L
  6.                     xpub: pubkeylist.root_xpub
    / o# p7 w  Q6 A0 i% p
  7.                   }
    & W1 m. l! x) p- N
  8.                 }, {- k. a' Z- [8 }. i/ p# v# d
  9.                   type: 'data',
    & q% s) t& s5 e
  10.                   raw_data: {* B. ~4 n) W3 P4 i% E" {6 m
  11.                     // 参数偏移量 在一个合约里是固定的
    2 K1 e9 i) _" \
  12.                     value: '13000000'
    8 k! m; K( V+ Y3 [/ v
  13.                   }
    8 `- T: X& L. `
  14.                 }],
    $ z5 E/ b- @! _0 ?
  15.                 output_id: output_id,
    6 I6 C6 I1 B2 F5 y
  16.                 type: 'spend_account_unspent_output'9 h- O  M9 J& v2 w4 \5 e
  17.               }0 N0 y- M& i/ q8 {1 A$ m9 F* e
  18.               const controlAction = {: Y0 y6 C6 \& E5 k; m6 Y( m
  19.                 type: 'control_program',
    9 X$ d) T( Q6 q0 y
  20.                 amount: 100000000,
    + C% V- s4 n# Z8 D# Q4 B
  21.                 asset_id: asset_id,
    " B* Z6 S  e, P: y8 M# m
  22.                 control_program:control_program* \6 u- X& y; j+ T1 @; c9 D3 s
  23.               }, C& Z8 B6 d# o3 F& R
  24.               const gasAction = {4 R( [  s) d$ H; v4 q% E
  25.                 type: 'spend_account',
    + a0 R7 v! p9 A+ n
  26.                 account_id:account_id,# _2 ~" k- \: f: F3 R* U7 n8 }
  27.                 asset_alias: 'BTM',) i. k+ _* ?4 P8 ~# F5 A' J
  28.                 amount: 50000000/ c, N6 o! M& x) }
  29.               }
复制代码

/ z% ?; u; r) h# R执行合约的参数构造如下:
5 O0 |0 ?: |2 i9 `- [: }, t* _( H- s" W7 o8 W, q# z
         
  1. const spendAccountUnspentOutput = {
    ; e* O0 ?8 ?* q# q  V
  2.                 arguments: [{6 l. T% e% Q$ o8 p* x$ f* T
  3.                   type: 'data',
    8 j9 r6 E6 _) T1 A/ E
  4.                   raw_data: {$ C7 m: f8 ~; F  x5 J* E
  5.                     // 执行合约输入资产偏移量
    ) a, N) N4 c7 \
  6.                     value: '00000000'
    . s, R2 ~7 e2 H" o/ y) l
  7.                   }
    ! @; K7 y. k, N% p# p
  8.                 }],
    8 J5 I# k, p. i- v9 A* w- ~- ^7 s
  9.                 output_id: output_id,$ f" T* V% `! s' M" C% m- ^8 J. X/ ~
  10.                 type: 'spend_account_unspent_output'0 s, e; k6 m3 r
  11.               }
    ( }3 Y# i5 |$ T8 G- A6 i
  12.               // 合约执行提供的资产
    ' J6 E2 i+ g9 D, z/ v8 D" l
  13.               const issueControlAction = {$ m4 X6 v* ]$ N- J5 s9 G3 z
  14.                 control_program: control_program,
    ) y! C# P# b6 ^
  15.                 amount:  100000000,9 \6 @5 I* d& T3 e( l1 ^2 g
  16.                 asset_id: asset_id,
    / P  Y3 }# e7 h: o
  17.                 type: 'control_program'
    ! o, Q& p7 B+ r
  18.               }
    + y7 M0 O' C' |3 N* C' S8 I2 Y
  19.               // 合约执行提供的资产
    + r9 B  \. }! o7 u$ z
  20.               const issueSpendAction = {
    ( o9 v, M# |0 A+ ^, C
  21.                 account_id: account_id,
    ) c) O0 H9 O# M4 e
  22.                 amount: 100000000,# k& |6 f6 {! L9 k, ~1 @) S
  23.                 asset_id: asset_id,
    5 g; c( ?! A, `. n
  24.                 type: 'spend_account'/ `0 g0 `6 U( D2 ~/ L2 {
  25.               }
    * X/ R: H7 e/ \# l! l
  26.               // 旷工费+ D9 P' {& g' F2 ]& M' e5 i
  27.               const gasAction = {4 Q; m+ V% V( m  r
  28.                 type: 'spend_account',% o8 V# x: O! n  d) Y4 n
  29.                 account_id: account_id,
    " {+ h3 P, {* Y2 T$ Y# r
  30.                 asset_alias: 'BTM',
    8 R  S; P' _3 `
  31.                 amount: 500000007 U: `& \) I3 v* e$ d4 O
  32.               }) T; Y; N% w- |$ B7 A
  33.               // 合约执行获得资产对象
    9 z* j0 }; ?) O8 s) T$ h7 x  m
  34.               const controlAction = {; A: y- @( q1 W) k, j% g
  35.                 type: 'control_program',
    ' I. x( u. z$ z* l' v/ o0 N4 i
  36.                 amount:  100000000,2 U( p3 s( E' j5 G5 x* K) N& V
  37.                 asset_id: asset_id,
    * z9 F% ]0 u1 l' C, k6 ]
  38.                 control_program: compileData.control_program$ r% m0 ^( T" C* G3 Y7 U+ }
  39.               }
复制代码

" r7 x/ I: L0 Zbuild 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档0 {2 [) c# y5 y
备注
+ I, g, G5 Q" g调用比原基于okhttp接口javautil 如下:
& d7 i* N$ L. {, |# Z5 `  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {; y& M( O4 q8 Q" W7 v
  2.         OkHttpClient client = new OkHttpClient();' ~' W/ F$ \/ q, [# }% U# r" K
  3.         MediaType mediaType = MediaType.parse("application/json");
    ' \6 Q, l+ c" l1 t
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);7 T- U5 f: U& r
  5.         Request request = new Request.Builder()1 K; V) U& c' p8 n/ f
  6.                 .url(bytomApiserverUrl+"/"+method)/ K1 A8 x! j5 d" |( a
  7.                 .post(body)
    ( }  ~6 Q+ x* s% q8 d$ l/ ?
  8.                 .addHeader("cache-control", "no-cache")6 D' H1 [1 K/ x- P: L  @; }
  9.                 .addHeader("Connection", "close")) O! P5 V! p- ^5 f
  10.                 .build();0 L% [$ L( _$ Y' b- U
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){$ y% I- q$ p! D( V6 c+ a- F4 M, Y9 J
  12.         }else {3 r/ q( J- v$ R( U) F0 S4 K- c! J  o
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
    5 i* V: c0 f& c7 g3 ~
  14.             String authHeader = "Basic " + new String(encodedAuth);
    $ h0 K, n+ }- @1 x- A& m# c3 _, g
  15.             request = new Request.Builder()) |& l; p9 u( h% P/ |
  16.                     .url(bytomApiserverUrl+"/"+method)
    ( E' F2 _& S: {0 _1 R
  17.                     .post(body)  b4 u0 R, ]' a% T
  18.                     .addHeader("authorization", authHeader)
    5 J# H2 n* z1 r6 A7 J0 F' \
  19.                     .addHeader("cache-control", "no-cache")
    9 h) E) o, o3 f0 E5 ?
  20.                     .addHeader("Connection", "close")- I) z! v2 K5 E1 z( l# O0 U
  21.                     .build();2 p8 |, G! u, q) E3 U% s3 _. }
  22.         }9 {7 d. @& M0 k! ^  w* u& z
  23.         Response response = client.newCall(request).execute();9 d3 r$ s/ R& {. }
  24.         return response.body().string();8 q! X# S! P* u7 M+ D
  25.     }
复制代码

- y; a9 S$ p  i! _0 k! ^' W6 W/ F( C
6 X& [" d! H: A0 z
比原项目仓库:2 F& [+ N" A0 ~
Github地址:https://github.com/Bytom/bytom
$ G/ D& s' V1 E# y' @! j* NGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1