摘要:储蓄分红合约简介储蓄分红合约指的是项目方发起了一个锁仓计划即储蓄合约和取现合约,用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润。
储蓄分红合约简介
储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润。用户可以在准备期内(dueBlockHeight)参与储蓄,按照合约规定可以 1:1 获取同等数量的储蓄票据资产,同时用户锁仓的资产(deposit)将放到取现合约中,并且项目方是无法动用的,等到锁仓期限(expireBlockHeight)一到,用户便可以调用取现合约将自己储蓄的资产连本待息一同取出来。其示意图如下:
从上图中可以看出,项目方发布了一个利润为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需要再构造一笔交易来提现。
合约源代码// 储蓄合约 import "./FixedLimitProfit" contract FixedLimitCollect(assetDeposited: Asset, totalAmountBill: Amount, totalAmountCapital: Amount, dueBlockHeight: Integer, expireBlockHeight: Integer, additionalBlockHeight: Integer, banker: Program, bankerKey: PublicKey) locks billAmount of billAsset { clause collect(amountDeposited: Amount, saver: Program) { verify below(dueBlockHeight) verify amountDeposited <= billAmount && totalAmountBill <= totalAmountCapital define sAmountDeposited: Integer = amountDeposited/100000000 define sTotalAmountBill: Integer = totalAmountBill/100000000 verify sAmountDeposited > 0 && sTotalAmountBill > 0 if amountDeposited < billAmount { lock amountDeposited of assetDeposited with FixedLimitProfit(billAsset, totalAmountBill, totalAmountCapital, expireBlockHeight, additionalBlockHeight, banker, bankerKey) lock amountDeposited of billAsset with saver lock billAmount-amountDeposited of billAsset with FixedLimitCollect(assetDeposited, totalAmountBill, totalAmountCapital, dueBlockHeight, expireBlockHeight, additionalBlockHeight, banker, bankerKey) } else { lock amountDeposited of assetDeposited with FixedLimitProfit(billAsset, totalAmountBill, totalAmountCapital, expireBlockHeight, additionalBlockHeight, banker, bankerKey) lock billAmount of billAsset with saver } } clause cancel(bankerSig: Signature) { verify above(dueBlockHeight) verify checkTxSig(bankerKey, bankerSig) unlock billAmount of billAsset } }
// 取现合约(本金加利息) contract FixedLimitProfit(assetBill: Asset, totalAmountBill: Amount, totalAmountCapital: Amount, expireBlockHeight: Integer, additionalBlockHeight: Integer, banker: Program, bankerKey: PublicKey) locks capitalAmount of capitalAsset { clause profit(amountBill: Amount, saver: Program) { verify above(expireBlockHeight) define sAmountBill: Integer = amountBill/100000000 define sTotalAmountBill: Integer = totalAmountBill/100000000 verify sAmountBill > 0 && sTotalAmountBill > 0 && amountBill < totalAmountBill define gain: Integer = totalAmountCapital*sAmountBill/sTotalAmountBill verify gain > 0 && gain <= capitalAmount if gain < capitalAmount { lock amountBill of assetBill with banker lock gain of capitalAsset with saver lock capitalAmount - gain of capitalAsset with FixedLimitProfit(assetBill, totalAmountBill, totalAmountCapital, expireBlockHeight, additionalBlockHeight, banker, bankerKey) } else { lock amountBill of assetBill with banker lock capitalAmount of capitalAsset with saver } } clause cancel(bankerSig: Signature) { verify above(additionalBlockHeight) verify checkTxSig(bankerKey, bankerSig) unlock capitalAmount of capitalAsset } }
合约的源代码说明可以具体参考Equity合约介绍.
注意事项:时间期限不是具体的时间,而是通过区块高度来大概估算的(平均区块时间间隔大概为2.5分钟)
比原的精度是8, 即 1BTM = 100000000 neu,正常情况下参与计算都是以neu为单位的,然而虚拟机的int64类型的最大值是9223372036854775807,为了避免数值太大导致计算溢出,所以对计算的金额提出了金额限制(即amountBill/100000000)
另外clause cancel是项目方的管理方法,如果储蓄或者取现没有满额,项目方也可以回收剩余的资产
编译并实例化合约编译Equity合约可以参考一下Equity编译器的介绍说明。假如储蓄合约FixedLimitCollect的参数如下:
assetDeposited :c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee totalAmountBill :10000000000 totalAmountCapital :20000000000 dueBlockHeight :1070 expireBlockHeight :1090 additionalBlockHeight :1100 banker :0014dedfd406c591aa221a047a260107f877da92fec5 bankerKey :055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf
其中bankerKey是管理员的publicKey,可以通过比原链的接口list-pubkeys来获取,注意管理员需要保存一下对应的rootXpub和Path,否则无法正确调用clause cancel。
实例化合约命令如下:
// 储蓄合约 ./equity FixedLimitCollect --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1070 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf // 取现合约 ./equity FixedLimitProfit --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf发布合约交易
发布合约交易即将资产锁定到合约中。由于目前无法在比原的dashboard上构造合约交易,所以需要借助外部工具来发送合约交易,比如postman。按照上述示意图所示,项目方需要发布1000个储蓄资产的储蓄合约和200个利息资产取现合约。假设项目方需要发布1000个储蓄资产(假如精度为8,那么1000个在比原链中表示为100000000000)的锁仓合约,那么他需要将对应数量的票据锁定在储蓄合约中,其交易模板如下:
{ "base_transaction": null, "actions": [ { "account_id": "0ILGLSTC00A02", "amount": 20000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "account_id": "0ILGLSTC00A02", "amount": 100000000000, "asset_id": "13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3", "type": "spend_account" }, { "amount": 100000000000, "asset_id": "13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3", "control_program": "20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c04024204022e040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4d3b02597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac747800c0", "type": "control_program" } ], "ttl": 0, "time_range": 1521625823 }
合约交易成功后,合约control_program对应的UTXO将会被所有用户查询到,使用比原链的接口list-unspent-outputs即可查询。
此外,开发者需要存储一下合约UTXO的assetID和program,以便在DAPP的前端页面的config配置文件和bufferserver缓冲服务器中调用。如上所示:
// 储蓄合约 assetID:13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3 program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c04024204022e040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4d3b02597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac747800c0 // 取现合约 assetID:c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c040242040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac747800c0
本期的比原储蓄分红合约就介绍在这里,下一期我们将详细介绍如何基于该合约开发dapp应用
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/24623.html
摘要:流程总结就是下载安装插件钱包,如果自己的不需要跳过这一步。然后将编译后的合约参数配置在的配置文件,如下图全红部分是测试网合约配置参数调用插件钱包。开发出优秀的应用。 安装使用插件钱包 1. 打开Google浏览器的应用商店,搜索Bystore showImg(https://segmentfault.com/img/bVbq0Ol?w=2554&h=1312); 下载链接:http:/...
摘要:比原项目仓库地址地址在开发合约的过程中你们有没有遇到一些问题呢比如编译合约的过程中不能实时动态的去检查我们所编译的合约文件是否正确,那么我今天就教大家一种很方便的方法。图中对应的参数表示实例化合约,对应的表示指定执行合约里面具体的函数。 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/B...
摘要:结语当前猜谜合约提供了体验版本,有兴趣的朋友可以做一个口令红包,简化一下用户操作和体验。 准备工作: 1、安装全节点钱包V1.0.5以上并同步完成; 2、已经发行一种资产,发行资产的方法具体见文章《如何在Bytom上发布资产?》 3、准备好一些BTM作为手续费; 设置谜语(锁定资产): 1、打开钱包,点击Equity合约按钮; showImg(https://segmentfault....
摘要:笔者看了官方的多个合约模版,于是准备部署一个简单的比较数字大小的合约。部署合约部署合约其实就是发起交易,在交易中带上合约程序。到此,发布和解锁合约就全部完成啦。 发行资产 在比原链上发行资产比较方便快捷,使用节点的dashboard图形界面操作就可以,先在 资产一栏新建资产showImg(https://segmentfault.com/img/bVbhq1s?w=1044&h=892...
阅读 3587·2021-09-13 10:28
阅读 1936·2021-08-10 09:43
阅读 1009·2019-08-30 15:44
阅读 3177·2019-08-30 13:14
阅读 1829·2019-08-29 16:56
阅读 2937·2019-08-29 16:35
阅读 2842·2019-08-29 12:58
阅读 863·2019-08-26 13:46