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

Loading player…

Intro Workshop: Build your own Dapp with Dapp-Learning

Ethereum MalaysiaMon, Oct 9, 2023, 12:00 AM

Are you interested in building a decentralized app on the blockchain but don’t know where to start or can’t find good resources for beginners? This workshop is for you! We are pleased to invite Alan from Dapp-Learning to talk about how you can start to learn and build your Dapp and give an introduction to the Solidity smart contract language for Ethereum. Dapp-Learning provides step-by-step guidance not only for junior developers but also for advanced developers to contribute to projects and onboard new learners. Check out their Github repo here: https://github.com/Dapp-Learning-DAO/Dapp-Learning Pre-requisites: Some experience with solidity and smart contracts are preferred, but a beginner can still get a sense of what smart contract development is all about from this presentation!

Transcript

okay good evening okay good evening everyone my name is Ellen I'm a celebrity developer and I'm also a member of Dublin community uh it is not going on to be here to talk about smart contract development using Foundry first allow me to introduce Gap learning community that learning community is a non-profit organization that designed for developers to step into blockchain the app development uh well developers can learn Define empty down crypto Etc knowledges and we hope we could not only give Junior developed developers a physical and easy to learn learning roadmap we hope we also can present an advanced Developers platform to communicate and to cooperate here in that learning community we have three major components first is our GitHub repo you can see here github.ap learning and in our GitHub repo we provide a list of learning tasks in our repo and develop scale work their way to relate these tasks from easy to difficult to learn the app development and uh or we also organized weekly meeting where develops can share their share technical presentation in our weekly meeting we have invented a lot of people from well-known projects to give technical presentations and we also have a translation tip that have translated the MIT open course blockchain and money uh which we think is a very great material for beginner to learn about blockchain and improve their financial knowledge okay so much for the uh introduction let's move on to today's Workshop uh in today's Workshop I will do a live coding and demonstrate how to use The Foundry framework to uh develop tests and deploy a WTH token contract and I hope by the end of this Workshop uh we'll we will have a working more from will be more familiar with the workflow smart contract development so what is uh WTH contract WTH stands for reptile reps Isa as the native coin in ethereum blockchain it's not erc20 compatible so if we want to departing some Define project we need to force the wrap our user coin into uh into a wrapped user token and add a one-to-one ratio so today we are going to build this rapid user tool contract and the our website contract should be erc20 compatible and the people can deposit there is and receive wrap these at the 121 ratio people should also be able to unwrap or withdraw their ease at the one to one ratio today we'll we'll be using a framework called Foundry to develop our smart contract Foundry is a portable fast and modular token for ethereum application development and it is written in Rust so it's placing it fast it consists of three components the major one is called Forge Forge is evm smart contract developing and testing framework uh it's not it's the function is not powerful and hard hat we need to write our tests in JavaScript or type screen so it requires us to learn a lot of API and libraries it also requires us to change from solidity to JavaScript and back to 3D during our development is a lot of context switch and contact switching costs are focused on the contrary in Foundry we read our smart Contracting solidity with rental tests in solidity and we also we can also write scripts in solidity so no no contact switching and we will have a better developer experience during development at The Foundry after the first Foundry provide a COI to podcasts it's a 2K for interacting with smart contract or sending transaction for uh for reading data from blockchain and Foundry also has a tool called anvil usia where we can spin up a local Instagram loan it is uh it's just like Ganesh and the Heart handle but much faster foreign first we need to install phone tray to install Foundry we can go to our website called getfundry.sh it will show us a command if you are in Linux or Mac you can run this command and it will install tool called Foundry app and then if and then you can just run Foundry out to update your Foundry to the latest version yeah you can use phone drop to install Foundry and you can use a phone drop to update your Foundry however if you are on Windows you need to build it from build from Source you need to install rust and get along the laundry repo and then build it from the source you can you can look the instruction in get foundry.sh website okay after installing Foundry we can use Forge we need to you need a project and we can choose the project name here we use publicity address or my project Main oh I also have I already have this so material is false and then you need property PHP here okay and it will you need a project called WTH let's go inside and see the project layout uh all source files The Source direct trading Clause includes all the source file for smart contract and test directory includes all the test case for smart contract and the other text Style by convention it ends with DOT T dot Sol and the likewise or the script folder inclusion or script if the answer is dot s dot s o l for our dependencies or libraries are in the lab folder you can see here we already have a fourth standard Library and uh in the in this this example project we also have found traded.com of file all the configuration parameters we can write it in this foundry.

com okay that's that's how to you need a project we are going to write the wrap this contract and directly so itself is a erc20 contract so we can uh we can use the erc20 implementation from the open zapping Library uh then how can we import external Library into our project you can do it by using 4G install and followed by the GitHub rifle open sampling is National Concept in contracts we can also add a tag or the commit uh append commit after this if we want okay after installing the dependence we can let's check the map folder we can see we have downloaded this openshamping contracts come on we can use four three mappings to check all the remarkings function made from tree framework automatically calculated rematch in your for dependency you if we values in this remapping instead of import a contract using this past we can use the the path in the uh in the front if we want to change change this path or change the name of it we can we can write in a file called mappings.txt or we can first add the default remapping to this remapping .txt file and then we added it here we add in the name for the for the open sampling contracts so that is it is compatible with the uh with the import test uh in Hot Head okay after this we here we already have uh an example Source cell called counter we copy it and rename it to uh WTH so and uh once we delete all the content and rename it to probability Edge the page details with the country where we want and here we can use spot View okay we can use sport field to build our our project after photo build we can see there are two extra folders one is cache saving all the cash flow while it's called saving all the or the output file of the compilation you can see what things that are all the it has a Json file and the inside is Json file we can sort all the API or the bad codes Etc okay uh like we said like we said we want to import the es20 contract from open sampling you can do it very important [Music] and we inherit from this okay here we can set your specify our contract name to be my bread is so and the symbol for ris20 token to begin my w t h okay and we can first build it to see if there is any uh any combination error or we can use Ford Field Dot prefer to build uh what build top to run it in a watch mode so what is what is a watch mode uh watch mode you know watch mode yeah it's a watch for watch for your file change so every time we change our our source file and press save it so what to watch well for it build again so if there's any any syntax server we can we can recognize it immediately okay so am I happy for smart contract development is to to rather snap rather read read all the external functions and red stacked skeleton for this contract then use the test driven approach to write for red task force and then go back to the contract to write the implementations so here I would demonstrate my test test stream learn approach or wrap the user talk talk we need to sponsor deposit function so that people can people can deposit their user and get grab the user in return and I will skip the implementation for for now we will also have a withdrawal function to go into it the opposite way people can use the withdrawal function to to decrease their replies balance and get get a user back okay the compilation path now let's open a new terminal and introduce Foundry test so Foundry test 101 how to do testing in Foundry framework once we rename the rename the test file to a btf.t.so and we delete all the contents from this contract here we will use simple function X or function or public function that starts with test and that's wrong Forge test and see what's happening to show us the test result pass test so Foundry test one one what is a test case in in Foundry in Foundry or the function or the external or public function that starts with tests will be treated as a test case and if the test case reward the test failed otherwise it passed here we can see the test two fails failed under the test one part foreign so what is this import uh why is this the import search standard tester so in in the in the example file uh here we import the test contract from the watch standard Library by inheriting from this test contract we get three major capabilities the first one is uh the the capability to do assertion for example here here I make two variables A and B I can do assertion using a third equal a b to assert that these two variable equals and here and we need to increase the velocity level to level two of the using 2v in your argument we can see the log result and here we can see the log result from our assertion and we can also use a third grade to do a session and we can use custom less than to do a session this association should pass right like we expected so the the first functionality in this task test contract is to give us the the ability to do assertion and the second the functionality is to to help us to load more information on and make sure you how in this test contract it defines many log events starts from start with log and we can emit this event by using email name and the parameter by doing so we can emit our result in the in the test result okay the uh test contract provide provides us the ability to do assertion to block more information uh what's more important about this test contract is that it provides us the ability to to use cheat code to change the state of the ethereum virtual machine uh by using this cheat code you can you can see you are in a super mode I mean chemistry huh the most used uh one of the most used cheat code is VM the top rank it allows you to Mark a user for example here I have a external Excel function oh I forgot to mention this this console to library foreign [Music] test you can log information by using log log events to email you can also use a console or console to library that provided by this test contract and use console to download to to log information okay so okay with the logs this information and here I want to block the message sender for this function I want to know who make the message call to this function and I can add extra information that's parameter so if we call this function this way by using this Dot function name the color would be means contract address okay okay so this contract address if uh before c and the caller for this in the function is the same if we use the cheat code VM dot rank we can pretend to be anyone we can uh we can pretend to be any address even if we don't have the private key for that so padding VM top right let's see okay the corner for the inner function is address one and yeah by using this cheat code we can uh we can Mark a user and send a message called from that user even if we don't have the correctly we don't have the ownership of that address uh the other cheat code most used is called VM then dot deal it can set the balance of address to some value for example we can we can use this maker Trend function from this test contract who who said a certain address called Alice and let's see the initial balance of various oh it's zero and let's try to change it we can use vm.

do to set the balance of the dependence of ether or Addis to to vines or maybe we can try more try 100s and let's see okay now that is that it says 100 years in this testing environment okay after introducing the uh the usage of Forge tests let's go back to our red is a contract and read the unit tests for this contract first we want to write a unit test it's not important here we import here is a contract and in this use in this test case we first deploy a gravity user contract by using this new syntax and then remote user name dialysis we can also give values some initial balance negative two visa is their range and then goes to the next the active uh we make the deposit call from Alice and the value for their message call we can set it to one user so it means that is send one user to describe these contract by sending a message for to deposit function and after this uh acts we will make some assertion here the result and your assertion we can make is that after deposit the balance of values should be one useful the way inside is among not the value okay so here is our unit test for deposit function we can start and run it first okay it's fail as expected and we move back to our source cell our web please contract and finish the implementation for deposit uh the solution for this is quite easy actually we can use the uh on the underscore means function from the rc20 implementation uh in open sampling Library here we only need to mean uh the message value amount of wrap this token to the message sender okay let's go back to our test and see if this test part okay this test pass test deposit plus and speed expected then we move on to to write the unit test for uh with our withdrawal function we will also follow the arrange to connect to yourself step and here we deploy our web diesel contract Force we remove a user called analysis and we give values to use balance uh and here we first ask that is to deposit one is a into our radius contract then we ask areas to withdraw one user from our contract uh I've deposit and withdraw the assertion we can make is that the gravity's balance for Alice should be Zero by now okay we can use third View to see if there's any syntax server that has built successfully and we go back to our source source contract and how to implement how to implement this virtual logic it's also quite simple first we burn some amount from the master sender which means we decrease the balance for Mastery Center by this amount and then we we sent the the amount of Visa back to this user okay so this is which flow and then we save this file we go back to our test we run our test okay or test pass if we increase our velocity to Dash 4V we can not only see all the log thing for not only in the log info we can also see the call traces for each test case what is a call Trace inside these countries is we can see all the gas usage for for function call you can see the the contract and the the function name we can also see the parameter of the return value present value and also all the events uh emitted during this test case so checking the core traces is a it's very helpful during debugging and if we use verbosity level 3 Dash Triple Three we only show the we only show the courtresses for filling filling case so if we attend this to 0.5 to intentionally make this test case fail we are expected to see this to see the code choices for this case right okay here we have the unit test for for for reply so uh contract we can do some refactoring uh each each test case in Foundry is independent you can see it as a simulation so what's happening inside one test case will not affect the other test case but sometimes we have some logic that shared between each test case we can extract this logic into a function called setup make this one that is this topic like this this logic so by adding a function called setup each test case will firstly run the logic inside setup and then run the logic inside each test case here we can extract these two variables into storage to make it global so after this refactoring Republic yeah of this refactoring the two unit tests as expected okay that's so much for test uh let's move on to scripting let's rename this file to WTH so so okay uh comparable function a verbum contract named the full and there is a function called ROM we can make it return a value okay we can run the logic inside this form function by using Forge three and the path for this script you can see uh this function is is run as a script what first script does it is looking for a wrong function inside this path uh if the multiple contract inside the one source file you can specify the contract of the path okay and after running the script it will show you the result and it will show you the gas you you see use you can you can use this script functionality as a Play Break one for example uh and give you an example [Music] there we inherit we inherit from the script contract from third for the same Library so so that we can have the console the the log ability so if you want to know the difference uh between API dot income and the API dot in code pack you can write it as a script in in inside this raw function and the growing by using photo script okay we should use log bytes because we are logging out guys uh type okay here we can see the here we can see what's inside let's say is is the is the API standard encoding for this bank and uh what the inside the variable variable that's B is a packed version of uh the string ABC and what's more important about script is that it can it can send some transaction to the blockchain the script provider functionality our provider cheat code called broadcast or you can use the start broadcast and here we import our Graphics contract and we can deploy our web user contract it tells us if uh if we want to simulate downtrend transaction we can pass the RPC URL okay so here I pass my RPC URL for for the quality test now increase the velocity level okay after using first script and add a RPC URL and set the opacity level to four we can see on the on the upside if the simulation locally and on the bottom side you can see here is the simulation on chain is what is what would happen if we send this uh these transactions between the uh inside this broadcast cheat code is is what will happen have this transaction been sent to the blockchain you can see here uh we did we can deploy a graph this contract at this address so simulation on chain is not enough it can give a giveaway give us some information or or deploying script and you can also tell us the gas usage what we want is to actually send this transaction to the blockchain so that our ready user contract is deployed to the blockchain we can achieve this by append the broadcast dash dash broadcast to this photo script and we also want to very fun virusal contract to verify our contract we also need to send our ethernet Sky API key we also need to provide our product provide our private key of the sender and also the your trust of the sender okay it runs the local simulation force and downtrend simulation and they record the the own transformation and then send the transaction send the transaction to the blockchain okay you can see long-term company execution company success and go to guest free and the contract is deployed to this address and the contract verification let's go to the qualities scan and the series address creating and we can see the contract right this is our wrap this contract it means this contract is verified in graduate it's a scan and uh we can Interactive by using is a scan okay let us try to deposit about 0.18 we can use 0.1 is equal to p yeah I think that's 0.1 is Australia foreign okay it's not the way it is so we can type 0.

1 and we can check our balance here let's wait for this deposit to finish okay yeah it depends should work should be 0.1 user and we also want to withdraw it's okay let's go when it isn't the gas thing foreign okay as we expected and I forgot to mention this we can use the word test cash report to have a a gas estimation for deposit and the withdrawal function right okay uh that's so much for for this for the workshop to conclude you can uh write your smart contract in solidity you can also write your test case in solidity and also the scribbling script your script in solidity so you will remain in solitary land for for development during development and there's no contact switching uh it's a better developed experience experience okay and that's so much that's all for my for my workshop and if you want to learn learn more about smart contract developing and the app development you can go to our adapt learning GitHub repo to uh to learn more about it okay thank you all for listening thanks Ellen so we have a few questions in the chat and first question is from g-shan and he asked how hard it is for a hard hat user who will learn boundary like what are the possible difficulties you may find when you learn laundry okay it will be quite easy for a higher user to transform to Foundry you only because all your test case and uh script will be insulated so if you can write 70 you can use country to do smart contract development so I also found something that is new which is CM and function asks what VM stands for in boundary uh VM if VM is an instant for the cheat code so the the bunch of cheat codes you can check the phone triple add to the California sh and yeah it has a a bunch of this cheat code inside this cheat code interface so VM is a instance of this cheat code interface yep okay another questions from two he asked what would commonly cause the test to fail uh River the assertion if the if the expect value doesn't match the the actual value the test will fail so basically uh if the intest case it reverse it will fail okay yeah and to ask again what my smart contract be considered battery if all tests classes or is there house training we must do before running in production uh silent uh can you repeat repeat the question we asked the smart contract consider block free if all the test classes no absolutely no uh actually unit test is not enough uh after we write some unit tests the purpose of viewing tests is to to to help us gain some understanding of how our external API is called better user and the only unit test is not enough for the security of our smart contract normally actually after we develop our smart contract we will do a fuzzy testing will write test code for fuzzy testing for property based testing so what is property based testing is that you need to uh come up with some invariant some property that should hold within uh within the range of input and then boundary provide a function called body test it will randomly input some value and see if this input will break this property so you you'll read your property based test and you test it against a range of inputs and try to find some some input some Edge case that that will break your smart contract uh normally we will not not conceive our smart contract but free yeah yeah to add on a little bit on that uh even though the smart contract may pass the auditing but there's no but free like in the smart contract there's still some risks to some records to type into your smart projects so yeah and yeah final questions is from topics and he asked how we connect iot devices with ethereum smart contract wow I'm not an expert in this area but I think it's value by calling some by making some RPC code to the museum blockchain okay and he also asked how we can simulate multiple entries for performance tasks [Music] um I'm a single body performance test because many scene will Remain the numerical will affect the weather your transaction uh we will will be accepted in in the blockchain blog uh the latency and sometimes the relationship between you you uh between your your RPC node and the menu node will uh will will will influence the the performance yeah all right um so I think that's it anyone still has any questions okay and if no I think that's a wrap um you guys can found the recording in our YouTube channel it's called ethereum Malaysia so yeah uh lastly uh thank you Ellen for having this amazing workshop and thanks everyone for joining us this evening and yeah thank you have a great yeah thank you for listening bye yeah good night bye-bye

Automatic transcript — names and jargon may be misspelled.