使用shuttle实现bytom上跨链资产交换
蕙质兰心283
发表于 2023-1-10 02:30:43
155
0
0
该工具主要的功能是实现不同资产在比原链上交换,首先是在比原链上不同资产进行交换。这个主要使用币币交换合约来进行资产的交换。如果是在比原的主侧链上进行跨链资产交换,则使用的是哈希时间锁合约来完成两条不同链上的资产交换。
下面我们来看一下操作的具体步骤:. S% a% k9 h! L# P7 `+ f( h6 q
第一步: 首先搭建节点环境) s3 m [9 P3 r) J" X9 h; U
1.1 golang的版本高于1.12,同时设置好的$GOPATH环境目录
1.2 启动并配置好bytom节点,参考:bytom readme.md
1.3 源码构建:Shuttle仍处于测试阶段,因此存储库代码将经常更改。您可以直接从源代码构建工具。参考下面的命令:7 X( |0 I& s% @" R! N
$ git clone https://github.com/Bytom/shuttle.git $GOPATH/src/github.com/shuttle8 E3 O! [. S, T; E% x4 Z) @/ S% t
$ cd $GOPATH/src/github.com/shuttle8 G+ H% I% f A- E1 R
$ make install1 m: ~5 b% B5 t. L
Shuttle help 相关命令如下:' `1 j9 D/ k- W
$ swap -h+ O2 j7 ~: v" j. |( K) Z
swap is a commond line client for bytom contract3 x9 J/ n6 _. ^& F, V
Usage:5 g+ [, r/ c% P
swap [flags]5 H o# ]0 j6 A# H
swap [command]
vailable Commands:
callHTLC call HTLC contract for asset swapping; O( N: j% W+ z
callTradeoff call tradeoff contract for asset swapping
cancelHTLC cancel HTLC contract for asset swapping
cancelTradeoff cancel tradeoff contract for asset swapping" G8 d1 G0 y- q) c' _% H
deployHTLC deploy HTLC contract: w L' ?, o: G9 O4 Z% P, y. A8 Z
deployTradeoff deploy tradeoff contract
help Help about any command) h& H" C3 M7 F" t' W
Flags:
-h, --help help for swap
Use "swap [command] --help" for more information about a command.
第二步:同一条链上进行跨链资产交换
如果你想在单一一条链上交换比原资产,不论是bytom主链还是vapor侧链,都直接使用Tradeoff合约可以实现资产的交换,整个交换流程图如下: o0 I7 o. w! J
1 I1 n+ V8 T d7 c6 l! l' ]
首先我们启动bytom节点,为了测试,你可以启动solnet节点:
$ bytomd init --chain_id=solonet --home $HOME/bytom/solonet # init bytom solonet node
$ bytomd node --home $HOME/bytom/solonet --mining # launch bytom solonet node and start mining
搭建完测试节点以后,可以在区块链上创建账户,发行资产(issue asset)用来测试
创建账户参考
发行资产参考
例如:在bytom主链上,账户A有200BTC,账户B有10个BTM,他们就可以是用shuttle工具进行交换。
####2.1 下面正式部署 tradeoff合约(下面是合约部署相关命令以及相关参数)# }6 p. d, {& k% c) ~: N! { G+ m
$ swap deployTradeoff -h
deploy tradeoff contract# k; l4 C5 ~; i: `! g% a
Usage:
swap deployTradeoff [contract flags(paramenters and locked value)] [txFee flag] [URL flags(ip and port)] [flags]
Flags:
--amountLocked uint tradeoff contract locked value with amount //锁定资产的数量
--amountRequested uint tradeoff contract paramenter with requested amount //锁定需要兑换的资产数量, q+ l# Q4 H" i* U1 _
--assetLocked string tradeoff contract locked value with assetID //资产ID( U% s( _, i' L& j* q {. U
--assetRequested string tradeoff contract paramenter with requested assetID //兑换的资产ID: A' J! M; b$ [4 v; O9 W( [, Q- V
--cancelKey string tradeoff contract paramenter with seller pubkey for cancelling the contract //解锁合约需要用到的pubkey i! p) R, p" i0 }1 |* g: ]
-h, --help help for deployTradeoff8 g% I6 m5 \4 v5 w4 d/ G4 k3 S* _1 i
--ip string network address (default "127.0.0.1")) r/ C0 q1 k2 I5 u$ z; E0 m* q
--port string network port (default "9888")
--seller string tradeoff contract paramenter with seller control-program( e3 f1 f+ b$ b9 L
--txFee uint contract transaction fee (default 40000000)$ t# \6 l( J, ^8 R! A9 G# r- c) t1 h
下面是实例化部署合约的过程:( b; J( K; `; h8 M( `2 A% ?2 |
$ swap deployTradeoff 10CJPO1HG0A02 12345 --amountLocked 20000000000 --amountRequested 1000000000 --assetLocked bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a --assetRequested ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff --cancelKey 3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e --seller 00145dd7b82556226d563b6e7d573fe61d23bd461c1f --txFee 40000000
--> contractUTXOID: 34996b0838108de8c614bc018e8fdbbfc08a47ffbe0fd6d7f41892b90de3999f" E% I5 g! f8 {0 o) X+ i) V& I
合约部署好了以后,2.5 分钟后,比原链上的一个新的区块被矿工打包后,部署的合约就可以被认证。
2.2 调用tradeoff合约(下面是调用合约的命令)# p+ M* K) _- v: R# j4 y3 u
$ swap callTradeoff -h
call tradeoff contract for asset swapping
Usage:& O7 q( A. ~; j* L7 [
swap callTradeoff [txFee flag] [URL flags(ip and port)] [flags]
Flags:
-h, --help help for callTradeoff& D7 c: \& K! S N
--ip string network address (default "127.0.0.1")
--port string network port (default "9888")$ n, P# q4 x" v1 L1 t9 h) g
--txFee uint contract transaction fee (default 40000000)1 K" |$ `# c" a( e' L( X- I/ C
调用合约实例化参数如下:
$ swap callTradeoff 10CKAD3000A02 12345 00140fdee108543d305308097019ceb5aec3da60ec66 34996b0838108de8c614bc018e8fdbbfc08a47ffbe0fd6d7f41892b90de3999f) b' {- ?! M0 a1 l5 q
--> txID: 55e43274d2d92504a903a13e3f6517d63434fc19a2fa0e1fc0a9a5c8c75e8f6c3 w0 u6 o7 ^# S% r. Y* b0 t
调用完以后,当交易在新的区块中被确认,整个资产交换过程就完成,如下图:
" V" u8 _) d" [
交换完成后,现在账户账户A有200个BTC,账户B有10个BTM。$ E4 n( w z' G5 l8 v, a# P' _
2.3 取消tradeoff合约(下面是取消tradeoff合约的相关命令)
如果有人想取消tradeoff合约交易,可以调用该命令取消,如下:; M$ s2 l/ m8 _' z
$ swap cancelTradeoff -h
cancel tradeoff contract for asset swapping, C7 B4 b& o3 z0 q7 i3 d* A {! [
Usage:
swap cancelTradeoff [txFee flag] [URL flags(ip and port)] [flags]
Flags:9 f( h& A* Y1 W: f0 s3 f+ M
-h, --help help for cancelTradeoff
--ip string network address (default "127.0.0.1") h1 Q6 @8 w/ t' K
--port string network port (default "9888")
--txFee uint contract transaction fee (default 40000000)' l( ~, v0 m; l# D; ]
取消tradeoff合约的实例化如下:
$ swap deployTradeoff 10CJPO1HG0A02 12345 --amountLocked 20000000000 --amountRequested 1000000000 --assetLocked bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a --assetRequested ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff --cancelKey 3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e --seller 00145dd7b82556226d563b6e7d573fe61d23bd461c1f --txFee 40000000
--> contractUTXOID: e898ea907f8586b3211a46b69d0cddd363f8a850f559656570eb6afc6ac382c50 Z4 ]4 L! S. }$ a$ a) v
$ swap cancelTradeoff 10CJPO1HG0A02 12345 00145b0a81adc5c2d68a9967082a09c96e82d62aa058 e898ea907f8586b3211a46b69d0cddd363f8a850f559656570eb6afc6ac382c5
--> txID: 0f75db743196b8b2e514c49d6483dfba9bd5ab8e913a7559ecaea0324977313f& C G6 S5 K) M1 S5 n5 {$ k& l
示例图如下:# T) z/ r* E1 d/ f
v2 q% B+ W: w, S3 o! `
上面整个流程就是在同一条链上不同资产进行交换的整个过程,下面我们来看一下不同链上的两个资产进行交换,假设以bytom和vapor为例。
第三步:不同链上资产进行交换$ k0 U' |/ s1 j6 {# f# K7 Y4 V/ Z
你可以使用该工具在bytom和vapor上交换资产:4 a$ ^6 u$ f6 Q$ W4 O3 Q! D7 V
$ i0 W- W. r1 B2 ?, ~' {$ d5 e
3.1 搭建并启动bytom和vapor节点, V. R1 M) P( k3 p# ?: Z
为了方便测试。你可以启动bytom和vapor的solonet节点
启动bytom solonet 节点:: v) s. O9 A2 g$ A$ o
$ bytomd init --chain_id=solonet --home $HOME/bytom/solonet # init bytom solonet node' a+ x* I* k4 g% G7 |
$ bytomd node --home $HOME/bytom/solonet --mining # launch bytom solonet node and start mining6 ?- q0 p; e* c4 G6 d# K6 N
启动vapor solonet 节点:$ a& q. v6 v. a5 m2 p3 v. z! a/ `
$ vapord init --chain_id=solonet --home $HOME/bytom/vapor-solonet # init vapor solonet node
$ vapord node --home $HOME/bytom/vapor-solonet --mining # launch vapor solonet node and start mining" P$ P. Q5 ^$ k. d q5 E+ ^ C2 B' P
3.2 部署HTLC合约(合约的部署命令如下)+ l' X; _) j* _% |1 m
$ swap deployHTLC -h
deploy HTLC contract
Usage:
swap deployHTLC [contract flags(paramenters and locked value)] [txFee flag] [URL flags(ip and port)] [flags]
Flags:
--amountLocked uint HTLC contract locked value with amount. `4 v; e% ~) T: `
--assetLocked string HTLC contract locked value with assetID
--blockHeight uint HTLC contract locked value with blockHeight
--hash string HTLC contract locked value with hash
-h, --help help for deployHTLC% k, Y* H0 T n. u) Q/ Q& T
--ip string network address (default "127.0.0.1")% Z7 n0 _- B1 q w9 s
--port string network port (default "9888")# e: M- W# ~1 e; G* [- H
--recipient string HTLC contract paramenter with recipientPublicKey
--sender string HTLC contract paramenter with sender PublicKey
--txFee uint contract transaction fee (default 40000000) \' b# k, M2 `, V1 e$ X' A
部署HTLC合约的实例参数如下:6 b; x! x; Y) p4 Y0 ]' g) x
$ swap deployHTLC 11BB7TC8G0A02 12345 --sender 7262584844d4c14f512d1b6c9838e62c320e1d7887e7185bfea920c72a944e44 --recipient 562013c2f9082f1db52a2571034428921dd6eec8c010c2b2387f5b6125ff4aa7 --blockHeight 1200 --hash 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 --assetLocked bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a --amountLocked 200000000002 _! A" E, S( ^; h
--> contractUTXOID: e1b104a03f4135b45d1c4c5fabbdca4dd0555653a588b71d790d45c4ffb2c50e
3.3 调用HTLC合约
$ swap callHTLC -h
call HTLC contract for asset swapping6 L5 z' @. O1 j4 w! \+ W8 M
Usage:% [2 E5 W0 y" `/ W2 s+ P& e
swap callHTLC [txFee flag] [URL flags(ip and port)] [flags]
Flags:7 N6 @$ P4 b U0 V4 W* T* |
-h, --help help for callHTLC. @; b7 @0 @$ _& f/ W
--ip string network address (default "127.0.0.1")( t& U% i$ e- ]4 {
--port string network port (default "9888") h9 n6 w+ p) h2 b8 L3 ?3 ^- o- {
--txFee uint contract transaction fee (default 40000000)
调用HTLC合约的实力化参数如下:1 m1 V' s5 ?$ E% x- `4 `5 U
$ swap callHTLC 11BB86V300A04 12345 0014230cb75fcfcc70c580ce7f1d21c1e374d27334a8 68656c6c6f e1b104a03f4135b45d1c4c5fabbdca4dd0555653a588b71d790d45c4ffb2c50e
--> txID: df57b7906684e3d85adf59073ccbc0a3c5114b165626e9791f3269e9e57c319e
实际的交换过程如下:
3.4 取消HTLC合约(命令参数如下)/ i+ @/ O O( C T8 Q
$ swap cancelHTLC -h- M' a; u& p" A2 k6 X: ^
cancel HTLC contract for asset swapping) V2 j: d: ^# t! {9 x9 F
Usage:3 u" E9 i# ?! Z
swap cancelHTLC [txFee flag] [URL flags(ip and port)] [flags]9 y* p2 I& x. m8 I1 \: \$ h, u
Flags:
-h, --help help for cancelHTLC
--ip string network address (default "127.0.0.1")
--port string network port (default "9888")* M& C% p t& l7 u, N! J" |& d
--txFee uint contract transaction fee (default 40000000)
取消HTLC合约的实例化参数如下:1 H' J- V/ O" V1 |
$ swap deployHTLC 11BB7TC8G0A02 12345 --sender 7262584844d4c14f512d1b6c9838e62c320e1d7887e7185bfea920c72a944e44 --recipient 562013c2f9082f1db52a2571034428921dd6eec8c010c2b2387f5b6125ff4aa7 --blockHeight 1200 --hash 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 --assetLocked bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a --amountLocked 20000000000
--> contractUTXOID: 68ed6b948b93544ea135482f1acd93d6b10cdc88f52d44133d264a5ee86b1ebd
$ swap cancelHTLC 11BB7TC8G0A02 12345 001434fec270871c1f3420db85831f59511b2dd2a026 68ed6b948b93544ea135482f1acd93d6b10cdc88f52d44133d264a5ee86b1ebd
--> txID: c70a467e94b287d29c6d91a2cb6f8ef3c7ef4dba315d99acf2dc9ff698996270 ' [8 r, K) I4 P% V4 D8 L
示例图如下:
) h4 l$ N5 X! U; S
取消HTLC合约以后,a4发起HTLC合约兑换的资产自动返回账户。, G( @1 W8 ~9 O' k: M
这些都是a账户在bytom上的操作,a5账户同时在侧链上实现相同的操作就可以了。
成为第一个吐槽的人