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?
2 Answers
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.
-
I would like to add an OR unlock condition other than date, such as Eth price target, is it possible?– cesko80Commented Nov 23, 2022 at 18:20
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