Current location: Home > article > Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial - Ep70 Text
Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial - Ep70
Time: 2025-07-11 11:52:53 Source: Codora.ai Author: swift Reading: 362 times
going tochoose the contract we want to return tobut we also have top ai certificationsto pass the originalrequest ID associated with the originalcall now in our Lottery contract our endLottery function isn't going to returnanything and even if it did it would bereally difficult for us to get thatreturn type in our python so what wewant to do to keep track of when thiscontract actually entered thecalculating winner state is we want todo what's called emitting an eventevents are pieces of data executed inthe blockchain and stored in theblockchain but are not accessible by anysmart contracts you can kind of think ofthem as the print lines of a blockchainor the print statements of ablockchain we can go to this log sectionwhich also includes all the differentevents now there's a lot of informationhere so we're actually going to do anevent ourself just so that we can seewhat this really looks like you can seehere that when we call this end Lotteryfunction in the logs if we scroll to thebottom there's an event here calledRandomness request this was spit out bythe vrf coordinator this was spit out bythe vrf consumer base that we inheritedand it even has some data that's alreadybeen decoded one of those pieces of datais the request ID now to add an event wefirst need to create our event type soat the top we'll go ahead and do eventrequested randoms say bytes32 request ID so now we've identified anew type of event called requestedRandomness it's really similar to theenum in this regard to emit one of theseevents all we have to do in our endLottery bid is we'll do emit requestedRandomness and then request ID becauserequested Randomness takes a byes 32as an input parameter and we're going topass it that request ID as an inputparameter now that we have this eventbeing emitted back in our test when wecall end Lottery it will actually emitone of these events to ourtransaction so what we can say then istransaction equals lottery. endLottery same way we've always doneit but now we can look inside thistransaction object inside of thistransaction object is actually anattribute calledevents which stores all of our events wecan then look for certain event namewhich we know is requested Randomness sowe'll say out of all the events look forthe requested Randomnessevent and in there in that requestedRandomnessevent find the requestID request IDnow we can say requestIDC me requestID is going to get grabbed from thisevent that we emit so these events aregoing to be really helpful for writingtests these events are also reallyhelpful for a number of other reasonsone of the big ones is is upgrading oursmart contracts or understanding when amapping is updated but for now we'regoing to be using them for testing nowthat we have this request ID what we cando is pretend to be the chain link nodeand use this call back with Randomnessfunction to dummy getting a randomnumber back from the chain link node sowhat we're going to do is we're going tocall our get Contractfunction and get that vrf coordinator ofcourse we're going to have to import itfrom our helpful scripts and we're goingto call that call back with Randomnessfunction so we're going to do dot callbackwithRandomness and we need to passit this request ID a random number andthen the contract to return to so we'lldo request ID we'll do some randomnumber like we'll saystatic RNG equals777 so we'll say the random number thatwe're going to return is going to be777 and then we'll do lottery. addresswe're going to return it to thelottery once again we're going to sayfrom count because this is making astate change and now that we've got acall back what we can do is do ourasserts now so this is us dummyinggetting a response from a chain Lakenode and this is how we mock responsesin our tests to make our lives way wayeasier so now we can do we can figureout who the winner of this Lotteryactually is these are our three enterentriesthat means it's777 mod 3 since our random number isgoing to be 777 if we call out acalculator777 / 3 it divides evenly so we knowthat this means the answer to this isgoing to be zero AKA our account isgoing to be the winner so let's assertthe lottery do recent winner is going tobe equal to our accountright because we set a recentwinner in here we transfer them somemoney we'll assert lottery.balance is now zero because we'retransferring this account all of themoney and let's even make sure that theaccount gets more money right so we'lldo starting balance of our accountstarting balance of accountequals count. balance count. balance andwe'll dobalanceof Lottery equals lottery. balance andthen we'll assert account. balance isnow going to be these two added togetherstarted balance of accountplus the balance of thelottery because we should get all of thelottery winnings hereokay so this is a long test but this isprobably the most important test let'smake sure this is doing what we want itto do do brownie test- K test can pickwinner correctly let's see if this worksoops forgot to puta a parentheses here parentheses herelet's try this again and perfect we areactually updating paying out and runningour Lottery correctly and fairwith true Randomness this is incrediblyexciting and we've admitted events inour Lottery contract to use in theoffchain event logging of ethereum Iknow we're getting ansy to actually runthis on a real chain so let's go aheadand do our last bit here create ourintegration test which we will run on anactual chain and then if we wanted to wego ahead and try our deploy Lotteryfunction on a real chain as well allright so let's do our integration testhere this is where we're going toactually test on a real live chain we'regoing to use ring be because that's thenetwork that we've been using this wholetime which is great now I'm being alittle bit liberal in the way that we'redoing our tests here and we're justgoing to do one test for thisintegration test but keep in mind you'regoing to want to test every piece
(Editor in charge: swift)
Latest content
- ·Web3 Developer in 2024 Roadmap_ Solidity, Smart Contract, and Blockchain Development [Full Course] - Ep57
- ·Solana Developer Bootcamp 2024 - Learn Blockchain and Full Stack Web3 Development - Projects 1-9 - Ep38
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep253
- ·Solana Developer Bootcamp 2024 - Learn Blockchain and Full Stack Web3 Development - Projects 1-9 - Ep41
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep282
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep166
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep149
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep196
- ·GenAI Essentials – Full Course for Beginners - Ep18
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep127
Hot content
- ·Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial - Ep123
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep35
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep133
- ·Web3 & Crypto Will Change The World As We Know It! Yat Siu - Ep3
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep11
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep120
- ·Getting started with Web3 in 2025 _ Deep Dive Explainer - Ep1
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep149
- ·GenAI Essentials – Full Course for Beginners - Ep67
- ·Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep46