Testing
Web3Bridge·Wed, Oct 9, 2024, 12:00 AM
Transcript
all right um so basically I'll be taking you guys through init testing first testing integration testing and for testing so now I would like to prob your proud knowledge of testing I want to believe we've all written smart contract in solidity is that correct or this is just our first time writing um smart contract in general if you want to say something please onute your mic and like you know talk my first time solidity SM contracts before okay so um every other person have written Sol smart contract right cases for it is that correct yes yes awesome so um can someone give me like the differences between units testing and force testing integration testing and F testing just like just like prob prior knowledge of smart contract testing unit testing integration testing false testing and for testing anyone can um give me the differences between this for test um mechanisms okay um Sabo can you go first do you have any idea how how unit testing work what unit testing is um unit testing is like testing your code in bits to see if they work like if they work they supposed to work if they behave the way it's meant to behave what say that again I said unit testing is testing your code in bits to know if the if it really behaves the way it's supposed to okay so Daniel do you agree with um Sabo uh yes um but to add to that um yes in unit testing you test um a particular Cod that's why it's called Unit so you're testing unit of codes but then I agree to him that's how it is okay cool so what differentiates that I'm still on you Daniel so what differentiates that um from integration testing um honestly I'm not run integration testing before so okay okay so um Victoria do you okay okay Victoria please go ahead do you want do you care to like share the differences between integration testing and unit testing okay so I think you mentioned four tests I did hear those ones uh it couldn't get like what we called but uh my own understanding of unit testing and integration test sorry now based on my knowledge of content so unit testing is basically test code like let's say a function right to test how just one single function works then integration testing is when you want to test the whole flow let's say you're writing it to do smart contract you want to test how the add function works with the remove function with the you know um the whole integration of the two to make sure that it is working why you test that's integration testing but unit testing is testing just one particular unit of your code just a function or yeah okay thank you so um emuel uelo do you agree with Victoria yes I agree with her okay awesome so um can you shed more light on how these two different kind of testing differ from false testing um I think I'm hearing for of false for the first time here and FSE f u z z Oh F yes I have no idea about that well I've seen you before but I have no idea about that I think it has to do with like um interaction with like the contract when it has been deployed something of that nature I don't know okay I don't exactly agree with you but um so anyone with the contrary opinion as regards for testing so um is like stress testing your um Cod to see where it feels so emmanu did you catch that no sir wasn't AUD enough again okay um I was saying first testing is was like is like stress testing so um something where you run repeated codes with different values with um on your codes to see where it feels so it's like you're testing an app with 1 2 3 4 5 6 to maybe it feels at 00 million so like that's where we call that's what we call like false testing that's why I understand by false T do you catch that yeah I do oh cool so and um that then that leads us to festing anyone with the idea of how testing works we say want to like the main what do we mean anyone okay so it's we've done something like that at the course that just ended so it's just basically means um when something is on the me net so you want to do like um a rerun of how it will look on the main net right just like know bring it locally then test it as see it's on the main net before you actually put it on the main net just to see how it work if it's actually on the main net so you have like this um PSE like test environment that you can use as see on the main so just basically poing something and bringing it locally to test how it will really work when it's going goes live that's not strangers awesome so did everyone catch that or I have trate so in the course of the class if we don't understand something please do well to call my attention to it um and I will be asking a whole lot of questions right because I'm working with premise that we all have an idea of how this whole thing works and the only thing we just doing is testing um testing our contract and building on our existing knowledge so with the whole forms of testing we talked about just now we'll be touching them one after the other so be starting need testing so um let I want to believe we all have St found install our local machines right yes yes sir okay so um it's I kind of prefer um sound feedback compared to the thumbs up so like you know I can't keep like switching screens like check if you're sending thumbs up you understand so um can you all see my screen is it um clear enough yes it is it's tin one more zoom in how about that is that okay cool all right so make that's so yeah I said I believe we all have stocket found install on our local machine right yes yes so I believe we all know how to initialize initialize a new found um f the project um so morning so we have our project initialized so yeah so let's take this off for now is this okay awesome so St unit testing basically we were trying to like you know check how um each section of a particular like the operator as a whole now you like test each function to see how they work to see if they are working properly as expected so let's say uh we want to test let's let's [Music] noral so let's say we have a simple function that basically like uh say adds two um two numbers together so let's say [Music] function add so this returns so the returns uh the summation of a plus b right so now we want to test if this function works right we can basically write our test cases inside this SLE file since it's just basically need test right but to get started you need to specify the test key sorry sir just can you zoom in more like I can barely see but now is that okay more yes I yes yes thank you so uh we want to like you know you know we writing our test inside the same file as our function right so we have to like specify this else the compil to like pick it up so then we have to like put it in the mode say test so now we want to test if this function works as expected right um and I want to believe that you are familiar with the super keyword so can someone tell me what this does the super keyword it's used to assess something outside of a model basically both thank you very much for that yeah um so now we we now have access to the add function inside our our test block over here so we can go ahead to um to test our function so basically you would wonder like if we do ENT Forge test how would the compiler pick up our test cases so we can do that by specifying a test keyword on each of those function if you don't specify this key this test keyword on each of those functions um you able to like it won't read this as the test function so we specify the keyword on line 10 over here and then let's say um function sorry function test can have this please if you get confused at any point in time call by attention to it right so and then U to check the validity of our sum we need to like make use of the Asser keyword um and please I'll be calling people at random to like you know ask questions just to be sure you guys are following So Daniel uh are you familiar with the ass keyword and what does it do okay uh yeah I understand that um okay just like in solidity um instead of asset now we're going to use um maybe uh instead of yeah like in have been require or something like that but here is to assert that I don't know how to say it but it's the is the substitution for the um how do I say say it now okay if it was solidity it wouldn't be asset we use even though we have the asset um statement in solidity but here I think it's only asset we use to test that is to to to check you know between two values or something like that okay thank you um Sabo want to give that is sorry is it Sabo or aoku which one do prefer are you on call yes I'm Sabo is fine yeah Sabo is fine all right go on so do you give function okay um we use ass to like when we want it to proceed when the condition is true like it should the condition has to be true for it to pass that's when we use asset good that's that's a perfect answer thank you for that so so now we want to like um okay so we want to uh make sure that our function works perfectly fine because our expectation is that when we have two and three together it should give us five and if this is wrong that means our function is incorrect it's faulty is buggy incorrect so of course we all know that if we are two and three together you should give us five anything contrary is going to fail so [Music] um still big so uh let's if you run our test you you take your you run your test withn for test that's a key can run your test and the comper is able to pick up this test case because of this test um keyword that will specify on the function and you have to make sure that if you're naming your test you have to like start with test test let's say you want like test subtraction now that's like test subtraction test if it works test like test as like you know come before the you can see our tests I okay okay yeah yeah that's right yeah so technically it's running one you know our our test is not in here right so you picked up zero test from there uh and then one test from here which is only one test from source so yeah that means our um our function works so let's say we have um let's we have six right of course this is definitely going to fail let's run this again so you can see it's definitely going to like you know fail so this is one of the ways you like um do a unit testing so um I'm sure if You' been working well with kyos M contract you must have seen let say a function that panics function that fails and stuff like that and a function that you expect to F how can you handle functions like that so let's say we have a function that is expected to F okay for example now this function we expect it to F but then we don't want it to like you know return to us like this do you get so if you're expecting a function a function to fil let's have this let's this to so let's we have a function we expect it to fail so in so now in stag Foundry we have a methods exposed to us to like you know assert if a to ass a um unexpected failure right and so let's say this is two of course 2 + 10 is definitely not equals to five so we expect this test to fail but then we we run these test cases like this run these two tests like this don't forget we're expecting this test to fill right so as you can see one test passed one test failed right but this is not a good um developer experience so we need both steps to pass right so we can specify an expected failure keyword over here so this should [Music] pic and then uh what we expecting that a method this this basically is a top right uh then expecting that two should be equals to Sor this is definitely going to be going to so this is Pur specified so if you're R test of course this is meant to fail this is meant to fail right but then the fact that we already expecting that it's going to fail so it's not going run into a failure a failure respon like give a failure response to us right so since this pass that means our logic is also so correct so don't forget let me commment this out again and run the test if you run this it's going to return a fail response to you but the fact that you're already expecting that it is going to fail and then you had a the Panic keyword in your test file then it's the comer is going to oh you know that it's going to fail and you can like um so it's expecting okay it's going to fail and then it's expecting this error message as well so once this error message M matches what was returned what is being returned over here then it's going to like you know pass right so this is also one of the one of the ways like you know do a unit um testing uh also so let me quickly Touch first testing using the same function so let's say we have this same uh same test file here so test out P so the beautiful thing with st found is the fact that you don't have to um write your you know when when you're dealing with like the traditional foring right you passing random um random figures to make sure that your test breaks but then St Foundry has made it a lot easier such that you don't have to like pass these things manually it like imputs the values at randoms on your your behalf and one of the ways to like do a first test with st Foundry is just like you know specify that okay put in arguments here put in argument here so let's say uh we have say we have say x 252 then say y also F 252 then it's going to sending X and Y and we expecting that the summation should be equal to X+ y so in this in this kind of scenario now right the compiler is going to passing random figures in here to pass in random figures and going to like stress the program to uh find at what point will it break so if I'm to run this test now um I'm to run this test now [Music] say for that so as you can see look at number of runs here you run over the program two 56 times with you know arbitrary figures with random figures like make sure that okay the program works perfectly fine or the program breaks sorry e sorry about that so yeah so um so the program is going to like you know passing random figures in here to make sure like okay the test passes so that's basic um Force testing so this is the basic things you need to know about Force testing and if you need to know more and stuff like that the perfect uh resource is The stet Foundry um Foundry documentation here think I'm going to send this so you like no more and stuff like that you can head over to stations like you know uh see more um more stuff that that are open more meths that are open for use so um the next thing we be touching is integration texting so basically it allows us to say know the flow of things oh please is anyone confused up to this point please if you're confused um singy let me r or clarify any confusion is anyone confused please confirm if you can still hear me yes we can hear you yes sir all right so is anyone confused up to this point so yeah I have a question go on please so I'm aware the we could use we using scab before to run our programs and there was a scab test function that runs test so why migrating to SN Forge now for testing please so just yeah yeah so basically stack founder actually like makes a whole lot of things easier right a whole lot of things easier when it comes to writing your smart contract and even posting your smart contracts no where in the course of the class we'll be talking about certain treat schools that are open to you open for use with stet Foundry so basically St scub under the Ood right so it's just that the way we're like approaching it is what kind of differs but then it's still works the same way as SC does is that cool cool all right nice so um let's go ahead to writing uh um so basically once to like do an integration test so then with an integ integration test of course we need to like have a smart contract right so let's quickly create a basic ownable smart contract we are going to be writing this smart contract together because I'll be asking quite a number of questions to like you know check how far we've gone with writing smart contract we can so let's initialize another another [Music] project um and Forge in it [Music] moment so let's create an enable contract enable see enable let's open this up as well sorry is there a class Governor yes there is okay uh take do you want to send something to us yeah yeah is online K is online now K is the class govern okay so if I need to send anything I'll just send it to chat box um so now we' initialized cont what's that or you can just our telegram we have like a telegram for this class okay okay no problem you not been added yeah yeah yeah yeah no problem so uh we want to like initialize our um ownable smart contract so the first thing to do this so the first thing we are going to do is to uh outline what we intend to achieve right so uh basically we want to have a smart contract that allows only owner to set the data also would need a way a way [Music] to obtain the data set um also we need a function to return the owner it's on the own of the contract of the contract um what else again you need the function to transfer ownership to transfer ownership um what else what else what else okay let's let's keep it pretty simple so we can quickly you know um cover up so um where do we go from here can somebody help me out where do we go from here yeah we need to define the interface thank you very much Define the interface um so a Cara what does the interface do is a car on the call yeah I am go on please what does the interface do are you online or you on site I'm on site okay good so what does interface do actually what the interface does is it serves as it's where we Define all the functions we are going to be interacting with or we're going to be implementing or that will be using in the contract so it it allows other contracts or it allows other contract to be able to use or to be able to call our contract to to interact with our contract so you can Ser as a middle man and also it help guide us to WR with what we intend um to to lay out in the in the contract so Ser as a building block like as a framewor or skeleton something like that so does anybody disagree with um yeah um I don't disagree with him please your screen size is a bit tiny see about now better this is better all right good um okay so let's define our functions right so we need the function to allows only the owner to set data correct so let's say we have function set data um new passing the value that want to save value which has to be I want to believe that none of us is alien to chos syx correct is that correct all right so then we have we've defined our set data then we need also we need a way to obtain the data set so another function say get data so uh [Music] um so it's going to be done sorry I don't think we need the yeah that's right thank you for that thank you who was that what's your name U all right thanks U um then we need the function to return the owner owner of the contract so say function owner so this [Music] also oh this is definitely contract address it will not beIN yet so can anyone help me out where do we obtain our contract address yeah we are obtaining from the stes thank you very much that's emanu is that is that Emanuel again yes sir all right yes oh there an ER also right awesome so the last okay function to transfer ownership so if you're transfering ownership of course you are going to require a new address new owner so let's do that function transfer ownership [Music] so constra this this good so is anyone confused up to this point is anyone confused up to this point um signify if anyone is confused up to this point are we all clear up to this points I need feedback yeah I just have a question go on so um I don't the rep self I don't understand what it means so if you can basically refering we referencing the contract State you get that part we referencing the contract that's why we specifying this okay okay yeah so other question from anybody um who else is on the call Mary Mary Gaton so where do we go from here we're about to start tracking our contract Where Do We Go From Here Mary gon can you help us out real quick yeah after that we'll use I'm I'm on igpc so I think we have to implement the traits now variables State variables we about to start our contract like what do we do first we use the main function anybody wants to help M house all right so we need to contract all right so sorry who was that who was that who spoke just now okay so we need to declare our contract then we give name give um so let's see so we Define a block [Music] okay yeah that's true so yeah uh so according to Mary then we initialize our storage earlier which is specified storage keyword put our if I'm going too fast please bring me back so yeah so what do we need uh we need a um a state variable to store our owner and we also need a state variable to store our data so uh so let's say what's the nature of owner contract address as contract address sorry uh then also our data each of our data is a two so our storage is set um where do we go from there let's define our Constructor sorry oh specify the sticks of so so what are we okay we passing in the initial owner let's initialize that from scratch so have as as owner initial should be contract address um does anyone see oh oh yeah we meant to this so we can basically since we have it up here we can use make of the super keyword so use so contract address so we don't have like our file so uh let's initialize our owner um right pass on that looks about right okay so um let's also set our initial data right so cell do right we can just it it all right so quick question for the class okay let's assume that we have um let's say here we have here 252 right and we FS to initialize this taxu in here what is going to be the value at deployment of our data anybody anyone all right so uh David do you agree with Emmanuel David D David D is that correct yeah do you agree with since he's not on the call so K KY do you agree with emuel that the initial value is going to be zero or you have a contrary view no I don't have a contrary opinion I agree all right all right let's go ahead so um where do we go from here we have our function we have our our interface we have our storage we have our Constructor so we should go ahead to uh implementation is very important so say enable what's the name oh all right the cont oh [Music] no I think we need to use super yeah thank you very much for that so we need to we need to import this guy over here um WR side here [Music] saying shouldn't be empty all right let's go I think is there anyone confused up to this point no but do we have to always add the I to the interface to the name of the interface yeah it's more like the D process you can actually the it's more like okay how everyone that kind so um let's so what's our first function in our our interface block so set data let's Implement our set data set uh data so um ref Ser that's we contract contract p then uh what kind of are We inut 252 new value 2522 um then what are the features of asset data only the owner should be able to call this function right so it only makes more sense to First Implement our only owner modification first let's have a private function so we don't have tocl that we can use the generate generate said about creating a so can does anyone know the implication of this gener Strait anyone just implenting an intern function or a private function all right thank you very much for that let this um say function only owner so so uh what would this do it checks that the person calling the function right is the owner uh and then there's a method from St use um use to get the color to get the color of the particular function so use Stack n to use the get get address method address method so so let's say let color equals get col address So the plan is to check that the person call this function is the owner so we can also assert that color is equal to so that's self read can another err message says col is not we oh no this looks good any confusion Sabo any confusion any confusion no confusion on my side how about no confusion okay um so now uh we can now proceed like make a check that only the owner should be able to call this set data function so since this function is a private function you can equally s dot only or so now this function checks that the person calling the set data function is the owner if it's if it's not the owner it's going to be so then we can proceed to set our data right new value okay so yeah I think our data function is done then we can move on to the next function all the next function get data say function data we can off contract St this contract St basically a 252 so it's definely going to a f 252 so quick question for the class hold on a moment let me type this out first um self so what's difference between this block this line 38 and U so can anyone tell me differ between this block of this line of code and this line of code and this line of code so the first one would not return return you like sem first one I think the second one I've not used the second one before but I think also does same thing returns does the same thing but what makes this unique from this like what's what exactly is the main differences between this two this guy and this guy over here you can use either one interchangeably right which is very very correct what differentiates this guy from this guy I think is the syntax the first one you don't have to close with semicolon the last one you close it with a semicolon that's really nice see there at the moment yeah just just like last I said the when you close with a semicolon when you with the semic then you have to you have to put the return statement that me you have to explicitly states that you are returning this particular parts of the of the of this function but when you don't close with a semicolon then the it will automatically pick pick it since there's a return since there a return there so it automatically pick the one that is not closing with semic and return return so thank you so basically when you make keyw you have to like close it with a semic Col so you use this or you use this you don't close it with a semicolon and return it just like that or you use or you use the return keyword and closing a semicolon that one works fine okay I don't know whether this is also the same thing you know when you exp explicitly explicitly you make use of it no other you know program after that can actually execute yeah yeah that's right so um let's go ahead so I I see you guys really do have a knowledge of the SM contract so what what's the next function we need a function to return the owner need to move first um function it function owner the function to return the owner of the contract [Music] s then this going to return so the is going to return work to us the contract address um then we can basically say cell dot now yeah good so we have our three function set so okay so our transfer ownership function as well let that so mind you this is throwing an error based on the fact that all our definitions here have not been implemented so the we Implement every single one of these um of these tra in our implementation block this error is going to go away so function transfer ownership so weting to State you have to like make reference to s [Music] then contract address then um so I want to make that only the only the owner can transfer ownership so um we can just basically copy this Lo then um so we need to obtain the previous owner first we do so let's just try straight to it do we all know do do we all know how um emit works with cont we need to it to emit event you all know how we work or we need to touch it quick response please please tou let's all right so um so basically we want when we transfer ownership we need to like emit event and what do we want the content of the of the event should be so let's say the address of the previous owner and the address of the new owner uh and if you want to do that of our to what um if you can just put over here [Music] so it's going to like Drop it to run into [Music] then what else then we have an so now what's going to be the content of our of our event we said we already said um the previous owner right and the um the new owner to only Mak more sense to have a stru to to accommodate okay let's the same here so struct so let's I okay should transfer then uh we can Define so say previous owner definitely going to be contact address contract address then our second key um so the new owner new owner also contract address oh sorry this is going to be just from Ah that's so now we can confident lies of this here now so we can say [Music] um yeah sounds about right so now we' Implement our event yeah so so now the next thing is to make of this event in our smart contract so now we know that once we transfer owner is going toate an event so we need the address of the previous owner and then we need the address of the new owner right so let's say let's previous owner equals um self do [Music] owner that's read so we done this first um okay then we go to just set it like that so let's pass thats about right so let's emit our event so we have ourself the emit keyword um then what's the name of our event yeah I should transfer so uh you can have this hereu piix in prev and um oh and new okay uh2 error we need to the other okay yeah the key should be there actually before the value the what the key from the struct oh thank you for that thank you very much actually we can do it this way I guess this should work uh that works yeah it works you know all right so thanks for that um thank you Victoria thank you where do we go from here I think our contract is looking good what have you not implemented this [Music] so anybody confused up to this point abig girl I don't think I've heard your voice in this call abig there hi yes I'm here I have not what you okay are you on S are you online I'm on site okay so um where do you think we should go we should go from here well I was expecting you to say let's let's just go ahead and test the contract anyway let's just go ahead and test the contract so so um so we we we are now going to apply some of the knowledge we had during our unit testing in our um in our test in our integration testing right so basically we could have our test file over here we just have like you make sure we specify the CFG keyword and CG test keyword blah blah blah uh but then for uh code readability and code neatness we should let's have it in a separate um file right so um quick question have we all deployed on um seia have you deployed a contract on seia before like St not nor SE like have we all deployed on SE yes once okay so um can anyone tell me the deployment process you know like the key things that happens before deployment because basically we are trying to um we are trying to imitate what happens in real time in our local machine in our test file right so do anyone can anyone like um explain to me like the processes involved like the key things that are very very Paramount when it comes to um deploying a stet smart contract so I think is to declare declare the the contract before deploying it okay I think I need to give Dan a round of applause give me a moment yeah I think I just gave you a clap so yeah we need to deploy sorry we need to declare our contract and then deploy our contract before we can interact with it um so um also are we all familiar with the concept of dispatcher are we all familiar with concept of dispatcher yeah I have an idea hold on before you see anything um Benjamin do you have any do you have an idea of how of what a dispatcher is yes I do okay can you explain it to us like what does dispat is like the it's like let me I don't want to use the word interface because of um because of what interface means in this sense but it's like the medium we use to interact with a smart contract so if I want to call the function of a smart contract I will use the dispatcher of um that is generated by the compiler from the tra or interface of that contract to call those functions all right so Victoria do you agree with Benjamin yes yes okay so um yeah from the last class um the last yeah the last class the tutor said um the dispatcher is meant for making external calls all right of course yeah of course so thank you for that so um yeah we already said we need like we need to declare a smart contract right so the beautiful thing is that stet Foundry has made this a lot more easier of EAS it for us so we can basically just you know use um what's it called use the declare keyword from SN for STD right so then I think let's let's import our dispatcher trait TR dispatcher so we can basically say use the name of our project we making reference to unable from our test file use um and the Beautiful Thing with this is that we specify this and add a dispatcher keyword to it it is open for us dispat aome um excent okay so these are like I'm trying to like you know um what's it called like set up the per s before we proceed to writing our test proper so now we need to have let's let's create instead of um deploying at every point in time that we write a test case which it makes more sense to have an helper function to help us declare and deploy our smart contract in our test file so let's say we have a function deploy contract so this is going to like make our testing very easy Once you have this you just have to like call this function to deploy deploy sorry Contra so um to do by address [Music] okay okay so um let's contract um equals so we have to declare so uh deare is going to take this name argument you can unra it is that correct yeah good comp so uh then we declare this is how you declare with st so then we are open to so um the next is clear the next is deployed right so in our deploy contract the top right so we can say contract TOS deploy that's moment so if you can recall we have a Constructor here so if we don't pass in the argument it's going to fail it's going to like throw an error so it makes more sense to have an admin address passing so address um so this class is meant to end by six right no seven seven okay right give me a [Music] moment let's admin address um equals say Ad me okay let's typ cast this before we can type cast we need the the try into keyword try method from call so I'm sure we familiar with typ casting correct am I correct are we familiar with type casting yes yes okay good um try to so can basically [Music] so since we tting right we need to annotate you need to like tell it that we need you to tcast this into a contract add oh this is meant to dra an error why on a moment please that's yes I think the great what do you think they into like and a capital I or something oh thank you so okay this is basically across we need to like make a return and stuff like that so uh so now basically this yeah we said something about um passing in a passing in the passing in the arguments for the Constructor so it's it then um this is going if we pass it in like this it is meant to dra an error because [Music] expecting so this is a tople right so this is meant to return a contract address to one thing like two so we have to like leave that space open so this thison an error so what this eror again I up a moment let's contract on [Music] first what [Music] happen something was wrong I used the varable I here can we try spamming the like unrap again the unrap do we need to have a p to it at the end to it at the end of unrap no not that one this yes but I doubt no it's not anyway let's deug let's see where the error is coming from can somebody tell me why this is not in an error again oh I see is it because the contract address you are returning is not the type was not there I doesn't matter any let's you know what let's let's go ahead if we encounter other errors the B but basically this is our um this is our helper function to help go on all right I see that um nor when we want to type cast we use trying to and but in this case we just uning without in line n line n no this is meant I'm not casting here trying is meant to typ cast I'm not typ casting here I'm just wrapping what is been return from declare if I don't unrap it it to go into contract if I'm typ casting then I make of trying to but I'm Notting here I'm just unwrapping what is inside the what is by the C okay so what what what is return by and that's and what contrast what well let's check the type over here it's not rning the type I'm not sure the kind of type it is me to right I'm not exactly like into that but probably in the course of the class I find it I definitely let you know right all right go on vict okay so what's the ense of what you did in line eight like why did you have to typ cast the admin to contract address why not just use it as normal contract address okay so um it's just basically for readability you get I can basically just go on my wallet address on my wallet and copy a random contract address right and P it in here like I code it but for if you going through the code base later on you might be confused as to what kind of address is this so for basic readability it's just basically typ casting a string to an address so if you see this now if you see this now you will know that oh oh okay this is admin address or let's say if I have let's say if I have let's I have another address here let's say fake admin address fake address I can basically say here as well fake mean do you get it's basically for readability so this this is been typ casted into a string sorry this string is being casted into a contract address I answer your question yes so if if you a string is being into an address does that mean that anytime that he trying to fetch that particular address it should be with a string as an address or will it be able to petch the deployer address at this point at this point this is definitely this is going to return like an X like the returns from this is going to be an X like it be like a contract address normally do you get so if you the deployer address yes that is address so the owner this is the owner's address the one you are passing into the Constructor here the initial owner do you get yes yes I get yes I get this one so how will you be able to reach that admin is that owner's address how will you be able to link this admin to that deployer address you mean this yes how would you be able to link this at me to the de player address that no no no no the person deploying this contract now right now you know when we run the test case or run test right it's the inbu address like the like the inbu um uh address private and stuff like that is embed inside of St that is deploying this for us but then meanwhile meanwhile if you're familiar with start prank with noral solidity we can do something similar although it's although we can use a different uh keyword when it comes to this one so you can you can specify who deploys this contract address but then but meanwhile you know we are we are only passing in the the initial owner as at the Constructor level so we not setting MS over here so we are passing the initial owner yet so it doesn't matter who deploys it do you get but then we can still specify who deploys a particular contract we can make use of stet foundaries cheat code to get the method that that is going to like help us to um to specify who deploys a contract who calls a function and stuffs like that are we good yeah yeah we are good what was just what was confusing me was the fact that okay like this this is fit to deploy the contract now in the in the contract itself we have a Constructor that Tak the address the address of the deployer set it as the owner of of the contract now in this pit that that is meant to deploy that address now you know if like okay let me put it like this in in let's say we are doing we trying to deploy and a solid contract with ad now with in ad now in in our deployment stre we going to have a kind of maybe and that that is going to have the deployer um address or private key and that is going to translate in our deployment script something something like that like I'm just trying to like like there's a linkage between whatever scam we using in our script to the deployer address or the deployer private key to just give that connection like what I'm just trying to understand now like how does this admin now how does the how does the script now make it understand that okay this admin keyword that is here now is is a place holder for for the deployer address don't get it don't get it this have been addressed here is not the deployer something else is deploying it for us in our test case in our test file this have been addressed just been passed into the Constructor so they so this agre can take the position of an owner of the owner now according to our contract logic we are setting the owner here we are setting the owner from the argument that we are passing into our Constructor not like we are targeting the deployer of the contract do you get is is the owner do you get yeah so I think we we are done with our helper function so now this this basically deploys deploy declares our contract uh deploys it and then it Returns the contract address of the um of the contract to us of our horable contract to us so now we can now move to our T proper now and then if you want to remember we writing our unit test the first thing we did was to declare our test um keyword keyword um and then we say know still the same the Let's test so the first okay let's okay let's test initial data you know we are setting our initial data Constructor level here so now our initial our initial owner has to be this admin address and our initial data has to be one so let's write a function to test that this um this U that this um this state we set over here is valid so uh so let's say data so is anyone confused up to this point before I move forward ad are you confused uh David are you confused I need like vocal feedback rather thano I can know you're actually following David are you confused David D is David on call yes I'm I'm on the call okay that kind took you so long to respond so are you following yeah any confusion so far yeah I watch the playb well I feel it's best to try and catch up right and if you have questions on the goal you ask because trust me when you say go back to it sometimes you end up not going back to it so try and catch up you so B are you confused long you go B are you confused or we good so is Fatima a bot no no I'm not confused thank you oh great so is f my B uh anyway let's go on um so let's write a function to test our initial data um so let's first let's first deploy contract sorry contract contract address is now going to be so already have function to deploy this address you can basically just call the function here and mind you passing the name of your contract that's theary is it the name of the contract so it can pick it up from the target it has to be the same if it's not the same it's going to fail because it stre that there is nothing like unable contract so and then the next thing is to we have our contract address now right next thing is to interact with our contract address with our contract with our dispatcher um so let's dispatcher oh Lord dispatcher let dispat equals to tr dispat you can p this thing here so so what argument does our H take anybody quick response contract address thank you contract address so we have our um we can now interact with our contract our dispat is now set up right so let's say uh so now we need to check we want to test our initial data right so we need to check let's say let's sa our initial data in a state in a in a local variable let say initial sorry say initial data equals to this is how you interact to dispat do so we are getting data data is our function here get data so so this is going to data to us so get data yeah get data and then let's check our initial owner let's initial owner equals to this spa disp dot um sorry okay so basically now basically we know that our initial data is this admin address and one the one we passed in here and one here so if our if our logic is correct initial data has to be one and the initial owner has to be his admin address so and how can we achieve that how can we check make use of the asset keyword so let's have let's have the same address over because we going to check check this it so basically we want to like test if this initial data this initial own is equal to this admin address that we passed in our deployment right so and also we going to check if the initial data is one the one we pass in in like the one we haded in our contract so we can EAS say ass but initial date is to admin address so if false um [Music] incorrect correct mean I think that one should be initial owner oh thank you right so then also Asser that our initial data is equal to one incorrect um dat all right so now let's let's run our first test let's see how this goes so you can run your test Case by just simply running SN Forge test so it's going to like um pick up all the test all the test that is inside your smart basically we only have one test just one contract comp okay all right so why is it failing head deploy not found oh okay you must start theg is right here [Music] I think you have to import deploy to be part of this like of the method and [Music] con the way you imported the SN Forge sdt online one and you imported decl are you not supposed to import deploy also there let's see [Music] Le I need I need more hies to look at this more to look at this what is possibly wrong same address is which is [Music] to to church s [Music] yeah so now if if there is one thing I appreciate with Kyo here most of the the errors like it's they are kind of clear with the herrow now so let me show you something let me pi this off again the the again so now if you check the error message over here says something like the plan found on the type of contract class like you do import that where I got in that I did not import the contract class truth because deploy method like is meant to be found like in here so um Kyo errors are like quite easy to like Trace you get so just always learn to go through the error message first the from sorry to see that so yeah collected one test and then it passed so that means um when we set our state at our Constructor level everything worked as um as expected so now let's test for um another scenario so um let's test for when we set our data you know we we we sorry let's test this function next this uh set data function you already tested data Let's test the set data function next so let's not forget specify the test keyword we don't specify it w pick it up as a test function test set data work yeah so now um still the basic it's still the same um what the same same flow so you can just let's just copy this three lines it yeah then let's set data so dispart dot s so now mind you this function is going to fil or hold on first let's let's test this let's test it out so as you can see this test this second test test SED works this test failed why now the this brings us back toar is ITA Yeah question asked us earlier that how will the contract know the owner from the deployer and the color of the function right so uh the beautiful thing with stet found is that it's um it it opens up method to uh do stuff certain things like for example now we want to like uh let's say mo sorry say um use the address of the like more like maybe like um like borrow the the address of the admin what's that like prank in normal solidity thank you very much yes thank you for the word yeah more like prank the admin address right so um let me see okay so please start not found to Che [Music] reference so these are CH codes basically this methods are available for use like use them anyhow you want depend on use Cas right and the guy we need the the KE code we need over here is this guy over here so this changes the color of the address for the contract so now for our can you see can is this B enough right is it B enough no no no no good enough yeah good so now we need this cheats color address so now we need fun we need this street color address um uh this this street color address method for us to like prank the address of um of the owner right and this is a method available for us on stack Foundry so basically it takes in Target right target contact address the color address like the color address you want to you want to prank and a span a span and how do we know what this guy is we have something like a span over here these are like how you the way you like maybe debug your um uh your test and the things you need what you doing and stuff like that and you can if you look at Che span Che span is an En an enom that specify how long the CH the tget should cheat for whe indefinitely Or for number of four so basically this works this is going to work for us just copy this guy so so let's import it let's import the guy from is anybody confused is anybody confused yes you confused can I hear me okay yeah so I have a question yes okay so I have a question about the contract class used the other time like what the use CU I understand the use of the special and all that but like contract class like what need for so this guy basically allows us to have access to methods like deploy so when this contract class was missing this deploy function this deploy method did not work because we did not implement the trait in our own smart it has been implemented from stet Foundry right so we only let like bringing the tra into our own local machine for us to make use of the deploy method so without this contract pass straight we will not have access to De if you notice this depl came straight from s Forge STD right so so does deploy you know we cannot we could not import deploy directly from here just we deploy C the way we imported de cleare right so we needed the contract class straight so that we can have access to the deploy method so without this contract class trit we cannot have access to the deploy method there one like you saying that um you want to uh you want to call set data without importing able tra like you know we've been calling um we've been calling get data over here right this was only made possible because we imported this our I enable trade dispat right is almost the same do you get so without the contract class trate we which has the implementation of uh the deployer there was no there was no we like you know make use of the deploy method on our contract do we get yes I do yes and also a question so yesterday the so the meor was like dispers are generated automatically for your contract like each contract CU I see that it's coming from on and we did actually declare itable so it's coming it's been generated like how is this generation process like that is very correct that is very correct yeah it's been generated to hor under the Ood and believe me technically we cannot go into the details of how it is being implemented under the you so has abstracted all we have to do just it's just the same way if you notice here you notice this private function here too function we have here just have a generat trait all this generates the trait under the wood for us we don't have like necessar since we are staying on the application layer we don't have like necessar like no like the into into unless you want Tobe dive deep into it then you can go ahe like check out how it works the hood you could take as the personal project okay I'll do that thank you all right so let's go on um so now we need to prank the address of the admin for us to be able to call this set data function uh so we already imported the cheat col address Che col address right so the way you use start prank right um You have to like put it above the the function you want to call although we can specify number of CES we want this function to number of CES we want this caller to be able to like call right um but then we have like specify that okay just put above the the function you want to call and then what what are the parameters takes the target address then color address and Che span so the that is the the address you want to interact it which is this contract address so this guy over here uh then it takes in D in the color address the person that you want wants to be the color of the function that's the admin here then Tak in a span yeah CH span um so CH span is CH span is a CH span is an enom right so let's um let's import chip span over here as well to make of it pass in ch yeah so we can set the uh can set it to indefinite or we can just say okay make only this number of calls so let's just say we wanted to make just two calls I get two so if you want it to be indefinite we can just pass an indefinite over here as well does not Tak a parameter only target target Cod takes in a parameter um sorry an argument um I think this should this is so yeah I think this this is fine so let's let's test this first let's see if it's going to if it works first before we proceed Dev of the test logic oh this isic okay great oh awesome so yeah we've been able to successfully prank the address of the admin that was why we were able to um call this set data so we need to be sure that the data actually is correct so our new data is going to be six right so we need like check if truly it is six since we setting it to six so let's say let new sorry let new data equals to dispat do get let's get so then we can put in a little ass statement over here new data is equal to six if it is not then data set F so let's this again great so yeah our test works now any coffee shop to this point of I kind of lost it from where we started using this sheet thing H I don't know if you quickly run through it again I'm sorry please so J color address is a cheat code like a cheat sheet from stet Foundry has been implemented already on stet Foundry all you can and then you have access to make of it in your test file right so basically what it does is to is to Mark the address of the color like the person you want it to be the color of that function it pranks it it's it works it works similar ways the way start prank stop prank on Works in solidity so this guy allows you to um prank an address that you want it to you want to be the color of a particular function so now we want this ad address to be color of this function right so we can invoke the cheat color address method passing the target cont address you want to like you know you want to interact with it passing the P want it to be the color and then passing the sheet span like the span um argument so basically this span argument the span argument is for the you can see from doation like I takes in a che span it takes in a span it takes three argument Target col address and then span right and then we further now you know we saw Che span of course and we now found out okay what exactly is this cheat span so we found it over here we realize that cheat span is an enom that has two options you know the way enom work if there option it's either a or B depending on the definition of the en so we have we say okay it is um want to choose indefinitely want the col to like call an indefinite number of functions or we we want the col to only call a few or particular a certain number of function so then then we pick we pick the target C so let's say we want to pick indefinite now let's say we pick indefinite now yeah if you passing Che span and don't forget we first imported cheat span from stet Foundry this cheat span we imported it here first because of course it is a method in in stack St found right so we first imported it and then let's pass in one of the one of the options inside the H which is you know we made use of um Target SCS earlier so let's make use of indefinite and see if that works so now by application when we set this that means the color of this this set data function is going to be the address that we specify in which is is the admin and we expect it [Music] to so is it clear now um uel uel ask question y it's clear all right awesome sorry I just want to see like sorry I just want to see where you imported your um cheat color address from because I it on my on my system keeps failing I don't know why okay from st4 yesterday that's the same thing I don't know but if I do start prank it works but if I do cheat I don't know it doesn't work oh what's the version of your stet found I'm using the lat latest version there has been there has been upgrades start prank works on previous versions okay okay let me let me try yeah so um confusion from here I mean at this point I need I need a um I need feedback any confusion from here are we all good yes sir yeah we're good so let's test one more um one more test one more scenario um okay so let's test if a fake had been tries to um wait hold on okay let's test if a if a fake admin tries to like maybe interact with set data right so it's Bic this this guy is basically this guy we want to like make sure that no other admins can uh can call this data function so let's let's just copy this function so so let's call it test test um f admin set data fields F right so um technically let's replace this guy with fake admin let's say fake admin address and let's call this fake admin so deploy set the admin blah blah blah then let's copy this and uh you want this fake adress this fake ad should be the one to like call this um function right so let's Del this not oh no let's let's leave this let's say let me say so since the will remain unchange so just to be sure that um just be sure that the data we set at at deployment Remains the Same so let's say data check and the data check is One initial the initial data deployment is one so let's and so by implication if this if this function should go through right and the person is able to like set to six then there's a very big problem with with our contract there's a very big vulnerability with our contract right so this is meant to fail so uh let's clear this so as you can see that okay one of the test failed with the error color is not the owner so that means yes our smart contract our contract works right but then there are better ways to handle the failed tests right if you can remember if you can if you if you can remember when we were talking about our unit test issues right we add um we had an option that allowed us to test to test what is it an option that allowed us to sorry had an option that allowed us to are that okay this test should fail it should like Panic right it's inste of like you know okay field field in your console and Stu like that better always like handle this so we need to U expect a panic message right so um so let's set it over here so um sorry so yeah but then mind you we expecting this this um function to fill like this right but then we want to like make sure that we don't get this fil um message in our console so we expecting that color it should return an error saying color is not the owner so if we copy this and paste this in here and then run it all over again then would have and our field um error message properly moment as you can see now we no longer have a fi test again because already to the compiler to expect that okay for this test return an error saying the color is so already like okay once this test and then return then you uh so guessing this this is uh settled so can we test the eoms to see if they emit what we expect is there way we can test that oh yeah that's that's true we we can actually do that um I have to like take sorry my moment so let's do this um let me send something in the chat box [Music] so um I just sent something in the chat box um in your it's it's actually going to be part of your what's it called your um exercise so go on the documentation and figure out how to test um smart contract events so although I still have I have an exercise for you guys to like do like right now like right now I'm going to push this contract um okay let me do this I'm going to create a g file a gist file right now I'm going to uh paste in the the Kyo contract and then the test contract right and you guys are going to write two more functions I mean sorry two more test cases yeah two more test cases one is okay on let's have it over here wait are we are we were we C long no okay so I'm going to push this um to do so uh say um one um write write the following Tes one test if [Music] transfer box um then also test if transfer ownership by admin Works test scenario can we test okay and you take home so you're going to you'll be doing this you didn't this to on call right now on call right now I you would have only about um say 30 minutes to get that done right only about 30 minutes to get that done so so you have to think fast you only have 30 minutes to get it done um so then they take home this one will be submitted today is what today is Friday so this is going to be submitted um tomorrow sorry you have a class tomorrow right no no no okay beautiful so um this is this this second one is going to be submitted um tomorrow by by 4 p.m. any submission sorry sir hello sir what's that sorry sir I I I I really like to come here please because um I I I don't know for others but I for instance I still have um last week we had some outings and and so my catching up with is still a b is the speaking it's it's still a bit behind and not to talk of this the fact that we have like two two that are closing in and I know it's not like we are submitting and we sure but then just we trying to just push to just make sure that we have it submitted and that's that and then we have another one to submitted so I don't know if you would actually give us more time so that we can actually put put much effort in making sure that we we get to I we don't want to just was it and just do it just for doing sake and have it done but we we want to make sure that we know it and we know it well so if you could actually give us time so that we can clear out all our in fact we still have one assignment from when is Day T there's another assignment again that I've not even touched and I just have to plead like if you could just temper Justice temper Mery no I don't even know one anyone you help us please please sir okay um no problem I do this on moment so um I understand your concern right it's one of the reasons why you're in Bridge you get you need to learn how to multitask but given the fact that you Leed and stuff like that so you can all submit it tomorrow by 4:30 instead of 4 p.m. I think that's so give me a moment let me push this to GitHub gist uh I need to log in my details first just what what is so yeah you are doing this on call right now and you have to like think fast you have only 30 minutes to get it done and then you push it to you create a gist push it there then you submit it on your I'm not on where I'm not on your telegram group page [Music] um onam group page pass yeah I'm I'm trying to reach out to the admin to add you to it is that fine so do that so yeah yes so do that that um so that once the once you guys are done you'll submit it like there then we review it within five 10 15 minutes cool so I think I should stop recording now
Automatic transcript — names and jargon may be misspelled.