New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

ETHKL Smart Contracts 101 Workshop

Ethereum MalaysiaSat, Oct 9, 2021, 12:00 AM

An introduction to smart contracts useful to anyone who has: - coding proficiency in any language - basic knowledge of Ethereum - understanding of how to operate Ethereum wallets such as Metamask - interacted with DeFi platforms as a user Join the Ethereum Malaysia meetup group to join similar events in future: https://www.meetup.com/ETHMALAYSIA!

Transcript

right very diverse group which is nice um it's just so you guys know what i actually a lot of times i encounter some of the some of the best programmers in the space they are their background is actually not computer science per se they will be coming from very different part of the world like for example even engineering mechanical engineering as well a lot of them are artists as well uh maybe i can introduce myself my my name is caleb i'm with eaterscan right now i do funky things i do some contract stuff i write some of the back-end stuff as well occasionally i do try to hack some contracts but that's not for profit for uh it's mostly for fun things like that for smart contract i'll try to guide you guys through some of the more entry-level stuff let's try to see where we can get with that and after that we can see if anybody has any more in-depth question then you guys you guys can ask then from there we can see how to move on there could be a subsequent session i'm not sure where we are off into more in-depth details or quirks of the evm for example so is everyone in here already one two three four five six i think we have enough to start now and i just asked for the others who haven't introduced themselves just do it by chat so that uh caleb we can start already i think it's harry and r who hasn't introduced themselves yet yeah oh and there's one more geron who is i think not in malaysia um cool yeah so for can you guys introduce yourself as well just to like help caleb um like curate the class better by the chat it's fine hey harry uh i should specify uh maybe like some of your programming background i think i'm here just um right so so this is how i usually start what i would recommend for people to start using is to just just to start with using remix but before we start into that uh today what we will be doing is we will try to deploy a few contracts through one of the dairy and test nets of wingerby and we have from there we'll start interacting with those contracts to see how does it look and feel like some of the things which are bring you through as well is some of the specifics to blockchain uh things like events why we use events things like how storage is being managed how do we call certain things on blockchain and stuff like that everything will be based on solidity and i won't touch any details on libraries for now so you would be able to see even devtooling i won't touch it for now so you'll be able to see how it works under the hood uh once you guys are used to say deploying contracts through remix then you can start looking into integrating certain development tools for example truffle or hard hat into your development schedule that could even involve things like open supplement and garnish but that's for later for much later for now can you guys do me a favor do you already have an address on ethereum if you do can you paste me your address on the chat box uh i will drop you guys some ring winkabi if so you guys can start off or if you know how to get it from a foster you can go ahead and give a ticket as well but otherwise if you don't have any wrinkly if just drop your address i actually asked all of them to uh send rikubi so they should have a little bit at least everybody gonna if don't have to spin me up i got quite a bit so i can just distribute to you guys otherwise if everybody already has it i'm just gonna jump straight right screen sharing [Music] i won't be able to see your chat box so let's go here yeah so if you guys can go just connect to remix.etherium.hawk one of the first things you need to do is to load the solidity plugin right here so you won't be able to get this panel if you guys have anything which is stopping you guys just raise your hands up any time at all right now you would be able to see some of the radially made contracts but let's discard them for now so for solidity usually we would start with something right here uh pragma solidity this indicates the version of solidity which you'll be using so if you can see there's quite a bunch right here different solidity versions would have different features the later ones will have better abstraction for example more bug fixes some of the earlier ones would have less restrictive controls i personally quite like to use version 4 and version 5 still but i should start making version 6 7 more of a day-to-day build anyhow so yeah to start programming in solidity basically what you have to do is first to define the primary solidity this one this basically what this tells the compiler is that i wouldn't be able to compile this contract using any versions from version six onwards so if for example i use version 6.12 that will compound as well so basically anything from version 6 onwards um not major versions so for example if i switch to version seven you'll start complaining so we would first define a contract that's basically a contract name and here i will just start with a very simple example where we would store a string so this becomes our variable and if we have a string we would then need to define how do we set the string as well as how to get the data from a string there are things we can do right here so if we define a string to be as such it's actually in longhand it's actually private but of course if we do it as public uh there'll be a slight difference where you can directly access the you create getters it creates getters for the variable where you will be able to access it even if you don't explicitly create a function to call it but anyhow let's let's move on to setting the function store something so right here we want to store something into the string here we would get an error by the right yeah so this is one of the things which they introduced in later versions of solidity to make it more explicit for people who are calling on it in older versions you don't need to specify it in newer versions for certain data types you need to specify this so what it basically means is that for certain data types you can think of them as things like objects you where you would reference to them using a pointer you want to specify that it's a memory it's more of a syntax thing so if you for so basically what you just need to remember for structs for string types for byte types if you admit this you would see this you basically see this sort of complaint when you try to compile the contract so just remember you should put in a memory right there so this effective if we store something onto this particular variable and to get it since we have not defined a getter for this particular variable something you would want to do it as a public with a view returns as well as this being a string so what's that right here it's the same thing so since we're setting a pointer so we shouldn't do this i'm just gonna steal something from right here okay it's a warning so technically we can sort of fight ignore that right let's see yeah we can ignore the warning so if i have anyone have everyone gotten this far yet which one uh for example have have you managed to type in all this code into your id you have managed to get it yep yeah if you compile it do you and most just see a warning for example and this one i'll explain a bit what this means but if you are just coding for fun right now you don't really need to bother with it so but so far any any questions i think it's quite straightforward it's basically you're storing a variable you're getting yeah you guys all good i'm gonna move on right so in remix there's this nice thing called the javascript vm it's it's a duplicate of the evm it's embedded into the remix id as uh some is something like the evm is implemented in javascript when it runs natively you are given some amount of if to start with uh all the interactions can be done through the console right here so if for example if right here if you were to click deploy it's not deployed into the blockchain but it just deploying the test right here so you would be able to already start interacting with the contract so if you click on get something since we have not assigned anything to begin with we would not see anything but however if we were to type in something we'll have a string you would see there is a transaction which has been done right here there will be a hash and everything you can see the input data it's a bit less clear right here but i will show you through the scan how they look like so at this point of time if you were to retrieve the string then you would be able to see what's the data on the line right is it clear so far i am open to any questions from here because from here we will move on to actually deploying on inquiry caleb nicholas asked what's the difference between all three environments okay so javascript vm it's a native vm routine within the remix id itself it's something like the test something like garnet is basically a test environment injected web 3 it's what you have inject right here so metamask i have injected rinkabi test network so that's where it will connect to i'll show you in a bit how do you connect to it and how do you start deploying on scan web3 provider it connects to an endpoint so you can define it as a local node which you are currently hosting yourself you can use infura otherwise for example it's basically your own hosted nota so what happens is remix will connect to your own localhost you can deploy contracts using all nodes alternatively you could also connect matamas through your own node by adding your own rpc endpoint as well but that's basically a difference of all three of them any other questions i don't believe so okay cool cool i mean if anyone else has questions i'm not sure if you guys are allowed to unmute yourself i can just pop by and ask her uh right so this is quite boring to look at so sorry i just i i just allowed people to admit i didn't realize that was one so you should be able to meet yourself anytime so just unmute yourself just stop me or something like that no just ask me whatever you want to ask right so let's try deploying this actually on ring kirby hi harry so for metamask that's the underscore oh what do you mean underscore a this one here we go uh it's an input variable so right here what we are defining is that store something is a function we can pass in a variable so that it would eventually be stored into i have a string so this one is something like just a just a definer for an argument for us to pass in cool right so metamask make sure you have it pointed to wrinkle b right here so if i'm doing this correctly if i click on deploy it should talk meta mask so i'm i hope most of us we know what all these things are guest price well guest price have been expensive recently guess limit you guys know what gas limit is right then i mean oh before that let me go through one thing which i think is important to share optimization uh it's not so much important right now if you're starting to learn but eventually when you start building more things on it then you start realizing there's actually a lot of restrictions when you build on blockchains each one each contract address is allowed to have up to 24 kb worth of byte code now that's not a lot um so if you need if you like say yeah just just about roughly over 24 kb you can try optimization uh optimization will help you reduce some size of your white code however it doesn't mean that the higher number the higher the optimization count it the number right here what it means is that how many times you expect people to interact with your contract so if it's like for example a one-time deployment and you don't expect a lot of people to ping your contract all the time you can set it to zero it's if i'm not mistaken zero it's actually much smaller more times less than you can set for a higher higher amount number higher number higher number of times so if you see things like if i don't know mistaken unity stock has quite a high number of optimization count so what that does is basically tells the contract that i should optimize it so whenever people call the contract it should be cheaper for people to call the contract instead of me deploying my deployment will be bigger but it will be cheaper for people to interact with it so just keep in mind you can set it to defaults for now but eventually when you start developing more complex contracts you'll come into problems with the buy code being being too big uh doing that timing experiment around using optimization or you can start using things like libraries and stuff like that but anyhow let's move on let's deploy this on rinkaby [Music] there we go so what we would want to do is to verify and publish it so we can get that nice little feature where we can interact with the contract directly on the scan so single file the difference between them three if as long as you are developing on the rule of development you'll realize that there are few ways where the compiler will be able to read files right now it's right now the easiest way is of course to code everything for example a notepad or something and everything gets a it's a single page but you could also deploy it on multiple pages you can code in multiple pages and after that you can do something like you can import certain files into your contract and do stuff like that i won't go there yet but if you start experimenting with more depth tooling you'll start abstracting a lot of those things from you but it will tell you also you need to import certain files in order to connect to certain features but for now we are fine with using single file we have to select the version we are using so as earlier on remix let's see what you were compiling here in version 6.12 open source license any open source developers here so if there are open source developers now i think you might be quite familiar with some of the licenses right here yeah for example mit is a very permissive license some of it it's more copy left stuff like that just this one has nothing to do with the the verification process by itself by small towards uh on either scan whether you can do certain filtering or you can or from or from a legal perspective whether you can copy certain bunch of the codes and stuff like that before you get into trouble uh caleb yes if i accidentally rejected the deployment on my wallet do i need to do anything because when i try to deploy it says uh the transaction field afterwards oh okay if you do see that uh try to go to your transaction and see what's the problem is it an out of gas problem or is it because like when you compile you can compile the code right yes so if you can compile the code double check to see if you're sending enough gas uh maybe you can share your well this is great you can share your transaction hash right here let's take a look to see what's the issue where is my skype i don't think i have the translation hash again um i'm currently stuck right here it's all right um if you don't have it you cannot say hmm that could look something right so that looks all right if you try to deploy let's try the point oh um injector but i'm not seeing a web 3 on the drop down so you might want to can you try refreshing the page to see okay because when i clicked deploy the first time there was a pop-up of my metamask but i rejected it and then i tried again but then it just didn't pop up again yeah let's write one oh can you go to mcmaster take a quick look there you go okay all right oh you have to give it permission to access okay cool all right okay there we go uh try to deploy and see i think i have free for it right okay cool all right okay thanks sure let's go back yeah so these are some of the things which you'll see now if for example your transaction gets uh if you can't prompt metamask for some reason if the see whether metamask is properly connected and injecting right through other things like for example if your transaction runs out of gas then you need to bump up your gas sometimes it's just weird that guess estimation is wrong but who knows what's actually happening in the background yeah if you see that's wrong then you need to start doing some some troubleshooting let's see um right so i am here simple simple runs is the same so nope i'm not a robot but there are no bicycles right here ah let me true very fine publish [Music] one question there what does the verify actually do right is everybody and has everybody managed to get to this step so far or forget my verification help also verify your contracts because etherscan has this feature where if there is contracts which are similar which is an exact match with one another then you would see it being verified as well so as long as i see my transaction is done in um wrinkle by in it the scan means it's um it's done right yeah it's deployed so if you see something like this uh maybe i can describe the transaction right here what you should see is it would say this contract is being created there's a bunch of weird bytecodes right here if you see something of this nature it means it is creating a contract if you click on it you should see it go into a link where it's displaying a contract address you'll see a contract time right here i'm pausing for like 30 seconds so if anybody has any issues or questions can ask them so uh i have a question so this verify and publish contract so it's good is it something that's necessary to be done okay um so the interesting thing is this um it's not necessary to be done per se you can leave it say for example if you're not doing it this is how the contract would look like right i'm sure i have an example which i did not verify in the past yeah there you go it will look like this bunch of gibberish well if we are developing something for the open community typically we would want to verify it so others from the community would be able to see what the code is actually doing because by codes is not exactly the most intuitive to read it's optional of course for example if you claim to have some proprietary code which you don't really want to open to the world you can leave it as bi-code that's perfectly fine as well but otherwise if you're developing for the open community then the suggestion is always to publish and verify your code somewhere at the very least in a way where people can reproduce it can read it and see and understand what's happening in the background i mean at worst what you can do is you can publish in github and tell people yeah this is the link people can read it up they can try to recreate the right codes by codes themselves otherwise it's definitely a much better practice to publish and modify only the scan and so my follow-up question would be if i need to copy and paste my code into this verification should i put comments as well saying what each function is doing each line you could you could you could do that yeah put the comments is always nice so that you can remind yourself what the contract is actually doing yeah you can do that comments basically you can do it as a block command like this or you can do it like double double double backslash you can do that so here in this case it also includes the the actual source code or is it like a decompiled version um so this would be an actual source code if you want to if if it's the compound it will look quite different because it is the compound usually will be compiling back to a certain sort of like stackish language uh it would not be one to one with the with the with the solidity language which we are putting in because uh when it's translated to the to evm byte codes it's fairly different i would say it's more like you're dealing with a bunch of memory then you're specifying what would you be doing with these bunch of memories um interestingly let's see if this still works uh i tried putting api uh game in the verify published contract full school see all right um could you show your screen uh okay try graphic uh yeah [Music] let me open up that ah okay um so right here i will get to construct arguments in a bit but if you go down to your construct arguments uh you should remove this uh actually yeah i can see why you put it in um abi encoder it actually means something else but for now your contract don't have abi that it doesn't have that constructor arguments yet so you can remove that so basically the constructor arguments can remove it i will explain a bit what construct arguments are um yeah i will show it to you guys so just clear off the constructor arguments the few um yeah you should clear off the constructor arguments viewer i'll explain in a bit more quite a few does uh later i will show you guys how to deploy contracts with pre-defined values that means during deploy time you can set certain arguments that's what we call construct arguments during that time you need to use it but typically eater scan would be would be able to approximate for most cases but if you need to then you can put in the values itself as well but the construct argument for this case should be empty yeah so to answer your rune earlier if it comes to interpreting the bytecode themselves it's actually much lower level so it looks it looks really weird actually you need it's more of interpreting the stack the memory the operations done on the stack and stuff like that so solidity is actually considered fairly abstract lady but that's that's the best we can get to like if we were to decompile the buy codes that's why it's always recommended that just publish the sorry solidity code so people can actually understand understand on a higher level what's happening right there right should i move on so once you have this on ether scan pretty much you can start interacting with the contract as well of course get something will be empty because we did not write anything to it so if i write something there we go so during this time you wouldn't be able to likewise read the variable let me try something [Music] okay now i'm grieving to myself about this because i did something i did some changes for this which i forwarded myself but anyhow yeah i did some code changes for the right contract page right here but anyhow this is how you can see you know so what you have effectively done is that you have deployed a contract on winkerby testnet you can actually simulate the same steps on livenet mainnet uh it's basically a method of just changing the drop down right here changing it to mainnet and it will actually cost you money uh how much depends on the network guest price at the moment um probably for something like this is probably 20 30 bucks us dollar if you're not mistaken whereabouts you can interact with the contract you don't necessarily need to develop your own app you could develop your own dm uh but usually people want people use this as a fast way to test their contracts to be as a fallback for the community to be able to interact with the country in case of their own website going down and you basically get the full suite of everything you can read you can write you can investigate and see what's a code up to yeah so has everyone managed to get this far so far yeah if you have managed to get yourself so far this far right now it's i would like to congratulate everyone because you have technically successfully deployed a contract on ethereum basically that's it uh that's why developing on ethereum is nice in a sense you don't need to over complicate yourself with setting up and stuff like that just go on remix start typing typing out code then you just deploy it it's more of a you can already do something with it right today so what we are doing right here it's a very simple example but imagine if you can expand it and start doing more things for example you start storing more variables or you can specify a web page where if certain variables are set it would display something for example so that's where from it's it's from there onwards where you can start getting creative and start doing quite a lot of things on but before we move to that um let's this one is it's extremely basic and in fact if you have done any form of programming before this is very basic stuff you have variables your functions you have argument parameters and stuff like that um there are a lot more things which are very quirk or very niche related to ethereum itself one of the examples yeah it's called events i'm not sure if you guys have seen events before um for example if you were to transfer erc20 tokens all right if you were to transfer the rc 20 tokens if you were to look at its code essentially it's decrementing from one side and incrementing on the other side so it's minus on one side plus on the other side that's it but then again you wouldn't be able to see certain things happening on it so for example on the logs you'll be able to see transfer the transfer event and stuff like that yeah sure go ahead uh i have a question so just now i write the contract so the first time i write i type in if i write something and then i can see when i read the contract that uh get something uh that message so the next time i write another contract if i write something two right so the letters get something is if i write something too is it possible for me to know uh the previous uh get something good question uh there are ways to do it the most efficient way is what i'm going to introduce in 15 seconds so i was touching on events earlier so for example if you have something happening in the past how would you be able to find out about it in blockchain terms it's actually not easy it's only easy because it scan does everything for you but if for example you want to do something like you want to know if and you want to know from a single address perspective what are all the transactions which have been sent by this address what on the back end you actually need to do is to index every single block from genesis read to see whether this address has sent any transaction in this block and you store it to a database somewhere that's how complicated it gets and imagine if you have like a medium blocks to go through you have to look through them each one by one you have to pass through the data you have to look it up and search for that particular address then you store into database it would take hours and hours of indexing time that's what we usually call it so when it comes to wanting to find something which has happened in the past very efficiently we typically do it in contract terms we typically do it in terms of events so how does an event look like so for example the earlier shuha i mentioned that if for example i would put i have write something to how we know that there is an earlier information which we have entered using events you will be able to pass it quite quickly but it will come more to a time where if you are doing things like uh displaying certain data or if you are doing some sort of search engine search engine through the blockchain to pass the data it's more relevant to you but if you're coming from a perspective of an end user what you could do is to for example look through certain events which have happened in the person so i don't think i have a good example right here but if you look at the token of course i don't have a token right here so essentially what all the transactions which are happening if you go from the perspective of an account you see all the transfers is essentially some sort of event which is being admitted onto the blockchain so if you look at erc20 tokens these are essentially all events so you can see past transfers or what are the past things which has been done if you need to look up for specific data then you need to customize things a bit erc20 is defined as a standard so it's easy to retrieve data like for example if you need to look for certain things uh you will need to customize a certain event you need to filter it it's a lot more efficient to retrieve because events they use a certain primitive bloom filters you can go through the blocks a lot more fast than having to pass each block individually manually so how how for the for the purpose of this uh i'll just start by showing you guys how to code an event first say for example changed change string string something should be like that so what we have done here is that we have specified something like a variable we have specified that an event will be emitted but not even there yet let's specify that there is an event which we can admit changed every time where we have stored something right right so if i were to deploy and if i want to go through the same steps so when this happens are we rewriting a new contract yes you are writing a new contract if we have time later i might introduce you guys on ways of how to use uh upgradable contracts but that one is a lot more technical so let me go through the primitives of what we have right here first now so you know how to deploy a contract second step is if you want to index certain information if you want to present certain information from these contracts you can use events to show that certain events are triggered certain changes are done so let's do the first one let's see how this looks like this is number one so if you have an event in your contract you'll be able to see this other under the transaction if you look at logs you wouldn't be able to see what that particular transaction has done so it's a more explicit way of telling people that certain change and connection it's also a more explicit and it's a more efficient way for developers to be able to retrieve from the blockchain what exactly has happened especially when you start developing more complex applications when you need to retrieve data events will be able to help you quite a bit so this is number one so what happens if i if i update this okay we're waiting harry asked if we can build an erc20 contract we could um if but even erc20 contracts um okay so the thing is i typically like to start with something a lot simpler so it's by understanding where you're coming from here because uh your background yeah you are you are a developer so it's much easier for you to catch up um yeah but uh it it's definitely something which i like to do come out from small chunks and after that you'll start working on more complex examples especially for example erc20 contracts i would need to start explaining things about like the what was the eip20 what other standard standards defined the reason why we use return boolean in the end of the transfer stuff like that um we could i mean we can set up another session to work on going through some of the more complex details as well especially from a yeah yeah c20 perspective but i guess not now we can start with something more basic we can put up something more complicated eventually but it's a good suggestion i probably should come up with some course materials which it surrounds tokens instead that could probably help quite a bit uh hi caleb yeah yeah uh i was just suggesting like but because erc20 maybe will cover a little bit of the common details and maybe a little bit of the what actually the token is the smart contract actually can do so i just suggested it's up to you whether you want to it's a nice suggestion i yeah i shouldn't explain the small small detail and more complex details so that everybody know what actually smart can track can do what actually cool cool yeah that's why it's just you have done smart contract stuff before haven't you i done but yeah but it's just sometimes you're out of touch then you just want to know right cool uh yeah i mean on the subsequent classes maybe i can see you again we can discuss further on how to structure the uh some of the courses as well okay again thanks right so for events um the second transaction right here so if you compare the log from one side to the other side you will be able to see basically each transaction how does it affect the state of the contractor so that's where that's where it comes in um if you want to know at which point each transaction when interacting with a contract does certain changes you will be able to see uh what has been done uh from uh from a passing perspective is a lot more efficient as well you don't need to look through every single block and stuff like that so are we covered with events so far any questions so so most of the time these events you will use them uh when when you are um [Music] watching for new blogs or or is it like uh something that's that is directly usable with it within the other scan as well yeah so events are very much used in interest and yes um you would typically raise an event and you want to for example index a certain information onto the blockchain so while the while the blockchain stores information which basically continually appends information onto the database you will want to trigger certain important points so for example you will start using events when you want to track changes exp changes which you want to take note of so for example for you could be when certain data points have been updated for example for other people for example we would be editing certain events to tell people that certain transactions have happened certain transfers can happen uh it depends on the contract we share building events to have a course guest cost overhead so you need to be careful with that as well but otherwise it really depends on what you're looking to sort of like throw out there to make it easier for you to know what's actually happening okay sorry taylor so can i say events is more like a marker for like the points of those that you think is uh relevant data that you want to retrieve later yep that's a very good way of putting it uh example is it something that is like um something you see in like uh owner for nfp that it keeps changing and is that how they use events to to track that yup let me quickly do up the nft specs for a bit so if on an nft if i create an event which says owner changed then yes you will be able to see that the owner has changed if i want mistaken that is indeed a specification for this uh yes owner change you can admit an event it makes it much easier so for example if you were to pass through the data on the blockchain you would be able to look for certain things like event uh you can specify a filter saying that you want to look for the events starting with the function saying that it's owner change for example so you wouldn't be then able to very quickly return the list of owners which have changed throughout the time so all right thank you that's right caleb i got one two questions uh yeah sure regarding events uh are there different kind of events or only one type of event and the second question whether it will trigger once or we can trigger any time means after we triggered we wanna trigger the same event how it will work can we work okay cool interesting question um right uh events basically that there is only i would say one type of event in the sense where this primitive the event primitive itself is a type by itself within events there are different ways to do things the easiest way is just to admit something right here you could that is another keyword called index index when you start doing things like filtering through the events you can specify that there are certain keywords you want to index for example if you have done database related work uh you will have an index key which makes searching a lot more efficient and can filter specifically on certain values as well that's the different sort of like ways you can use event as for how many events you want to trigger you can trigger as many as you like typically for developers for smart contract developers we will trigger event very typically once in a function because we want to show people that this function has happened uh sometimes you might trigger it twice but i very rarely see that in fact most of the time is that when you do a sequence of action you would probably trigger it like two times or three times but that's because you are triggering like 10 different functions at one time so easiest way to see this let's look at unison so you can see the list of events right here um there are so many events for this reason being because unison essentially is actually interacting with multiple different type of contracts at one time so not just transfer it has to it has to trigger on the other erc20 token and approval to be able to allow it to do the transfer uh it has to do some sort of sync as well on the if i'm not mistaken this should be on the unison contract itself yep yep yes to sync and update the balance on because it's using some sort of bonding curve you have to sing and update the balances on both the results so you can call an event multiple times uh i would personally not do it too many times only at the areas where i need to keep tab of certain things happening but essentially it's up to developer the developer can just add as much as they like uh caleb my question uh was whether i can i know trade mostly the event will trigger once because in the function you are doing a lot of things trigger once the function you have completed your work so my question was whether what that event is trigger that we want to check that you went somewhere it's stored or you want to read that event again can we read that event again or this we just can trigger once only and it can be read once only or you can yeah you can read it multiple times basically it's stored in blockchain so for example i don't use web3.

js that much but you can look up uh when you come to developing the apps already and you are starting to use libraries you can start looking for event filters okay mostly use scenario filters you can use to really win so you can pass it through the transaction receipt itself or you can set up certain things like uh material has something called processing so you can process the blockchain by itself and after that you can set certain filters to be a bit high but essentially you can call it multiple times it's fine you can read it as many times as you like because it's stored in blockchain forever it is it's not uh is any gas involved in reading the event and will read the event no no uh okay i'll just give you a straight answer because i'm caveating quite a lot of things at the back of my head uh to read the events now okay fine okay uh my question when i change the code right and then i redeploy the contract uh it creates a new contact address i just want to confirm that uh basically a developer cannot simply change the contract and using the same address is that true yes we ask the risk always okay what is that okay there is a certain deployment pattern which we use in on ethereum is called the upgradable contract uh or proxy contracts are so what we can do is that we have one database contract and another contract which is an implementation contract we typically don't use the term database contract we use certain proxy contracts so what happens is that the proxy contract itself would store all the information whereas the implementation defines what you can do on the proxy contract so during that time what you could if it effectively do is on the proxy contract you can say i want to update my implementation so what users would see they are still interacting with the same contract but on the back end effectively they are using a different set of implementation and different set of rules that's one thing another thing it's a bit more that it's slowed down the primitive rules are on the evm there's something called create two now what create tool does is that it allows you to deploy contracts ahead of time in a way that you know what is the contract address which you will you will be deploying to so it's basically a very deterministic way of you deploying contracts the side effect to this is that you can self-disrupt the contract and you can redeploy on top of it now that's not necessarily good in a way because then if you know if you are deploying contracts using create2 it just means that you can sort of like silently change and rock out the implementation you can change it to something else entirely there are ways to get around it so if you were to interact with a contract on ethereum i would say this just be careful just just be aware of how the deployment is done stuff like that uh very typically people will just deploy contract ss some teams will prefer to use upgradeable contracts because if anything happens they can upgrade it and they can stop bad things from happening or they can alternatively improve on it but in most cases you should see contracts as they are once they are upgraded you will not be able to change it unless you explicitly use a certain pattern which allows one reading uh is this how governor stockton was because uh i'm not really sure about governance because from what i've heard if you let's say vote for something and the work is accepted then you can change the contract automatically this is how you do it okay so if you ask me there are i would do it in two ways there are basically two ways to do it one is that i can do the governance in a way such that if the vote passes i can change from pointing from abc contract to xyz contract that's one way of doing it another way to do it is that like you have mentioned i can do it so that i would upgrade i will trigger and upgrade on the implementation contract itself uh this one is very much team specific there's no real right or wrong i have my own thoughts about how to do it so but you can definitely use that sort of deployment pattern on governance uh during voting sessions or when you're using when you're deploying governance tokens or stuff like that awesome any other questions otherwise i would move on to construct arguments right so if no questions then let's move on using the same code base so uh yeah those people those of you who might have already noticed it every time we deploy a contract we have to trigger store something to be able to store a certain value right here but there are certain cases where you can deploy a contract and really pre-define what you want to store in it one of the ways is that you can specify in the contractor or you can pass it as an argument so how do we pull out the constructor arguments for this case you would need to use the constructor keyword it's always public for constructors it's a warning so one of the ways we can do it we can enter it through a constructor or we can do something like this right or we can do something like this [Music] so if we have a constructor right here what we are basically telling the contract is that during deploy time specify that we will be able to enter some sort of information when as we are deploying this contract within the transaction so if you can see right here uh if you have done this correctly you will be able to see that we can deploy with a certain bunch of strings right here so you can see something i will show you guys in a bit how this look how this actually looks like this one will be important actually so if you can see right here this is what we were looking at earlier constructed arguments is a bunch of bytecode which will append to the buy code as you are deploying the contract which essentially this is there you go it's essentially the string which we have attached together with the deployment time so if i want to just quickly verify and publish this [Music] uh one quick note you would very very typically see the constructed arguments to be appended to the bicone at the back right here this is important because eventually one of the days you will realize that you cannot work for your contract on meter scan very typically it's because the constructor arguments sort of like estimated wrongly we are not able to very confidently tell you what the constructor arguments are so you would need to sort of like extract it manually and tell us what the construct argument so in this case how would they look like when we read contract we would already see it as a default default string so since our constructor we did not define any events whatsoever there's no events which is attached but then again our string would already have a default default value right to it so if we right contract and we start updating it just like before we would be able to see the the read contract changing up again something will change to something else for example so is this straightforward so far any questions i feel if anybody has any problems getting up to this point nope then i'm going to move on so it so to record uh we have went through how to deploy a contract very basic one we have went through things like events we have went through things like constructor argument if we go through anything else we have went through okay so the next thing we should probably touch on is uh mapping a mapping is interesting so i'm kelly i have a question to ask sometimes i got confused when memory and putting the input in the function when i use memory and when should i use um storage okay so uh you would use memory if it's like for example for the instance of that particular transaction is just there for temporary so storage you would actually record it onto the blockchain but as memory memory you would sort of like just use it for that particular transaction and after that discard it that's the easiest way to think of it if you are using it for a function for say for example for a function as a function argument very typically you would use memory because it sort of like doesn't make sense if you are interacting with a function externally it doesn't try to retrieve any storage items instead for example if you are trying to retrieve something from way which is being stored on the blockchain then you should specify its storage so um i can probably do an example for this but not right away because that would probably i will start going into structs for example now let me think of an example which can give you later let us ping me on on ifkl or something uh then i can i will probably quote an example then you can see what someone differences just a question on constructor so my understanding is that you can start something uh when you deploy the contract right what is the example real real life example that we can use for the constructor that's a good question so for example i have created templates in the templates i don't want to say for example like hardcode anything i want to allow people to deploy their own contracts possibly through a factory contract so during that time i would then instead of trying to embed things in this fashion instead of embedding things in this fashion i would then put it into a constructor so when people deploy it say for example there's a factory which they can use to deploy their own erc20 tokens i would then allow them to be able to specify in the constructor as part of the input parameters what are the values they want to specify for their own erc20 tokens say for example they want different names or different decimal points they will be able to do it should argue arguments that's one of the ways to use it if you are building up five right swing both ways are fine straight uh so it is an advantage of using this instead of what you are writing right now thanks for coming i i missed you for a while uh okay so when you use constructor just now you pass some parameter during the deploy and then right now what you are doing is you have coded it inside the function so either way uh it's fine or is there any advantage uh one over the other depends depends on reusability so if you hard code it for example right here for the person who is deploying it i would need to change it right here i need to change that i have a string manually whereas for example if someone were to someone if you were to build something like this so if you want to do some define something like address a equals to new sample like this wouldn't compound because that's not a function should be example one i think address i need to try to recall how do i do the syntax from factory really but essentially the idea is that if you are using a factory then you will be able to define something like uh when you are deploying a factory you will be able to define something like your own stream or even some sort of constructor argument right here so when people were to deploy the factory they would be able to specify whatever they want by themselves actually let's do this so there is the factory a factory contract is a contract that you use to deploy another contract yes right that's correct uh okay i think i get the idea all right thanks i'm super curious what the i think i will check that out in a bit i can't remember the syntax i thought the syntax or something similar to this yeah so um you are here so far anything else you guys want to ask if not then i would just move on to mapping so uh uh have you guys heard of the race i think most of most of you guys would have heard of arrays so arrays basically you can sort of like specify but it's in a sequence where you can sort of like create a variable which is where the date storage you can expand the storage can start appending more values of the same type onto it um mapping is slightly different mapping in a sense it's more it's more specific to solidity in a sense so what happens is with mappings you would be able to specify a key value pair so if you specify say for example an index you will be able to retrieve the information for the particular index it's i have not exactly seen any other programming languages do this here um i've seen it on solidity so that's why i think it's worth sharing um so during we can we can do something like to define the mapping it's something like this so i would specify an index say for example you into 5 6 to a string and i would say [Music] ready for example so of course it's not going to have a lot because i'm just retrieved i mean i'll just remove either string right here so this is what you would do i think the syntax is like this we want to say for example in this case since it's a key value pair we would want to be able to add whatever we like so it's been two five six see instead so it's called a equals change it would also slightly change this as well okay something so in this case uh since we can start appending and we can start storing a lot more data in the mapping we will be able to also right here specify what is the information we want to retrieve so return map value i get everything right i think so well never know until we deploy so let's do that oh okay sorry i got a bit curious so i think i was missing this what's this okay we can try this later yeah i forgot to i forgot to i forgot to specify that factory is supposed to be an instance uh an instance of uh it's sort of an inheritance it's supposed to be an instance of example one we can try this later so let's do this [Music] so if you look at read and write contract at this point slightly different see get something would specify an index so right contract you would be able to specify an index as well as a string get something right here since i think we have defined a default string so you will be able to see a default string but if we start updating it we can choose not to overwrite what's the previous value we can assign it say for example each user you get a different value instead essentially mapping is what is being used under erc20 tokens so each person will be able to hold a certain amount each address essentially will be able to hold a certain amount of value under this practical contract so for example since we have already initialized initialized zero if we check one it will be empty so we will put something right here second string for example so the cool thing about mapping is that you get a huge amount of space to play with and cost wise it's a lot more efficient than using arrays the only problem is that it's unstructured so you would not be able to for example sequentially pass through it unless you are indexing it in a way where you know what is the index key say for example in this case since we are using 0 1 2 3 index so we wouldn't be able to pass it in way where we go from 0 1 to 3 but it's not the case for every time say for example in the case of implementation of the rc20 tokens the entry point they will use instead of using uin256 which is basically a numeric value what they would do is they would use an address instead so during that time it's unstructured you wouldn't be able to know who has actually been added to this unstructured data so unless you specifically know that an address exists otherwise it will return zero so back to one second string so my things are really cool uh i would say play around more with them see what you can do with it you'll get a huge amount of space to work with you can do things like you can map to strut so you can map to certain mappings as well so so far you guys managed to understand what mappings are yup uh just a question caleb can you remap existing values depends on how you want to do it yeah i i don't understand dismissive or anything actually it depends on how you want to call data so say for example if i don't want to allow people to remap i wouldn't write something here so store something i would do require that map value probably something like slot equals to this is a red contract example oh i cannot do it i have to probably use some sort of it oh sorry i'm using this should be this fine yeah string comparison doesn't work i have to cast it to a chart hash instead but anyway okay but uh meaning it's possible what you can do is something like uh you have to make sure that it's not something or you can identify that if the mapping already has something then you would not allow people to override it for example or you can even do certain things like that certain points in the registry where you don't allow people to pump in data as well as possible also [Music] so it just depends on your implementation i just want to know what is real life example uh you use this for okay real life example erc20 tokens so quick one you you would typically not use it for a string i just use it for a string because a lot of us we are very very familiar with what strings are easier to explain the way oh great nobody verifies their contract on rinkaby so real life example yes so this is an address this is a uint so remember earlier our mapping is there's always an entry point so this entry point would be your address with your balance so this is how you would use mapping this is basically an address where you allow another address to spend a certain amount so you can do mappings within my things uh monsters this is basically an address at what point you have already uh say for example deploy the transaction or something do certain actions so this is basically how the real-life example would look like every time you interact with erc20 tokens when you are transferring value in and out essentially what we are doing is a key pair value so your address and what the value is so the value could increment could decrement does it answer your question yeah yes harry uh yeah can we iterate uh all the mappings or is just one to one we need to pass every time index or whatever the i would say to iterate through mappings it's impossible uh the easy easy way to explain it is to say it's impossible because the thing is mappings you could say for example if we were to do it as an address right here it would be next to impossible to tell what are the addresses which are being stored within this mapping so mapping you get a huge amount of space say uh it's 256 beats 26 and 56 bits which you get to work with so it's a huge amount of space which you'll be hydrating through you can either kill the end of time and you won't be able to get every of every bunch of data if you want to be smarter about it yes you can do something interesting so you could define an index key so that's why that's why for example right here what i'm doing is that i'm defining and you in 256 to string so i could iterate through it up to the point say for example i don't see a string being returned then i can tell myself yeah this is the end of the line uh yeah i just wanted to know whether solidity provided any function where we can iterate all the ones yeah quick answer no okay it's just too big all right so do we still have any questions i actually want to quickly show you guys factory before we wrap this up inside able to get the syntax correct anyway so anybody else yeah maybe one question on the mapping also uh so uh you use a you inst 256 but can you also use a boolean or something to pull you into screen yes yep you can so boolean would be very much the same thing when you specify true what is the return value when you specify force what's the return value uh you could use other data types as well in it for example so yes yes you can i would um because we had one and a half hours i would actually encourage this play around with some of the it's a very simple example but the idea is that to get uh get you guys to like start playing around with it because it's so simple anybody can work on it can we do mapping inside another mapping yes yes you can that's amazing things about mappings actually i have not tried this myself how far can we map it that will be a nice thing to play with so this one for example is a mapping of a mapping so the idea of allowance is that say this address how much allowance are you allowing another address to spend on a certain amount of tokens which you have so that's a mapping within the mapping right one more question i just like got curious uh when we do mapping inside the mapping uh my my contract uh the when we calculate the price the gas right you will get higher right if you do multiple so it would be much simpler simpler you just use one to one mapping yes all right okay yep that's right so the more things you do the money nobody we do it's not a pattern right to do mapping inside the mapping or it's a pattern they can people use depends on your need if you do need it then i would just say go ahead and use it it's by itself i would say it's extremely efficient since it's one of the lower level primitives uh i mean it's already available in solidity itself so it's quite optimized if you need it just go ahead and use it okay to comparing mapping and arrays so you're saying that mapping used less gas in the contract compared to arrays so yeah so the problem with the race is that as it grows larger you will end up using more gas so as you start appending things to it it would start increasing every interaction you would have with the array you'll end up using more gas and stuff like that mapping it's but sometimes you just don't have a choice you have to use a race that just go in and use arrays otherwise if you can use mappings go ahead and use snapping can we can we remove uh data that we've already put inside the mapping it's something like when you have finished spending your tokens your erc20 tokens then essentially it's removing your uh your sort of your data within the mapping but the interesting thing with mapping is that it's ahead of time you are already given this amount of storage space so 256 bits you get access to the entire registry of the storage space so it's already pre-defined if you were to revert it to mean it's basically just turning it back to zero so you see your balance being zero for example and stuff like that whereas for the difference with array the the difference with array is that array you need to like pop off the stack you need to remove that element then only you can consider it as it's really being removed you guys i'm sorry one question for me uh uh just i don't know if you want to mention this now or maybe some other time on hygiene of um if people do things on mainnet how they can like help not load the state i mean if you guys want to experiment and you're willing to pay to experiment then just go ahead and say uh it's it is a public good so nobody can stop anyone from doing something i would say as much as that but i mean yeah it's expensive to spend to deploy contracts on mainland right now so might as well just use drinker beer finger b rockster and cover and you can use that if you want to test in prawn you can do that as well i i do typically test improv but i mean if you want to you can put a self-destruct and after that can destruct it later but let's just keep your test in that's one hour right i know we are above i'll pass our time but i will you guys want to see a factory example or should i keep it for late i'll try keep it for next time but there are other things i do we have in my list to share things like delegate core but it's a lot of things which are very specific to solidity and developing in blockchains i see i even skipped the part where we are talking about arrays and stuff like that because that's very traditional programming so you guys have five minutes i'm fine uh it's up to you because uh you are and harris is the organizer okay cool i will just quickly go through factories um since the question has already been asked great new contract to discuss well that's always contract do i want to do that hmm let's see what am i missing ah do i need to go to home or create an interface [Music] why not i figured this out later than i'll share in the group uh i'm probably forgetting some syntax stuff i did not check this out before i started this session so it's typically some syntax which i'm missing out uh yeah especially on solidity version 6 i'm quite sure there are some syntax changes really on yeah on version 5 then i need to downgrade my code so that wouldn't work as well things like constructor things are constructed wouldn't work so i i i'll figure out on the factory part and i'll probably share with you guys some later on the reason why we would use constructor as an input how we would use constructor arguments for input so because the thing is the fact that it's when we are using factory contracts we can then allow people to enter whatever they want so during that time you can then if you start specifying it as a it as an input argument it would help people to be able to create whatever instance of a contract they like so they can start start defining whatever input parameters they like say for example if it's an erc20 token then you can specify what's the name of the contract or decimals and what's the initializing number and things like that balance but i will work on this and i'll let you guys know i'm quite sure it's something to do with inheritance which i which i've forgotten about yeah okay uh just now you said something about the smart contract size 24 kilo bit economic session uh where do we check that yeah and my second question is inside this uh remix studio uh there is one thing called publish to ipfs what is this okay so the 24 kilo the 24k kb part if you try to deploy um something which is more than 24kb you will get an error but even before that when you try to compile it the sosi compiler would really complain about it it will tell you that your contract is more than 34 kilobytes and if you try to force deploy it then you won't end up things like running out of gas or it will revert so even if you want to deploy it you won't be able to deploy it so come again with the second question with remix id i publish to ipfs what is that okay there are okay ipfs essentially is a decentralized storage so if you were to publish to ipl password essentially does is that it will publish the source code to ipfs so it's basically a way for developers to store their source codes on some sort of decentralized storage so other people will be able to access it now there are other options like for example swarm so it's just a nifty way for people to be able to store code on ipfs and after that when they want to retrieve it later they can also retrieve it but you can also do that on eater schedule i mean you can deal with that yeah but uh i mean you get people saying that yeah if the scan is not decentralized per se so much before to keep it on ipf and stuff like that it depends on yourself yeah since is a block explorer having the code right there to be able to look and interact with it will be quite nice or solar we might want to organize a decentralized internet or decentralized storage meetup with someone like ipfs or in some other some future media in fact i use in fact you used to be able to like link to ipf as a website so you can very quickly hack up a html website load it on ipfs then if you're using matamas you can load it in browser quite nice actually cool so any other questions i want to keep you guys long i figured this out then i'll share it on the group but i don't want to keep it guys caleb uh uh one last question i just wasn't curious i was wondering whether uh can we interact with the deployed contract uh like if you said just like any other erc token deployed can i if i'm writing a new contract can i talk with that contract and use some function or it's not possible but you are talking about interacting in which sense say for example like a website or no just i just want to interact with other contracts oh okay you want to interact with other contracts yes it's possible it's the inheritance partner what you would do is that you would specify no yeah yeah i will inheritance is another but i won't just want to interact with the deployed contract already is on the like a rinkerby we have address already deployed can i directly talk with it or can i use this function in the contract itself or yeah you can you can go from contract to contract that's possible uh you can also go from interface to contract uh third way of doing it it's uh the third way of doing it is that you can use a certain function called delegate call so you can delegate call to an an existing contract and use the implementation of the contract to be able to amend your own state so yes you can you can sort of like do some really funky things that you can bring across five contracts then you have one contract which coordinates one or five of them that's possible yeah because i just like thinking because people deploy the contract already so rather than using create a new inheritance just use the existing yeah or deployed contract and it would be more useful then that's a that's a nice thing too nice way to think about it because if you're already thinking about that that's uh that's basically two things one is like we are talking about composability of contracts on ethereum so for example we have uh things like compound if uh sorry ave we have like for example the entire yearn ecosystem is basically plugging in very different money lego staffie levels so that they can do very different stuff it's under a single contract it's essentially composable so one contract will interact five other contracts do some crazy things uh you can also think about uh proxy contracts so contracts which can upgrade so essentially it's actually two sets of contracts it it's a deep rabbit hole which you're going to go down into it's actually quite amazing you will end up creating a lot of crazy stuff you can you can do a lot of crazy things using that yeah okay thank you any other questions okay uh if no more questions then um i know caleb keeps talking about other people's time but we are actually taking a lot of his time so uh please thank him everyone um caleb's taking time off from doing some very important work for the whole network uh so yeah and he's not taking any money uh from us all of the payments is actually going directly to ekl uh for paying for for food and stuff like that so yeah please thank caleb everybody thanks caleb thanks thank you appreciate it for listening to me rambling

Automatic transcript — names and jargon may be misspelled.