時間鎖定和歸屬智能合約
我想創建一個智能合約,負責將資金鎖定和/或歸屬給多個受益人(由智能合約的所有者輸入)。它將歸屬和鎖定的資金需要來自另一個代幣智能合約。這甚至可能嗎?我的問題是你如何從 ERC20 代幣智能合約中獲得資金到我的歸屬/鎖定智能合約,以便它可以根據一些既定規則操縱資金。
非常感謝<3
是的,你可以做到。展示一個完整的實現太複雜了,所以我假設你知道如何處理 ERC20 合約。
通常,當部署 ERC20 時,初始供應被“鑄造”給部署者。下一步是將一些代幣轉移到時間鎖定合約中。時間鎖定合約可以簡單地持有特定地址的餘額,直到某個時間或塊號過去,然後允許索賠人地址退出。
這是一個非常快速且未經測試的塗鴉,可為您提供一些想法。從 openzeppelin 的 ERC20 介面開始:https ://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC20/IERC20.sol
pragma solidity ^0.4.24; /** * @title ERC20 interface * @dev see https://github.com/ethereum/EIPs/issues/20 */ interface IERC20 { function totalSupply() external view returns (uint256); function balanceOf(address who) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function transfer(address to, uint256 value) external returns (bool); function approve(address spender, uint256 value) external returns (bool); function transferFrom(address from, address to, uint256 value) external returns (bool); event Transfer( address indexed from, address indexed to, uint256 value ); event Approval( address indexed owner, address indexed spender, uint256 value ); }
然後,使用上面定義的 ERC20 介面繼續執行時間鎖定合約。
我們將製作編號的鎖箱。使用者需要指定包含其資金的盒子的編號。通過查看事件日誌很容易發現這一點。編號框意味著合約可以輕鬆地為單個地址處理多個批次,具有不同的金額和截止日期。
如果您以前沒有見過,存款功能使用
transferFrom
. 這假定使用者首先簽署了一份approve
授權該合約,以幫助自己獲得一定數量的資金。這通常是從客戶端協調的。就這樣,客戶端發送approve(addressTimeLock, uintAmount)
到令牌合約,然後發送deposit(addressBeneficiary, uintAmount, uintDeadline)
到 timeLock 合約。contract TimeLock { IERC20 token; struct LockBoxStruct { address beneficiary; uint balance; uint releaseTime; } LockBoxStruct[] public lockBoxStructs; // This could be a mapping by address, but these numbered lockBoxes support possibility of multiple tranches per address event LogLockBoxDeposit(address sender, uint amount, uint releaseTime); event LogLockBoxWithdrawal(address receiver, uint amount); constructor(address tokenContract) public { token = IERC20(tokenContract); } function deposit(address beneficiary, uint amount, uint releaseTime) public returns(bool success) { require(token.transferFrom(msg.sender, address(this), amount)); LockBoxStruct memory l; l.beneficiary = beneficiary; l.balance = amount; l.releaseTime = releaseTime; lockBoxStructs.push(l); emit LogLockBoxDeposit(msg.sender, amount, releaseTime); return true; } function withdraw(uint lockBoxNumber) public returns(bool success) { LockBoxStruct storage l = lockBoxStructs[lockBoxNumber]; require(l.beneficiary == msg.sender); require(l.releaseTime <= now); uint amount = l.balance; l.balance = 0; emit LogLockBoxWithdrawal(msg.sender, amount); require(token.transfer(msg.sender, amount)); return true; } }
owner
如果受益人無法簽署交易,而需要索取存放在本契約中的代幣,則沒有任何概念也無法恢復代幣。輸入驗證可以加強,但為了清楚起見,我希望它盡可能短。希望能幫助到你。