Smart-Lock

如何將 eth 鎖定在智能合約中一段時間?

  • May 11, 2020

有沒有辦法在發送者地址可以贖回之前將一定數量的 eth 發送到智能合約地址設置延遲時間?

下面的程式碼允許多個使用者將乙太幣鎖定在智能合約中。每個受試者可以在一定時間後(在本例中為 1 天)取出他/她存入的乙太幣。

pragma solidity ^0.5.1;

contract timeLock {

   uint256 lockTime = 1 days;

   struct locked{
       uint256 expire;
       uint256 amount;
   }

   mapping(address => locked) users;

   function lockEther() public payable {
       require(msg.value>0);
       locked storage userInfo = users[msg.sender];
       userInfo.expire = block.timestamp + lockTime;
       userInfo.amount = msg.value;
   }

   function withdraw() public {
       require(block.timestamp>=users[msg.sender].expire);
       locked storage userInfo = users[msg.sender];
       uint256 value = userInfo.amount;
       userInfo.expire = 0;
       userInfo.amount = 0;
       msg.sender.transfer(value);
   }



}

希望這可以幫助。

引用自:https://ethereum.stackexchange.com/questions/73427