摘要:儲蓄分紅合約簡介儲蓄分紅合約指的是項目方發(fā)起了一個鎖倉計劃即儲蓄合約和取現(xiàn)合約,用戶可以在準備期自由選擇鎖倉金額參與該計劃,等到鎖倉到期之后還可以自動獲取鎖倉的利潤。
儲蓄分紅合約簡介
儲蓄分紅合約指的是項目方發(fā)起了一個鎖倉計劃(即儲蓄合約和取現(xiàn)合約),用戶可以在準備期自由選擇鎖倉金額參與該計劃,等到鎖倉到期之后還可以自動獲取鎖倉的利潤。用戶可以在準備期內(nèi)(dueBlockHeight)參與儲蓄,按照合約規(guī)定可以 1:1 獲取同等數(shù)量的儲蓄票據(jù)資產(chǎn),同時用戶鎖倉的資產(chǎn)(deposit)將放到取現(xiàn)合約中,并且項目方是無法動用的,等到鎖倉期限(expireBlockHeight)一到,用戶便可以調(diào)用取現(xiàn)合約將自己儲蓄的資產(chǎn)連本待息一同取出來。其示意圖如下:
從上圖中可以看出,項目方發(fā)布了一個利潤為20%的鎖倉項目,其中儲蓄合約FixedLimitCollect鎖定了1000個票據(jù)資產(chǎn)(bill),同時項目方將200個儲蓄資產(chǎn)(deposit)鎖定到利息合約中。待項目方發(fā)布完合約之后,所有用戶便可以參與了。例如上圖中user1調(diào)用合約儲蓄了500,這500個儲蓄資產(chǎn)將被鎖定在取現(xiàn)合約FixedLimitProfit中,同時user1獲得了500個票據(jù)資產(chǎn),剩余找零的資產(chǎn)將繼續(xù)鎖定在儲蓄合約FixedLimitCollect中,以此類推,user2和user3也是相同的流程,直到儲蓄合約沒有資產(chǎn)為止。取現(xiàn)合約FixedLimitProfit跟儲蓄合約的模型大致相同,只是取現(xiàn)合約是由多個UTXO組成的,用戶在取現(xiàn)的時候可以并行操作。但是如果合約中的面值不能支持用戶一次性取現(xiàn)的話,需要分多次提取。例如user1擁有500個票據(jù)資產(chǎn),而可以獲得的本息總額為600,但是取現(xiàn)的UTXO面值為500,那么user1一次最多只能取500,剩下的100需要再構(gòu)造一筆交易來提現(xiàn)。
合約源代碼// 儲蓄合約 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 } }
// 取現(xiàn)合約(本金加利息) 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合約介紹.
注意事項:時間期限不是具體的時間,而是通過區(qū)塊高度來大概估算的(平均區(qū)塊時間間隔大概為2.5分鐘)
比原的精度是8, 即 1BTM = 100000000 neu,正常情況下參與計算都是以neu為單位的,然而虛擬機的int64類型的最大值是9223372036854775807,為了避免數(shù)值太大導致計算溢出,所以對計算的金額提出了金額限制(即amountBill/100000000)
另外clause cancel是項目方的管理方法,如果儲蓄或者取現(xiàn)沒有滿額,項目方也可以回收剩余的資產(chǎn)
編譯并實例化合約編譯Equity合約可以參考一下Equity編譯器的介紹說明。假如儲蓄合約FixedLimitCollect的參數(shù)如下:
assetDeposited :c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee totalAmountBill :10000000000 totalAmountCapital :20000000000 dueBlockHeight :1070 expireBlockHeight :1090 additionalBlockHeight :1100 banker :0014dedfd406c591aa221a047a260107f877da92fec5 bankerKey :055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf
其中bankerKey是管理員的publicKey,可以通過比原鏈的接口list-pubkeys來獲取,注意管理員需要保存一下對應的rootXpub和Path,否則無法正確調(diào)用clause cancel。
實例化合約命令如下:
// 儲蓄合約 ./equity FixedLimitCollect --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1070 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf // 取現(xiàn)合約 ./equity FixedLimitProfit --instance c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee 10000000000 20000000000 1090 1100 0014dedfd406c591aa221a047a260107f877da92fec5 055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf發(fā)布合約交易
發(fā)布合約交易即將資產(chǎn)鎖定到合約中。由于目前無法在比原的dashboard上構(gòu)造合約交易,所以需要借助外部工具來發(fā)送合約交易,比如postman。按照上述示意圖所示,項目方需要發(fā)布1000個儲蓄資產(chǎn)的儲蓄合約和200個利息資產(chǎn)取現(xiàn)合約。假設項目方需要發(fā)布1000個儲蓄資產(chǎn)(假如精度為8,那么1000個在比原鏈中表示為100000000000)的鎖倉合約,那么他需要將對應數(shù)量的票據(jù)鎖定在儲蓄合約中,其交易模板如下:
{ "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即可查詢。
此外,開發(fā)者需要存儲一下合約UTXO的assetID和program,以便在DAPP的前端頁面的config配置文件和bufferserver緩沖服務器中調(diào)用。如上所示:
// 儲蓄合約 assetID:13016eff73ffb7539a69e122f80f5c1cc94446773ac3f64dec290429f87e73b3 program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c04024204022e040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4d3b02597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac747800c0 // 取現(xiàn)合約 assetID:c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee program:20055539eb36abcaaf127c63ae20e3d049cd28d0f1fe569df84da3aedb018ca1bf160014dedfd406c591aa221a047a260107f877da92fec5024c040242040500c817a8040500e40b540220c6b12af8326df37b8d77c77bfa2547e083cbacde15cc48da56d4aa4e4235a3ee4caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac747800c0
本期的比原儲蓄分紅合約就介紹在這里,下一期我們將詳細介紹如何基于該合約開發(fā)dapp應用
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/24623.html
摘要:流程總結(jié)就是下載安裝插件錢包,如果自己的不需要跳過這一步。然后將編譯后的合約參數(shù)配置在的配置文件,如下圖全紅部分是測試網(wǎng)合約配置參數(shù)調(diào)用插件錢包。開發(fā)出優(yōu)秀的應用。 安裝使用插件錢包 1. 打開Google瀏覽器的應用商店,搜索Bystore showImg(https://segmentfault.com/img/bVbq0Ol?w=2554&h=1312); 下載鏈接:http:/...
摘要:比原項目倉庫地址地址在開發(fā)合約的過程中你們有沒有遇到一些問題呢比如編譯合約的過程中不能實時動態(tài)的去檢查我們所編譯的合約文件是否正確,那么我今天就教大家一種很方便的方法。圖中對應的參數(shù)表示實例化合約,對應的表示指定執(zhí)行合約里面具體的函數(shù)。 比原項目倉庫: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/B...
摘要:結(jié)語當前猜謎合約提供了體驗版本,有興趣的朋友可以做一個口令紅包,簡化一下用戶操作和體驗。 準備工作: 1、安裝全節(jié)點錢包V1.0.5以上并同步完成; 2、已經(jīng)發(fā)行一種資產(chǎn),發(fā)行資產(chǎn)的方法具體見文章《如何在Bytom上發(fā)布資產(chǎn)?》 3、準備好一些BTM作為手續(xù)費; 設置謎語(鎖定資產(chǎn)): 1、打開錢包,點擊Equity合約按鈕; showImg(https://segmentfault....
摘要:筆者看了官方的多個合約模版,于是準備部署一個簡單的比較數(shù)字大小的合約。部署合約部署合約其實就是發(fā)起交易,在交易中帶上合約程序。到此,發(fā)布和解鎖合約就全部完成啦。 發(fā)行資產(chǎn) 在比原鏈上發(fā)行資產(chǎn)比較方便快捷,使用節(jié)點的dashboard圖形界面操作就可以,先在 資產(chǎn)一欄新建資產(chǎn)showImg(https://segmentfault.com/img/bVbhq1s?w=1044&h=892...
閱讀 3588·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