Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:" D- U: ^6 @* Y
  1. contract TradeOffer(assetRequested: Asset,
    ' a$ O" J$ n" ]8 z
  2.                     amountRequested: Amount,& m( s3 F  {% K" s) H4 j
  3.                     seller: Program,- ~3 ]8 ]2 }3 G# ?, B
  4.                     cancelKey: PublicKey) locks offered {$ f. c8 m6 K9 n! S) h9 J
  5.   clause trade() requires payment: amountRequested of assetRequested {
    1 e# ^* T1 _7 p
  6.     lock payment with seller
    : \) \! T+ P) P& t- v' _* t# f
  7.     unlock offered4 [/ b! r) q/ Q& Q
  8.   }
    ) m1 X% Z0 M3 O, ~
  9.   clause cancel(sellerSig: Signature) {
    " S7 S2 [9 }4 K9 [" y: }
  10.     verify checkTxSig(cancelKey, sellerSig)
      Q% b& U' I& R- a: a: K4 l
  11.     unlock offered/ \+ w# @6 L! G& A9 g% L
  12.   }
    2 Q: B+ R$ c3 q& k# k" z% q  D
  13. }
复制代码

& C, p5 ?4 b) I! m' N; d6 J导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
- B0 |& z7 |. L锁定合约
# U  m/ d- E0 I7 L/ h第一步:调用create-account-receiver 生成 control_program
) E) w) ^" [; V6 e
: F4 H* v. w% T( f. j. k* h以下是相关代码片段:+ w, }7 g8 E/ I  A
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");* F; S0 o# {7 S; c
第二步调用list-pubkeys 获取 pubkey2 C' F& v5 Q1 k9 i

% @8 ]; Z9 F8 Q5 J/ Z5 C* ?; z2 v
以下是相关代码片段:
9 W: w% o  X: J8 U$ g7 zsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");( D4 S/ H4 E' M" d/ e* P8 k
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
! _- ~- \, Z* n6 r1 c) O- C' [) x& t3 X, K5 Q

% Q: |" s7 i& l8 O) K以下是相关代码片段:, ^2 ~- l# ^5 n0 u$ v* {
           
  1. JSONObject param=new JSONObject();% t9 g' t$ \0 t" C" D, r! l. z/ c
  2.             JSONArray agrs=new JSONArray();
    * Z5 y6 z9 R3 v$ r) Y. F2 v6 `* S
  3.             //合约的四个参数值* B! s& X2 E7 ?( b3 r
  4.             JSONObject assetParam=new JSONObject();
    " b/ F& y7 |! Q* t
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
    " c5 V/ z: w9 ^1 s4 O( @
  6.             agrs.put(assetParam);
    , c5 U! Q# s$ W1 U  F  L1 v$ h' j
  7.             JSONObject amountParam=new JSONObject();0 ~6 W) ?8 M) |% Q  b# g: B
  8.             amountParam.put("integer",200000000l);/ N4 Z6 U8 N' d8 A" F& G
  9.             agrs.put(amountParam);, g+ G" X6 V. E3 g
  10.             JSONObject programParam=new JSONObject();
    * ]3 b6 _) |/ F# ^+ n
  11.             programParam.put("string",control_program);& C: A3 m7 _, z/ t- j7 D' G  ^
  12.             agrs.put(programParam);+ f# C2 J4 j' _0 x/ e' ~
  13.             JSONObject publicKeyParam=new JSONObject();+ o" L6 p2 h  W' S  ^
  14.             publicKeyParam.put("string",pubkey);
    % G5 E+ ]$ N3 l- s/ u; s
  15.             agrs.put(publicKeyParam);9 d$ p& b- ?0 ]. ?2 V" o
  16.             param.put("agrs",agrs);
    ) G2 m7 R" j/ s( V
  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 } }");+ m3 q+ Z) h! {; b2 j0 u* |# X
  18.             //调用编译合约$ k) i+ M6 w% a0 ]: V" O
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码

! w% X5 b1 |/ v- l" i第四步:将program 传入build-transaction接口去build一个交易的到data% R4 [, F- F* r; s+ k) I0 p# v* r
( ]9 v* v& c" k, s0 T
7 [- o: w/ z# A1 T" v
以下是相关代码片段:& I' ]$ F6 U! N9 t" ^' ?9 P
           
  1. param=new JSONObject();/ F+ H% X6 _3 m
  2.             agrs=new JSONArray();
    $ `" r; i. F( O
  3.             JSONObject spendAccount=new JSONObject();
    , P( O( K7 R  V' q' e; L
  4.             spendAccount.put("account_id","0H757LPD00A02");7 c" l  F6 A* y; s% Z# [7 k
  5.             spendAccount.put("amount",9909099090000l);
    & }! c6 |5 W6 L) p8 r# ]
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    1 |! G, d1 x5 A
  7.             spendAccount.put("type","spend_account");
    ; s7 |0 I2 I: b# G' ~+ w
  8.             agrs.put(spendAccount);, f  _: j) a1 v# u8 k
  9.             JSONObject controlAccount=new JSONObject();
      ~8 \# K" S* n- R# ]
  10.             controlAccount.put("control_program",program);3 h- m, p/ w. W3 d: F5 J3 S
  11.             controlAccount.put("amount",9909099090000l);6 z: O1 x" f& B, m1 _: j1 [
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");- Z( H4 q1 f# A/ \$ |; r
  13.             controlAccount.put("type","control_program");! ^) V2 k+ D8 w6 g1 c
  14.             agrs.put(controlAccount);& ?9 p/ L+ N: v0 |
  15.             JSONObject spendAccount2=new JSONObject();
    + z* G1 X& O6 `' b/ X8 f$ X
  16.             spendAccount2.put("account_id","0H757LPD00A02");) @1 ~, h4 U" m* _
  17.             spendAccount2.put("amount",6000000l);* Y' l4 B; H( o0 H) T
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");. s* Z8 ^* V( ^
  19.             spendAccount2.put("type","spend_account");" M2 O" b. W% v4 }& m. `4 G) o( p
  20.             agrs.put(spendAccount2);
    , O1 {' m0 F0 a$ O2 M" K
  21.             param.put("actions",agrs);
    ! G" [5 G; {1 b" d
  22.             param.put("ttl",0);
    2 a4 o" W' o/ u1 O" P- ]+ O
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

0 [8 \4 j% ]$ O* Z# l第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction0 h" D: N  N2 n/ u1 B7 @
3 Z" z( I$ Q( @: G9 m" d2 X
& }8 H3 d& Z" V: D- N
以下是相关代码片段:7 j. {2 |, Z6 F4 y
            param=new JSONObject();7 A# l2 B! j$ i% ]- H' r
            param.put("password","xxx");2 d& X0 K! D$ K; _
            param.put("transaction",data);
/ r4 |- T& o- Z* q            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
1 p9 D- }5 ]) I5 t1 S第六步:调用submit-transactions提交交易
" y5 q4 ~: J& [5 s
  x7 \  p  T* x+ m以下是相关代码片段:; h- P+ v- F* x' x0 k: A+ O
            param=new JSONObject();
# i: b" X$ J- O+ p) ^/ q* m* S) B7 n% k            param.put("raw_transaction",raw_transaction);4 \6 v& X+ a% N0 V
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");! f& I& h0 X* Z3 |
解锁/取消合约5 G; s0 v1 O6 o# p& y& l- h
首先需要decode出生成合约时候的参数
8 S+ b$ T$ i2 O$ W4 G, Y0 n调用list-unspent-outputs 获取生成的合约信息获取program
0 [$ a# D, n. V  D: o
4 ~6 _) M6 v4 N$ K6 [( E以下是相关代码片段:
* l. A& B# C6 S( }9 Nparam=new JSONObject();
: c1 W8 Q; ?6 R) H' f        param.put("id",outputid);# f/ r" D$ ~# F9 P  N% ~& F: e3 o$ g
        param.put("smart_contract",true);
/ g' |  j! ]& p  P6 F% o        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");$ B9 j" m% l* S
调用decode-program 传入获取生成的合约参数信息: ~. O0 Y" D; p: y
9 v# o+ F& P0 ?0 N4 q: o
以下是相关代码片段:7 T6 K3 ^$ I8 n
param=new JSONObject();5 n# g1 s3 z1 D2 J4 y2 z: v
        param.put("program",program);
) }( I% y& p3 y- ?4 C        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
, h1 I0 Y2 G% r6 l9 F需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
' k* X# \$ R: _8 ~3 m9 d$ _解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
2 z5 b+ u2 ]+ P' b3 j取消合约的构造参数如下:
1 x2 G& M: M5 Q8 W3 X
2 ?. t1 z$ k( F) T/ y            
  1.   spendAccountUnspentOutput = arguments: [{( ^6 D0 D1 i; `
  2.                   type: 'raw_tx_signature',
    + l/ l. ~1 y+ e9 I
  3.                   // 生成合约第二步的pubkeylist 详情
    / ?) t) y0 x4 c: F( }
  4.                   raw_data: {
    0 @% Q  _9 e1 T2 X" N: i! _& t/ a
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,* M! ?  ?' S8 L9 x6 L5 n7 m
  6.                     xpub: pubkeylist.root_xpub, h% |& z: S! S( }) l* u
  7.                   }; G9 f9 Q6 \/ ^0 u0 t
  8.                 }, {& k6 s) ^/ G- v" \
  9.                   type: 'data',! X9 T. k& W+ P
  10.                   raw_data: {0 @) |* p5 G+ Q
  11.                     // 参数偏移量 在一个合约里是固定的
    $ h+ Z6 @& m6 k
  12.                     value: '13000000'
    9 B) j9 t9 s& S
  13.                   }
    . B9 x8 P: p) b# ^
  14.                 }],
    . J# w/ W, U: u3 y" Q3 K- s5 f
  15.                 output_id: output_id,
    ) C& V' ^! Y' G" X
  16.                 type: 'spend_account_unspent_output'4 f' c3 Q+ m' T3 O
  17.               }! S# i6 _% f5 z1 @
  18.               const controlAction = {
    , H1 @* s; X8 k
  19.                 type: 'control_program',
    - {+ _) l6 R$ T6 P# ~& |7 j
  20.                 amount: 100000000,
    6 ~8 H9 `8 s+ l, @, c# v# O% U5 H
  21.                 asset_id: asset_id,
    , Z+ ^0 e0 \* Z# h* G  |) \6 }
  22.                 control_program:control_program* [4 T# r1 Y% V9 Q) `; j
  23.               }
    ! |9 M( }  {& C( B
  24.               const gasAction = {
    ! ?  Q* [& F% R* s) H+ k
  25.                 type: 'spend_account',1 z) d( U, D* R, v* w
  26.                 account_id:account_id,* z- L' q, ?! L- K: ~- z/ X/ P
  27.                 asset_alias: 'BTM',! j  C, v% ?3 a* J
  28.                 amount: 500000000 T. a( [- u- |6 _9 n
  29.               }
复制代码
8 Q1 p/ e3 a( I% y
执行合约的参数构造如下:+ {  c6 j* K' ~; |
3 q, R) `, q8 H( H" I8 n. A- U
         
  1. const spendAccountUnspentOutput = {
    2 M' E9 V8 h& |
  2.                 arguments: [{& e% }/ W7 L% L0 p+ n' z% E
  3.                   type: 'data',
    . j2 s' _  c; _/ f3 @  [3 |
  4.                   raw_data: {
    $ a* ^# I7 h" R$ c
  5.                     // 执行合约输入资产偏移量7 h8 U/ B3 T% ^: W
  6.                     value: '00000000'% Y' v4 j$ L  l' _' U
  7.                   }7 R/ N; ]% K% b* P+ D' F
  8.                 }],
    9 G0 F, h* N4 d( A0 m
  9.                 output_id: output_id,
    ) {' m  J" \" {7 H2 Q
  10.                 type: 'spend_account_unspent_output'+ _( ~3 e7 V7 {. X' \. K
  11.               }
    7 }4 Q: k* n( Q6 y5 A( |
  12.               // 合约执行提供的资产+ g  K8 j% y/ e3 i/ U
  13.               const issueControlAction = {
    " z) ]- f9 i1 ?- [% q  o9 R) K
  14.                 control_program: control_program,
    ' ~% O( c1 v5 Y
  15.                 amount:  100000000,
    * [. w! Q* W$ P" h
  16.                 asset_id: asset_id,' ?' J: Y* ~& Z% s
  17.                 type: 'control_program'' o. z- \0 [5 h' Y) A
  18.               }, Z, Z0 N) J$ D8 L0 B! J- X8 S
  19.               // 合约执行提供的资产
    ( J* }  A! P8 C3 a  X+ p3 B; I
  20.               const issueSpendAction = {2 v7 M# W: J9 N7 ~  R3 _4 ~
  21.                 account_id: account_id,, w" p/ V, a! z: H" U8 Q% x+ s) l
  22.                 amount: 100000000,
    1 q* z4 e; K( Q1 k6 Z
  23.                 asset_id: asset_id,* X) X+ ?4 p4 H' c, w
  24.                 type: 'spend_account'# E% r+ s+ d. r$ D6 b+ `0 n0 @
  25.               }( }' `6 s) [* Y# r" g# k# i
  26.               // 旷工费. C4 P8 R4 T2 R5 M
  27.               const gasAction = {
    5 i; P1 H! [0 S+ s% r4 A
  28.                 type: 'spend_account',
    1 u% s: d! o$ W2 C
  29.                 account_id: account_id,# A/ w, ?) D, K
  30.                 asset_alias: 'BTM',7 R. ^7 X; q8 @8 d, e$ R# A
  31.                 amount: 50000000
      `# N+ k* F, p6 v$ K! R, `
  32.               }8 K- O' d) B$ q/ p7 t2 A5 Q
  33.               // 合约执行获得资产对象
      \: z% b) K5 I6 _% O* [' E: b
  34.               const controlAction = {
    " N% P$ A/ C  z$ f* |
  35.                 type: 'control_program',
    3 R7 T, _6 i6 H# O. `2 ^. E3 L/ D
  36.                 amount:  100000000,
    ' W" c7 s" v/ r5 L- E
  37.                 asset_id: asset_id,
    4 P/ V2 k& Y5 |/ N4 i( T/ s3 h
  38.                 control_program: compileData.control_program
    7 o8 \8 q2 P$ u" u9 R; P
  39.               }
复制代码
/ K+ L( x2 H/ g+ l' ~8 Z( i
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档
" I+ D8 l; ~/ N# r2 w" O备注2 H! o6 E8 V% {! M% y
调用比原基于okhttp接口javautil 如下:
7 X! z* W; O, e0 G1 e! c  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {) S/ L5 [: F5 l# c" q
  2.         OkHttpClient client = new OkHttpClient();
    3 H/ \0 G3 m) L, w- f3 `7 L
  3.         MediaType mediaType = MediaType.parse("application/json");9 _, B" Y' H2 [
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);
    / `6 ?: h  }! d0 O% G+ ?, x
  5.         Request request = new Request.Builder()) Q* s2 J) I) N. S- X: u
  6.                 .url(bytomApiserverUrl+"/"+method)
    8 u1 j! W; E* t+ O- m
  7.                 .post(body)( A% k# {& ~1 n. R. J: M
  8.                 .addHeader("cache-control", "no-cache"). R  a" O7 {3 Q# s' x3 I7 K8 _
  9.                 .addHeader("Connection", "close")
    " V! n1 N9 z$ o  @4 u
  10.                 .build();
    " n5 y6 ]3 @  c, V) ^9 Q: A4 w
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){$ ?+ w: a8 l8 K
  12.         }else {
    9 V9 z7 L: @& H$ f, I7 @) Q
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));4 a+ H* Q( U2 Y" ^+ l! q
  14.             String authHeader = "Basic " + new String(encodedAuth);
    3 Y9 i) M0 X1 J0 X' t; p* \" Z
  15.             request = new Request.Builder()$ ^$ d- T. L' k6 d  v
  16.                     .url(bytomApiserverUrl+"/"+method)
    4 w  W9 k' A- S. N6 j
  17.                     .post(body)
    ; Z# Q& ?7 V' S' t! _' q1 q7 Q' m$ h
  18.                     .addHeader("authorization", authHeader)
    / N' g2 x# P3 X) r' Z  h
  19.                     .addHeader("cache-control", "no-cache")
    4 g5 z" Q8 g: X! I) J  _( N
  20.                     .addHeader("Connection", "close")
    * ^2 _6 }. g$ y! u
  21.                     .build();
      ^+ \& M( h6 b! @1 P2 Y- W4 F
  22.         }5 N7 N  c8 z0 G: y# v6 ~+ W* J
  23.         Response response = client.newCall(request).execute();  z% _) m# t  P  V! @
  24.         return response.body().string();% t5 y$ q0 p) n/ o) n8 w) V
  25.     }
复制代码
) P& }8 H& G9 U0 S. Z" K+ J
" s0 c8 ~5 w3 [" P/ w* y8 Y* o

! b" X* k0 _9 [4 o0 F比原项目仓库:" z- O. S8 E! Q5 O
Github地址:https://github.com/Bytom/bytom
/ R: }+ u8 w$ G, v5 P( t: |* YGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1