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

[java] Time: 2025-07-11 11:24:48 Source: AIHackNode Author: article Click: 26 times
rate and langchain tutorialwe can addfunctions as if un 256 was an object ora struct or a contract that we actuallycreated so how do we do this well let'screate a new contract in our contractsfolder we create a new file we're goingto call it price converterdoou and our price converter doou isgoing to be a library that we're goingto attach to a un 256 so how do youactually create a library and what is alibrary well a library is going to bereally really similar to a Smartcontract it's going to start with spdxlicenseidentifier MIT we're going to give it apragmasolidity0.8.0 and instead of typing contract forthe name of the contract we're going todo library for the name of the librarywe're going to call it price converternow libraries can't have any statevariables and they also can't send etherand all the functions in a library aregoing to be internal so what we can dois we can go back to fundme doou we cangrab get price get version and getconversion rate copy them all deletethem from fundme doou and paste theminto our library and of course sincewe're using aggregator V3 inter face inhere we can also copy the import fromfunme and since we're not using theaggregative V3 interface in our contractanymore and we're using our priceconverter we can paste it into our priceconverter now if we compile priceconverter doou we see that it actuallypasses now all the functions inside ofour library need to be internal andwe're going to make this Library priceconverter different functions we cancall on U 256 for example we're going tobe able to do message. value that getconversion rate we're going to directlybe able to get the conversion rate of avalue of a un 256 as if that was afunction for the whole time so firstlet's make this internal let's make getconversion rate internal and we'll makeget version internal now that we havethis Library price converter back in ourfundme we can now import this priceconverter and attach it to un 256 sowe'll doimport. priceon converter do soul and infundme will do using priceconverter or uint 256 of course if wecompile our fundme now this line isgetting an issue because it's saying heyget conversion rate isn't defined now inour library the first variable that getspassed to the function is going to bethe object that it's called on itself soin fundme Doo let's go ahead comment outthis line for now if we do message.value. get conversion rate this issecretly the same as if we did getconversion rate of message. Val in ourprice converter Library the message. Valis going to be passed as the inputparameter to get a conversion rate forget price and get version we don'treally care about the number so we'rejust going to leave it blank for now soinstead of require get conversion rateof message. Val we can now do message.val. getet conversion rate and compilethat you'll see that here we're notpassing a variable even though our getconversion rate function says hey I'mexpecting a variable again the reasonfor this is is this message. value isconsidered the first parameter for anyof these Library functions and that'show it works if we wanted anothervariable in here like you and256 somethingelse now we would want to pass somethingelse in here 1 2 3 and this one twothree would get assigned to thissomething else but we're going to deletethat for nowokay great and in doing that we'veminimized our fundme contract a lot bymoving a lot of that math and priceconversion stuff into our priceconverter library.Soul one of the most common librariesthat was used for the longest time wasthis Library called safemath doso andyou'll probably see it a lot ofdifferent places we're going to go offon a quick little tangent here and teachyou about safe math so let's closefundme and close price converter andlet's create a new file called safemaathtester. and let's start with some basicstuff in here safe maath was all overthe place before version 0.8 of solidityand now it's almost in no contracts whathappened why is safe math no longer usedas much well let's create samplecontract this is a section that youdon't have to follow along if you don'twant to code along with me but if youwant to you absolutely still can this isgoing to be a contract we are going todeploy on a JavaScript virtual machineuh we can use any version of soliditybefore version 0.8 of solidity so forexample we'll use pragma carrot0.6.0 and we'll create contract safemaath tester. Soul now if I create aunint 8 and I set it topublic big number and I set this to255 oops safe math tester let's go aheadand compile safe math tester with 06.7 fragma solidity the maximum size ofa uint 8 is going to be 255 this isgoing to be the biggest number that wecan fit in an uint 8 and if I were todeploy this to a JavaScript VM or even atest Network save math tester let's goahead and deployit if I hit big number we're going toget255 but what happens if I create afunction called add that sets big numberequal to big number plus one let's savethat delete that old contract and deploywell right now big number is 255 whathappens when we add one to big numberwhen 255 is the max size a u 8 can bewell let's hit add now let's check whatbig number is big number gets reset tozero so what's going on well prior toversion 0.8 of solidity un integers andintegers ran on this concept of beingunchecked which means that if you passedthe upper limit of a number it wouldjust wrap around and start back from thelowest number it could be so if I calladd a whole bunch more times and hit bignumber now it's eight if I were to hitthis add button a ton more times and getit back to 255 it would then continue towrap over to zero so one of the mostpopular libraries that was out there wasthis safe maath Library which wouldbasically basically check to make surethat you weren't wrapping around a un256 or an n256 basically it

(Editor in charge: html)

Related content
Wonderful recommendations
Popular Clicks
Friendly links