5

I want to write a solidity function like

 function func(uint n, parameters*) payable public {

 ""wait n seconds""

 other statements  
 }

Is it possible? It seems that delay parameter in oraclize_query gives the partial answer, but it limits to no bigger than 60 days. Is there any solution for this task for arbitrary n?

Thanks in advance.

0

4 Answers 4

6

There is no concept of delay on solidity or ethereum. The VM runs the code in its own speed, and the final state is the only thing viewable on the blockchain (even if a function took some time to run, you wouldn't be able to see it while running).

When you're using oraclize, you're giving an out-of-blockchain process an instruction to do something, which might include a time delay, but on the blockchain itself it's impossible.

2

Yes, you can achieve the delay action in solidity using ethereum alarm clock contract. It will trigger the action after n seconds or days. you can refer ethereum alarm clock for more info. They have their contract deployed in Ropston test, so you can also test it before going for live.

2

This in not per se possible, because it would cause a loop checking on the time (which would require a very high amout of gas). Every miner is running your script on his mining device and you couldn't expect them to "wait".

You could consider the time variables to build a timer with multiple function calls. But the now Variable is only an alias for block.timestamp which won't help you if you only want to make one function call.

But you could build an external listener/oracle which sets a variable (e.g. in python with web3.py). This Variable is checked by a modifier in the second part of the function.

modifier timerOver()  {
   require(timerExpired == true);
   _;

function firstFunction(uint n, parameters*) payable public {
   emit startTimer(msg.sender, funcName);
}

function secondFunction(uint n, parameters*) timer public {
   // other statements 
}

In your python script you have a filter running on the startTimer-Event which triggers the change of the variable after an amount of time.

I hope this could help

1

The blockchain is synchronous by design and doesn't allow you to perform actions between blocks in a single transaction. This means, that if you want to "wait X seconds and then do something else" you'd need to do it in 2 transactions.

Some actor will always have to spend the gas to make these transactions. If you're looking for something like a cron scheduler, trigger-based mechanism, or an if-this-do-that type deal, there are two ways you can do this, with either decentralized or centralized automation.

Decentralized Automation

You can use a decentralized network of oracles to call smart contracts. Using Chainlink Keepers can call the function you specified at any time or event trigger focused. The oracles pay the gas associated with the call, and you pay for a subscription model to the Chainlink nodes.

The way, you're contract stays decentralized all the way down to the automation level, and you never have to worry about a centralized actor intentionally not sending a transaction. Another option to do this would be Gelato.

Centralized Automation

You can also have "traditional" infrastructure call your functions on a schedule, just keep in mind this means that you are relying on a centralized actor in your contracts.

There are forms of centralized automation that you can take such as:

  • Your own server/set of scripts
  • Openzeppelin Defender
  • Tenderly
  • Incentive Mechanisms for people to call your functions etc

Disclaimer: I work on the Chainlink Project

This is similar to this StackOverflow question.

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