audio
audioduration (s)
5.06
30.7
text
stringlengths
75
434
eath, or any native blockchain skyrocketed the chain that nodes itself to pay the gas fee. So the chain link nodes have a calculated price have a calculated variable called the gas price per link, which fluctuates based off the price of the actual chain, so that they never go bankrupt.
Basically, the price of a request changes based off the price of gas for that blockchain, you can kind of think of this as the link per gas, if you will, for now we can kind of just set it to whatever we want. And we'll just set it to one e nine, which is going to be equivalent to 1123456789. So
now that we have the base fee, and the gas price link, we'll grab this base fee, we'll have these be the arguments for our VR chord, enter V to mock, so we'll say and actually we can delete that will say const, args equals base fee, and gas price
link, then we can take this args variable and just plop it in here. Now we can do log mocks deployed, then we can do log that a little line like this to let people know that this deploy script is done, then we'll just do a module that exports dot tags equals all and mocks. So now that we have a view of
coordinator v2 Mock deployed, we'll come back over to our raffle and make some code around it. Similar to what we just did with our deploy mocks. We can say if development chains that includes network dot name, we'll do some stuff. And we need to
import development chains from our helper Hardhead config. And we need to import network from hard hat. My VS code automatically added them Wow, thanks VS code. Let's even just do const args and make this variable down here. Stick it in args. Our first argument is going to need to be this Vera fi
to coordinator so let's make a variable we'll say let VRF core need torby to address and if we're on a development chain, we're going to grab that mock contract. So we'll say const PRF, Cor de ATAR V to mock equals await ethers dot get
contract, the RF cord the cord to NATO Tor v two MK, and then we can set the RF coordinator v two address equals VRF core denater V two mach dot
address. Cool. We have that address here. Else if we're not on a local network, the VRF v2 coordinator address is simply going to be derived from our network config. So let's import the network config as well from our helper Hardhead config and
we'll say else VRF coordinator v2 address equals network config of our chain ID. Let's which actually sorry, we do need the chain ID cons chain ID equals network dot config dot chain ID, chain ID of VR coordinator V to chain ID. You're a coordinator
v2. Alright, perfect. We've got the setup to work with our VRF coordinator v2 address. What else do we need from our raffle? Well, we need an entrance fee, we probably want to change the entrance fee depending on what chain we're on, we're on a more expensive chain, we might want to make this higher than others. So let's go ahead back to our helper Hardhead config and make
an entrance fee based off of the blockchain, so for NP, maybe we want to make it 0.01 eath. So we could say ethers dot utils, parse ether of 0.01. And once again, thank you VS code for
automatically dropping that in for me. And we're also going to want an entrance fee for our heart app, we can also set a default in here, but let's just be a little more explicit. So we'll say 31337. The name of this is Art app, we don't need to give it a veneer of coordinator v2 address because
we're going to deploy a mock. But we do want an entrance fee. And let's just give it the exact same entrance fee here. So we'll say ethers dot utils dot parse ether 0.01 E. Alright, great. So on our deploy here, we can just say const entrance fee equals
network config. Chain ID of entrance fee. Let's start populating our arcs here. So the first one is going to be your Fe two coordinator address. Next one is going to be our entrance fee. Got it? Got it. Now we need our gas lane. On Rigby and other
networks, there are different gas lanes that we can choose from, let's grab the only gas Lane from Rinckey, the 30, gray key hash, let's drop this, of course into our network config as gasoline, pop it in here, we're hard hat, our mock actually doesn't care what gas line we're working on, because
we're going to be mocking the gasoline anyways. So we can just say gas lane, we can just go ahead and use the same one or really anything here, it doesn't really matter. Now here we'll say const, gasoline equals network config. Chain ID, gasoline, and we'll grab the gas lane. And we'll
stick it into our argument array, we've got this one, we've got this one, we've got this one. Now it's time for the subscription ID. Now if you haven't run through docstoc chain link for the chain link VRF, I highly recommend you do so so that you can understand what this subscription ID is we know that we can actually make a subscription ID using that front
end using that website VRF dot chain dot link, which is great and all but what if we're on all local chains, we can get a subscription ID no problem in here. But it's a little bit harder on a local network. Now I'm actually going to teach you how to create and fund subscription it is completely
programmatically. So you don't even need to use the UI if you don't want to. However, for the purpose of this course, we're still going to use the user interface, we're still going to use that website for us to get our own subscription IDs. But you could 100% automate the process of creating a subscription ID and funding a subscription ID. Because when
you create and fund subscription IDs, you're just calling create subscription and fund subscription on that smart contract. So on our development chain, we have our V RF coordinator v2 Mock, and what we're going to do and on our development chain, we're going to create that subscription. So we're gonna say const, transaction, response equals
await VRF coordinator v2 Mock dot create sub scription. And then we'll run const transaction receipt equals weight trends action response, dot Wait, wait one block confirmation. And
inside this transaction receipt, there's actually an event that's emitted with our subscription that we can get this is another place where emitting events is incredibly helpful. So in fact, if we open back up a Vera v2 coordinator mark, and we look for create subscription, we see we emit subscription created
with the subscription ID, we can actually get this event emitted from our transaction receipt. Now to assign it, let's go ahead and create a sub subscription ID up here. And then we'll say subscription ID equals transaction receipt that events
of zero dot args dot sub ID. And again, be sure to watch that events video if you want to learn more about how to work with events in hardware. Now that we have a subscription, we need to fund the subscription on a real network, you'd need the link token to actually fund the subscription. The current
iteration of the mock allows you to fund a subscription without the link token. So what we can do is we can just run a wait your F corps inator Mark, if coordinator B to Mach dot fund subscription, and we'll give it the subscription ID and we'll
need to do some fundamental this, we can just create some variable, we'll say const VRF, subscription fund amount equals ethers dot utils, dot parse ether, of, we'll say 30 VRF subscription, fundament. We'll just paste that down here. We
could do this as well for a real test net or live networks. But just so that we become familiar with the user interface, we're not going to do a test net programmatically. And for a test net, we're just going to use exactly what we've been doing so far. Or we can put a subscription ID and our helper config. So we'll say subscription ID, we'll put
something in here right now, we'll just leave it as zero. But later on, when we actually create a subscription, we'll update our subscription ID. And so we'll say subscription ID equals network config. Change ID of subscription ID. Perfect. Now
we can add this to our arguments array. What else do we need subscription ID, we need a callback gas limit or callback, gas limit is going to vary network to network. So once again, we're gonna go into our helper configure callback gas limit. And for us, we'll set a pretty high limit of 500,000
gas. So we'll say call a gas limit of 5.123 500,000. Gas. And for hard hat, we'll do the same thing. So we can say const, callback, gas limit equals network config, chain ID,
callback, gas limit, grab this, put it into our argument array. What else do we need? All we need now is the interval. So we can change this network to network as well. Rigby will say, interval will ever just be 30 seconds for both hard hat and
for Rigby. So we'll do the same thing here. Say const. Interval equals network config. Chain ID, interval, and we'll grab this, pop it in the end of our array. All right, awesome.
Now we have an argument array and drop it right in here and perfect. Everything in our constructor for our raffle contract. Great. This is looking fantastic. We've got wait confirmations we got logging arguments Deployer. Okay, well, what next? Well, let's go ahead and add that verification piece.
So once again, create a new folder utils, new file verify.js. We can either copy paste this from our last project, or we can grab this from the GitHub repo associated with this course, once we have our verify script in here, we're going to import it by saying const. Verify equals require dot
dot slash helper Hardhead config. And then we can add that same bit of code down here to verify our contract, we'll say if we're not on a development chain. And we have process
studying v dot ether scan API key, then we're going to log verifying dot dot and then await verify raffle dot address and the arcs. Now we'll just do a log of a whole bunch of hyphens say that this script is done module dot exports dot tags
equals say this will be all and raffle. All right, so let's test this out. We'll do hardhat deploy, or yarn, hard hat deploy and see if this script works correctly. Looks like we ran into an error network config not defined. So let's spell this
right. Network config. There we go. And that looks much better. Let's try this again. It looks like our deploy scripts are working well. local networks protected, deploying mocks deployed VRF coordinator v2 Mock mocks deployed and then we went ahead and deployed our raffle. Awesome, this is massive. Now
we're not going to test the deploying this to a test net quite yet, because well, we don't have any unit tests yet. We need to write unit tests before we want to ever test running this on a test net. So we have our deploy script, we have our contracts, that means it's time for us to write some tests. We'll come over here we'll create a new folder called
test. And for now, we'll just make our unit tests. So unit tests, and in here we create a new file called raffle.test.js. And let's write some unit tests. Now for these unit tests, we are going to be a little bit verbose here, we're going to make our coverage really, really good here. It's not gonna be perfect,
but this is going to be pretty verbose. So I'm gonna go pretty quickly here. So you can feel free to pause, slow me down, speed me up whatever you need to learn this section. It is really good muscle memory to go through writing these tests and understand what you should be thinking about when you're
writing these tests. So feel free to speed up the parts You already know and slow down the new parts. Because we are going to go over some new information here. writing tests may seem like a tedious process. But I promise as you get better at writing these tests, you'll realize that these are the
things that you can rely on when stuff doesn't work. And when you're not sure how to code, something, getting this muscle memory down, writing these tests is going to make you a fantastic engineer. So let's go through and we'll write some of these verbose tests here to try to make this really good and have this have really good coverage. And if you want to go back later
on, and see if you can give it even more coverage, and even better tests, please feel free to do so. But let's get started. Let's write some tests. So we're going to start out pretty much the same way we've been starting everything out, we're going to grab our development chains, so that we only run our unit tests on a development chain. So we'll do const development chains,
equals require dot dot slash dot dot slash helper, RD, hard hat config, and then we'll say not development chains, that includes network dot name, and describe that skip. Otherwise,
we'll do describe, so this first describe is going to be our raffle unit tests. And this is going to be an async. function. So raffle unit tests so that it comes on the next line, it looks a little bit better. Alright, great. Now, what are some of the main things that we're going to need to deploy? Well, we're
gonna need to deploy a raffle, we're probably going to want a VRF core to a tour be to mock graded before each, that's going to be an async function, where we go ahead and we get these will say const. Employer equals await, get named accounts.
So we're gonna need to import get named accounts or require get name accounts from hard hat, then we're going to want to deploy these using our fixtures. So we can say await deployments. And then we're going to import deployments as well from hardhat dot fixture, and we're going to call all, we're going to deploy
everything. And again, if we look at our one, our raffle has the alt tag, and our Oh, our mocks also have the alt tag, okay, perfect. Once we deploy everything, we can say raffle equals await ethers dot get to contract. And we got to import
ethers from hard hat, like so. And we'll say it will get the raffle contract and we'll connect it to our Deployer. And then we're going to do the same thing with VRF coordinator v2 Mock equals await ethers dot get contract, the RF coordinator, B
to MK connect this to deployer as well. Alright, great, our first set of tests describe, they're going to be the constructor. And this is going to be an async function. And let's do this. Let's create an IT initial initializes the
raffle correctly, this is going to be an async function. Now I just want to make a note, because ideally, we make our tests have just one assert, or it just keep that in mind is
that ideally, we want to have just one assert for it. But we're going to have a bunch because like I said, we're being a little bit loose here. So we want to just make sure that our raffle is initialized correctly. So we'll say const raffle state, and we'll get that raffle state because we want to make sure
that we start in an open raffle state. So we'll say const, raffle state equals await raffle dot get raffle state. And then we want to say assert dot equal ope and then we need to import
assert from Chai. So assert equals require Chai assert dot equal raffle state.to string because again, raffle state is going to be a big number. And even though a raffle state is of type raffle state, it'll return a zero if it's open and a one if
it's calculating. So this gets transformed just into a un 256. When we call it like this, a raffle state variable here will be a big number. So we want to just to string a file. So assert dot equal raffle state DOT two string zero. We'll also make
sure our interval gets set correctly. So we'll do const interval equals await raffle dot get interval. And I don't know if we have one of those it's actually C inter ball see if we
have one of those. We don't have a good interval let's go ahead and add again interval function so we'll do function get in trouble. We have public view returns you went to six return I
underscore interval will have good interval will say raffle dot get interval and will also say assert dot equal interval.to string. It should equal one Ever is in our helper config, right?
Because we're using the interval and helper config. So we say interval.to string should equal. So we'll import that as well. Network config. And we'll say the interval should equal network config. Of let's also make, get our chain ID up here,
chain ID, say const. Chain ID equals network dot config, a chain ID, network config of the chain ID of interval. Alright, cool. So let's test this so far, HH test or yarn, hard hat test.
And cool looks like it passed. And we have our little gas output here. Awesome. Let's go to our Hardhead config, just so that it doesn't always print out at gas, but there for now. So I'm going to copy paste the gas reporter section from our last project, like so. And we're going to have enabled gas be
false for now. So now if we run a Hardhead test, again, we shouldn't have that gas bid printed out, we should just see the tests and perfect, that's what we see. And our constructor test passes. Yay, what's next, got our constructor. And we probably could have written more tests for the rest of these. But let's just move on. Alright, enter raffle, that's going to be
our next describe block. So it will do describe, enter, raffle. And this is going to be an async function. And we'll say it reverts when you don't pay enough, right, because
one of the first things that we check is that they're paying enough. So we want to make sure that this actually reverts if they don't pay enough. So this will be an async function. Where we're going to do that same expect await thing. So we're going to import expect from Chai, which comes from those
waffle matchers. And we're gonna say await, expect raffle dot enter a raffle. And we're not going to pass any value here, we're going to expect it to dot b dot reverted with. And if we
look here, we want it to be reverted with this raffle not enough eath entered. So we can put that in quotes raffle not enough eath entered. Now we can try this out. Make sure that it actually works. Ah ah test dash dash grep. Put this in quotes
that you don't pay enough. And awesome. We're passing there. What else do we want to test? Well, we want to test that if the raffle isn't open, we also revert but we'll test that in a little bit. As we kind of test the rest of the functionality. We want to see that it records players when they enter. So this
will be an async function. And now we'll enter the raffle. First we're going to need that raffle entrance fee. Let's go ahead and save that at the top. So we'll say let raffle VRF coordinator be to mock raffle entrance fee and then our before each will say raffle entrance fee equals a weight ethers dot
get entrance fee. This should be raffle dot get entrance fee. So now we have this raffle entrance fee, you can use it to enter the raffle we'll say a weight raffle dot enter raffle with a value of
raffle entrance fee. And we can make sure that our Deployer here has been correctly recorded. So since right now we're connected to the Deployer. We'll just make sure that that deployer actually is in our contract. So we'll say const player from contract
equals await raffle dot get player of zero, because we record them in our players array and we have our get players function which pulls them out. And then we'll say assert dot equal player, player from contract should be the Deployer.
So now we can grep for this in our hard head test. Make sure this works. HH test dash dash grep. Deployer is not defined. We've got it up here. But we actually didn't save it globally. So we got to do let common Deployer. And we'll say
deployer equals await get named accounts. And we'll wrap it like this to get the deployer Awesome. Let's try this again. And awesome. What else should this do? Well, it's also emitting an event. So let's make sure it emits an event. So we'll
say it emits event on enter. This will be an async function as well. And this will be the first time that we're testing to make sure a function emits an event and the syntax is going to look really similar to what we test for when we check to see if
an error is fired. So we're gonna say await expect raffle dot enter raffle with value of raffle entrance fee dot two dot
omit and this.to dot emit we get from During woful, these chime matches for emitting events we can do a wait expect to emit, and then the event that we're expecting to emit. So we're saying to omit the raffle contract to emit a raffle enter
event. We can copy this, try to test this in our terminal. So we'll say hard hat. Test dash dash grep. Just put this in quotes. And that passes as well. Great. Let's now go ahead and
test to make sure that we can't enter the raffle whenever this raffle is not open or it's calculating. So we'll say it doesn't allow entrance, when raffle is Cal cumulating. And this will be an async function. And first we'll enter will say
await raffle dot enter raffle value is raffle entrance fee. Now what we want to do we want to get this raffle into a closed state. So we want to get it out
of its open state. Well, how do we move this raffle from raffle dot open to raffle dot closed in perform upkeep, remove the raffle from raffle that open to raffle dot calculating but perform upkeep can only be called if check upkeep returns
true. Otherwise, it'll revert with raffle upkeep not needed. So what we need to do is we need to make check upkeep return true. And we will pretend to be the channeling keeper network to keep calling checkup keep waiting for it to be true. And once we make it true, then we'll pretend to be the channeling