3

Is there any way to send an amount of eth to a smart contract address setting a delay time before the sender address could redeem?

1
  • Add a modifier to the payment function that requires the blocknumber to be greater than a certain number in the future?
    – 0xsegfault
    Jul 29, 2019 at 21:31

2 Answers 2

0

The code below allows several users to lock ether in a smart contract. Each subject can withdraw the ether that he/she deposited after a certain amount of time (in this example 1 day).

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);
    }



}

Hope this helps.

1
  • I would like to add an OR unlock condition other than date, such as Eth price target, is it possible?
    – cesko80
    Nov 23, 2022 at 18:20
1

OpenZeppelin has a TokenTimelock contract and you could use a similar idea for Ether. https://docs.openzeppelin.com/contracts/2.x/api/token/erc20#TokenTimelock

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.