储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润。用户可以在准备期内(dueBlockHeight)参与储蓄,按照合约规定可以 1:1 获取同等数量的储蓄票据资产,同时用户锁仓的资产(deposit)将放到取现合约中,并且项目方是无法动用的,等到锁仓期限(expireBlockHeight)一到,用户便可以调用取现合约将自己储蓄的资产连本待息一同取出来。其示意图如下:
1 y0 S$ h) |; o' y
从上图中可以看出,项目方发布了一个利润为20%的锁仓项目,其中储蓄合约FixedLimitCollect锁定了1000个票据资产(bill),同时项目方将200个储蓄资产(deposit)锁定到利息合约中。待项目方发布完合约之后,所有用户便可以参与了。例如上图中user1调用合约储蓄了500,这500个储蓄资产将被锁定在取现合约FixedLimitProfit中,同时user1获得了500个票据资产,剩余找零的资产将继续锁定在储蓄合约FixedLimitCollect中,以此类推,user2和user3也是相同的流程,直到储蓄合约没有资产为止。取现合约FixedLimitProfit跟储蓄合约的模型大致相同,只是取现合约是由多个UTXO组成的,用户在取现的时候可以并行操作。但是如果合约中的面值不能支持用户一次性取现的话,需要分多次提取。例如user1拥有500个票据资产,而可以获得的本息总额为600,但是取现的UTXO面值为500,那么user1一次最多只能取500,剩下的100需要再构造一笔交易来提现。
合约源代码& Z4 |. l2 ?7 h I3 B7 n
// 储蓄合约7 s# M7 }' e0 b9 p n
import "./FixedLimitProfit"
contract FixedLimitCollect(assetDeposited: Asset,9 H: I) G+ f& R! `
totalAmountBill: Amount,
totalAmountCapital: Amount,+ \8 ?; i7 F; v! }0 J/ s( b
dueBlockHeight: Integer,
expireBlockHeight: Integer,. T3 H5 I: u1 P4 O
additionalBlockHeight: Integer,6 o* k( N/ w" \
banker: Program,( @7 }5 y0 b* i* o( H
bankerKey: PublicKey) locks billAmount of billAsset {% v# N) g- b9 U9 ?! h
clause collect(amountDeposited: Amount, saver: Program) {4 q. T5 B) g" {& ^5 W
verify below(dueBlockHeight)
verify amountDeposited 0 && sTotalAmountBill > 0
if amountDeposited
// 取现合约(本金加利息)- V' `" w# c6 Z. X# p) z
contract FixedLimitProfit(assetBill: Asset,
totalAmountBill: Amount,1 \; u' g. v% ]
totalAmountCapital: Amount,% }$ [+ h! o' z& ~( ^
expireBlockHeight: Integer,6 N8 s2 q0 H" b2 V% O7 U
additionalBlockHeight: Integer,
banker: Program,5 s* F' C9 M4 E$ P& P/ ~
bankerKey: PublicKey) locks capitalAmount of capitalAsset {6 {* H2 i5 f9 D4 W& X( q
clause profit(amountBill: Amount, saver: Program) {0 ~8 E- M$ _1 L0 P# z* ^; e# G8 C
verify above(expireBlockHeight)0 I7 a. ^! B, J) J" X4 M9 O% [
define sAmountBill: Integer = amountBill/100000000
define sTotalAmountBill: Integer = totalAmountBill/100000000
verify sAmountBill > 0 && sTotalAmountBill > 0 && amountBill 0 && gain
合约的源代码说明可以具体参考Equity合约介绍.( N0 v/ o& U; ~# ?' V
注意事项:
编译并实例化合约# F. J j u4 J+ S/ I
编译Equity合约可以参考一下Equity编译器的介绍说明。假如储蓄合约FixedLimitCollect的参数如下:
assetDeposited :c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee
totalAmountBill :10000000000
totalAmountCapital :20000000000
dueBlockHeight :10707 E4 r, L+ D# `
expireBlockHeight :1090
additionalBlockHeight :1100
banker :0014dedfd406c591aa221a047a260107f877da92fec5. B: t0 a& S4 a7 V
bankerKey :055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf b% z4 i, @2 x0 L; z* {
其中bankerKey是管理员的publicKey,可以通过比原链的接口list-pubkeys来获取,注意管理员需要保存一下对应的rootXpub和Path,否则无法正确调用clause cancel。- T. f/ N. d3 g$ M
实例化合约命令如下:+ B9 V1 J5 k2 j! k a1 K
// 储蓄合约
./equity FixedLimitCollect --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1070 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf2 w/ R3 J7 Z. l' o& U/ G
// 取现合约! ?! L. t7 |, O& C4 H( h& `
./equity FixedLimitProfit --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf
发布合约交易9 Q) V$ f( W& ?5 z
发布合约交易即将资产锁定到合约中。由于目前无法在比原的dashboard上构造合约交易,所以需要借助外部工具来发送合约交易,比如postman。按照上述示意图所示,项目方需要发布1000个储蓄资产的储蓄合约和200个利息资产取现合约。假设项目方需要发布1000个储蓄资产(假如精度为8,那么1000个在比原链中表示为100000000000)的锁仓合约,那么他需要将对应数量的票据锁定在储蓄合约中,其交易模板如下:
{8 B; h5 ?# Y1 j8 Y
"base_transaction": null,
"actions": [
{
"account_id": "0ILGLSTC00A02",1 Z2 c q# B9 w
"amount": 20000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",& m3 O0 B6 L! q6 ?( \) O
"type": "spend_account"
},
{) q% m3 F# C# k! y5 [4 b
"account_id": "0ILGLSTC00A02",
"amount": 100000000000,- p! ^3 n& Z4 X' K) v& W
"asset_id": "13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3",- @- a( q' ?+ T8 }. {- ^; H
"type": "spend_account"
},
{
"amount": 100000000000,- G& }/ `$ ]# y, u( @" z
"asset_id": "13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3", P1 ?, R9 u5 [6 D" m, I
"control_program": "20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c04024204022e040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4d3b02597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac747800c0",
"type": "control_program"
}8 q' J" n; x, Y5 h. r" w8 P
],3 n0 X; Y8 X( x$ H; V: O7 K, Z- s
"ttl": 0,; ^- M$ p @9 s0 O: m4 T, W+ I |
"time_range": 1521625823
}
合约交易成功后,合约control_program对应的UTXO将会被所有用户查询到,使用比原链的接口list-unspent-outputs即可查询。! Y! n" ~- i' e/ s5 `2 k
此外,开发者需要存储一下合约UTXO的assetID和program,以便在DAPP的前端页面的config配置文件和bufferserver缓冲服务器中调用。如上所示:
// 储蓄合约$ u7 ~3 | f& ~; |% K
assetID:13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3: }( v2 ^3 @- N7 B: z: l; g
program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c04024204022e040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4d3b02597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac747800c03 q9 G- c0 [5 Q! X" d' [
// 取现合约
assetID:c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee* m$ E# q% j! E% Q
program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c040242040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac747800c09 D" x* ?, t* B. J) [) Z
本期的比原储蓄分红合约就介绍在这里,下一期我们将详细介绍如何基于该合约开发dapp应用