Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:5 }  T, q- |( @& R7 }
  1. contract TradeOffer(assetRequested: Asset,, Z, _( m( ]3 i: \1 D$ _/ p' Q9 P' H
  2.                     amountRequested: Amount,3 n0 F0 w+ A  c2 H6 j+ \
  3.                     seller: Program,
    6 ?4 L0 k6 R& i3 O6 j! m
  4.                     cancelKey: PublicKey) locks offered {
    * U& w7 L8 p& h! b: S
  5.   clause trade() requires payment: amountRequested of assetRequested {+ C/ n1 t! V( i" c* L$ a: d
  6.     lock payment with seller
    - {: v* ]! E+ l, i
  7.     unlock offered
    8 z& W" U9 b6 |6 f
  8.   }( x2 I# w3 d, p. t4 l& A0 M
  9.   clause cancel(sellerSig: Signature) {$ [! m1 O$ V8 ]8 o
  10.     verify checkTxSig(cancelKey, sellerSig)
    3 Q6 r- d6 ?! h: d0 u
  11.     unlock offered
    - T. D+ Y& c+ d# o6 V% t
  12.   }% b& u4 l/ `% L
  13. }
复制代码

$ i2 d5 _* F6 K3 z6 Z! h) i9 P+ }导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
. \5 `: o# G; z8 Y+ ?0 v+ n. V锁定合约) E. H6 P$ o; |/ [2 f& ~
第一步:调用create-account-receiver 生成 control_program
: q8 O4 i) z7 ^- T" ~
4 R# m; D9 c+ i: i( _  s5 c2 v4 K以下是相关代码片段:
3 R; V6 ^; C+ r" {! g4 @3 UsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");- J, X7 F2 i* {5 h. q0 ?
第二步调用list-pubkeys 获取 pubkey; I( \( Q# ?7 _: l2 F  m& D

" y$ l- n/ y0 e7 ]7 X
# l7 k$ O+ F# R% ~/ t% A0 c1 C以下是相关代码片段:8 i* R, }1 i  l2 `3 g
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
1 ^4 q; Y$ J2 Z4 Z  ^第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
5 }, w) x  t: N5 T) C; I1 s5 S. M9 `( c* a% T

# T# F2 z+ Q  P" s" x( ^1 n8 h4 d以下是相关代码片段:( b7 C: S1 J5 L# G
           
  1. JSONObject param=new JSONObject();
    : ~) V7 h& F  ?% J& C6 v
  2.             JSONArray agrs=new JSONArray();8 I& ^" B( |" z9 o- m& n
  3.             //合约的四个参数值
    1 S9 R! Q8 x' x
  4.             JSONObject assetParam=new JSONObject();
    " _: ?+ s6 ^! j8 q" Q7 V
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");- f. o3 h( o+ ?8 a
  6.             agrs.put(assetParam);
    & E6 P. \% L) H# u5 ], h; [
  7.             JSONObject amountParam=new JSONObject();/ t6 Q% F3 x' C. S; s( U6 a5 \3 c& e
  8.             amountParam.put("integer",200000000l);
    + J! }. S! K* G- G( {
  9.             agrs.put(amountParam);+ U4 @  v# L# N( d
  10.             JSONObject programParam=new JSONObject();
    4 Z9 r. y" m8 n/ p/ d3 X
  11.             programParam.put("string",control_program);
    5 C$ X- o, |4 j
  12.             agrs.put(programParam);
    + O2 g( a4 S- _2 f
  13.             JSONObject publicKeyParam=new JSONObject();+ N0 h2 x; \/ r# h: b# _' d5 ~* l
  14.             publicKeyParam.put("string",pubkey);
    ; f% J$ t9 u8 u9 w1 E% z2 y
  15.             agrs.put(publicKeyParam);. N5 U. y5 |1 H3 F4 b- k
  16.             param.put("agrs",agrs);0 {0 e& J/ v- |6 I# f
  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 } }");9 X3 }  s; V" r- O% i
  18.             //调用编译合约
    ' ~" r+ L& c/ ]5 p1 l5 w0 Z
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
) a! x6 ?  j4 e7 H! q6 R% D& T
第四步:将program 传入build-transaction接口去build一个交易的到data# X7 c3 |3 H+ _1 R/ S# f. O" Q
6 Z% i8 p* }/ T4 U  B! t  Z

# m0 e$ y) M" W以下是相关代码片段:6 k; F/ f, n/ d$ i! H. B3 N
           
  1. param=new JSONObject();' A0 T& t4 \4 a3 g$ M
  2.             agrs=new JSONArray();( y5 E1 f& |* }) a. b
  3.             JSONObject spendAccount=new JSONObject();+ E: o# U: {6 Z  d
  4.             spendAccount.put("account_id","0H757LPD00A02");3 [9 r5 e0 q, |; c% N( D
  5.             spendAccount.put("amount",9909099090000l);, _% n7 J' |& g* k& u" B! h1 W( M
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    , |& z- W0 {+ M/ T, {% V. _
  7.             spendAccount.put("type","spend_account");1 \* G8 {# E+ u- N
  8.             agrs.put(spendAccount);  J9 t, j( B' }; t
  9.             JSONObject controlAccount=new JSONObject();+ h. }4 q+ p' c/ X" M* {
  10.             controlAccount.put("control_program",program);$ H  v6 y9 G/ D- b# E) z
  11.             controlAccount.put("amount",9909099090000l);
    + R9 Q- k$ c# @  W5 r0 A& p! f" B- Y
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");; S- \( b" L, q( E$ i
  13.             controlAccount.put("type","control_program");( g8 x' M! c5 u' t9 k9 x3 Y
  14.             agrs.put(controlAccount);: u2 m1 X. ~, e* v" ^" M8 A
  15.             JSONObject spendAccount2=new JSONObject();
    * ~' g& u, S+ w( Y
  16.             spendAccount2.put("account_id","0H757LPD00A02");
    0 P0 p/ v1 @5 R/ {! O
  17.             spendAccount2.put("amount",6000000l);( H2 `* x5 O1 m. C" ~/ r2 R
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    . B9 H. i* `1 ?0 C
  19.             spendAccount2.put("type","spend_account");5 w" U2 P9 `# m# a) q" D
  20.             agrs.put(spendAccount2);9 E" H3 d2 l9 j$ k- [% q' v
  21.             param.put("actions",agrs);
    + }  h6 I' N- P: q; w. Z! a+ A" y, H
  22.             param.put("ttl",0);/ S2 Y! V8 o2 ?; }5 o/ R
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

; ^& G! ]; l5 X( g$ m第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction9 }, z8 B! o8 S, k1 d7 Y) m

( t: k( u! Z$ j; F7 A+ U5 P  g3 H4 V& F8 x# o4 \  d) U
以下是相关代码片段:' d% k$ C  N+ G; R+ S
            param=new JSONObject();
: }1 l9 `5 f+ q) o0 Z  D" R            param.put("password","xxx");6 f, w( ^3 j. r$ N
            param.put("transaction",data);
; Z/ D# E% g+ k1 L0 g5 |+ I            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
/ x0 E0 e) {, V第六步:调用submit-transactions提交交易
1 m3 C% @3 Z- l7 Z0 K' J& A% _2 U' R# k0 R- h" u6 g5 m( T
以下是相关代码片段:
7 ^" n6 I) {: P" v            param=new JSONObject();; `' i& Y( M% ^! R6 M3 L
            param.put("raw_transaction",raw_transaction);6 l9 m+ w1 \. N" z  k% ~
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");1 u0 y/ b: V$ ^8 d
解锁/取消合约
1 Q+ r* G, z) H9 M7 Y" Z+ F首先需要decode出生成合约时候的参数0 t. Z4 U. u) }
调用list-unspent-outputs 获取生成的合约信息获取program8 p( Z/ A$ y. g/ r, ]7 f
7 {$ W2 O4 ^$ a) ^3 u+ b
以下是相关代码片段:" `2 Z7 r; k1 _1 a! C. {
param=new JSONObject();
+ \: W, j( F* c0 ?) G6 ]: L        param.put("id",outputid);  V( G& h# i6 r3 j6 ^/ u
        param.put("smart_contract",true);
% `: J8 ~3 a1 t7 Q- r# L6 [9 C5 Y        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");
( p( Q& C$ h$ o调用decode-program 传入获取生成的合约参数信息
9 p) L9 b7 `' ?6 Z. B8 S
* I% j. u8 n- J& N( V/ p8 E: {8 N以下是相关代码片段:
6 T1 M- q! [# y! C  xparam=new JSONObject();
+ y! }2 V& ?& o% f2 P        param.put("program",program);8 z8 ?" _, }+ ^: E) G
        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
. E; V4 [% _% c& n- v( m' M; g需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)1 ^! z3 @1 E1 K
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
" ^2 \0 n' ]- C2 t$ x取消合约的构造参数如下:
  j0 S% x9 S+ b0 m* G+ |3 @0 e  k' Y9 k! ^) ^
            
  1.   spendAccountUnspentOutput = arguments: [{/ q5 H# @! D) Y, k& j& k8 L
  2.                   type: 'raw_tx_signature',
    - b. V% R( ?! Y; b3 _$ e3 |2 M. T2 N
  3.                   // 生成合约第二步的pubkeylist 详情
    5 w1 A1 Q: |1 K) ?3 J8 k9 V
  4.                   raw_data: {
    6 l! r. H& m$ i9 c% R( o1 t" k6 B
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,# \1 \, B9 ]. W0 K8 e
  6.                     xpub: pubkeylist.root_xpub+ g  u8 o$ n8 f
  7.                   }
    % q% N7 H" R9 O
  8.                 }, {+ X/ @! H0 K: ^3 G; t, q
  9.                   type: 'data',9 _: ]0 r6 S; T
  10.                   raw_data: {  A/ a3 ^# T. M1 [8 B
  11.                     // 参数偏移量 在一个合约里是固定的 & s. ?0 L8 C! Y- L, f- n7 a
  12.                     value: '13000000', b$ l0 ~7 }0 `) G& j
  13.                   }
    ! s7 z9 x! E% `- H. \+ `& O9 g4 x
  14.                 }],+ \( |8 w9 f3 F9 n1 k- v+ O
  15.                 output_id: output_id," g9 N# M- W( {, W  i
  16.                 type: 'spend_account_unspent_output'! n2 t8 s% D$ Y1 B6 D1 M
  17.               }
    ! N% h/ p, B- v; q' o; X6 j! }
  18.               const controlAction = {/ D" F% V* u9 u$ ~1 N3 C
  19.                 type: 'control_program',0 [. ~& C) j; e9 y* d! o, ?1 T5 r" v
  20.                 amount: 100000000,5 e/ W0 ~& k1 ^* b9 G8 Q' _
  21.                 asset_id: asset_id,
    ! v, ?/ A3 t; R
  22.                 control_program:control_program" @' @% g8 Q4 S7 V' b2 o1 @* w" Q
  23.               }7 |7 |6 @$ V  [4 y" S
  24.               const gasAction = {
    & ?9 {* r5 E0 s0 {" i
  25.                 type: 'spend_account',; |# L6 Y& T, Z4 Y4 [2 V
  26.                 account_id:account_id,
    , e! @$ k2 @9 k! i! v
  27.                 asset_alias: 'BTM',
    / Q+ A. A0 Q- @/ l3 o9 \
  28.                 amount: 50000000
    7 a: T, }% [% e2 H3 j3 s
  29.               }
复制代码
+ B5 \& S! g* L9 k$ V7 ^" D
执行合约的参数构造如下:% o3 s) w1 @6 M2 K
) Y& t& z. F5 F' j/ D8 j" D
         
  1. const spendAccountUnspentOutput = {/ f8 I6 _3 {! p- t# {1 |  i4 h
  2.                 arguments: [{
    6 |9 i& L, t( o% i# N
  3.                   type: 'data',
      D# T( |$ d8 i% G; f, e
  4.                   raw_data: {4 j" H6 B) e1 c2 j# P3 a6 h
  5.                     // 执行合约输入资产偏移量
    2 X7 G2 I7 p! l( T. Y) y
  6.                     value: '00000000'$ d! ], b7 K; @( C. n
  7.                   }6 \/ [$ e+ u. ~! W$ b1 K3 X/ `
  8.                 }],
    * c$ ~& Q8 T. u' ?
  9.                 output_id: output_id,7 D0 B9 b: T" v& a' Q
  10.                 type: 'spend_account_unspent_output'& ?3 L9 ^) N; C1 J! @% j% r" W
  11.               }
    6 o3 {1 `; D! z) z: ?
  12.               // 合约执行提供的资产  O( u! G* a8 l5 d% X8 U, t
  13.               const issueControlAction = {
    : n% R  G: ^! r' ?* i
  14.                 control_program: control_program,
    ' m3 R5 P& k2 m* G. v
  15.                 amount:  100000000,
    9 O: n, P7 l6 R" [8 `
  16.                 asset_id: asset_id,
    - l% w, f- a8 c
  17.                 type: 'control_program'
    ( s& u+ ~$ J4 O& @/ V7 A
  18.               }  G  V9 y6 c( [+ f8 i3 D1 T6 P9 C3 ], N
  19.               // 合约执行提供的资产% |2 I/ w: x: a! d( u6 U. _
  20.               const issueSpendAction = {
    2 _" p# o3 P4 o9 T- s5 t7 k7 \
  21.                 account_id: account_id,4 M% e# }% b1 e6 ~4 l% c9 A* b
  22.                 amount: 100000000,
    , S2 p+ G+ h' Y* ]* d9 W. I
  23.                 asset_id: asset_id,- o3 j3 O4 V. i7 g5 H
  24.                 type: 'spend_account'' k7 ^$ k/ h" F- B; J
  25.               }0 W; z/ V, R7 O+ |- |; q
  26.               // 旷工费
    ( P, O. q+ ^! h
  27.               const gasAction = {* N+ E) W8 {5 j0 }2 B2 V$ X
  28.                 type: 'spend_account',5 m' j8 ]- x. c7 \+ |1 ]
  29.                 account_id: account_id,4 o. K- o- y! q6 m3 ]- r
  30.                 asset_alias: 'BTM',) h8 X7 p* p) A9 M5 _* N
  31.                 amount: 50000000
    $ [6 r% J# s$ Z; D, k
  32.               }3 d) |- _7 `1 V' G
  33.               // 合约执行获得资产对象
    $ B& \, a" \6 z2 [5 e
  34.               const controlAction = {
    5 ]4 S+ \0 F: r& f% K
  35.                 type: 'control_program',! H0 ]/ N2 x. T
  36.                 amount:  100000000,
    8 l6 S5 U, N+ d+ o- D0 i2 J0 Z& S
  37.                 asset_id: asset_id,( ^7 w& y* I: t! O. `) E' O( B- a
  38.                 control_program: compileData.control_program
    2 F+ e( s% F; L* I
  39.               }
复制代码
$ t) D; }4 O* W# I& N! p/ I
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档
# J2 K: o! `1 `$ h$ H# a6 u备注) Q# E- a6 }" C8 e5 A
调用比原基于okhttp接口javautil 如下:
5 _  a7 Y- F6 }, X+ Q  k9 x( u  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
    ; V0 [, b) l; P; l' n$ |
  2.         OkHttpClient client = new OkHttpClient();7 x5 J! ^- C7 _* m3 I, `" ^& U
  3.         MediaType mediaType = MediaType.parse("application/json");
    % u. f9 E  H* }' O
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);  h% {/ C8 P1 ]+ N+ I
  5.         Request request = new Request.Builder()
    * U, k9 m) Z6 v- J
  6.                 .url(bytomApiserverUrl+"/"+method)9 p( S% @1 P" }, E% d, ?; Y
  7.                 .post(body)
    & o' N% `( }) Y2 ?4 X5 ?  M
  8.                 .addHeader("cache-control", "no-cache")
    4 r% Z5 R( M7 m+ G
  9.                 .addHeader("Connection", "close")* f" ^+ m, B; F$ |
  10.                 .build();( J& a8 E) J: [$ b
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
    ' J, T" k- R4 P8 o! c. Q  x( M( T
  12.         }else {
    . A2 W* V4 Z3 j
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));% J' C  p' E2 L9 ~; G
  14.             String authHeader = "Basic " + new String(encodedAuth);$ J3 I% |2 D3 _$ E. j2 @
  15.             request = new Request.Builder()
    6 P: A/ P5 M7 s' b. l" n% T' u! j
  16.                     .url(bytomApiserverUrl+"/"+method)
    # a: G3 }! l& E
  17.                     .post(body)
    4 N0 _+ J* u; g' ]+ Y
  18.                     .addHeader("authorization", authHeader)
    7 h; K$ s0 N! S) j  q, j" I1 D
  19.                     .addHeader("cache-control", "no-cache")
    7 H- `9 a. B; g% |8 ~# @
  20.                     .addHeader("Connection", "close")8 y* M1 X4 g  p5 e$ a8 y  k
  21.                     .build();
    5 h( ?! q/ }0 P: z* {6 v. N) b
  22.         }
    ( C7 M8 u+ l, |. |9 w
  23.         Response response = client.newCall(request).execute();
    & C. s% O. [4 u* H
  24.         return response.body().string();( D5 }5 s/ V, b$ v" n
  25.     }
复制代码

# J5 l& q* v7 X* x  Y: b7 J. O, r6 E& s# a

! G% h. ?/ x( m7 j( @# B* M比原项目仓库:; M2 F& l, J5 D3 G8 \* Q, R
Github地址:https://github.com/Bytom/bytom# N' L5 M' [' o5 C5 M: q# ~( n, ]
Gitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1