Implemented VRFConsumerV2 with a custom automated Python Script #1675
abdullahpichoo
started this conversation in
Show and tell
Replies: 2 comments 8 replies
-
This is sick!! Amazing implementation of VRFv2, this is going to help a lot the community, have you considered to write an article on medium out of this? |
Beta Was this translation helpful? Give feedback.
8 replies
-
I was trying to recreate it for VRFv2 too but failed as I'm newbie in blockchain and coding, great job @abdullahpichoo!!! I will study hard your work, thank you for this |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Intro
Hi everyone! Here's a little something I coded. In the tutorial, Patrick is using the VRFCoordinatorV1 contract to get a random number and the way he implemented it inside the Lottery contract just didn't seem right to me. Also, the Chainlink VRF has been updated to V2 and I didn't find a go-to code in this repo for it, so I coded it myself.
How ChainLink VRFv2 Works:
The Chainlink VRFv2 is Subscription Based and here's how you can get a Random Number:
How I Coded it
There were some hardcoded snippets on the ChainLink website for the VRFv2 and how to get random numbers using it, but it would take someone new quite some time to understand. Now here's how I made Lottery Version 2:
Making a Contract that manages and creates subscriptions
Here's how I changed the Lottery Contract
chooseWinner
that takes a random number as a parameter.How to get the Random Number?
I made a separate script that automates the retrieval of random numbers. Here's what the script does:
All of this process is automated through a single function and can also be done through a menu interface if you want to see it's working.
DeployV2 Script
The only change I made in the deploy script is that it imports the
get_random
function from thedeploy_VRFv2
script and stores the returned random number in a variable. It then passes the random number to thechooseWinner
function and concludes the Lottery.That's all. I hope this can help someone out as I really struggled with this and it would be a win for me if a fellow programmer could benefit from what I've done here.
Cheers!
MyVRFv2.sol
LotteryV2
deploy_myVRFv2.py
Beta Was this translation helpful? Give feedback.
All reactions