Random delay inside a smart contract function

84 Views Asked by At

So, I'm trying to implement an anti-sniping feature that would make the users wait a random time between 0-12 seconds - specifically 0, 3, 6, 9 or 12 seconds because my smart contract would be on BSC. And the delay is in chunks of 3 because I noticed a new block is mined every 3s.

I was wondering what the most gas-efficient way to introduce such a delay to the smart contract is?

I think using a while loop and comparing block.timestamp with when the tx was initiated would cost a lot of gas.

Looking for optimal solutions!

1

There are 1 best solutions below

0
Muhammad Hassan On BEST ANSWER

The EVM doesn't support sleep or delay functions like in traditional programming because every operation in a transaction must be completed within the same block.

Instead of trying to delay the execution of a transaction, consider implementing a mechanism where a transaction is only valid after a certain block number or timestamp.