Solidity – How to Use Timer in Escrow Contracts

contract-developmentethereum-classicsolidity

I have an escrow contract where party A is sending some token to party B and right now held in escrow. I want to use the timer function in smart contract so that the token will be held for a certain period of time. After that time, the token will be reverted back to originator account, that is party A.

I am adding the escrow in an ERC20 smart contract so I can call it inside the smart contract.

How can I create a holding frame of 30 days in smart contract and after 30 days the token will be reverted back?

Best Answer

You can, for instance, have the function that creates the escrow to save the date of expiration

function createEscrow(...){
    expirationDate = now + 30 days;
    // more code for your escrow
}

The to finalize the escrow you add in your function

function endEscrow(...){
    require(expirationDate < now);
    // more code for your escrow finalization
}

If you want this to be executed automatically you can use services like ETH-Tempus, that schedule execution of contracts and is available on Rinkeby for free.

Disclaimer: I wrote the code for ETH-Tempus

Related Topic