Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
2 p  h& F2 `0 x9 P/ t. j' ?
  1. contract TradeOffer(assetRequested: Asset,
    7 d5 l5 ?- N3 u+ `
  2.                     amountRequested: Amount,
    ) `' K8 c1 ^3 k3 E3 r/ a+ O
  3.                     seller: Program,
    3 k0 {5 k4 y, E9 N7 Q
  4.                     cancelKey: PublicKey) locks offered {+ z4 _4 J$ ^/ }
  5.   clause trade() requires payment: amountRequested of assetRequested {
    3 \: f- f- B  N8 P
  6.     lock payment with seller
    9 X) c& G% _& x+ ^+ i% W% _5 p
  7.     unlock offered
    " a% G9 s/ \* O8 Q
  8.   }9 z2 W) V. v' n4 H3 t. h4 @
  9.   clause cancel(sellerSig: Signature) {
    ; w* A  A4 a) J  _& j3 t
  10.     verify checkTxSig(cancelKey, sellerSig)
    ' X, @6 P7 h) B5 l2 h0 V5 ]
  11.     unlock offered
    $ }. b& O3 s; h, C& \$ [
  12.   }) |, P  q( Y5 N: j
  13. }
复制代码
$ X) L/ `1 z* `) n% C* D
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
4 s4 i+ K' p8 ^' i- m  V# F  w锁定合约6 s* Z* ?5 V! p! f  `! V
第一步:调用create-account-receiver 生成 control_program
! Y4 N. u0 c8 o7 B% D
, Q' M8 \$ T7 k8 @% K, {& G以下是相关代码片段:
3 w5 Q" r# I$ KsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");0 L+ g; ]# a& n2 Z0 c& \
第二步调用list-pubkeys 获取 pubkey
: W" t" J6 v! k1 z* f9 @
" [) E* z7 h, z& W+ Q; r( ]3 x" N, _/ Y" {- n0 @
以下是相关代码片段:
4 s/ [6 [7 O. R; O/ t& }4 k( {sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
% L( \4 N6 Q/ J7 B9 c+ z第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
% f  ?' Y4 o9 f/ D- S
% z5 ]  k. G) t4 H0 ^% q" `# [: S. a2 m3 M
以下是相关代码片段:. R% Z( v! U- ]8 M- f5 T$ d+ O
           
  1. JSONObject param=new JSONObject();3 v( j* ^; J) b' K5 R1 V' u; x
  2.             JSONArray agrs=new JSONArray();! s2 I) \4 ~5 Q9 V
  3.             //合约的四个参数值  ?' @; t1 {! K) L- b0 h
  4.             JSONObject assetParam=new JSONObject();
    : r* c4 t7 c+ J3 X/ }
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");' `! E% K) r0 k; i9 v3 B
  6.             agrs.put(assetParam);; t/ q% ~3 T8 d+ S6 v6 }! X
  7.             JSONObject amountParam=new JSONObject();
      X6 q4 N, L/ K- e" \; X% k
  8.             amountParam.put("integer",200000000l);
    / Y8 n* r/ Q! J; R4 D' q- @; ~1 C9 l
  9.             agrs.put(amountParam);! s% E% R; z# {+ ?4 c( K9 i
  10.             JSONObject programParam=new JSONObject();5 n! [# E% j5 g$ _" p( @  _. R
  11.             programParam.put("string",control_program);
    : y: o0 w" c3 R9 F- ~
  12.             agrs.put(programParam);5 w) D; p; O, O
  13.             JSONObject publicKeyParam=new JSONObject();
    ! q4 I# h3 R6 C% s
  14.             publicKeyParam.put("string",pubkey);, L5 r+ ~% n7 u' |- r& `
  15.             agrs.put(publicKeyParam);( n. g( o  S( p
  16.             param.put("agrs",agrs);
    ' E2 `+ o: \5 m9 p6 T/ w
  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 } }");0 t0 c; |$ J" w7 e& l
  18.             //调用编译合约9 ^% L% L( P4 Y4 q4 r
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
/ i$ w6 o! b9 I& u) p' N2 I( J6 `
第四步:将program 传入build-transaction接口去build一个交易的到data5 n3 i( k6 R1 ~6 D  A
- [7 h; V  p* P

( m$ N  D5 N- P( w以下是相关代码片段:4 ?. c  t- L% P. ]) R( J
           
  1. param=new JSONObject();& R4 \1 ]2 C/ Y$ _+ L- F. w9 X
  2.             agrs=new JSONArray();, M. T- n/ M  I& u! U
  3.             JSONObject spendAccount=new JSONObject();; z* v  H( y# O+ p1 ?+ _
  4.             spendAccount.put("account_id","0H757LPD00A02");# J% C+ K& G' F9 M1 M  z1 l
  5.             spendAccount.put("amount",9909099090000l);
    3 M/ ~+ P( D9 F1 |" q6 N
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");) l# g  C0 t$ j% D# s* ^* \: B
  7.             spendAccount.put("type","spend_account");
    / e$ C' G6 E3 V8 T1 m
  8.             agrs.put(spendAccount);: t% b2 O) K/ {7 {. ?
  9.             JSONObject controlAccount=new JSONObject();" r+ P9 n/ q! m% m4 C
  10.             controlAccount.put("control_program",program);" n4 ?0 G: Y; ~" Z1 h* A& b
  11.             controlAccount.put("amount",9909099090000l);( ~  _9 _. Q) M" @0 ~
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");9 W, V4 F! v; u2 _: A: t
  13.             controlAccount.put("type","control_program");
    + @; c; _4 y( \! Y
  14.             agrs.put(controlAccount);
    % B; r) `1 D3 P+ S" s
  15.             JSONObject spendAccount2=new JSONObject();7 t2 c0 n1 Q& m" ?, w) b
  16.             spendAccount2.put("account_id","0H757LPD00A02");
    ' D9 r# q' e- w# V) O4 V5 \; r0 e- B* @
  17.             spendAccount2.put("amount",6000000l);1 A% e" D: f4 X! s- q. V. ~
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    ; R3 K5 S) x$ e* A- E
  19.             spendAccount2.put("type","spend_account");
    " D" Y8 k- H- r) L& D
  20.             agrs.put(spendAccount2);
    6 H  z+ p8 i& w/ B1 U* n
  21.             param.put("actions",agrs);( l( ]. i& }2 x
  22.             param.put("ttl",0);- y5 }( K2 e9 }2 C. s6 e2 R
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码
8 S5 R8 L7 {7 P- t
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
5 o! V1 m8 ?$ G. }' `8 M) {( u* S- w1 F* K3 M- t

& B' w5 s6 Y7 ^+ m  u: ]以下是相关代码片段:5 q4 L4 j. y$ T1 }; L! T
            param=new JSONObject();
+ b$ j4 C3 s' l& d8 |6 [# _            param.put("password","xxx");
2 }) }- a. }6 [  [# _            param.put("transaction",data);
9 G, _* N. r8 e& {8 D            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
# j+ T" m& u9 g( |# K, b5 s3 R, P! M第六步:调用submit-transactions提交交易
2 E$ ?+ X& s- t' U/ Y' ~% @# Y9 b8 y$ e0 \- ]. f9 r2 Z3 L& o
以下是相关代码片段:5 w% [1 K% h  ?9 m4 f4 t# ^
            param=new JSONObject();2 S- _- l" B8 i( I1 {
            param.put("raw_transaction",raw_transaction);
; p, q2 X) O/ Y1 h' M9 {  W            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");+ i* H$ S, o7 a/ d* l# E
解锁/取消合约
9 m8 T: h; q, U* A) ]% D4 f首先需要decode出生成合约时候的参数$ b) W: w6 @0 P& r9 @& H( R6 o
调用list-unspent-outputs 获取生成的合约信息获取program
% S! N8 O/ N% E+ ]$ E" i+ A$ k6 N
以下是相关代码片段:: |7 ?0 x2 O3 t' q$ c
param=new JSONObject();
9 D# e) [% G3 u5 }5 g% `2 {        param.put("id",outputid);
: W8 d6 X" d- z; `5 V8 x. J+ ^9 I        param.put("smart_contract",true);
. @( ?3 f& U% @( R        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");
& i0 Z7 h; A' ?5 F% r调用decode-program 传入获取生成的合约参数信息, _- @/ T& M. E) s

5 |+ F8 P( l/ K& ^8 [# P, R$ d以下是相关代码片段:  f% n" [! ]3 a" u- C3 X
param=new JSONObject();
$ V8 Y' d; w+ N$ h. M        param.put("program",program);
" A4 Z* H; s: X2 r, P4 b7 i+ y1 T& @        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");4 T4 b& S. U% ^7 T
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)% {4 K0 B2 H8 W& J
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
7 \5 ?, E4 v3 `) x9 H取消合约的构造参数如下:
% A4 p' E5 N8 \. E4 g  R2 j! d7 `; R6 N$ q2 V
            
  1.   spendAccountUnspentOutput = arguments: [{# B9 q* }7 ^9 u) o- y7 F
  2.                   type: 'raw_tx_signature',
    $ r9 [# c2 F7 y4 U# H  `- W
  3.                   // 生成合约第二步的pubkeylist 详情9 {4 u- ]6 ?# Z- ?: d( {
  4.                   raw_data: {- i# Y5 v  T% q0 i% B0 p
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,1 e5 J7 F2 F0 R
  6.                     xpub: pubkeylist.root_xpub
    1 a1 _7 k0 ?8 j6 A8 [9 Q$ x1 T
  7.                   }+ O4 N0 ~# k! l3 `
  8.                 }, {
    1 d0 A, I( `( y4 _
  9.                   type: 'data',3 X/ Y6 @7 a+ V0 ~) M
  10.                   raw_data: {
    , r6 ?: o. h$ X" a
  11.                     // 参数偏移量 在一个合约里是固定的
    8 B/ g) N: [9 j  v& X3 S+ v
  12.                     value: '13000000'! H/ o$ N' P& u4 G1 J# T2 R
  13.                   }
    : c- i' Z" [/ u0 |
  14.                 }],
      B# l) ?, F+ O7 ~5 K
  15.                 output_id: output_id,
    ! v, V, }  s6 @7 k$ c' `3 i6 n
  16.                 type: 'spend_account_unspent_output'
    / _7 p+ m7 K) h2 Q
  17.               }* E% ~( N6 y- `
  18.               const controlAction = {$ v- U) Z5 a, X0 ?4 r" k* E$ ?
  19.                 type: 'control_program',& X3 {# {, y; k1 B2 X
  20.                 amount: 100000000,
    2 n% j& m9 N# f* f# I
  21.                 asset_id: asset_id,
    - M3 K& u6 B- E9 ]3 T7 Y7 M0 o
  22.                 control_program:control_program
    . _& _- o) N5 g' ~( E
  23.               }) Q1 j( Z/ Q3 F! c5 l2 w
  24.               const gasAction = {
    ) e/ A7 ^8 p; g7 B
  25.                 type: 'spend_account',
    7 u% y$ e$ i% f. I$ m
  26.                 account_id:account_id,
    9 ~/ m# U9 u. j- k
  27.                 asset_alias: 'BTM',
    # s6 J* w2 W$ |  o5 p+ _
  28.                 amount: 50000000# F& K* s2 H4 f- q: B8 \# K: Q
  29.               }
复制代码
" U; u+ e/ ^! C5 P
执行合约的参数构造如下:
5 z, x8 L; I: d# _% i
9 d6 a4 s; I5 {. x; C         
  1. const spendAccountUnspentOutput = {  z# g  V$ A0 L: f( F. C
  2.                 arguments: [{- b# k* g2 ^! o% L! x! C" N
  3.                   type: 'data',
    1 ?0 H$ B* @  F  O4 B  @  T
  4.                   raw_data: {% \5 V+ w6 T9 ]) S, D, E
  5.                     // 执行合约输入资产偏移量: N; l7 ]; ^. L2 }8 K. ]6 K! I- \
  6.                     value: '00000000'
    : t9 X+ n5 V8 S9 V, ^
  7.                   }
    ; C+ H, l- Q; b
  8.                 }],
    * {1 i/ s; o4 v7 W/ z
  9.                 output_id: output_id,
    8 a' s, d" Z3 V8 Y. f8 [- t
  10.                 type: 'spend_account_unspent_output'4 h/ t8 V* s6 {
  11.               }
    ' V  ]% ]1 ~4 V9 K% c4 n. n
  12.               // 合约执行提供的资产
    / w8 ]3 k3 x2 l2 c
  13.               const issueControlAction = {& n9 c6 u3 I# D, w! }) D
  14.                 control_program: control_program,, \8 q9 h5 e, C# e- n6 `
  15.                 amount:  100000000,
    , @; q" _- P0 Z- B5 ]
  16.                 asset_id: asset_id,
    - n' p9 F0 C" N/ `/ L/ u3 B
  17.                 type: 'control_program'5 P8 L' Y0 L3 D+ B5 ]0 p4 m
  18.               }6 v( X  N" h$ a, W: N
  19.               // 合约执行提供的资产& J2 y3 a; }$ m  `) D6 ^! z- C' M
  20.               const issueSpendAction = {
    - p5 }6 h* c+ y- l/ \- J& U$ S5 V
  21.                 account_id: account_id,
    # U; R# h) m2 n+ H2 U4 r. J
  22.                 amount: 100000000,
    0 {$ ^& Z, ^$ C, f- f
  23.                 asset_id: asset_id,6 M1 V9 e" b  D; P* o' q
  24.                 type: 'spend_account'
    ) w0 x2 Q' r) O9 G1 W7 U
  25.               }
    ) }4 `5 C: @7 G# C6 |% x6 c
  26.               // 旷工费
    & W( N  f4 w/ o+ \2 `- e/ x- h9 `$ l
  27.               const gasAction = {% I( q4 @" i7 P
  28.                 type: 'spend_account',
    & f9 ]* D: _1 O# a
  29.                 account_id: account_id,- L2 V8 N; p/ C+ [
  30.                 asset_alias: 'BTM',
    4 R" i* d; K; _! h" F
  31.                 amount: 50000000! P& J) [) y/ n% n9 F
  32.               }
    6 W* |' |' J7 d/ [! v- ]
  33.               // 合约执行获得资产对象
    " W& @2 ?6 C: _, h
  34.               const controlAction = {4 W: V6 [3 V& l9 l" p& j6 }
  35.                 type: 'control_program',- G" X1 h9 L, M6 s. s, d8 B
  36.                 amount:  100000000,# N$ d* T- v' o5 `, _4 a# q
  37.                 asset_id: asset_id,. L6 z4 x( a- s3 k/ y
  38.                 control_program: compileData.control_program
    2 D! h* m1 Y. a/ m
  39.               }
复制代码
1 s# a7 ^( y3 \5 N  a8 U# m! G
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档; T! G4 p' j- [, C  u1 r! L! L
备注7 H9 s" W5 Y$ t% b3 r- {; e
调用比原基于okhttp接口javautil 如下:
+ Q, C, N6 E1 k2 R- U4 c. W. j1 Z  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
    / w# ^5 v# T& R7 q% r
  2.         OkHttpClient client = new OkHttpClient();) m  k! k, t# A# {% P4 z) g$ p4 J4 w
  3.         MediaType mediaType = MediaType.parse("application/json");7 S- y( G# o% p1 Y
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);
    8 V9 d) ?% w( s6 x
  5.         Request request = new Request.Builder()9 C7 J7 Q1 f9 K2 ~# y9 y# z
  6.                 .url(bytomApiserverUrl+"/"+method). r+ `6 j7 z4 |) X3 B
  7.                 .post(body)
    . O( B) b- _0 ~3 X
  8.                 .addHeader("cache-control", "no-cache")
    3 [. d( X) A# E, N$ Q" g
  9.                 .addHeader("Connection", "close")
    3 D7 f4 M; u/ L1 V1 l% H
  10.                 .build();4 s9 Y: J9 F8 v7 o
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){$ B3 `. m2 A% F2 u2 L  D) v
  12.         }else {
    1 h9 r6 W% W3 Y5 ?8 |6 J
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
    ( q; F4 N8 e1 Z! t
  14.             String authHeader = "Basic " + new String(encodedAuth);6 R- J( M! _( Z- `+ K  m7 t
  15.             request = new Request.Builder()
    * R5 {. G6 O4 B7 L  r% d9 i5 v
  16.                     .url(bytomApiserverUrl+"/"+method)
    - O7 C1 ]4 g, S3 k+ B+ ~
  17.                     .post(body)
      F0 j2 i8 L6 f1 |4 N, l
  18.                     .addHeader("authorization", authHeader)5 N( d( |8 I4 ^( x9 w% T
  19.                     .addHeader("cache-control", "no-cache")& i% v, m9 L1 e0 m/ h, s
  20.                     .addHeader("Connection", "close")
    ; }, W- j  K& ], t5 ~
  21.                     .build();- U$ n0 o* n" J, _
  22.         }
    1 |! V, J2 ]+ z4 H) B0 Q5 d) G+ N
  23.         Response response = client.newCall(request).execute();
    ; }6 @* W& w* @4 S$ ^/ v: J
  24.         return response.body().string();9 d% O( ?& @' ]7 W
  25.     }
复制代码
4 e5 s6 c+ ]7 W& R* P
# ^5 P, T9 U) y' F, ?
! \0 i! v" I7 ^1 ]
比原项目仓库:+ h3 z4 x3 V6 n7 z0 F
Github地址:https://github.com/Bytom/bytom
3 F) \7 M/ @; s! H2 ~% h# C6 S* pGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1