For a specific purpose, I would like to create a smart contract in ETH blockchain and its job is to send a specific amount of ETH to a defined address when the time comes (unix time or specific block number).

I'm just get started with Ethereum and blockchain, so forgive me if this question is noob. But someone has a code sample that I can follow and change to fit my purpose?

Here's all I need:

  • time come (unix time / block number) -> send defined ETH amount to defined ETH address/another smart contract address

  • with customized gas & gwei

up vote 7 down vote accepted

In fact, this is quite complicated. Ethereum contracts cannot by themselves start or initiate a transaction at a particular point in time. You have two options to solve this problem: External calls or a withdraw pattern, see below.

External Calls

At the start of every transaction or chain of transactions (i.e. contracts calling other contracts or sending Ether to other contracts) there needs to be an initial external call by a regular account.

So if you want your contract to send Ether to a predefined address within a particular time window, either you or someone else needs to start this transaction. If you don't want to do it yourself or you are looking for some automated approach, you may want to check out services such as Ethereum Alarm Clock. It is currently working on mainnet. You could also set up a cron job on your own server that starts the transaction if you don't want to use EAC.

Still, bottom line is: You will always need a trusted third party or service that is external to the Ethereum blockchain for scheduled function executions.

Withdraw Pattern Alternative

As an alternative, you could offer the beneficiary of your contract that she or he is allowed to withdraw funds from the contract if a particular point in time is reached:

function withdraw(){
   // money can only be withdrawn if time has come
   require(now > timeHasCome);
   // money can only be withdrawn by beneficiary
   require(msg.sender == beneficiary);
   // make sure beneficiary can only withdraw once
   // by deleting his address from the contract's memory
   beneficiary = address(0);
   // finally send the funds
   msg.sender.transfer(predefinedAmount);
}

So if timeHasCome, beneficiary is allowed to withdraw predefinedAmount once by calling the withdraw function of your contract.

Note that now is the time of block creation and is set by the miner. now can be manipulated within a couple of seconds, so its usage is safe only if you operate on timescales such as hours or days.

A contract cannot wake up itself to execute something, this is not something implemented in Ethereum.

There is a service that allows to do that, Ethereum Alarm Clock. It's a smart contract that registers actions and dates, and people can call the contract that calls the scheduled action.

It relies on users having an interest (an Ether reward) to do so.

At the moment, the service is functioning on mainnet.

Alternatively you can do is schedule the call, and it will be executed after the limit date if someone calls a special function in your contract. You could actually do the call yourself, or call it with a script on your computer.

The simplest and the most reliable way is to use Ethereum Alarm Clock.

You can use MyCrypto wallet, toggle 'Send Later' switch, set time of execution and set data field with call to your smart contract.

Here is comprehensive tutorial and video how to do it: https://medium.com/@MyCrypto/schedule-your-ethereum-transactions-now-on-mycrypto-92a6305f7dcc.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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