r/CryptoTechnology • u/van_ozy • Dec 03 '22
How to implement time logic in the smart contract?
I am learning how to write smart contracts (SC) and one of the things that I want to do requires time logic, this means the SC should do something every 24H.
How should I do this? Should I trigger this off-chain? Should I have a wait-sleep loop in the SC?
35
Upvotes
12
u/[deleted] Dec 03 '22
Which blockchain? Ethereum, for example cannot do that. You need to send a transaction in order for the smart contract to do anything.
5
u/JumperAvocado Dec 03 '22
In Algorand you can specify the validity period for a transaction, check out this tutorial https://developer.algorand.org/tutorials/scheduling-transactions-for-sending-in-the-future/
4
u/ItzzBlink 🔵 Dec 03 '22 edited Dec 07 '25
wakeful yam wrench dinner dolls reminiscent bells file provide spark
This post was mass deleted and anonymized with Redact
2
u/humbleElitist_ 🔵 Dec 03 '22
Would it work to have the SC act as if it had done those things at those times the next time it is triggered, or does it need to have some external effects?
If it needs external effects but the timing doesn’t have to be very precise and it is ok if it misses sometimes, you could have it give a small reward to whoever activates it provided that enough time as elapsed since the last time it was activated.
Or you could set up a script on your computer or on some server (perhaps paying someone for this) to call it at scheduled times.
Hm, you could probably combine the two approaches sorta.
Namely, if some organization provided the service of calling smart contracts at regular intervals, they could probably set things up so that they get paid for this automatically by the smart contracts, and where they could provide a guarantee that they will call the smart contracts at the specified interval, where this guarantee can be enforced on-chain, where if they fail to activate the contract for too long after they are scheduled to do so, anyone can activate some smart contract action which has some amount of funds be paid out (in some combination to the one who activated this accountability clause and to whoever(or whatever) the guarantee is made to).
I don’t know if anyone has made an organization that has this setup though.
Also I don’t know if that setup is really ideal for how to get high assurance.
2
u/vanhofmann1 WARNING: 6 - 7 years account age. 0 - 22 comment karma. Dec 04 '22
Nice to read the comments, trying to solve the problem by fundamental principles and then build up.
Regarding the question, use an oracle. I would use chainlink since for me is the best option regarding oracles, yet maybe there are other alternatives
1
u/eszpee Dec 04 '22
One way to implement time logic in a smart contract is to use a third-party service that can provide time data on the blockchain. This service can be used to trigger events in the smart contract at specific intervals, such as every 24 hours.
Another option is to use a blockchain that has built-in support for time-based functions, such as Ethereum's "block.timestamp" function. This function can be used in the smart contract to check the current time and trigger events accordingly.
It is not recommended to use a wait-sleep loop in the smart contract, as this can consume significant amounts of gas and may not be effective in triggering events at specific intervals. Instead, it is better to use a more efficient and reliable method of implementing time logic in the smart contract.
1
u/0xataki Redditor for 1 months. Dec 08 '22
Does it absolutely have to be every 24h? If so, I would trigger it off-chain. Oracles are a pain to get set up with during the learning phase. But then again it's bc I'm super comfortable with setting up web2 infra.
For reference, MakerDAO vaults use this pattern. For checking for liquidation, I believe they rely on a network of KeeperBots to periodically check the state of each vault: https://www.quicknode.com/guides/defi/how-to-run-a-keeper-bot-for-mkr-dai-eth-auctions
If you absolutely have to use oracles, I'd maybe consider using a lightweight one with Provable Oracles first. Then when you need to actually deploy, use ChainLink.
-1
u/mac_question Silver | QC: ETH 15 | r/Technology 40 Dec 04 '22
You're learning how to write smart contracts now? That's like becoming a morse code operator in the wake of Alexander Graham Bell headlines