Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
% A2 q/ `% ]6 U" n$ s( i- L" W
  1. contract TradeOffer(assetRequested: Asset,& i- X% X% ^9 U. w, e  G
  2.                     amountRequested: Amount,
    / k6 |2 {% c. \- T! U
  3.                     seller: Program,
    ) p& @$ ^4 e% _1 W$ e4 d4 s. u
  4.                     cancelKey: PublicKey) locks offered {+ e; o% I% d8 G1 T$ F# X/ j
  5.   clause trade() requires payment: amountRequested of assetRequested {
    9 j) k  E( ~2 x& Q' ]. M; W" F
  6.     lock payment with seller: ]  }; P1 Y& u
  7.     unlock offered$ s. b' x) L# \+ Y0 j6 T; h9 k) `5 {
  8.   }
    + u7 v" S" w8 W  o7 F* r2 b
  9.   clause cancel(sellerSig: Signature) {
      K1 m5 E5 e. M' e
  10.     verify checkTxSig(cancelKey, sellerSig)
    6 H  ^$ ], }8 {' q. x! J
  11.     unlock offered
    - Q- j' t- I4 a4 O8 u# `' B5 g! J
  12.   }
    8 e0 y: h. \2 @2 b. b
  13. }
复制代码
, y! i2 K0 h0 B6 D, g* V
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
: n" N* H) x* r, Y0 y- C% n) h/ V锁定合约
1 \. `; ^: F9 E* g0 N% x第一步:调用create-account-receiver 生成 control_program
1 h2 R( y1 v& q& \" j4 A/ r( B
; j2 i8 y3 W7 A# v: D: h以下是相关代码片段:% m( e& y- W6 s
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");' C9 I# t0 I! T
第二步调用list-pubkeys 获取 pubkey, o* J6 F+ H8 W( o/ P/ ?

1 D5 l% c0 O7 _4 v4 B7 y! b" L, E* t- R) E) `6 v
以下是相关代码片段:9 [% k& x0 N+ i; m2 k: @1 S% H5 {
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");2 \' F2 |: \3 D  b
第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序
, V1 b. J8 P6 U
; `3 S8 @# u' Z( S0 w4 o# |9 s( z/ K2 n' ~  w* v
以下是相关代码片段:9 V3 J- k; [) q" G+ B* P
           
  1. JSONObject param=new JSONObject();. \) G! l2 t4 h, T
  2.             JSONArray agrs=new JSONArray();
    ; h  y/ j4 C) g" {
  3.             //合约的四个参数值
    % _0 L. {+ t' J$ r$ N
  4.             JSONObject assetParam=new JSONObject();
    $ ~- {9 {2 ]0 Z; M( W0 X9 T
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");& ^) n# w' v! n/ g
  6.             agrs.put(assetParam);
    : Y$ m3 W2 j6 I& c% k6 _" s
  7.             JSONObject amountParam=new JSONObject();  X) Y  i& y6 \6 i, L
  8.             amountParam.put("integer",200000000l);  U& l( n* q. y- P; s
  9.             agrs.put(amountParam);9 t! v; n) u( c% c
  10.             JSONObject programParam=new JSONObject();, @2 E0 h# _$ D" N4 _6 y
  11.             programParam.put("string",control_program);8 N0 n) o- P* e: d4 M9 Z% v
  12.             agrs.put(programParam);
    3 U6 b9 f  R' U3 g+ b
  13.             JSONObject publicKeyParam=new JSONObject();3 L" F6 ]9 s* a
  14.             publicKeyParam.put("string",pubkey);5 f& Y! J! ?4 ]9 Z* k
  15.             agrs.put(publicKeyParam);
    2 [# T  }8 l8 g; _
  16.             param.put("agrs",agrs);
    $ }% v8 M  X; ?. A/ M3 B
  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 } }");( S9 v( T- t' R! z7 b
  18.             //调用编译合约4 {# Z1 b, y2 x, n1 o5 C
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
8 b/ r- d- m0 w* n0 L! u
第四步:将program 传入build-transaction接口去build一个交易的到data4 B9 b, y& G( m$ I2 o/ ~' S

6 i8 m4 r1 T' a! e& W
% N( t0 l/ d5 }. `4 J; u, Y$ K以下是相关代码片段:' h/ Q' \" H1 ~0 }2 ]
           
  1. param=new JSONObject();
    6 V+ t8 Z& C" ?8 H$ v& x4 ?. e
  2.             agrs=new JSONArray();4 X3 t( e5 O" _- h
  3.             JSONObject spendAccount=new JSONObject();( i/ u7 V/ u) g2 Y$ Q: x
  4.             spendAccount.put("account_id","0H757LPD00A02");0 q+ }( \) y6 i8 K" E
  5.             spendAccount.put("amount",9909099090000l);
    , Y6 |6 y0 t; d6 u9 Q
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");4 y5 ?+ i2 h9 b" V( m1 R
  7.             spendAccount.put("type","spend_account");" Y: j  M0 M% ~# v$ K! x1 M
  8.             agrs.put(spendAccount);( y% A4 H$ j; K3 p; T
  9.             JSONObject controlAccount=new JSONObject();
    & {9 u# m8 e2 o6 n6 z9 l5 B1 f, l
  10.             controlAccount.put("control_program",program);
    ; B2 E8 |* G) H+ b( E5 q5 A
  11.             controlAccount.put("amount",9909099090000l);
    # K" e$ j1 X) K
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    8 G# A2 s1 A- c" ]0 l7 {: v
  13.             controlAccount.put("type","control_program");4 k( N# X  K- x6 `' y9 |
  14.             agrs.put(controlAccount);
    # |  Y4 o# L9 e
  15.             JSONObject spendAccount2=new JSONObject();; g- v: X  s+ F* ?. N$ {; K0 O/ l
  16.             spendAccount2.put("account_id","0H757LPD00A02");
    % [9 o$ S# B' ~
  17.             spendAccount2.put("amount",6000000l);7 `9 h0 B- R2 f  _, X8 q
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    : T9 h) i; K; K
  19.             spendAccount2.put("type","spend_account");: V6 n7 R# U6 i4 `( |6 r) ^
  20.             agrs.put(spendAccount2);6 B' I% e$ P4 m. b* Q. ]
  21.             param.put("actions",agrs);* u, z, ~. s; [
  22.             param.put("ttl",0);9 H+ R# Z8 e+ C; R
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码
8 r- J& e7 w* C( m( H# ?5 V
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction
$ _  H5 n$ o8 J) z9 r8 a! \: P* ]4 U/ Y  V# S
* `) I8 g0 i/ y; }1 \
以下是相关代码片段:
6 i/ [4 a( T( [- u  H            param=new JSONObject();2 }5 d; `6 d  i+ [, w% h: X
            param.put("password","xxx");
+ S' M" r. _* A, C" ?( s9 @            param.put("transaction",data);5 V, q# w% E2 s* }9 ]9 [8 i% {$ f
            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");9 N. I) J' ?/ E& K6 a
第六步:调用submit-transactions提交交易
0 r' k( f/ b$ u. m) v5 f
% h6 T( c/ {/ ]2 m* c( A以下是相关代码片段:, p2 ^& A, T: _/ w
            param=new JSONObject();7 r- k5 X4 ~% W# R
            param.put("raw_transaction",raw_transaction);: W" W1 P. `: b$ M  ?; `& r
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");" X$ z+ ]( f( n) y  A/ _8 |$ d
解锁/取消合约$ s2 F- W% P# g& g# o# y
首先需要decode出生成合约时候的参数
( G4 W* ]& I* R0 g# ^* ^调用list-unspent-outputs 获取生成的合约信息获取program
0 H9 P, Y; y4 Y, o
8 J. S- |$ G; K# v6 d1 g以下是相关代码片段:6 ~$ @" r' Y& z3 p; x/ l9 Z1 D
param=new JSONObject();* B1 a0 W  X8 t# U$ Q
        param.put("id",outputid);! r& D9 o, a$ M( @, l+ g/ j% j
        param.put("smart_contract",true);
5 R2 k8 ~) P9 B        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");' e, c$ j  j  C) {  b5 u4 F
调用decode-program 传入获取生成的合约参数信息
' a- K& C  R9 K! [; H5 z
6 M; _) d7 ]5 |/ I5 ^: x5 q以下是相关代码片段:9 U9 Z. j* u% @
param=new JSONObject();. I# Q8 ]7 v7 w: X9 e# t
        param.put("program",program);( ?4 N9 z. q$ t& [4 \! ^
        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");" S" _$ G# I2 l/ w* G/ {8 I$ ~
需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)$ B8 y1 w7 l6 Y9 a4 o
解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可
" T2 P  V2 H$ r; D! z取消合约的构造参数如下:& ^+ a+ V) _" f$ v3 _9 i. e
, M6 w" v$ N9 s
            
  1.   spendAccountUnspentOutput = arguments: [{
    $ T% N5 ^7 C! W, E
  2.                   type: 'raw_tx_signature',& U" W! A2 r8 Q# @5 ~7 E
  3.                   // 生成合约第二步的pubkeylist 详情
    3 z, c; K" u4 |& a) ]
  4.                   raw_data: {# V( U* d0 _; \( ?' c5 O+ E! H7 c
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,% X& H) w) ?# a8 X& O/ t
  6.                     xpub: pubkeylist.root_xpub
    2 x  o- u, Y. H- F" L1 A
  7.                   }( f/ D8 r, _7 S8 q
  8.                 }, {
    ( |( u! i6 {$ }4 G4 ]
  9.                   type: 'data',9 S  p; {, z: \3 ?0 x7 z
  10.                   raw_data: {: _  m: w; J2 _
  11.                     // 参数偏移量 在一个合约里是固定的 . N' g, \( Y( g! @0 b+ d% l- P3 x
  12.                     value: '13000000'$ O$ C) \* ?& J/ L
  13.                   }
    # h0 I/ @9 |% b) w
  14.                 }],0 V8 U) T  ]2 B
  15.                 output_id: output_id,: o8 v6 A% a, L" j2 w. G0 T
  16.                 type: 'spend_account_unspent_output'
    ' h6 }% i1 x& F, d
  17.               }
    / i* p7 J: p9 x3 g  ?/ I
  18.               const controlAction = {9 @9 t; X/ x/ M7 V: w. P6 K/ X
  19.                 type: 'control_program',& I/ n+ N7 {& P  N- `
  20.                 amount: 100000000,
      y( A8 d8 I) m; h/ E
  21.                 asset_id: asset_id,
    9 x2 C" w  |' _$ C
  22.                 control_program:control_program
    / ?: b3 H* t3 V) ^: Z. A. M. h
  23.               }
    % y* n+ w1 l' f3 [! p
  24.               const gasAction = {
    ' [. B  t- \" C
  25.                 type: 'spend_account',: }% A7 l' r  b2 T7 ], |
  26.                 account_id:account_id," N0 \: [# {  r4 p3 F
  27.                 asset_alias: 'BTM',6 J+ E" E; P  S1 R8 ^7 W" {  a
  28.                 amount: 50000000
    $ @& `% ]- Q3 _  {# Q$ |6 l/ [
  29.               }
复制代码

4 c4 U& U5 L. O' D% c执行合约的参数构造如下:- O( C6 w5 B; t5 A  A

/ B0 g0 U5 q1 n; ]         
  1. const spendAccountUnspentOutput = {/ s1 R* l2 F: {
  2.                 arguments: [{# L5 o8 K, w- l5 t! L; m( R
  3.                   type: 'data',
    8 {. B" _/ V# A) g8 @8 U
  4.                   raw_data: {; ^1 \- `3 D/ s% l" k
  5.                     // 执行合约输入资产偏移量
    1 C  |! I5 ^- v4 P0 q8 W) z+ O1 m
  6.                     value: '00000000'
      P: r" U1 v6 b, V
  7.                   }
    # X3 m) K3 ]* S* k% j
  8.                 }],* x" Z+ k" J+ h9 G
  9.                 output_id: output_id,
    $ b! ^! ]$ W  E
  10.                 type: 'spend_account_unspent_output'9 G  L  H8 M. S8 @( W- l
  11.               }
    # g6 I, u8 y8 o& i5 v8 e6 g2 w1 g
  12.               // 合约执行提供的资产
    ( W' _5 U7 S7 U' T- i9 S
  13.               const issueControlAction = {, T0 x! x* P$ g9 p: Z% ]3 n! E
  14.                 control_program: control_program,: P% d  g% n1 V* `* K+ ?4 X/ m
  15.                 amount:  100000000," l7 A- j8 I# L% w& ]- i, U% N
  16.                 asset_id: asset_id,& B$ ?$ v3 W' J7 x8 X& [
  17.                 type: 'control_program') J+ n# W$ l* E
  18.               }
    4 r( ?& {( u% S1 F1 {
  19.               // 合约执行提供的资产2 [9 m& A/ N) e
  20.               const issueSpendAction = {, m$ I: N+ U) c2 E- u4 U
  21.                 account_id: account_id,
    ! y- Y3 ~1 w" X- R7 m9 k. c
  22.                 amount: 100000000,
    1 A3 b! H+ y$ E. s8 g& `
  23.                 asset_id: asset_id,4 m3 ]- ^2 i1 v" J# C5 S( C; s' b0 J# |. M. \
  24.                 type: 'spend_account'
    1 u* Z1 i9 q( e8 A4 x
  25.               }5 ?( m0 x+ V8 s# x0 j8 ?, E: q
  26.               // 旷工费8 _5 M- y/ b# _. I$ F2 l  `: I6 j. Q
  27.               const gasAction = {
    : K( R4 M6 F6 D( \6 q3 L
  28.                 type: 'spend_account',
    0 k" O0 d9 W& S
  29.                 account_id: account_id,
    * y5 p' [+ r( H. G8 l6 U
  30.                 asset_alias: 'BTM',6 K8 H' O7 Q- q9 O. w  b; k
  31.                 amount: 50000000- v" _/ }4 G; @
  32.               }4 G+ }) H' l+ F, Q' ~5 l4 p  B- T
  33.               // 合约执行获得资产对象
    % v" p4 g0 f$ X
  34.               const controlAction = {
    3 o4 L! ]- q# O+ i. S7 i1 X1 U7 O% X
  35.                 type: 'control_program',9 q% l7 @2 }3 W1 ]8 j( F6 ~
  36.                 amount:  100000000,  ?* f5 _; N0 ^
  37.                 asset_id: asset_id,' W% i6 V4 ]0 _" i  @
  38.                 control_program: compileData.control_program9 D7 f% I: x4 ~! u* Y0 d
  39.               }
复制代码

1 e# r- x2 W! C  u4 Y! U3 w- Xbuild 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档9 a7 h* Q) f2 e) i  P9 D& V9 Z
备注
. R5 c2 {; m* y! k9 b调用比原基于okhttp接口javautil 如下:
% D9 s3 O4 g4 N  D) t6 z$ p) ~  s, z  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
    1 X. o; ~# g" V- M( i
  2.         OkHttpClient client = new OkHttpClient();, u7 `( h2 F2 s0 b7 s4 I6 y8 @
  3.         MediaType mediaType = MediaType.parse("application/json");
    * m" B/ R7 P9 W+ j2 E1 s$ h
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);  M3 @6 V8 w4 M7 n8 _2 ^
  5.         Request request = new Request.Builder()
    ! L) Z8 W: v8 E) y: P: G
  6.                 .url(bytomApiserverUrl+"/"+method)$ d$ X/ q4 L  m
  7.                 .post(body)
      q; K& w# _" W2 R4 E+ l
  8.                 .addHeader("cache-control", "no-cache")2 i5 c: j) u/ Z# r0 |" h
  9.                 .addHeader("Connection", "close")
    4 X  P+ ^* X# z+ b) d
  10.                 .build();9 C2 m0 e) t; v, B# }/ R4 y
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
    3 E9 }: c, h3 U
  12.         }else {/ q! K4 g+ t8 Z( a
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));' c/ P' L, s, C$ J8 I
  14.             String authHeader = "Basic " + new String(encodedAuth);( N! H; }) ]4 Z3 l9 A5 G/ c
  15.             request = new Request.Builder()% M5 L1 ]6 b) i
  16.                     .url(bytomApiserverUrl+"/"+method)  c! k. ]4 _# E6 z3 v0 U  ~1 k* d
  17.                     .post(body)
    / p6 N+ O( I& z' |1 {0 {
  18.                     .addHeader("authorization", authHeader)9 F* e& q( S: u7 {7 F( Z5 s
  19.                     .addHeader("cache-control", "no-cache")( l7 @$ ?1 N0 B& G
  20.                     .addHeader("Connection", "close"); }* K! A, H0 p7 ^5 N
  21.                     .build();0 Y! e9 J) S/ v; g% W, r$ u
  22.         }
    * U) g: l& _1 r) b
  23.         Response response = client.newCall(request).execute();* U- }7 Q5 U, _, P
  24.         return response.body().string();( w0 @- x' [6 F3 L9 @4 i+ I
  25.     }
复制代码

' a! r- _1 `5 a
9 \  c% K& q  X( h, U% h8 u# N: t+ }6 \: {6 \& K" l# S9 u
比原项目仓库:- Q7 a- X. W4 V4 E: B
Github地址:https://github.com/Bytom/bytom, p0 E% z& T) F: E# |
Gitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1