4. snFoundry, Components in Smart contracts, ERC20 & ERC721
Starknet BootCamp · November 2023 · 1:11:10
GianM of Quantum3 Labs explains snFoundry, smart-contract components, and ERC-20 and ERC-721 implementations.
Speakers
Topics
From Starknet BootCamp, published by OpenBuild; community: OpenBuild.
Transcript
okay hi everyone we are in the open Bill stand boot camp this time we are in the session four Lesson Four so we're going to talk a bit we want to talk about testing our smart contract remember before we already talk about smart contracts here on the lesson number two and three we deploy it we play with remix car Starling Katana testet now this time we're going to talk about testing how it goes remember also you have all Theo on the GTH Hub open GTH that you can check here as well so let's start we're want to talk about testing in general and then we're going to go to the coding session like I said already
work with my contract with remix CYO Starling Katana tet goly now we on the testing we're going to talk about what how is testing web three what is upgradeable smart contract different type of test a bit of local dep net and Stor fun that's is going to be our main tool for this session and then the like coding when we talk about testing most of the time you know when you do the software development you do this move fast and break things it's going to work most of the time but not really on web three sometimes we have the thinking like like a NASA no room for errors but if we are beginners so it's not possible
to this we also have this one once deploy it it can't be fixed so it cannot be fixed it depends depends if we have gradable Smart contract or not let's see about it web is highly adversarial so it means there are a lot of hackers or attackers they want to exploit some BS and some protocols can lose a lot of money because of what because even a small B they can you know get a huge impact on their business so it can be catastrophic so the idea is defense coding rigorous testing and Co out all of this for to deploy yeah what upgradeable smart contract so actually
it means that your smart contract once it it is it has been deployed you can upgrade some functionality depends how did you how did you set up my contract before to deploy so most of the time yeah it's okay it's really okay on test net but on Main net is not most of people don't suggest to do it unless yeah there are some you know special special cases when you can do this but most of the time try to avoid um upgradeable SM contract now we want to talk about automated test we have different kind of testes test the basic one with un need test integration test and end to end test so the idea to un
test is test just a public or a private function there so for this we have to Mo some dependencies for integration test now we have the public interface only so you test with the public interface with simulator F also we try to emulate the sequence and end to end is going to include the UI user interface and the user Behavior here is the way individual functions we can use unit test if we are done with this we can do the integration test with a public interface at the end we can go for the end to end test so actually kaido in stet we can use a stet foundary to build a unit test intution
test and also the kir test itself on this lesson we're going to show the example with these two the tools for to end they offer the Press so on unit test is going to be individual function so it's a y box it means you can see you can check everything inside it inside the function in the test but for integration test because we're going to work only with a public interface so it's going to be like a black box only with inputs and outputs we canot see what's in the middle on the process so that's the idea of the the test so it means or unit test you need you need less amount of amount of time see as well but if you want to you know go for
to test you don't really am amount of time it's going to be slow but if you finish this you know you're going to get really it's going you're going to get a lot of value on your project when you can cover most of the test with yeah there are more tests for manual testing to test with the UI for quality assurance test where you can input a lot of ranch yeah a range of input parameters so the idea is that your function is going to handle a lot of unexpected values check what the this so it's one really interesting and the four test formal verification test we're not
going to talk much about it just the previous one going to be enough the test test here we also have the local de like before you remember if you use Katana structor or architecture something like this you have your de in your local laptop laptop with the ATP with the jspc this is a URL sequencer user accounts already redeploy it already see it with some found it with some monor smart contract and Universal deyer for this one we have like we mentioned a katana is build it by the dojo team and we also have a star dead net it is
maintained by Stace sure this is the most important tool now start found in the past last year or few months ago months ago we produce a lot of protostar but now it has changed to start fing develop it by Soft formation yeah they offer unit unit fation F and Fork test also we can test with cheat Cod it's going to be useful so if you have experience with laundry in solidity and you can get really famili with this tool build with r so I mean this tool has been build with r fast actually and we have the forge command the C command for for Runner C is more like a CLI they
have the GitHub the book you can see there now yeah so the idea is always test before deployment think twice about upgradable smart contract we're going to work with this the common test with unit integration and with this one start found testing Cod yeah that's it let's move into the coding for the coding we want to check yeah you might remember this contract have a look there for this contract is our ownable contract we it before on lesson three we play with this we deploy it on Deet on testet but we didn't add any test on it so you know
the idea is always to work with testing so this time I'm going to show you the testing in this case we're going to use Kyo test so it means I'm not using St found now otherwise I have to adhere on the dependencies St fundry but this time for this example I'm just going to show how is testing with KY test like you said like you can see like you can see we can we have to import all of these functions sorry all of these modules here this is the most important from stet this deploy is called it's going to help us to deploy our our smart contract on the testing environment so let's see
how it goes so to play to do the testing here we are using only scar after our scar bu work we're going to do the scar test so for this we have to set up our you know environment for the testing we have to add this flag here this attributes if you test test and then create a module for testing this test then import all all what you need and then for our first testing have to add this T PL set up the the gas we're going to use consume and then we have to get an name to our function in this case it just test uh the idea of this is just to deploy the smart contract and check if
the owner if the owner of the smart contract is the one that we already we want to set so for this for example we're going to create our admin address varable it's going to be a contract address type but for this we are going to pass we're want to create this SP two value 252 data type because in some ways like a string so we create this but this is p252 and this is like a contract address they are different kind of data type we're want to try we're want to try to you know to cast or to convert from F to contract address so we want to try into and then unwrap value we can get we can get the comparation from here to
contract address okay then remember in order to deploy a smart contract we have to pass all the all all of the input uh parameter for the Constructor function remember here our Constructor function here it's going to take s but what like First parameter is going to it requires a initial owner so we have to set up who is going to be the owner of the ownable smart so we have to pass this value in our testing so how how do we accomplish this so we have to create an array of data so it's going to be our first value is going to be the contract address of the admin is here if our Constructor
needs two more values we have to we can ADH here we can ADH here like this but in this case we don't need it we just need one but yeah always we have to pass like array and we're going to call it call data the call data okay we already have these two values variables now we can call the B this deploy call so for this it requir like four four parameters the first one is going to be the Clash hash of our of our smart contract so we're going to set up you know ownable so smart contract then we set up like a a class has it is a test class has so we just call it you know it's just like a
value there we don't need to worry too much about it this for testing Try inter W and then we have to add like a SOL SOL value for Randomness you can change here zero is okay then we have to pass the call data the input parameters in this case is same but it should be like a spam so it means to read only this data is read only we canot change it this then we have this FL like false so if you want to check more about deoy scod you can check the documentation here calls here in here in the documentation they have this Ploy
and they also explain about call deploy from zero what does it means so it means FL use for the cont address computation if not said the color address will be used as the new contract deployer address otherwise Z yeah that's it then we're going to return values we're going to get the the address of the Deo contract that's what we want and the second element is the response away from the contract conru I Time pro SP but this one we don't need it we only need this for our example so let's go back there that's why here we have we PCH the first value and second we just you know we just say okay we don't need the second value so yeah this is going to be
the address of our deployer smart contract so after that remember we have to create the dispatcher so to create the dispatcher we have to pass the contract address going to be our smart contract we have the dispatcher now working here and now we can access to the functions that we Define in our smart contract so we can do this with owner so we can check who is the owner here and we're going to assert we're going to check if our owner in the smart contract is actually the admin address that we Define that we set up here so yeah that's it this like a very small unit test but useful so if you're using
a scarp and you try to work on this way here is to run the test like car test and it's working what happen is I CH it yeah it's failing because now un fix it so it's going to work again so yeah so this is one way how to test the our program our smart contract for sure this is only one unit test for sure we want to add more testing here we're going to show them later but this is the very basic one yeah that's it after this we're going to show The Foundry how we can use a standard Foundry this because actually most of of the people they
suggest to use Foundry instead of this like traditional way you can find found more useful they have like you can do any kind of testing there maybe here is a bit limited but still working so let's move into the another example where we can just start found but first I want to check the just now just now the released the a new version is 013 compilation they are using 2 yeah because just now they just releas there is a new version of Card and also for the starnet 2.4 uh they release this
version but we're going to still working with this 12 because you know sometimes we have to wait a bit more for new releases so that's why uh on this video we're going to still working with the version number 12 and yeah let's check here so this is like the GitHub repository so they show you here how to play how to build your test with the start pundry what is the installation so in your case uh you have to follow this rules since they just have this new release Here thing we have to install the version 12 yeah remember so we have to do this okay we have to press this you our terminal then we can do f app
but in our case we're going to install if we do f app we're going to get the ver the version 13 so in the our case we're going to use this second option from the app and then 12 here so let's do it 12 yeah okay okay great let's open one more time let's see what version we have for okay we have this version so it's okay yeah so it's okay yeah like just say check with this command what version do you have you can check more there they also have the book but remember always to check sometimes uh it's not easy to keep the same piece on the book and repo so it means maybe there are
some parts of the book where the team is still working updating the content of of the book because you know these tools change a lot so fast so yeah always be a about it carefully so how to use this I show you uh we have to install St pundry with and then after we install what we can do is we can add like a dependency of this in our scar project so I'm going to show you my scar project I have this project it's on with the scar you see but here this time I'm going to add this forch so I'm going to add the St fun dependency here and also I'm going to I
have to specify is going to be the 12 the version 12 so good and for the startet we still using this 2.3.1 but yeah they just few few days ago they released that 2.4 the version 13 but from this lesson we're going still working on with this so yeah so this is our boting example so we're going to check quickly so the idea is like uh one contract one address one user can vote for zero or for one for yes or for not and it can only vote one time cannot be it's not allowing to vote two times or three times only one time
we have to count this how many votings are on each side so that's the general idea so for this we have to you know always to create our interface our smart contract so we have this like a boat so this both we have ref cells so it means we're going to mutate the smart the con State when we do the voting we have to send pass the value if we vote for zero or for one so this like the writing function and the reading function or the view function this one we going to read the contct state this read only that's why we have the snapshot here we will to return how many Bots um with zero how
many Vols with one that's it after we defined our interface we have to Define our contract itself so we create our mode Mo module it's going to be voting contract our case yeah we Define our storage we Define how many votes for yes how many votes for no the voters in this case we use here to2 but up to you you can change it to u64 okay but for example we keep like this then we have this botter so it's going to help us to do the mapping we're want to map a user or a user contract user address we're want to map it with a
bull value so it's going to be false or a true so if this false it means like a the user haven't vot yet if it is true it means the user already boats so it's going to help us to check here for later we find our storage and then yeah we are going to implement our interfaces so Implement our interface for boting contract so for the boat yeah we have to pass a mutable reference our state but first we're want to check what is the input the input must be zero or one then we have to call the color so the color of this smart contct of this function
and then we have to rip if this Coler or if this user has already vot or not we're want to check here for example s is going to be our contract State and then BS here in our storage B is here and then we're going to read we going to read the first value of the mapping so for this we're going to pass the color Contra address if it is the First Time The Returning value is going to be false so all of this is going to be false false and false so it's going to become like false time false become true of false is true so the thir is going to work here in case the color haven't B yet okay now we're going to check if it
is zero we're going to add one value on the no votes VES or no so we going to read the previous value and just add plus one same for one if you vote to for one we have to go to the g bo right here g bo and change the value you're going to change it we're going to add one uh one to the previous value so yeah that's it after it's done we're going to set up the this color has already votes we're going to go to voters this mapping we're going to pass the color address the bull value this case is going to be true true so later this voter this user cannot vote one more time in case we want to
get the bats first we're going to pass how many no bols we have and how many yes bols we have see way yes quite simple now because the idea is just to show the the testing so this is for our basic concept of our smart contract and now we we go to the testing file check here I have my Logic the logic of my smart contract on this file but for the testing I create a new folder and I create this test contract Kyo file now we're going to check how is the sequence to work with this remember we have to import everything not everything but all
what we need on our case here we can call this Forge STD so it's the same name like our Forge here our St Foundry we have to import this declare contract class start plan Che targets remember here we're going to go here first and then we're going to check one by one so we're going to check if the counter of the BS is changing or not we're going to test if really the B counter is changing or not this is the goal of the of this testing so remember before to do everything we have to deploy our smart
contract so that's why we have to built by our own more deploy contract function we have to pass the address so here I'm GNA move here this is the deploy contract we have to build by ourself so this going to get L 252 name so here inside they call declare so we're going to declare our smart contract we're going to pass the name the name is of our smart contract is boting contract so we're going to declare it here our voting contract and we're going to get the contract with this value with the we can deploy and deploy our contract we are going to get the contract so how do
we deploy first remember first we declare this declare is already buil it on the stet boundary after that we deploy it so we have to pass the data the input value of our Constructor but in our in this example we don't have a Constructor or we don't have input for our Constructor so it's going to be empty AR right of values snapshot but empty after that we deploy it's going to return a contract address that's why we don't have this because the r return value is going to go to here great so let's recover here we pass the name of our smart contract and then
we get the contract address this part yeah now second we have the expected VES Val so it's also a helper function here we have to pass the che address and these two values so let's check here expected both counter values on this part so here here first inut parameter is the contract address and then we have to pass the expected no VES and expected your on this part once we already have the deployed contract address we can easily build the dispatcher so we build our dispatcher here we just pass the contact address we have the dispatcher so now we can read the the values so get B we're going to get both remember get B
is going to give us the going to return the no the number the C the number of no BS the number of yes BS so after we deployed our smart contract we have to check if the no bo from our from the smart contracts is the same like if the same value the one that we expected so we have to pass this to our so yeah remember when we deployed when we just deployed the smart contract all of the values they are like a default values so they are they must be zero so that's why we do this we check here if they are zero or not so they might they might pass here they might pass this this ass okay if it is working let's continue
let's move on and now after this we're going to call out dispatcher again and in this case we're going to vote we're going to vote and we're going to set up one here so remember when we vote we can vote only for zero or for one this we're voting for one one is going to mean vote for one is going to mean yes here one is yes so remember here when we pass these values first we have to pass the no the number of no BS and the number of yes BS in this case we vot for yes so we have one here yes so it might work let's try let's try how it goes yeah in this case
they are I'm running all the functions but yeah one of them is test B counter chain here this B cont chain it pass so it means all of this is working as we expected oh yeah that's great it's good after that we can move to the next example of the test we're going to test both in multiple times from different account yeah something similar we have to deploy our contract we're want to pass the name of the my contract we're going to get the contract address and then similar we have to check this we are starting from zero or not they might be the default values if it's okay then we we can vote so here now we have this
new helper function it's going to be both with color address this here the full definition so this time yeah we have to pass the contract address the contract of the deployed smart contract and the prank address and the value here the call prank address because you know it's part of the vocabulary they use of found so actually it means we can set up like a the owner of the address or the Coler the Coler of the of this function can be any any address so because we are testing we can set up the color of the function it's going to be this address
or another address so yeah that's why but on this context they call it like a br address and here let's see for example yeah both with color address so we have to pass the color address that's going to be this one so when we are in this function we have to start this star Frank star Frank is like a cheat code which in the T fundry fundry tool so it means uh we're going to set up the the color of this function is going to be this granted address for that we have to pass this contract address here on this format Che Target one and then here we should to put the contact address and
then our color address it's going to be the prank so from here the new color of this function is going to be this address and then dispatcher and then we vote with the value we pass so just like this if you want to check more about what is the star rank here here like they say they have the book we can just using Che codes they have some examples here they also talk about star PR so this we can trade various properties of the transaction yeah from this case St com it's going to CH the color address so it passes our validation yeah you can say this examples here also stop prank so yeah we're going to stop during so
that color is not going to be the color anymore they have more a lot of codes you can check that but let's see how to use it so remember this is is the our smart contract address this is the Coler address so now prank is useful because for this time for this function the color address is 101 for the next one the new color address is 102 and for this one the new color address is 103 so yeah that's that's how it work check here B for one we have two vs for one and three V for zero remember zero is no v no V is the first this V is uh later so we must have three no bats and two
two yes bats that's how our expected both counter values going to work so let's do it one more time and then yeah we have this T voting multiple times they voting multiple times has passed so all of this is working that's good lastly we have this testing tting T from same account yeah the idea is to always avoid that some address or some user that already have votes they cannot vote anymore so we also have to test this functionality so what do we how do we do here is again we have to deploy smart contract in this case we we just write
the prank here so remember we have to pass the contract address on this format and then this is going to be the address of our new color or prank it address prank it color so this going to be the address yeah we have to format into in order to receive this in order to the start PR it's going to work after that we going to we have to create our dispatcher but in this case it's going to be a safe save dispatcher you can check more also about save dispatcher here here save dispatcher testing yeah good so it means like we can use Save dispatcher when we try to do this when
we try to test something with panic here we you can see panic panic panic here so using this F dispatcher we can test that the function impact panics with expected message yeah so that's what that's what we try to accomplish that's why we use this time save dispatcher so save dispatcher is going to boat one for one and this time we have to unra well okay when we do this we have to match yeah match dispatcher so in this case remember it's already have vot one so now they try to vote for zero so what's going to happen so it might return so it
is okay it should Panic let say if it is error we have to assert here the Panic data Panic data at zero and then you have already B Panic data so at the end this is going to panic yeah because remember we already vote for one we cannot vote for zero so anyway it's going to panic so but here the match is handling uh all of this panic situation so it means our smart contract must might must work because we are handling this like a a panic case that's what it's working yeah maybe it might look a bit un complicated but
the the IDE is always to check now if it is okay it's not going to be okay yeah so it's not going to be okay if it is okay we should return this should Panic it's not why it's not going to be okay because we already bot so we cannot vote one more time but in case uh if it is okay we should Panic so in this case what's going to really happen is going to go error here when we do this after that it's going to come here to panic the error and we going to S this this Panic we're want to handle this situation so why the test pass yeah that's it that's it actually funding fund is a really great tool they are working very hard and I can see is
improve a lot a lot a lot so there is always something to check there that's it for testing actually I'm going to talk about the homework here the homework here is to implement this uh to implement the test for our ownable example remember our our ownable example from last from just few minutes ago we talk about ownable example we show this testing with hyro test but in this case we have to the H word is to add here we have to add here the St Foundry build two or three test I want to check later how many test so we're going to we have to build three test in our new file we
have to use a start found for this example so yeah that's going to be the H I'm going to show the details later so this is for testing after this and we're going to talk about different topic we're going to talk about components maybe you heard about components before this SE so yeah in a while let's continue with components okay okay like we're going to talk about components components A New Concept here at this on Smart contract here is the scar start 2.3 2.3 version like one month ago two months ago at least yeah between one or two months ago they introduced
components here on the smart contracts actually it is still qu new but I think uh it's worth it to talk about it so I seen from next year most of the smart contracts you're going to include these components on the implementation so it's going to be good for all of us to know more about components so what's the idea of all of this well now on on the Kyo book you can check the K is also good resource for learning about St and KY this you can check all of this by your own but I'm on this part because we already talk about smart contract this but we didn't talk about components so
what's what's components they say Lego like building blocks for smart so let's read quickly this developing contract Shing a common logic and storage can be painful and from and this logic can hardly be Ru it and needs to be Reed in this contract but if but what if there was a way to snap just the extra functionality you need inside your cont separating the core logic of the of your contract from the rest here components components provide that we are modular at own encapsulating reusable Logic storage EV that can be incorporated into multiple cont this can be used to extend contract functionality without having to
implement theic C over the day so you know in your whole project you can have many smart contract in one project you can have like six seven 10 smart contract now with components you can reuse some pieces maybe two or three smart contract share the same implementation components is going to help us to reuse this uh implementation within this contracts inside of our project so that's it they say think of component as Lego blogs they allow you to reach your contract by plugin in a modu that you or someone else this model can be implemented while component or more complex here token full FL a component is a separate module
so it means when we're doing the coding we have to create a new module for the component so that can contain storage B and functions so this we going to be inside our company storage Vents and function and like a contract itself a component cannot be declared or deployed this logic will be eventually part of the contracts by code it has been so yeah we're going to deploy theod contract but our contract is going to integrate the compon logic here one more time so a component is very similar the contract it contains storage variable event and Function One More Time component cannot be deployed on its own the component code becomes part of the contract so there uh they see example
similar to our ownable contract where you have to work with that but here I'm going to show my own ownable contract example see yeah we're going to use this Dy we're going to use this and then I have to open my other okay now the idea for now is we are going to refactor this our our ownable contract we talk a lot about it we implement it on this way but now we're going to refactor our smart contract in order to use component so how is going to work remember here we can we have two two interfaces there one is related with data and the other one
is related with uh the ownable the ownable tra so thinking about with component and smart contract it's easy to take to refactor this like a trade or interface like interface for a component not for the smart contract so it means our smart contract is going to handle this and our component is going to handle this so maybe we have more smart contract here all of them they can call the component for the ownable of the or the ownership logic so yeah that's the idea remember now for now for this example so that's the idea to move from this implementation to the component
implementation remember this is still the previous example it's not the new example but I just want to show you what what is our strategy since we already have two interfaces one of them is going to become part of the components this going to be this on our interface so remember our storage has the data yeah I mean have the fields for data and owner so in the new example this owner field is going to be part of the component not yeah part of the component and then we can call here inside the storage we're want to see later same every time we want to we want to call the owner we have to call the the storage of the
component and then we call the owner we going to see the difference later on this part same on the implementation this is going to be almost same because we're going to keep this because everything related to data this is going to be on the smart contract so this is going to be all same but for this part we're going to here especially in this part we're going to call different attributes labels more traes in order to to implement this on component right now it is still it's follow it still part of the contract module on the owner and yeah that's it that's it so let's start doing the refactoring I already work on this so I'm going to show you hope you
guys can can see it great in order to make it easy actually I'm not going to implement this for data just get data and set data I have this interface you see isan and then I also have my interface ownable is actually the same ownable ownable train the same confer ownership with owner owner same and here remember this is the previous example we have the contract but in this new example for this lesson we want to work with component so now first we have to define the component and the component is going to be what it's going to be this have to implement this so remember first we're going to we have to implement the component like they say
you have to create a module with the name of the component call it ownable component and you will have to add this attribute here component and then remember the component is going to have their its own storage so is their own storage and only it's going to contain the data related with the owner of the ownership this case is contract address for the owner good and then event about the transfer what to transfer the ownership same this is almost similar like we have here but in this case is inside the component let me show you here yeah you can see it's pretty the same but the different is now events this kind of event is inside the module
of component but before it was in the Contra inside the cont and now the implementation oh yeah I'm going to open for the implementation yeah we have these functions but here now this is something new we have to like invisible as ownable this case and then we have to call all of this it looks you know like a bit messy with a lot of code here you can see maybe it's easy to keep in the prev version but the actually components is a great feature although maybe in the future the stting can work in order to make it more fre more like developer
friendly for now it's a lot of code here in order to you know to implement the function for the components or in this case yeah we have to create our horable implementation we have to call the contract State also this is new has component you have to add the has component and the contract State here the implementation of what uh ownable interface that is this okay but we have to pass the component state in this case and the contract state so yeah a lot of code here but that's it after that yeah we can Implement our ownership and our re owner it may be similar here yeah
yeah something similar the transfer ownership only owner in this case I just create one more function transfer ownership but it's going to be almost same like this let's later and then owner or read owner is the same so here check so this this function is not on the interface here so it means it's like kind of internal function that's why I have to create I have to implement this here below to accomplish that again I have to create a generate f for internal function similar like in the previous example for the private methods it looks
more simple on this case but now we're talking about components in components remember you have to add all of these all of these piece of code and after that you can build your internal functions here we build the only owner it's almost say like this only owner but remember every time you have to call in this case a self component state is different from self contract State because now we are in the component context here so we call a snapshop of the component state with the contact State here that's it you can more here for the transfer ownership yes almost same we have to call our owner write the
new owner on the owner field need the event so yeah that's it so the idea is to get familiar with this style with this style of implementation for component this style components we cover almost everything here how to declare the interface of the the component is pretty the same like for contracts we have to create storage the events uh Implement their external function and the private function so that's it so let's continue here yeah after we defined our component we can build our smart contract so now our smart contract is you can see this is
smaller than this example because yeah we have created first our component different module so on this mod we have to now start working on the my contract itself we call the attribute start contract we create ownable contract ownable project from our ownable project is here is my scar one here and I have to call the ownable component so I have to import the module here because this a another module here ownable component to import the ownable component here and then yeah I have to add all of this is also new I have to set up to call this macro on this macro
I have to pass I have to pass the part of the the own of the component in this case it's ownable component like this I have to set up like storage so it means what's the name of what's going to be the name of the storage on the component inside our smart cont so it's going to be ownable so here ownable event ownable event here I have my on events and that's it and then I just have to call this the implementation yeah implementation of my ownable components and also implementation of the internal functions so yeah this case you just try to set up according to your
use case to make it work components inside your smart contct after that we have to build our storage in this case our smart contract the the logic the main Logic the functionality of smart contract is going to be more basic on data so the component is going to be in charge of the ownable part so that's why this data is is the field or our smart contract and then we have a super storage this Super Storage is going to be actually the storage of our component and we're want to call it ownable this like me here ownable
but what is this it's going to be the remember ownable component storage the storage or our ownable component so we are link we we have to link here we have to link our our component with our contract that's why it's like this okay after that we can create our oh yeah I have our ownable event here almost same we have to create event but it it it might be like the ownable event from the a comp component so that's why here we just call the event from ownable compon on this event here in our on our smart contract great in order to the Constructor remember the Constructor is
going to handle the storage but the storage has two Fields here uh one FS for data and one felds for the component component storage for example here we just Define contact State contract address good if I want to work with data I could s that dat right one but if I want to call with ownable it's going to be like this self ownable is this name and then I have to go to owner what is owner owner is the field inside the storage ownable compony let's find it here you see owner so if I want to access this owner first I have to access this the storage the ownable here that I'm on the storage of the
component and then I can access to this owner so that's why it's a bit longer now self ownable this is the field on the smart contract this is the field on the component uh I have I can write it now I can pass ini so it's different yeah like the Constructor we we have here so different you can check and for get data yeah it's almost same and for set data I have to do also that here in this case let's check side data self in this case self but I'm going to I have to access to the storage of the component that's what I WR ownable only owner before don't need to do that but now yeah we
have to access there and yeah that's it that's it uh let's check if it is working or not car build is working you also can play with this testing and deploy it at least we on one you cany it on testet you can use any of your agent or your Bravos wallet and play with it and check if it is really working or not I did it on my side but yeah it can be a homor for you as well let's think about I think that's that's all for components on this example after this we're want to talk a bit more components on ERC 20 RC to one examples you can
check the of sing examples um they use components that's why I'm I'm trying to do an introduction to components here now so let's check now the open sing examples with components orc2 on this part we're going to talk about contracts on kairo basically we're going to talk about rc20 rc21 so yeah this is for tokens and n nft you might know so we're going to check the examples of the reference we found it on like you might know open sink is like of the best thing if we talk about smart contract especially about defy or you know or related to tokens they are
really famous especially on the eum solidity Community but now they also have this kind of implementation like they say we have this implementation WR in kaido for internet so that's good for us so that's the idea to check this let's go to the this part to the Token so here they talk about erc20 let's see2 token standard is a appication for token a Time token where all the units are exactly equal to each other so yeah to2020 component here provides an approximation of EIP 20 in so what does it means EIP like you may know is like
theal Improvement proposals so for the the community they have a lot of e proposals to improve the blockchain itself so your years 20 was introduced 2015 so it's it has been a long time so basically they set up a list of functions that we must to implement or take it in order to order to use a token standard so yeah we have name function symbol transfer allowance and all of this good but this is more for theum so like they say now on we can find the implementation in Kyo
for start so like they say it's just approximation of this but of the E20 but this is still quite good so let's check the interface can see the following interface represent the full API of the contract component the interface includes the the interface2 standard as well increase low on yeah for all Tok mention in dual interfaces dual interfaces it means like yeah it can support Tel case and snake case we call some functions like this one or this one so you can see for this year2 AB we have to implement like a trade it has all of these total supplies balance transfer
name this is for the itself this is for the metadata name symbol decimals for the safe allowance increase and decrease this is for the HL case they have this total Supply but they have also this SN case so both implementation the same just you know the name of the function is slightly different because of snake and come face same here yeah here here they also talk about all those starnet is not ebm compatible yes stet is not ebm so that why we have you know the stet ecosystem with different language like Kyo this component a to be as close possible to the2 standard so it's
component so it means remember before we talk about component so now uh when we are going to work with rc2 it's going to be a component here2 component here component so in this case is not going to be a it's not going to be a smart contract it's going to be a component so how we can use it in this case yeah they show this using contract for Kyo constructing contract require setting up the Constructor and inating the toing implementation this is how it looks like so we have to create our smart contract with this attribute it can be called my okay so here we have to import the erc20 component so we don't have to build by
ourself because open sing provides this rc20 component so we just have to import and then we have to Define like before we have to define the component macro the part is going to be this your component storage is going to be the erc20 storage on the component event is going to be the20 event on the company that's good and then we have to get the implementation for this implementation implementation coming only internal then in this case for this SM contract this is the storage and the storage only has fi from the component that's why we only got this one the year 20 component
storage so in this case is going to be the same the storage of the smart contract and the storage the component for event yeah we have we can declare like this but is going to be for erc20 component erc20 component and yeah easily we can get this the Constructor we have to pass the contract State initial Supply in order to meet our token contract receipt order to meet as well and then we have to initialize our token we have to pass the name of the symbol and yeah that's is and then you can customize according to your own use case that's pretty much the check on that did have
they also have like the instructions we have to find the in this case may the KY contracts here they explain about it so we can follow this instruction in order to to create our project so yeah we just have to add the dependency here like before create use this create my token yeah also for development yeah we can do the scard build and get that the target file and also we can run C so let's do the second one since the first one is quite simple so actually we going to get it into this we can say your component we going to check that we can check that we
have to do this G clone go into the files and build it and then we can do the testing so let's try this so here see oh yeah because have to pass the in my case to press like this yeah after that we have to do CD contracts contracts let's open it first let's see what's what is there inside okay we we are there we have to check here especially components and presets do most important folders for us in our case so like remember in our example here they just import this
erc20 component so we can check what is the what is inside here yeah because now we just import we don't know too much about it but now uh since we have access to the opening repository we can check that SO2 we're going to search for this component might be here yeah we found it so here we can check more about what is inside the erc20 component you know in the storage they have to define the name symbol so Supply balance and allowance see for balance they have this they're going to map to a contract address with um
u56 value and for legacy map they use this kind of tole for allow you know in allowance we have to set up how much money the owner allow to the sender to spend it so is it and they have this event we we Define the event here for transfers and approval we Define the transfer event then the approval event we have the errors here and this is a bit more interesting we have the total Supply but you can see here they use edable as year sequence implementation so they are implementing the functions for the components write the code like this have to pass a contact state that
has components contate here of the of this interface so we're implementing this interface let check here your interface here interface might be here so here they just have they only have the interface there is no implementation so here we implement it we just import the interface from there yeah import from there and then we can implement it with total Supply balance of L so it's pretty the same like U for solidity from approve same for the metadata the name symbol decimal yeah that's it and then it's going to repeat but in the C case
but implementation is going to be same and yeah that's it for the year20 we also have the presets here presets we're going to check here what are the what does it means presets here have presets so it means Le of ready to deploy pet availabl and their corresponding class here you can take all of this class hash in order to to work with any of this token these two token or accounts so that's it we're going to check here present what do we have yes 20% 20% offer basically functionality and provides a fixed Supply mechanism so it depends most sometimes it's fixed
Supply sometimes it can be dynamic that's here they say fix supply for distribution the fix Supply is set in the Constructor okay right remember here they are yes they just import the erc20 component it's pretty same like a example on the J have Define the event find the storage fixed fixed Supply on the Constructor this same value for is going to be minted with this internal mint you can see this underscore so it means it's like a internal means it's not exposed it's not public or external we can call it just that's it we just
call it inside of our smartphone and yeah this is this could be your template if you want to start working with erc20 just go to the presets and you can customize it according to your use case so the same happens for ERC 20 ERC 71 in this case we have to import rc2 component and also this rc5 component this is out of the scope but Che it later so most of the time we use component all of this implementation metadata C only metad with C only since we still we still have to handle this both cases C case that case so we have
to implement all of this but actually we are just importing from the component itself same for your api5 this case we have to Super Storage two components one component for the S21 and one component for rc5 same here our events our Constructor we're going to mean the asset in this case we have Beau if you have worked with nft before you know hand so say the token tokening token ID okay and then yeah we have this me assets it's like internal function private this is implementation for this
they use more internal functions and yeah that's it what is interesting here we have this test for example how do they test this all of this code so we can run scar test like I mentioned before they are going to test all of these modules but remember they use C 2.3 Let's see we have to run SC test but in the overview we can see they use this version of the C so is the same version same version as you might have in your in your own local machine otherwise try to down downgrade this version because right now is 2.4 the latest version and
oh we got all of these test pass there is a lot of test but we don't need it all of them we can just set up some test for us example we don't need we don't need this maybe we need we don't need this we don't need this we don't need this I think we need to count let's see one more time test but it might be less test the number of test might be smaller than before because we just want to test some some functionality not all of them okay that's good the most important ones are oh token justest the token and the sets
but it also includes the U Mo and account let's try account is maybe we don't need it if we got the error we have to change it yeah because of the account yeah yeah we cannot read the account so yeah one more time okay we got all of these kind of different test we're going to check some of them like 400 still too much like I said we have to go to this file test we can check the Press sets you can check this the2 how uh are do they do the testing so in this case this is just the helper functions let start here but going to be important this two so first uh this Cas they set
up the dispatcher but remember uh we just been talking about test before in this case uh they don't use you see they don't use the stet foundary they just use the chyo testing so it's going to be a bit different from what we saw before still fine it's working so no problem so this is how they work here and they have this set up dispatcher so what's we going to do is we Bend in this case with u when when we check up the dispatcher we have to create a call data with all the input parameters we have the name the symbol the supply and the owner all of this is the constant constant values they are here in constant so we have to
go to U then access electors open test oh they are inside of test so they might be here there you from here so this value is going to be the same along across all the test that's why they set up a C file you can see yeah this main value that's going to be name same symbol Supply so which one is you can change here according to your use case so no problem that after we got values for call data we call deploy so it's going to yeah
it's going to deploy our smart contract so we can also check what's here inside of deploy from utils and utils might be from test so we can check here utils let deploy what is this let me here deploy it's here oh yeah it's here let's it here okay remember they they are calling this deploy so it's almost say like so you might remember this deploy is called we we saw it before so actually they are like a wrapping this
emplo is called they just have to pass the contract clashes and the call data since the Sal is going to be almost same zero this FL is going to be false we can set up this helper function is going to help a lot yeah we just pass the clash cash that is a fixed value as well and the call data and then we deploy and we get our address of our smart contract deployed address and we create a dispatcher and we return the dispatcher like this that's why they do it here when they test the Constructor is here and then they are going to check if everything here you're want to check is
yeah if name is the same like this name here symbol own all of them yeah at the end this is only for for the test Constructor but they have for everything for the total supply for the balance or the test alow yeah you can see there are a lot of testing so yeah uh like like I said you can play with them and you can take they like a reference when you want to build your you know your own project and you want to add ERC 20 or ERC 721 implementation you can just call the the presets here and you can build your project and if you want to know more
about the details of this just check the test here and also check the implementation of the token can show that implementation of the token here they have also implementation yeah is a lot of code but yeah you see that's that's it so that's that's about actually the rc20 and RC71 implementation just try to play with this but remember yeah this is like the template the present but remember always so you have to add this like open accept dependency start F and you have to set up the the version of this yeah yeah
like I mentioned before we today we talk about component and testing so yeah this is the best example for component Plus testing because like like we mention erc20 erc20 implementation open simp they use component so yeah it's a great example to check to analyze and to see how it works the idea you like just try to take try to use the preset and work in your own project okay that's all for this home see you next time