I need to generate random number, on a single function hit and get it there only. I am not able to do that, due to block confirmations required which is set to min. 3. Is there any way to get this or to bypass block confirmation? Someone who has done this, please answer.
3 Answers
Smart contracts needs to be and are determinstic. So to truly generate verifable random number, the calculation has to take place off-chain which is why chainlink vrf takes at least 30 seconds to return the random number.
Chainlink vrf follows request and receive mechanism.
First you will request to generate a random number.
Second you will receive the random number.
There you have two transactions and the block time of ethereum is 15 seconds approx.
So you will need to wait at least for two blocks to mine before you will get the random number.
You can read more about it here: Random Numbers: Using Chainlink VRF
So the answer for your question using the chainlink vrf is no!
But you can use the other blockchains such as polygon which has much lower blocktime to achieve the same result you want.
Tell me if it helps!
-
-
-
1
It is impossible given the way ChainlinkVRF works. What you could do is adapt your contract to keep doing what it has to on fulfillRandomWords()
but this will make the fulfillment more expensive. Another way is to trigger a function with Chainlink Keepers once your new random number is received.
Since the minimumRequestBlockConfirmations
is set as 1, you can set requestConfirmations
as 1 and that means the VRF node waits for 1 block before it sends randomness to you.
But you cannot get the randomness immediately after you send a request. There are 2 reasons:
In Chainlink VRF mechanism, smart contract
VRFCoordinator
has to write your VRF request in the event log and waits for the off-chain VRF node to detect it. Off-chain VRF needs to get the request and then take some time to send randomness back to the blockchain, these 2 steps cannot be in the same block.VRF(Verifiable random function) algorithm takes an unpredictable seed to generate a random number. In Chainlink VRF, blockhash generated in the future is a part of the seed, so the VRF node has to wait for a block to get the unpredictable hash.
So it is impossible to get randomness right away, and, as Ad-h0c mentioned, blockchain is a deterministic system so randomness cannot be calculated on-chain 100%.