When you request random number, the Chainlink VRF2 returns it by calling following function.
function fulfillRandomWords(
uint256, /* requestId */
uint256[] memory randomWords
) internal override {
s_randomWords = randomWords;
randomNum = s_randomWords[0];
}
There is not guaranteed time. What is the best way to catch when it is arrived?
What I have:
def request_random():
account = get_account()
contract = AdvancedCollectible[-1]
# Request Random
tx = contract.requestRandom({"from": account})
print("Waiting for the callback...")
tx.wait(5)
while True:
random_num = contract.randomNum()
if random_num != 0:
print("Finally received random num:", random_num)
break
else:
print("Waiting another 5 sn")
time.sleep(5)
Another way would be events
but since fulfillRandomWords
function is called by coordinator, I can't create a transaction to listen event of this function.
I am sure there is better way to do so...
fulfillRandomWords
to trigger the event from there.