Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
, H' t2 A% l8 B3 F$ P2 K! z
  1. contract TradeOffer(assetRequested: Asset,
    " p9 Y- E" g9 ^) {$ K' }: y
  2.                     amountRequested: Amount,
    8 u; p* L0 _2 s8 p) e
  3.                     seller: Program,' Z3 y9 C- T0 j" b6 d. [
  4.                     cancelKey: PublicKey) locks offered {
    ( H0 d. L3 C9 R& W  f' i% @
  5.   clause trade() requires payment: amountRequested of assetRequested {! l/ N; t5 A. j3 m- U& s. b
  6.     lock payment with seller
    " n5 k7 O+ j( p
  7.     unlock offered
    - `1 W8 {- g6 T* V* d2 d+ V
  8.   }
      E& r* b! i( t8 B
  9.   clause cancel(sellerSig: Signature) {, T5 U- a5 U! `- {
  10.     verify checkTxSig(cancelKey, sellerSig)* M( `4 U4 C/ I: ^9 y( _2 C9 {
  11.     unlock offered
    ; d( m6 g: A7 I! f! S6 ?3 s2 Y
  12.   }& W+ T. r- ]' x7 L
  13. }
复制代码
/ b/ q* L4 }3 w  c0 \
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
! p( P0 n7 E1 Y% n  U7 h锁定合约( |$ s1 A! \. g! m
第一步:调用create-account-receiver 生成 control_program" v/ R& M7 i" J; v0 j8 w0 K
0 o  V- A2 c5 f4 h
以下是相关代码片段:
. O9 B& V8 s  FsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");& D5 n3 |/ r& }+ n
第二步调用list-pubkeys 获取 pubkey
6 o# u3 H& J+ @5 A1 @6 s5 j& ?
+ m4 G1 ?) `) y
# ~( t1 Z  u$ h+ k# Z# k4 q以下是相关代码片段:
% z  l1 i8 N8 }9 w) ^$ gsendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
7 ]! b! D% `  [3 L4 M第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
* A- u4 k: g6 o/ a- S( g2 F0 o( k- P, u
, G6 r9 Y' p' v$ \$ I* s( W
以下是相关代码片段:4 M; W- X- Z7 r: u
           
  1. JSONObject param=new JSONObject();
    7 [, Z( H0 S. ]& B' R- U2 [
  2.             JSONArray agrs=new JSONArray();
    . {7 O* O# M2 Q7 Z: h8 s
  3.             //合约的四个参数值
    ( \4 X# `+ m/ c( S4 I6 C# S5 I  Z
  4.             JSONObject assetParam=new JSONObject();2 H( _, N# P' U* G' Z! L( |
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
    ; R6 L1 T/ t' v4 |
  6.             agrs.put(assetParam);
    9 [3 |# x4 ?1 K& c8 Z' ^0 ^" U
  7.             JSONObject amountParam=new JSONObject();' J( r! [' s4 G* L2 N
  8.             amountParam.put("integer",200000000l);4 I( k7 q* {0 g. B$ F& R# G
  9.             agrs.put(amountParam);
    & t5 H  K9 L! q2 y3 \3 D1 t" j
  10.             JSONObject programParam=new JSONObject();
    ! p5 Y( b: @' n2 e: w! H; z
  11.             programParam.put("string",control_program);
    : @0 d8 R, F( ^, t% K' }
  12.             agrs.put(programParam);5 V/ f, c/ }: U* w' f9 k9 V4 z
  13.             JSONObject publicKeyParam=new JSONObject();
      t3 O4 o! D9 h( i) y4 h% Q7 A, E" H
  14.             publicKeyParam.put("string",pubkey);
    0 u) \& \6 I2 `( M* J( _
  15.             agrs.put(publicKeyParam);- y/ s8 B5 N& O0 o" h- Z  C* v! ^
  16.             param.put("agrs",agrs);
    / D2 }' d+ }2 G% {$ u0 P
  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 } }");7 h5 M: n3 J8 r9 A& p% P  A4 d
  18.             //调用编译合约* o& }7 F3 v4 |$ l) a- c
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
( Z2 r. n; p6 z& H/ V) S+ @
第四步:将program 传入build-transaction接口去build一个交易的到data+ h" N0 z; {) D, @7 U' |8 v" [

6 m: ]+ ?! {1 r$ `( t
# j8 I& _- }& C* [% \/ C3 m以下是相关代码片段:
* K8 J0 ]- G+ L1 W0 x6 ^, B4 X           
  1. param=new JSONObject();& I. Y4 V4 C# S
  2.             agrs=new JSONArray();
    & N, D( U) G; L  i2 W5 E
  3.             JSONObject spendAccount=new JSONObject();
    7 w! X, w( b1 @' N1 H& [
  4.             spendAccount.put("account_id","0H757LPD00A02");0 R9 b6 Z" P4 x2 b1 J6 s
  5.             spendAccount.put("amount",9909099090000l);$ T7 b  e4 E: s% S* n3 I2 T3 d
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    4 @, |" L; i: t( W
  7.             spendAccount.put("type","spend_account");& V: P; O4 U# `3 F
  8.             agrs.put(spendAccount);! T" a7 Z1 G2 ~' l4 f
  9.             JSONObject controlAccount=new JSONObject();' A- s4 Y! m, t. X4 H9 T' [1 L( n
  10.             controlAccount.put("control_program",program);
    ) ~8 D8 M. w; e% {+ J
  11.             controlAccount.put("amount",9909099090000l);
    # \1 R2 W# L' \
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");) o% b+ r3 H1 R0 d+ ^  p7 Q  f
  13.             controlAccount.put("type","control_program");! [# d$ a4 H% E6 b: h  o8 X  Q4 k
  14.             agrs.put(controlAccount);6 G3 |" O& F# f
  15.             JSONObject spendAccount2=new JSONObject();
    : V# C6 n9 F7 j1 J4 K
  16.             spendAccount2.put("account_id","0H757LPD00A02");
      d+ ^8 Z- R' C( j7 |9 V
  17.             spendAccount2.put("amount",6000000l);
    7 E: W4 Y1 I" U" ]+ W& `/ x
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    1 V7 G3 J4 E4 \& I8 d0 E) i3 h! B
  19.             spendAccount2.put("type","spend_account");# a& N& D* ^2 ?$ @
  20.             agrs.put(spendAccount2);
    * a' @8 j; k, D: W0 J, A2 o
  21.             param.put("actions",agrs);
    6 F! |- d! u% b+ D- R' f
  22.             param.put("ttl",0);
    # K+ W: O( `4 E% @3 v" o/ M
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码

4 c; T; {4 `  e! Q第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
9 f$ h! k" R9 U5 D
7 _7 Z1 Q4 |& P- m
& y8 \) ]  S7 x, Z& ^& q8 H以下是相关代码片段:
0 f( V+ N5 f4 s: X, b9 p2 ^4 G            param=new JSONObject();
* l  \5 D3 `; o            param.put("password","xxx");
* s, v" G; n+ Q            param.put("transaction",data);
9 r, f" ^( l$ L" ]) Z& z- s            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");5 z( N( M% Y* s6 b0 [! N1 O
第六步:调用submit-transactions提交交易  Q3 k) c1 j8 V" y  f  h
% R: C! ?, W- x7 [3 s7 e7 S
以下是相关代码片段:" |  s( X/ z' _' t
            param=new JSONObject();+ M; R: i- e9 T5 V! j3 a, w
            param.put("raw_transaction",raw_transaction);) h+ R" L* w) ^0 W
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");0 U1 y$ M9 Q' D$ v+ H' a
解锁/取消合约$ l: l/ `: Y7 ^! P- \" O
首先需要decode出生成合约时候的参数/ E: L, n) O) d; l7 X
调用list-unspent-outputs 获取生成的合约信息获取program% y: W" u1 ^1 i# a' y! r

( E$ O. _, f% s9 t, P3 k+ \  [以下是相关代码片段:
8 O" P- e" m# Y# W0 H4 g* Iparam=new JSONObject();
6 u/ I" D& i. I+ @7 a* ^, t& z        param.put("id",outputid);
+ `7 [3 @) [  U! |; Z8 n1 }        param.put("smart_contract",true);9 h+ x" R4 P6 m/ Z. t3 i( r
        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");" n2 h# v0 E/ y5 z4 I
调用decode-program 传入获取生成的合约参数信息- d0 k* @5 T+ {+ h1 \, v0 z3 s7 h
$ C* Z! P2 P, S
以下是相关代码片段:0 _% l- Q6 S& s, o$ |" x
param=new JSONObject();
9 H' v/ |- ~: z        param.put("program",program);
, X5 U4 J  ~( n9 d& I4 A" G        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
% B0 T3 u4 p& a" z需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍); R, V: H/ S% v9 V. \4 @
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可9 p6 n  I, z6 v& I3 w' i
取消合约的构造参数如下:: k- b( z. x& {& A
3 z8 ]) D4 z) i5 ]! ]" [
            
  1.   spendAccountUnspentOutput = arguments: [{. Y: X& D1 {( h* L
  2.                   type: 'raw_tx_signature',3 R0 w3 ]3 C; B. I
  3.                   // 生成合约第二步的pubkeylist 详情" H( ?$ y( n1 x- W2 |  L
  4.                   raw_data: {
    " v& J5 y, q* @% E  ]- n; J
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,
    . V/ L0 I; h6 v/ G. ]) ]2 h" Z
  6.                     xpub: pubkeylist.root_xpub
    : s; j: F7 F6 E: \* Z
  7.                   }  |1 B2 j$ X; m' M, P" D6 d3 r
  8.                 }, {/ N2 h1 ]: X' m( X& i
  9.                   type: 'data',9 G1 N! [0 E+ L! I' }2 `
  10.                   raw_data: {! _! T* @( r: {$ V7 u6 L# M5 u# K! A
  11.                     // 参数偏移量 在一个合约里是固定的 ' \. J% q6 y6 u1 j% s2 I
  12.                     value: '13000000'; `+ [- D/ R; g# l
  13.                   }
    ! \, o7 P$ e1 ^
  14.                 }],  S$ s# e4 S7 R! F3 o: ~. a
  15.                 output_id: output_id,3 _* _2 ?: J0 e( @* G
  16.                 type: 'spend_account_unspent_output'
    3 P( L( D7 j5 a* v. @1 G
  17.               }
    . ?) H# h9 z  n0 }" N
  18.               const controlAction = {0 x! ]/ u* }# n- F
  19.                 type: 'control_program',5 \* ?- X+ z4 _7 U4 ^6 B
  20.                 amount: 100000000,/ G; {, o% ^9 W
  21.                 asset_id: asset_id,5 W$ U' X4 i8 j$ X) _% o6 X
  22.                 control_program:control_program- `2 d/ y% F1 C0 X
  23.               }
    3 f- k) ~1 P) I" @6 b( y5 @
  24.               const gasAction = {* ~- {& o1 I9 w
  25.                 type: 'spend_account',6 M! b/ d3 Z2 s8 E$ T4 G* b
  26.                 account_id:account_id,
    & H  b9 q+ T, w( q9 L
  27.                 asset_alias: 'BTM',
    $ U* i/ u8 A8 ~/ N8 K/ d
  28.                 amount: 50000000% Y2 o8 i7 ^" R2 _4 G/ p
  29.               }
复制代码

. h5 c, [" Z  K) p- u" |- W执行合约的参数构造如下:
1 Q2 i# I% h: O( d, Y; U  Z
" A5 t( z' _+ z, H+ B- C         
  1. const spendAccountUnspentOutput = {
    7 M0 W% U+ u; j; ^
  2.                 arguments: [{+ [* g7 p! K/ F
  3.                   type: 'data',
    % ~. u& T: [$ `) E
  4.                   raw_data: {
    2 m1 a5 q9 n# a: n2 R# L
  5.                     // 执行合约输入资产偏移量
      `. I- W0 F$ @& j( R8 b
  6.                     value: '00000000'
    + F; R/ [0 @  P' h" t0 @2 N* s
  7.                   }
    * u9 M  ]* l; f
  8.                 }],6 j% ^/ W+ ]6 l0 V& J& a* W
  9.                 output_id: output_id,
    ' X1 |) G, }, j! N7 ~
  10.                 type: 'spend_account_unspent_output'
    3 L- u+ I) N/ e1 V6 d2 \# |, l
  11.               }$ |% z. P: F3 M& ~* w2 o- z: R
  12.               // 合约执行提供的资产
    ' P! _# P2 V  r, k+ ^3 T
  13.               const issueControlAction = {* k. Y8 ^9 C( {$ D9 p1 q
  14.                 control_program: control_program,
    . G, x/ |( c# @
  15.                 amount:  100000000,, `( m, D2 S/ J: e
  16.                 asset_id: asset_id,
      i$ {& @5 Z; _8 t* Y2 S
  17.                 type: 'control_program'6 c) |, p9 H7 d! ]
  18.               }1 H- |1 g( O" v
  19.               // 合约执行提供的资产- c% i' t* e+ `
  20.               const issueSpendAction = {/ g( h$ O3 i& J+ v5 v
  21.                 account_id: account_id,  M+ z, l. s" ~! r# p
  22.                 amount: 100000000,
    - M  I5 [$ t; D+ }0 |$ P5 W/ N% ]/ ~
  23.                 asset_id: asset_id,
    ! O' J- t6 p' e; c+ J1 W/ l0 ]
  24.                 type: 'spend_account'
    8 c- N2 W. s! @
  25.               }5 s0 Y% {% g. S/ P
  26.               // 旷工费: M8 _  P0 N& T* I# h8 w+ O8 }
  27.               const gasAction = {1 E! `4 n1 k2 Z/ g
  28.                 type: 'spend_account',3 }* N+ h" X# p/ n
  29.                 account_id: account_id,( ^( E7 H) e$ \
  30.                 asset_alias: 'BTM',. R0 u: Y6 Q; Y  g& U* P7 \
  31.                 amount: 50000000; ^7 r: ]; r9 P5 E' \; H. \
  32.               }
    ( N% A( f# X& O8 F' I1 W9 a
  33.               // 合约执行获得资产对象
    : v0 B- }2 v& [: f0 C. y
  34.               const controlAction = {
    1 o6 E. i. p% T
  35.                 type: 'control_program',$ N6 M  D. f" }
  36.                 amount:  100000000,
    ) z9 R# r7 @) X8 z0 [5 @
  37.                 asset_id: asset_id,
    5 {3 j! O7 A2 A$ c0 g7 A
  38.                 control_program: compileData.control_program9 ^/ T8 l6 d% q8 U( l
  39.               }
复制代码
& s" z" {/ o2 G, f, k. u
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档. d* {5 [" H& d0 t
备注
% q/ ~# q  H$ U" t- i调用比原基于okhttp接口javautil 如下:
8 [8 {( O. h  T" E6 N( k* O8 X. ]  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
    & h3 p- f- @6 ?3 ^9 q' @/ `
  2.         OkHttpClient client = new OkHttpClient();
    , ^. a: ~( m, P7 \1 h, Z
  3.         MediaType mediaType = MediaType.parse("application/json");
    $ [5 T' z3 S4 A7 ]& I
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);4 R; J. q& ^1 s0 ~6 e
  5.         Request request = new Request.Builder(); M; M3 ?* Y/ D
  6.                 .url(bytomApiserverUrl+"/"+method)
    & T: c- ?/ ?7 i5 |, g% a' ~
  7.                 .post(body)) U; D' o) a# S" d" k
  8.                 .addHeader("cache-control", "no-cache")
    + X7 V: T4 \9 ~, A
  9.                 .addHeader("Connection", "close")
    ; i. d) E" Y& U& q
  10.                 .build();
    & w9 @, _6 L' e7 ^: {# V) p
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
    3 c- W# t) \6 @" L
  12.         }else {
    8 l& G8 l' }# H/ R
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
    - ?; N7 L( E: y$ k
  14.             String authHeader = "Basic " + new String(encodedAuth);
    5 L* U% y+ b4 Q& v" I( j6 ~
  15.             request = new Request.Builder(): b$ P6 a0 F( [1 K8 g
  16.                     .url(bytomApiserverUrl+"/"+method)' F& U- q. y, D9 n" K2 V
  17.                     .post(body)
    ' _* `2 {0 w! G
  18.                     .addHeader("authorization", authHeader)  T& d% [1 Q1 |
  19.                     .addHeader("cache-control", "no-cache")
    + r$ _8 _* `2 S4 B6 d5 r- C* D4 q
  20.                     .addHeader("Connection", "close")
    9 L6 q+ Q( t. P6 r9 r. o# p
  21.                     .build();
    6 K: n8 F- w% }" Q! T
  22.         }
    6 u3 u6 T  E( Q! V! B2 v' a; P1 v& j
  23.         Response response = client.newCall(request).execute();& s% F3 n$ M; O+ `7 X# B
  24.         return response.body().string();
    3 n" E) Z! ]+ [  S5 m, Y" v# @2 `
  25.     }
复制代码

$ w# D& X3 Q* e/ V. p1 p; g
+ Q/ T/ B, b  j% b4 p5 F# q# r5 U! n2 V! ?
比原项目仓库:
" b# ^: h2 d: [7 w6 d- q9 CGithub地址:https://github.com/Bytom/bytom
) g. d( W, s% Y% Y& h3 x: v0 a, QGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1