Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep102

[go] Time: 2025-07-11 11:27:28 Source: AIHackNode Author: web3 Click: 70 times
main net but future of ai 2025whatabout a local network and that isexactly what we're going to talk aboutnow so we've modularized our code andparameterized our code so that we'regoing to use the address based off ofthe chain that we're on but what if weuse a chain that doesn't even have aprice feed address on it what do we dothere this is where we actually createthose mock contracts the idea of mockcontracts here is if the contractdoesn't exist we deploy a minimalversion of it for our local testing forour local testing and deploying MOX istechnically a deploy script so what wedo actually is back in our deploy folderis we're going to create a new file andwe're going to call it00- deploy mo. JS we started with 0because this is almost like thepre-employ stuff we only do thissometimes we don't always deploy mocksright we don't need to deploy mock torink b or polygon or ethereum mainnetbecause those already have these pricefeeds we're actually going to deploy ourown mock price feed contracts and in ourdeploy fundme script we're going to useour own contracts instead of alreadyestablished contracts if we're on anetwork that doesn't have any price feedcontracts like harad or Local Host forexample so let's write our deploy mockscript so the setup of this is going tolook nearly identical to our deployfundme and again if you want to set itup like this you absolutely can but I'mactually just going to copy this thispart paste it in here cuz that initialpart is going to be exactly the same ohand over here I just realized that we'rewe're calling this network thing withoutit being defined JavaScript will kind ofbe smart enough to know where thisnetwork thing is coming from but it canbe a little bit confusing so it's betterto be really explicit and say constNetwork equals require hardap thisnetwork thing is coming from hardap andwe're going to grab this line and we'realso going to use this at the top of ourscript here and then our top section isgoing to look exactly the same as wellwe're going to grab these three linesand paste them in deploy deployer chainID boom it's all going to be the samehere because we're setting up to deploysome stuff now we want to deploy a newcontract but if we look at our contractsfolder this is all we have right now sowe're going to need to add this mockthis fake contract to our contractsfolder now what we can do is in ourcontracts folder we want to separatethis file from the rest of our file sothat we know okay this isn't part of ourproject but it is part of our testing sowe're going to right click create newfolder and we can either call it Mox ortest I like to call mine test and insideof this folder we can go ahead and rightclick create a new file we're going tocreate a new file and call it mockV3agregator doou and this is where we'regoing to Define our mock price feedaggregator ourselves so how can wecreate our own fake price feed contractso we can test everything locally wellone thing we could do is we go to thechain link GitHub repo and go throughthe contracts and find one of theseprice feed addresses Source V8 or maybewe'll go back to Source we'll maybewe'll check inV6 looks like we could find some stuffand and look around and we couldprobably copy paste all this code butthat really seems like kind of a hugepain in the butt to have to copy allthis code now we absolutely could wecopy paste the code in here but we'regoing to do something a little bit moreclever so the chain link repo actuallycomes with some mocks if we go tocontracts SRC v0.6test they actually have a mock V3aggregator do soulle in here that we canuse as our mock so we could copy pasteeverything but we'd have to revamp alittle bit of it because it's doing somedot dot stuff it's talking to othercontracts that are locally in this filestructure that are not going to be inour file structure so instead thoughwhat we can do is we can use this nodemodules package to our advantage we canjust say pragma validity carat 0.6.0 weuse the same version that that packageis doing and then just do import atchainlink contracts srcv0.6 SL test SL mock B3aggregator soul and then we'll add andthen of course we'll add spdx licenseidentifierMIT and boom this is actually all weneed need if we just import the codelike this remember this is exactly thesame as copy pasting this contract intoour project of course with this pathresolve to where it actually is in ournode modules now actually I can run yarnhard hat compile and it'll also compilethis contract except for of course wehave an issue hey uh compiler versionsdon't match right what's uh what's upwith that now you're going to get intosituations you will be working withcontracts that are not the same versionof solidity as you why well becausecontracts keep being deployed all thetime and there are a ton of contractsthat are on version 0.4 solidity 0.5 0.67 8 and probably 9 10 or 15 billion orhowever many solidity versions will cometo be so in our config in our hard hat.config we can scroll to the bottom wecan scroll to where we're defining oursolidity version and we can actually addmultiple solidity versions so that ourcompiler can compile multiple versionsof solidity to do that we'll saysolidity and we'll turn it into anobject here we'll make sure to put thiscomma here and inside our solidityobject we'll putcompilers and we'll have a list ofcompilers our first one we'll say isversion0.8.9 and we'll say our second one isgoing to beversion0.6.0 and then we'll go ahead and savethat and it looks like mine wanted toformat it like this which is fine now wecan go ahead rerun yarn hard headcompile and boom compiled five solidityfiles successfully this means that ourmock V agregator should also have beencompiled and if we look inartifacts at chain link we do indeed seethis chainlink contract SRC bit andv 0.6in test we see this

(Editor in charge: swift)

Related content
Wonderful recommendations
Popular Clicks
Friendly links