Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
  x9 Y" G! k2 \) N& I9 X3 J% [
  1. contract TradeOffer(assetRequested: Asset,( ], R5 `4 h8 v& x
  2.                     amountRequested: Amount,
    # t( u9 P6 r* x  h$ c0 ^1 |
  3.                     seller: Program,% U; c  D" `) \5 Q
  4.                     cancelKey: PublicKey) locks offered {! k& [; u7 ~8 L2 p4 ~. \
  5.   clause trade() requires payment: amountRequested of assetRequested {
    " h3 V% D( X9 F
  6.     lock payment with seller" X8 Q2 a  [$ {, X$ m
  7.     unlock offered
    * n# m! c# n& x: x* D% B
  8.   }: g, \8 |7 i+ v1 u; `
  9.   clause cancel(sellerSig: Signature) {
    * H' v( q9 p- l  m& Q' [
  10.     verify checkTxSig(cancelKey, sellerSig). _8 C5 i3 n! F' e
  11.     unlock offered
    / S! ?- T/ [' N6 t
  12.   }
    $ q" D: k! ^$ _' o5 }
  13. }
复制代码

, W( v# l& f; X4 p$ M导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档" R1 Q* c8 K, w* E/ R. B0 U
锁定合约- y8 A2 r! _: L0 _8 M7 p1 Q
第一步:调用create-account-receiver 生成 control_program
+ Y$ Z9 F5 v$ T9 x5 Z7 L* k7 w0 L
; ^4 B4 m% h5 ], K0 e% R8 v% V以下是相关代码片段:/ L8 X- d) p6 B  |0 }
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");" ~- B) Z: N2 y# o" D& g
第二步调用list-pubkeys 获取 pubkey
" M. w7 n  q& P4 _, l, i/ P6 Q' O% k# H! y" `8 _& l
4 B; k4 {& b$ x
以下是相关代码片段:; r  p# z9 [5 L- l* W
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
3 ?+ B+ k  ~2 b$ r! S& x6 i8 l第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
% v/ e9 }) j' V" ~* J& }2 n0 E, H( Y1 O$ O. b
6 S$ N0 t- \& O% w0 f4 ?7 \; `/ f
以下是相关代码片段:9 j) |" W8 ~! X! v; K
           
  1. JSONObject param=new JSONObject();
    7 [7 ^8 L5 F% `4 H% u. D
  2.             JSONArray agrs=new JSONArray();9 j# K4 G' }/ H8 \" p
  3.             //合约的四个参数值
      m2 |0 r2 y. _/ t1 F
  4.             JSONObject assetParam=new JSONObject();
    5 q) [0 b0 |% X' i& y8 f: c- H
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
    # D9 K2 w( r4 {/ T
  6.             agrs.put(assetParam);
    7 l6 ~1 f. ^( l( I% w8 l
  7.             JSONObject amountParam=new JSONObject();
    % S* Z3 d2 {# \2 j& ?$ k
  8.             amountParam.put("integer",200000000l);
    : K% _2 X0 T" J# q: ]
  9.             agrs.put(amountParam);
    % j' G" D7 j" }
  10.             JSONObject programParam=new JSONObject();
    ) J7 ?* ]+ L: M- t4 G- E% y/ |% `
  11.             programParam.put("string",control_program);
    # c. W" Y( [9 X8 @" o
  12.             agrs.put(programParam);
    * P, y9 Y- w$ I
  13.             JSONObject publicKeyParam=new JSONObject();
    : S3 F7 u- o! |
  14.             publicKeyParam.put("string",pubkey);: t3 ]2 W7 W  l. d
  15.             agrs.put(publicKeyParam);/ n  T  w! n5 _8 }& t0 o
  16.             param.put("agrs",agrs);& C8 V; `* v" H+ s; h( g
  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 } }");
    8 ~9 q: m. X$ R9 m
  18.             //调用编译合约
    2 Q- X8 p1 ]! q' w" Y0 K
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
/ ?" i; R% a" Q) D
第四步:将program 传入build-transaction接口去build一个交易的到data& t9 V5 G5 o( M- t1 |% ~5 R

# V; ?8 P( x) v0 o7 k$ K+ V1 O' O+ Z  Y3 i! D9 l, }
以下是相关代码片段:2 }: {* D' j4 u  O
           
  1. param=new JSONObject();- |9 K4 E' h4 q1 `+ y! C0 p
  2.             agrs=new JSONArray();7 n- U0 f, S8 T$ p$ @% N% }
  3.             JSONObject spendAccount=new JSONObject();
    ' z) Z" Q  N( t* w- z5 A% V2 N
  4.             spendAccount.put("account_id","0H757LPD00A02");
    / O. O5 _' g+ |6 R
  5.             spendAccount.put("amount",9909099090000l);& P* z: A" `% q" S
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    + x2 c! A6 q/ W1 \9 Q5 z6 `
  7.             spendAccount.put("type","spend_account");, d* L# w4 [3 [
  8.             agrs.put(spendAccount);
    # Q/ W( b# b/ ^2 G9 j* G7 e3 j9 g( @  f
  9.             JSONObject controlAccount=new JSONObject();+ @5 z. f' i1 Z' f2 z- Q+ e$ S! z
  10.             controlAccount.put("control_program",program);# L! u% n% M$ j' e' B+ ]+ k7 x
  11.             controlAccount.put("amount",9909099090000l);
    ! \; _4 c  ]! `  j1 T
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");8 B- V- V. `" z
  13.             controlAccount.put("type","control_program");
      Y4 k: i5 _1 l9 G5 i# r
  14.             agrs.put(controlAccount);
    , q' |6 g$ p: m# {$ o
  15.             JSONObject spendAccount2=new JSONObject();( g7 }+ f/ {! N' E, ~3 b
  16.             spendAccount2.put("account_id","0H757LPD00A02");
    2 k- Z, y* C9 [# R
  17.             spendAccount2.put("amount",6000000l);
    3 F- C+ M, ^5 G
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    # S( X) [' q1 ~$ W: W. v: _+ i
  19.             spendAccount2.put("type","spend_account");- g5 m5 b- |4 M4 u
  20.             agrs.put(spendAccount2);4 T& b+ ~% g" g5 \4 l6 F+ i2 T
  21.             param.put("actions",agrs);  r. _/ k# {4 s6 F% R" P7 B
  22.             param.put("ttl",0);
    ) |0 f. s, T8 p% a2 Q. l* d3 A0 ^1 G
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

+ t3 n" J. A6 h3 c5 S第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
) b, \& ^8 j' r6 @% ?( n
" i4 k( P7 m! q( P8 D' G9 N8 P% p# L* C6 l1 I% Q9 @
以下是相关代码片段:
* h% ?+ |! c7 _0 Y; O$ i* ~0 d; [            param=new JSONObject();
3 P- b4 O# F& Y6 x5 Z            param.put("password","xxx");
! }* }7 q, L3 f  X3 }            param.put("transaction",data);) D* a: x) ~( R3 M+ z$ p* H
            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
1 y+ H+ w6 u+ [3 }第六步:调用submit-transactions提交交易6 H3 T4 U1 [: J, j
- j9 l- Y- _, I0 x' j
以下是相关代码片段:7 b! j4 ?1 ?+ ]% M' }' ~: Z) \% X
            param=new JSONObject();
2 V! }; K1 K& [2 Z% g            param.put("raw_transaction",raw_transaction);/ ~5 I( M" z. ?) G# _
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
& r( U0 D& _* `/ |解锁/取消合约
8 G2 _; I* k" q: X; p5 N' T) Y首先需要decode出生成合约时候的参数
( F4 s% R- S, v8 ^+ \! ]' C调用list-unspent-outputs 获取生成的合约信息获取program
) I, r6 _4 o0 i; v% T7 j7 S$ T; J: H2 A- \3 t3 u' t" ?1 i
以下是相关代码片段:
5 c& M& @+ O1 R: c' Vparam=new JSONObject();
* I* l5 N+ R3 Z7 x2 ?6 V3 `2 M        param.put("id",outputid);3 J8 P* g) T; {6 i: N$ {
        param.put("smart_contract",true);, b0 k$ s4 Q, q" \" G, @. m
        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");3 c" Q6 K) c0 E
调用decode-program 传入获取生成的合约参数信息
" O, y2 Y8 k7 n8 ?/ b. f
6 D$ x  x# q# u5 S以下是相关代码片段:
( z: L' o( c! }param=new JSONObject();
" D$ h3 I3 O( Q, H4 d5 }        param.put("program",program);
/ Z6 J5 F9 I. b  o5 `2 c& x        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
8 P. G( s  n: T' s/ w! z需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)/ `0 }1 K) C$ C# e! V- s
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可6 _+ a! W+ v0 K0 m7 c
取消合约的构造参数如下:8 T4 v1 z% J/ A: {

1 E, Y0 q! b+ u  H) G  P            
  1.   spendAccountUnspentOutput = arguments: [{
    " x- y! ^& u' B, ^) O/ T# E
  2.                   type: 'raw_tx_signature',; m/ Y, z* t1 }
  3.                   // 生成合约第二步的pubkeylist 详情
    : J6 z4 {: I$ S0 E- X
  4.                   raw_data: {
    " K* O) N6 |3 I: Q% ?( n; s
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,! z& Y* I, P# x4 u2 T' e
  6.                     xpub: pubkeylist.root_xpub
    ) ~/ {( Q- _' q* R* T- \
  7.                   }* Z6 W* f9 g# _- [6 v& X1 o
  8.                 }, {/ `; b- K* o/ G# Y, N! u% z
  9.                   type: 'data',5 w' [5 e/ Y% P+ e( K
  10.                   raw_data: {; B2 o- g# q5 p% Q2 j
  11.                     // 参数偏移量 在一个合约里是固定的 0 a" t: y( `% l
  12.                     value: '13000000'0 g: R. N( J; v7 J4 J
  13.                   }
    0 L( T  c" g7 ?) O/ c' ?
  14.                 }],; d4 @5 z, Y8 O. ?- h2 Z' c( _
  15.                 output_id: output_id,) [9 D+ F$ }7 @" @! s
  16.                 type: 'spend_account_unspent_output'
    " p# s$ ^2 S& T8 a7 u( S7 A0 E
  17.               }
    9 M% }9 M# a) s. w5 J: X
  18.               const controlAction = {
    8 E! M! R0 s3 [3 ]3 B
  19.                 type: 'control_program',9 s: [! v% ~& D2 K1 z7 f& Z+ U
  20.                 amount: 100000000,
    ! g  [8 p& @' Y! S# Z0 V- x
  21.                 asset_id: asset_id,7 R+ @: T0 Y, G- B* v9 V
  22.                 control_program:control_program
    ) k  G/ ~. c# m. n2 r+ M6 O. F( `
  23.               }. V3 A2 r" J0 G6 _, X
  24.               const gasAction = {6 w) K$ e" s" r3 U+ R# w
  25.                 type: 'spend_account',: K- Q9 X% N8 D9 @9 Q
  26.                 account_id:account_id,) i! v4 z4 N; s( \. j
  27.                 asset_alias: 'BTM',2 t* v6 x9 M# @) z4 f
  28.                 amount: 50000000
    2 v/ F- Z6 W6 p. K% z9 V
  29.               }
复制代码

. L* G; `, Q! P; ~  B; u: N6 X执行合约的参数构造如下:
) x% c( F" [- H- I! V3 a
$ z# P6 d- n$ X1 _5 [  K6 l0 w0 o         
  1. const spendAccountUnspentOutput = {" e: E9 m# J% o
  2.                 arguments: [{
    6 O- @1 h% c3 Y/ m# g' N
  3.                   type: 'data',
    / @1 S. p' k% Z7 h' k$ {: q
  4.                   raw_data: {
    1 O8 S9 K. ^5 S' C
  5.                     // 执行合约输入资产偏移量9 O9 {4 ]6 h* t. g) ?8 ?; x/ R
  6.                     value: '00000000'
    + ~) E5 Q. y9 ~5 l( D; @
  7.                   }
    6 V/ G! g7 e: e+ N) P* U/ X- ?# H9 d' n1 f
  8.                 }],
    8 U1 F& C( i/ O6 R
  9.                 output_id: output_id,
    / D9 t4 h2 ?0 Y
  10.                 type: 'spend_account_unspent_output'
      {" g4 d* ^* y  m
  11.               }
      i/ t8 I* c$ G; q, w
  12.               // 合约执行提供的资产
    . ^6 [7 u" p4 S* }* b
  13.               const issueControlAction = {! Z/ m/ s4 s5 Y: R4 R+ B" m$ r1 O+ u7 m1 R
  14.                 control_program: control_program,4 x( R/ C2 E+ C* [8 ]
  15.                 amount:  100000000,2 I' j; C* {1 b7 j" m% @5 B
  16.                 asset_id: asset_id,1 T# L/ n" u# h( Q0 F( z
  17.                 type: 'control_program'6 z+ @3 N+ j0 \* D
  18.               }
    8 ]% |3 F8 E8 W# J' y+ _" x
  19.               // 合约执行提供的资产! d7 B& L$ }# H4 d
  20.               const issueSpendAction = {& Z, W: n3 {1 K
  21.                 account_id: account_id,
    % S( y5 G/ K+ s3 `
  22.                 amount: 100000000,6 Y8 [+ v6 H. P, _/ h; A
  23.                 asset_id: asset_id,
    , d9 p+ ]6 m1 y1 U( p& m
  24.                 type: 'spend_account'7 g' W! o) Q% i# O+ l( k3 p* k6 C
  25.               }/ f$ `. |3 D, a: @1 B+ s
  26.               // 旷工费4 h3 E* o0 ]# r! Y( r  E
  27.               const gasAction = {6 \/ G! a: w$ U
  28.                 type: 'spend_account',0 H& x  z3 L  c
  29.                 account_id: account_id,2 [7 B& x7 R: S' E! Y" ?# |( n5 H- X
  30.                 asset_alias: 'BTM',9 c7 J; _7 A  g5 R
  31.                 amount: 50000000
    8 A' `/ Z# m+ {" ?
  32.               }9 b" R, r1 R( c! S  O$ |5 y
  33.               // 合约执行获得资产对象
    5 R6 F4 o* [. g. \9 n
  34.               const controlAction = {
    * o' o) \1 y! q/ y( E
  35.                 type: 'control_program',' ^- [! I( f2 u
  36.                 amount:  100000000,7 r0 a+ c6 L, u5 S3 @' T
  37.                 asset_id: asset_id,6 g. t5 _6 Z# N3 `
  38.                 control_program: compileData.control_program
    6 r, A3 I9 k; E7 B! S: t
  39.               }
复制代码
& ?! }5 F* K2 D( g+ c
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档
1 f9 O5 N/ d/ {0 h5 d! z4 Z备注# T- r1 C6 R; ?6 v
调用比原基于okhttp接口javautil 如下:1 X$ j5 v) j7 }
  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {8 h5 S4 d5 Y& |) M
  2.         OkHttpClient client = new OkHttpClient();
    8 M* L. W) s3 U6 c
  3.         MediaType mediaType = MediaType.parse("application/json");
    ; I* H- v& Z) D) @! U, ~
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);
    $ C3 H' o  P$ s# d. t
  5.         Request request = new Request.Builder(); j0 c. }. f: t  ~, }
  6.                 .url(bytomApiserverUrl+"/"+method)
    ! S8 }$ {% o* f* {5 Y
  7.                 .post(body)6 c5 ]! {5 w: [* q* g) f  Q
  8.                 .addHeader("cache-control", "no-cache")
    4 m' l5 K7 b; t0 i$ {
  9.                 .addHeader("Connection", "close")
    : L8 S" a5 O; O
  10.                 .build();: A7 e/ @0 j0 P0 Q0 D2 J4 L
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
    2 B9 x7 b; q7 `3 {+ k
  12.         }else {! c* n" E7 ?2 W/ P$ H3 F4 ?' Q
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));9 _% I: ]* }/ {
  14.             String authHeader = "Basic " + new String(encodedAuth);
    9 Y1 p6 c# E! @  J( Z2 o
  15.             request = new Request.Builder()
    : R; b+ F# u4 x
  16.                     .url(bytomApiserverUrl+"/"+method)
    " ]1 E1 `( L) i* f1 u" }. Q
  17.                     .post(body)/ g$ f) t8 t4 T5 t3 }6 ~
  18.                     .addHeader("authorization", authHeader)4 Z0 N! `0 D$ @9 w+ C' x, H% k
  19.                     .addHeader("cache-control", "no-cache")  g) x* n3 Z0 ]8 [
  20.                     .addHeader("Connection", "close")
    9 E1 M  _! F0 ?* c
  21.                     .build();
      A/ g; s2 h% L7 ?7 E) I
  22.         }; H* N. l2 m( H9 s6 d; m5 }. R
  23.         Response response = client.newCall(request).execute();
    $ D+ ~0 k5 C& J; }4 z. \: Q" K
  24.         return response.body().string();- j. s% I$ E2 z; t+ x8 a
  25.     }
复制代码
- M4 ]5 W: E- r8 S8 E# j* m
# j5 R5 ]: h3 a: u

1 A, L$ j6 Z& l) c. }比原项目仓库:$ H! X: Y! ]4 _# Y: R# u" c8 g0 k4 @0 V
Github地址:https://github.com/Bytom/bytom" x/ _! n! {( U- W7 Y2 q1 n/ {
Gitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1