Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:/ \' I8 K* ^8 ~+ T' L. r* e2 |9 X) q
  1. contract TradeOffer(assetRequested: Asset,
    : J8 E% E0 _$ M, }& E
  2.                     amountRequested: Amount,- }. W+ O! x/ J: X
  3.                     seller: Program,
    ' A* Y! f; W- k
  4.                     cancelKey: PublicKey) locks offered {
    + ^' G" y6 A7 h2 c# `. \
  5.   clause trade() requires payment: amountRequested of assetRequested {+ }: l+ I! k/ C, t$ |/ D2 R3 @
  6.     lock payment with seller
    9 N, B# d& v7 g4 b+ d& ]
  7.     unlock offered! a# K7 _( m- P- C; L- X
  8.   }
    8 u8 e5 w, q& b% `6 b6 S
  9.   clause cancel(sellerSig: Signature) {
      G! D: g1 [5 {" |5 ^4 K- X) [3 @8 J
  10.     verify checkTxSig(cancelKey, sellerSig)
    ' z2 L9 H1 d/ R) Q; t
  11.     unlock offered1 D! ?/ U9 j  H& ~2 U, Y* V/ G0 C0 Q
  12.   }5 l- {* F( Q/ J8 n, C; |7 S' S
  13. }
复制代码
# C) o9 H, ^9 y' ]" H
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
& A$ o: y, C- G0 T锁定合约" v# o! p: g' {( ~. n
第一步:调用create-account-receiver 生成 control_program7 _$ q. s+ F- _1 F. R% J( F: T
& R( d$ p; F% B9 C3 {4 e. }1 q
以下是相关代码片段:
  B  j. j. K: Y- @sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");
& J3 x& U5 I  [" |+ E) _第二步调用list-pubkeys 获取 pubkey* y2 J0 V/ `! |# y

: b% W& T7 V: _: E, I( z  x3 l1 n- l; Q: X. W- t6 r; _! {" B0 M5 r
以下是相关代码片段:! K2 B0 i2 R4 W
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
# ~/ k/ P) N" O2 y8 N  O: {第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
8 O/ q& h- n. i0 ?* C0 N* U* @# L5 p0 m! G3 z8 d6 e: t( W- I3 r9 }+ M3 @

  F# g, O- A2 |6 {' @' U以下是相关代码片段:
1 e4 ^5 N1 Y2 @           
  1. JSONObject param=new JSONObject();! l3 ]4 z3 U* Z2 y' [9 \; m
  2.             JSONArray agrs=new JSONArray();
    5 l: R- O% c6 n
  3.             //合约的四个参数值; R' k4 l7 f6 b* W8 I' L% U
  4.             JSONObject assetParam=new JSONObject();- R; P. q: i* d/ K5 u  |, ]
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");6 Y6 p9 V) K/ E# K( ~0 a
  6.             agrs.put(assetParam);
    , \* r7 Y, X3 R) b
  7.             JSONObject amountParam=new JSONObject();) `! G8 g/ }! ?) p" \: I9 B% Q
  8.             amountParam.put("integer",200000000l);6 w( `; T# c% R5 \# F
  9.             agrs.put(amountParam);, u2 q& c( g+ Q8 `  T  ?
  10.             JSONObject programParam=new JSONObject();
    4 L. {: d( X4 S5 `
  11.             programParam.put("string",control_program);
    ) `( G6 Y( V7 N: p7 \  y* v
  12.             agrs.put(programParam);
    . w6 D. x% b8 q! O5 t
  13.             JSONObject publicKeyParam=new JSONObject();9 J# ~- a9 q! M  w) C* @
  14.             publicKeyParam.put("string",pubkey);2 K; M; ?/ B3 J. x# Z* ]: L
  15.             agrs.put(publicKeyParam);% ]4 n, W1 @' v3 ^- w
  16.             param.put("agrs",agrs);5 O) h, x0 @, t- X6 l% a
  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 } }");
    5 t" M' Z8 B9 J8 {( E
  18.             //调用编译合约
    2 V/ o. I) C  n8 l+ ~
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码

. t% K6 n( \/ b7 i) I& c第四步:将program 传入build-transaction接口去build一个交易的到data- |1 e) Y* v9 @- g5 B7 r  ^
: Z3 b$ b1 L$ U9 o9 z

/ N6 k* j( m$ j3 |! U. E以下是相关代码片段:+ _% v% y# P" J
           
  1. param=new JSONObject();
    2 T  p0 k! a2 S
  2.             agrs=new JSONArray();
    ! A( ]! |" U  K1 u! s
  3.             JSONObject spendAccount=new JSONObject();/ t) g# _) c  V
  4.             spendAccount.put("account_id","0H757LPD00A02");. S7 V5 J, N) h3 s# g
  5.             spendAccount.put("amount",9909099090000l);
    & @6 s( Q5 \* j  j2 E
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");( W/ M. E2 x9 z' ^% Y
  7.             spendAccount.put("type","spend_account");
    , K' P" k- [1 A. l5 E+ E2 e. L
  8.             agrs.put(spendAccount);  V# s% Q4 j% s7 ]- k% c! W
  9.             JSONObject controlAccount=new JSONObject();
    2 D/ z7 ]2 A: `4 R5 i5 w9 z1 v
  10.             controlAccount.put("control_program",program);+ E: J& e3 W% |% K" G9 z2 j9 F
  11.             controlAccount.put("amount",9909099090000l);5 |" v9 j2 ]4 \- h6 i$ `
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");0 O2 Q6 S6 ^* g, F$ U; r# T
  13.             controlAccount.put("type","control_program");
    " F# _. S, w; N9 B
  14.             agrs.put(controlAccount);
    # {  q  }( y  ?/ L& D8 G: X
  15.             JSONObject spendAccount2=new JSONObject();) e$ Z8 P  N0 ^5 b- M' d, R
  16.             spendAccount2.put("account_id","0H757LPD00A02");8 Q/ {9 I: d! Q1 W$ v& X! ?
  17.             spendAccount2.put("amount",6000000l);- D* C2 a! C' E1 x( a
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");0 M8 Y/ n  L, @0 A4 z
  19.             spendAccount2.put("type","spend_account");
    2 g2 K1 R& w  x9 v" M% \
  20.             agrs.put(spendAccount2);
    , S& i8 o0 [# f$ z, W: E  o! q8 h
  21.             param.put("actions",agrs);+ F; i, n: _1 z5 L3 Q
  22.             param.put("ttl",0);! x) o) O) Z: k5 O, K
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

( s7 G( l7 s5 d5 x9 k第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
) A# N2 u. d' w$ u% g3 w0 K, ^( e' ?1 k
% q  l/ T0 P+ N7 \& {& |" T1 F2 l
以下是相关代码片段:
2 m: [0 e" ^! d8 t            param=new JSONObject();
9 Y0 Q4 t7 _7 W! k$ E            param.put("password","xxx");0 I! n, k2 n1 T4 u4 Q. b0 R
            param.put("transaction",data);
- j2 B$ k2 L' d7 j4 Q            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
5 b: @8 n& k# s$ _9 b第六步:调用submit-transactions提交交易
$ s6 z3 {; |! t5 s6 A7 k) s1 W, I+ P/ o& J0 q( D+ Z
以下是相关代码片段:% w2 J" v  a8 N! f" Q/ T& u3 `
            param=new JSONObject();: ^3 \# ^' i( V  R) x9 G
            param.put("raw_transaction",raw_transaction);2 |  ~  f, D  P! A, _( y* I2 q# w
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
$ G, l, t. K0 z0 R解锁/取消合约/ t. _: @( A7 G4 @6 n% g
首先需要decode出生成合约时候的参数
. X" ~: @% _* L' t调用list-unspent-outputs 获取生成的合约信息获取program0 j$ p; ~$ `: g6 w! y
2 e. v$ ?' l5 \. @& N7 g3 D; n
以下是相关代码片段:6 }* J0 V0 {+ Y: b5 x9 |
param=new JSONObject();
7 P/ _$ [- N9 i  I7 V; M        param.put("id",outputid);0 q: _2 D& h0 |5 S  \
        param.put("smart_contract",true);; I1 _3 D. ]( s' t, q
        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");5 ?. Z* b7 K7 @: v% g
调用decode-program 传入获取生成的合约参数信息
% a7 ^' Z/ b9 S
& M) a0 H' `' \  W以下是相关代码片段:$ f. Y' n4 u9 ~* _1 a' a. J
param=new JSONObject();
- M8 p% Q: J" O! B8 `) b        param.put("program",program);# w1 s9 b% x  y) x, p! v2 S! e# b
        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");  c- i3 h+ Q0 M+ n2 y
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)2 L' `! P: w0 Q6 K
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可0 a8 F) \, h3 j& ^- I
取消合约的构造参数如下:
0 w& `, m/ w$ u- P# @* j0 j3 O5 @+ w+ c( }( X
            
  1.   spendAccountUnspentOutput = arguments: [{
    7 z  `' U; b; q) s3 p
  2.                   type: 'raw_tx_signature',
    9 I2 H! d+ E' \/ A
  3.                   // 生成合约第二步的pubkeylist 详情1 ~& Q' `$ \, M9 ]: O
  4.                   raw_data: {) t. v4 R( h( m0 O2 e& y) J
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,' n( Q. y! v3 U4 A
  6.                     xpub: pubkeylist.root_xpub
    8 _  V) {  m5 X$ D1 N6 c
  7.                   }
    " ?8 [# ~; G7 C! K
  8.                 }, {
    / S/ z( K1 a' o. {8 ]
  9.                   type: 'data',
    " k$ y- ?% v: t+ n( h
  10.                   raw_data: {
    0 [6 V2 A, t/ x7 g/ S
  11.                     // 参数偏移量 在一个合约里是固定的 $ z: t: A/ R" z& x& h  ]
  12.                     value: '13000000'
      b; U5 B7 N7 a; o
  13.                   }
    8 j' v8 L. y! M1 b4 {
  14.                 }],
    3 ^' O0 D: V8 D$ \% B
  15.                 output_id: output_id,# m% v, `& Y1 g, j1 Z& S/ W" G
  16.                 type: 'spend_account_unspent_output'2 E% D- Q  x0 Y/ z/ l* V; T
  17.               }+ O5 \; Y# U2 ]8 l( c% o' t7 ?$ a8 y
  18.               const controlAction = {
    % _# z/ Z+ `$ q% w1 k0 c8 B
  19.                 type: 'control_program',( H0 F4 O  W# O" `
  20.                 amount: 100000000,
    8 y) b; o1 @# b1 [
  21.                 asset_id: asset_id,
    / ~, _; R- k; |9 F& X
  22.                 control_program:control_program% `9 l, @* [; p8 Z4 {; d* w8 c9 m. h; m
  23.               }) d7 }; e0 c7 m9 L& N  p
  24.               const gasAction = {
    - g5 j# B5 i9 O4 J! A9 F# |
  25.                 type: 'spend_account',
    % ~  i+ O) i& ~0 u8 \' q
  26.                 account_id:account_id,
    . a  ?) q  X& h( Z0 \
  27.                 asset_alias: 'BTM',1 ~3 b" K0 E% h* _' y
  28.                 amount: 50000000
    ) F- [! W9 W# K% a
  29.               }
复制代码
& y( t( `+ Q9 ?; E
执行合约的参数构造如下:
, e7 W. Q- O  ~& [% Y& N0 s9 Z" c& W. ^
         
  1. const spendAccountUnspentOutput = {! V0 p; g. J) L* m2 C
  2.                 arguments: [{8 E& j3 o! y. i2 e
  3.                   type: 'data',% c3 h$ k+ V+ b: Y( W
  4.                   raw_data: {
    + |9 d5 P0 t  P8 F
  5.                     // 执行合约输入资产偏移量; q) d& f$ \" p% B' Y$ |4 d
  6.                     value: '00000000'( c7 T' K) J, B; ~: M, {
  7.                   }
    * q/ D4 V$ g& b$ b
  8.                 }],# Q$ [) J1 U& t9 L+ R
  9.                 output_id: output_id,. s; x/ q& J$ E3 a
  10.                 type: 'spend_account_unspent_output') y( B( b: Z7 L& [1 G5 [, N
  11.               }4 J6 J& S% `4 b+ O5 E3 j) F
  12.               // 合约执行提供的资产
      Q( q& h2 W/ G/ _6 J) D, z
  13.               const issueControlAction = {9 n5 ?. B7 r9 w8 ]) `
  14.                 control_program: control_program,, |! b) L9 M8 F
  15.                 amount:  100000000,/ B3 Q9 Z3 w# i% S, A3 J) R
  16.                 asset_id: asset_id,
    + [. n6 a$ ?! _$ [
  17.                 type: 'control_program'
    / }) Z  b4 ^* `0 K  Z
  18.               }4 ^8 h5 m# C( b: L& h
  19.               // 合约执行提供的资产
    / {3 W& [% A- q6 w: ]5 n
  20.               const issueSpendAction = {" h% S$ ~1 o" [3 L
  21.                 account_id: account_id,9 P" e7 y  l8 `4 E/ `0 h/ D( W
  22.                 amount: 100000000,: l. I) @- p: h
  23.                 asset_id: asset_id,, \& d* ~: X! W  V5 `% }! s3 x
  24.                 type: 'spend_account'
    - X, `1 w8 ]; n) N/ E
  25.               }9 I: u. ]9 j; g8 |# B- w
  26.               // 旷工费
    1 S9 t0 C% A3 L0 S) v4 B$ b
  27.               const gasAction = {
    ) `1 Z% a- F- D7 B3 K8 Q6 x% G
  28.                 type: 'spend_account',
    + W$ {5 ^3 o; a% Y
  29.                 account_id: account_id,1 J2 H0 n5 b1 ]; _1 z
  30.                 asset_alias: 'BTM',7 j$ Q' B2 C6 v1 }# Z' p" ]
  31.                 amount: 500000009 I$ W3 U: x( w5 _. T$ X9 c
  32.               }
    $ U& ^6 P0 `5 j. @- U8 ?
  33.               // 合约执行获得资产对象7 i+ z7 _$ G9 N+ }" s& A6 Z
  34.               const controlAction = {; e9 I  C% }- d% C0 \
  35.                 type: 'control_program',
    ( k  W# n' K/ n. K) Z+ O, D* I
  36.                 amount:  100000000,0 l" P% A! l& g# l
  37.                 asset_id: asset_id,1 n' d! o& r1 ^3 k0 Z. }
  38.                 control_program: compileData.control_program; G6 N" ?8 e' Q6 W, K
  39.               }
复制代码

' X. X& \, _' Kbuild 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档2 n/ P# b2 k9 c( o+ O
备注
) X/ T5 h6 w# I3 Z调用比原基于okhttp接口javautil 如下:6 v  G" ]' D" h
  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {* M8 I% ~2 `6 c# {
  2.         OkHttpClient client = new OkHttpClient();0 Z" t0 A( E) [& L# K
  3.         MediaType mediaType = MediaType.parse("application/json");
    0 @6 k* M" P" A1 g( k
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);# I3 Z2 h, h" s9 `; ?- ~9 |
  5.         Request request = new Request.Builder(); r6 J# C$ y3 ?- l0 Y
  6.                 .url(bytomApiserverUrl+"/"+method)
    6 n* t# @) {# L
  7.                 .post(body)5 p$ F$ s! T9 R0 T* A5 J; Z/ F
  8.                 .addHeader("cache-control", "no-cache")  g; e( \0 F: E9 Q% i: K
  9.                 .addHeader("Connection", "close")1 x0 S& j" J4 f* @6 B  S; _
  10.                 .build();! H8 e; h! X; Y/ q4 f
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){! F' m' O: p2 v2 B/ {
  12.         }else {. G: [0 l& w( G" N2 W
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
    # l8 V: U- @# A$ j
  14.             String authHeader = "Basic " + new String(encodedAuth);
    : w1 C/ D( _% b" I/ {. d# v
  15.             request = new Request.Builder()& |4 `# e9 U8 i
  16.                     .url(bytomApiserverUrl+"/"+method)6 \3 X: V4 J# m' h
  17.                     .post(body)& I& z' m+ {3 B! W! i: U  S
  18.                     .addHeader("authorization", authHeader)' y; {- F3 u5 R% b1 |) A- T  k
  19.                     .addHeader("cache-control", "no-cache")
    6 r$ ?- T% \# l
  20.                     .addHeader("Connection", "close")2 L" l9 t8 B5 x0 B
  21.                     .build();
    ) g# i. F+ y# Z0 ^4 y
  22.         }  Z& `+ S* X2 t$ b
  23.         Response response = client.newCall(request).execute();, ^5 ]9 g) x# h  Z, e/ b, W* ^5 O; H
  24.         return response.body().string();) D/ B3 {: i  s8 _! M6 y
  25.     }
复制代码
9 d7 v  I1 \  J" ^
6 ]4 \) Y  D3 Z' S9 z
. \( j5 Q( m2 X! ~' I
比原项目仓库:$ i( l$ p- i  r- U. w2 }
Github地址:https://github.com/Bytom/bytom$ `; p6 |( y6 O# n( l- {4 L, [: @3 A
Gitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1