Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
本文解析的为比原提供的币币合约 模板如下:
$ {) z8 ?1 A  N: K5 E
  1. contract TradeOffer(assetRequested: Asset,
    8 h# [- B- ?6 o8 m4 p8 b. V
  2.                     amountRequested: Amount,5 E6 ], a. m( W% d5 e) j
  3.                     seller: Program,
    0 L* f  @9 m3 W% d+ i7 j/ g6 {8 n! k
  4.                     cancelKey: PublicKey) locks offered {
    & v/ v( Y* O) o4 _' @# S
  5.   clause trade() requires payment: amountRequested of assetRequested {
    ( o7 |" z4 a& D$ s2 w
  6.     lock payment with seller/ e! L7 T2 _; o( n) k
  7.     unlock offered
    $ @: T; P: A% O' s0 v  c. |9 k
  8.   }
    # B7 {4 b; C1 e) `, A: |
  9.   clause cancel(sellerSig: Signature) {& R4 m8 m% G0 f6 @3 [9 J) D1 v; t  _/ N
  10.     verify checkTxSig(cancelKey, sellerSig); J+ Z7 f+ l9 {8 K) y
  11.     unlock offered
    - Z: A) }3 p" P2 [
  12.   }7 C; Y% @+ f9 `$ F" ^/ g( p5 ]
  13. }
复制代码
( b( ?8 {" H3 z6 C: e9 A
导读: 初次接触比原只能合约的请点击比原智能合约入门 和 Equity 语言入门 学习,方便更好的理解该文档
2 Y; ~% ^' B+ z4 s5 W6 O1 p) c锁定合约0 J% P# x2 L- w
第一步:调用create-account-receiver 生成 control_program
/ v* \3 ?7 W- ?" u$ B # {+ V& v2 @/ U7 }
以下是相关代码片段:
! \2 v; ~8 \' z1 ?sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");
5 N( D# f' k3 n; e) A: ?- d. x第二步调用list-pubkeys 获取 pubkey1 |, y+ A( i' ~9 S) W

) B' ]7 i- u& e8 z5 O3 y
9 V9 F# K" D! G  U; [以下是相关代码片段:0 ?$ J7 u; p7 F. L6 w
sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");
0 V2 _" d. T) o第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序( D2 n* A1 q4 @' O1 {0 e
1 O4 N0 {; t$ D

( M+ Q' J4 }  N: q/ ]1 V  @9 x以下是相关代码片段:2 `9 [, N2 j+ D# ]: _# U, ^7 j7 r
           
  1. JSONObject param=new JSONObject();
    ( I' Y, O/ W" A6 F$ |5 e
  2.             JSONArray agrs=new JSONArray();* T9 k% F1 a8 C4 F. x8 ~
  3.             //合约的四个参数值, r9 E5 O, g$ Q: q
  4.             JSONObject assetParam=new JSONObject();9 R& [2 \9 I! _1 `- j
  5.             assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");/ u, {7 y0 ]: G( [
  6.             agrs.put(assetParam);
    ; {$ i4 j/ h9 i9 o* n
  7.             JSONObject amountParam=new JSONObject();6 a7 d6 t" e5 M1 t
  8.             amountParam.put("integer",200000000l);
    * A, f& `! t& s. @( E. B
  9.             agrs.put(amountParam);
    # R8 g3 }% w$ X
  10.             JSONObject programParam=new JSONObject();
    : Q! K0 w8 |  g7 p6 c
  11.             programParam.put("string",control_program);6 N3 a, `, Y7 s, N# b( s* b
  12.             agrs.put(programParam);* @' Q( D2 o9 J6 |
  13.             JSONObject publicKeyParam=new JSONObject();. X2 X: O- J1 ^# G  {* m
  14.             publicKeyParam.put("string",pubkey);
    : ?: d' J$ T# E, ~0 I% ^
  15.             agrs.put(publicKeyParam);1 W9 K) `% h* }, x4 M
  16.             param.put("agrs",agrs);
    ' H$ D+ Z  M" X9 ^9 E- i
  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 } }");# R7 H1 X' {0 |, [  y2 P2 Z
  18.             //调用编译合约
    . h! [4 x5 z# c: I* W* n0 G
  19.             sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");
复制代码
0 {1 \! C4 |# F) ]$ f9 k
第四步:将program 传入build-transaction接口去build一个交易的到data3 D; q+ o* Q3 P. d

  o( r7 W% ?. _& h+ A7 O8 }+ z% d
以下是相关代码片段:& @+ h- g5 Z* h7 J4 }
           
  1. param=new JSONObject();( C3 ~1 J0 J0 v% \9 N& C
  2.             agrs=new JSONArray();
    0 F2 \) v& q4 ?8 W8 ^+ R" ^* y
  3.             JSONObject spendAccount=new JSONObject();% P& K- z: \& N/ G
  4.             spendAccount.put("account_id","0H757LPD00A02");
    - z3 ?: a9 P' g7 v1 v3 H) }
  5.             spendAccount.put("amount",9909099090000l);
    ) f0 |+ }0 w& a% ]+ f; s* O" i5 v
  6.             spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
    + i8 P3 E% ]& n. A: B; E
  7.             spendAccount.put("type","spend_account");
    3 M# T4 a( q( `5 U& H) y4 s
  8.             agrs.put(spendAccount);
    0 `  V$ x5 s' I0 a
  9.             JSONObject controlAccount=new JSONObject();
    # H* K/ B1 U( ^4 y- X8 ^
  10.             controlAccount.put("control_program",program);
    0 u! e% D. x) w4 [  n7 a6 E
  11.             controlAccount.put("amount",9909099090000l);. }0 e$ g' v. A# s( C
  12.             controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");1 h+ O4 c  C0 Z$ x$ u
  13.             controlAccount.put("type","control_program");
    2 ]0 C( j1 I2 Y% C2 }/ z
  14.             agrs.put(controlAccount);/ n' r8 R  Y/ V6 U$ ~) d+ g+ G+ F
  15.             JSONObject spendAccount2=new JSONObject();4 N. m" j3 U! y% O1 X
  16.             spendAccount2.put("account_id","0H757LPD00A02");
    & G! |$ G7 f. ?7 W4 k& a+ o
  17.             spendAccount2.put("amount",6000000l);
    , y7 S/ i& k4 s( S( E, f
  18.             spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    4 z/ Y6 _: {* f* A+ h2 c
  19.             spendAccount2.put("type","spend_account");8 G7 L/ i: }& G1 O' L
  20.             agrs.put(spendAccount2);( N' r/ }  P7 t: _. {' q- s
  21.             param.put("actions",agrs);
    - w5 u1 e" O9 ]* u. |3 X
  22.             param.put("ttl",0);5 }& c4 K! ^5 E; r0 Q5 C
  23.             sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");
复制代码
5 P) J5 f" |2 b( e  j: J. h) j
第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction* D5 P" l6 ?+ z& U- a& s% ]2 ]# E
! v6 |* P/ U& m
/ Z( B; B# j7 J4 G5 d; F
以下是相关代码片段:% q, u6 @7 m- {" J. B8 o2 O
            param=new JSONObject();, P% {0 [: {0 A- Z" g
            param.put("password","xxx");
5 \" o+ Q; l/ Q$ L5 u( X' m            param.put("transaction",data);& _( U/ Q2 K: u: Q: O0 b
            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");
( g! _6 k" X8 t( R' A% a第六步:调用submit-transactions提交交易# c, _6 ?( s3 C1 p$ V1 }/ [; Q& L# g* |
" ?: _6 |+ l3 Q# W# `
以下是相关代码片段:
' q* e4 x8 z- P  ~            param=new JSONObject();
% C, `$ M( r8 @; U            param.put("raw_transaction",raw_transaction);
2 k; M: o* F7 c& F  c& g            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");
$ N$ ]* B4 I1 I/ g7 h+ r解锁/取消合约
: p0 C+ K; k) X' D! N5 [( h7 x首先需要decode出生成合约时候的参数6 O6 U0 r* q+ ^8 k/ {* q% G( N
调用list-unspent-outputs 获取生成的合约信息获取program
' a6 k; J' |3 t9 P+ u' ^, I0 p  P
6 J0 t0 i, i- D3 k以下是相关代码片段:
$ [% E. I7 u% M, I( [$ x1 }. |param=new JSONObject();( y, h1 L5 G$ |  @/ U2 |
        param.put("id",outputid);
& x: A7 s  r, t        param.put("smart_contract",true);
$ J0 M, s- d5 o% Z        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");' H; ~* \  ^* {/ T- \
调用decode-program 传入获取生成的合约参数信息  L8 b8 k: c" @9 h& s

/ a- W( ~$ n/ D) |4 a1 g. `以下是相关代码片段:
( B7 K/ h3 p5 e3 o9 Q5 Z3 lparam=new JSONObject();
$ x: v& t7 k4 F0 U4 B/ E) K1 y  w& _        param.put("program",program);
& W% d+ w0 R# Q8 w! M0 J        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
" P5 A( x' O  A- u$ o需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)
! [! I$ s) S" x! f/ V% t% P解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可" _8 Y0 v& V; `* U. A% `+ N2 K
取消合约的构造参数如下:; t3 _1 \0 x( Q4 v" |

% r8 Q* N8 f! m# s$ s            
  1.   spendAccountUnspentOutput = arguments: [{3 |- K1 h7 c( c; v$ n( `
  2.                   type: 'raw_tx_signature',3 b2 D; P6 S+ T5 x( d
  3.                   // 生成合约第二步的pubkeylist 详情' K4 ~& ?( O, T4 i8 V7 \5 [3 k4 {
  4.                   raw_data: {
    ; P& M- B* a0 {
  5.                     derivation_path: pubkeylist.pubkey_infos[0].derivation_path,2 e/ A$ E4 q2 Y' V6 Y: v, p2 }
  6.                     xpub: pubkeylist.root_xpub
    - E1 G5 Y! ~8 S+ k: R
  7.                   }
    / v5 M, h1 _1 M  O5 v
  8.                 }, {6 d2 t7 ]7 B7 ^$ y' k+ ^' L2 n; h' I5 @
  9.                   type: 'data',
    " ^4 M$ D- I+ X& w& c# B) J
  10.                   raw_data: {
    : P3 g" F* ]9 y7 l/ H. \4 s5 w
  11.                     // 参数偏移量 在一个合约里是固定的 ) \7 h4 W' v; Y6 w; U& i$ @8 P& O2 U
  12.                     value: '13000000'& |$ I  x& Q# Z: |
  13.                   }
    ) ?: v4 F8 g% R* u7 L
  14.                 }],
    8 \' W3 `- ?0 g% a$ p* r
  15.                 output_id: output_id,- Y! O, Z4 B2 T2 n2 F2 b. O; ?
  16.                 type: 'spend_account_unspent_output'
    + N$ w$ A8 }7 r& Q+ E5 M) O! F8 a
  17.               }
    4 _. U+ ]1 T( G7 s9 L. U
  18.               const controlAction = {9 K; J7 O1 K! n7 p0 ?
  19.                 type: 'control_program',
    . @' [/ Q& P- F3 e3 Y2 G
  20.                 amount: 100000000,5 r+ [  s4 f+ H& S$ Q0 |- W: |
  21.                 asset_id: asset_id,% t3 ~' l0 u% l" q+ _# `2 R
  22.                 control_program:control_program
    ' Y% K5 p. d( J; i/ [5 I
  23.               }+ S* `7 u4 ?& X6 y: G
  24.               const gasAction = {
    ) S6 b" A9 P' R" Y) `6 v) E8 e3 P
  25.                 type: 'spend_account',6 ~" c+ {* N+ a
  26.                 account_id:account_id,0 G0 A) \9 d+ p$ g: V% f! i
  27.                 asset_alias: 'BTM',
    $ A/ q& V  `% ?3 @4 f& b! l
  28.                 amount: 50000000
    : o8 V( b+ b3 Q8 \0 E! y! l. b
  29.               }
复制代码
; o9 P" W! \3 F' {" p
执行合约的参数构造如下:
/ q  L: P: m: m
# f2 M% R$ N* P0 i1 k         
  1. const spendAccountUnspentOutput = {
    ( J0 x" f  f- d1 l
  2.                 arguments: [{
    8 t! U; }1 j$ Q
  3.                   type: 'data',2 A8 u/ s1 W, z7 w2 T, q+ K
  4.                   raw_data: {# [! b; m6 v% X  L" O- g, k, Q
  5.                     // 执行合约输入资产偏移量( E8 w; a# m4 m
  6.                     value: '00000000'
    : L+ X: U+ y1 z/ l
  7.                   }& _+ {5 e; F' J% P
  8.                 }],: w, l5 M5 z5 U/ D' G4 S
  9.                 output_id: output_id,
    - e5 w5 s6 f! t' `, q
  10.                 type: 'spend_account_unspent_output'
    ) N+ d' G4 h' l
  11.               }
    . q: i) ?& n1 _
  12.               // 合约执行提供的资产/ O  ?/ F7 F, A7 `' ?! R# m+ L
  13.               const issueControlAction = {6 O2 a+ i/ }* T7 p6 \! \, P
  14.                 control_program: control_program,
    ) w2 N+ j# y. Y
  15.                 amount:  100000000,1 M: d% w7 ]1 b9 `
  16.                 asset_id: asset_id,, q5 M" w; ^! N8 [  l4 s
  17.                 type: 'control_program'
    ' X; o- `+ V1 q) u0 d- P
  18.               }* I# R4 e% V" d0 h- C. V  M% O
  19.               // 合约执行提供的资产. Q; _$ F3 O/ p4 `3 f! c5 i2 R1 Y
  20.               const issueSpendAction = {
    3 |% K% G3 s! K# L, o
  21.                 account_id: account_id,: p" f. }8 {. e7 R; P+ \
  22.                 amount: 100000000,
    # e2 ?0 g' v! R% {" I$ x
  23.                 asset_id: asset_id,
    ! B4 `# k- h/ x4 |- E5 M& Z
  24.                 type: 'spend_account'
    * |& S3 {8 |8 n! }9 a& m' `! D
  25.               }% i  m7 Z- ^8 }: s6 y* N! X
  26.               // 旷工费7 K6 h* o) V7 m
  27.               const gasAction = {
    . v6 }: y$ j4 C0 q
  28.                 type: 'spend_account',
    8 ]( i- f2 f6 `$ V5 h
  29.                 account_id: account_id,
    % {0 V" S1 U& M% r
  30.                 asset_alias: 'BTM',1 F" f; S; i  e4 I" [3 ^( _
  31.                 amount: 50000000, ^/ C2 f' o( G4 D% i8 y
  32.               }
    , A& z' T3 Z, V5 x
  33.               // 合约执行获得资产对象! c1 T- D$ J9 b$ c  `9 e7 ~
  34.               const controlAction = {
    ) e. ~3 p1 v4 V" x* t
  35.                 type: 'control_program',
    - H. A" E" o' O+ b! v! t( }$ J
  36.                 amount:  100000000,
    & U: m" G$ b# i8 M
  37.                 asset_id: asset_id,
    7 S9 a1 W' E1 D( v, }/ c
  38.                 control_program: compileData.control_program
    2 m$ I& R" k2 R4 C- g& U* Y+ J
  39.               }
复制代码
% D) B) |1 N0 ^$ {0 v* Q# A
build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档6 s; T4 ]2 `& M  B% b  o
备注
: v) p/ r( V. {4 v1 L- L调用比原基于okhttp接口javautil 如下:
# k9 |! R  e6 o5 n7 c+ a9 ]  
  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
    4 k0 `* y, Z" Q3 Y6 Q$ D+ C- T
  2.         OkHttpClient client = new OkHttpClient();
    5 g3 J6 W# G8 @* y9 b( Y) I
  3.         MediaType mediaType = MediaType.parse("application/json");
    , g' s1 |5 a- p& A
  4.         RequestBody body = RequestBody.create(mediaType, bodyStr);1 i% |1 M& u' {" a/ R: p
  5.         Request request = new Request.Builder()
    ; }% J, Q9 H6 }0 |) S! U" w
  6.                 .url(bytomApiserverUrl+"/"+method)
    & l, d6 B1 }8 d8 ^+ m# x: }9 u7 B
  7.                 .post(body)
    ; {% R: [- l- `$ V1 A2 C8 a
  8.                 .addHeader("cache-control", "no-cache")
    ) x& f" N0 @% ]2 P; p$ P$ M
  9.                 .addHeader("Connection", "close")
    7 T$ ]0 x- {; h  U% R
  10.                 .build();
    . k* O- |" N- h1 F( S, h4 f- f( v
  11.         if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){. {' [7 w6 g0 N7 }% o) E# |
  12.         }else {
    ) B' p  D6 D8 g
  13.             byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));9 a9 ]- Y' k) `* C. }. R' M
  14.             String authHeader = "Basic " + new String(encodedAuth);  x$ F" ?6 U4 N* @
  15.             request = new Request.Builder()
    $ X- [, m' C6 [/ t7 }! q
  16.                     .url(bytomApiserverUrl+"/"+method)
    0 S4 G  R; y- t* W
  17.                     .post(body)' g7 `! H% n+ s, @
  18.                     .addHeader("authorization", authHeader)
    # ?3 t% F& ?: X6 B: _
  19.                     .addHeader("cache-control", "no-cache")
    $ t0 S. c9 b: f- m: Z3 K
  20.                     .addHeader("Connection", "close")2 r5 x) u$ a3 g0 M; u, }
  21.                     .build();7 |( a  ~! J7 i# @% ?" _
  22.         }# D0 E! f- M8 `9 z) y6 |1 g- y8 D
  23.         Response response = client.newCall(request).execute();# C. m/ g$ W0 u
  24.         return response.body().string();
    5 g. ~: {) c. s: N# p& E4 N- C
  25.     }
复制代码

! a9 E% L4 [+ x9 E9 L. k8 L- E6 Q) W

- [2 R5 Z/ p: x, L- r! z: }! p比原项目仓库:
  m0 a2 [5 N. H1 iGithub地址:https://github.com/Bytom/bytom
# k2 ^- J8 ^3 _4 PGitee地址:https://gitee.com/BytomBlockchain/bytom
标签: 币币合约
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

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

    0

  • 关注

    2

  • 主题

    1