Smart kontract libraries
Last edit: @boluwatife_4523(opens in a new tab), 29 Éprel 2024
Yu nor nid to dey write efri smart kontract for yor project from di biginnin. Plenti open source smart kontract libraries dey wey get buildin blocks wey yu fit yus again and again for your project wey fit save yu from to dey do am again.
Prerequisites
Bifor yu jump enta smart kontract libraries, na good idia to ondastand di strukshure of smart kontract wella. Start to dey go to smart kontract anatomy if yu neva do am bifor.
Wetin dey inside library
Yu fit ushualy find two kain buildin blocks for smart kontract libraries: behaviours wey yu fit add to yor kontracts wey yu fit yus again, and implementashons of difren standards.
Behaviors
Wen yu dey write smart kontracts, good shans dey wey yu fo find yorsef dey write same pattans ova and ova, laik to dey assign one admin address to do operashons wey dem don protet for kontract, abi to dey add one emergency pause button if issue wey yu nor ekspet hapun.
Smart kontract libraries dey ushualy give implimentashons of dis behaviours wey yu fit yus again as libraries(opens in a new tab)abi thru inheritans(opens in a new tab) for Solidity.
As eksampol, to dey folow one simpol vashon of di Ownabol
kontract(opens in a new tab) from di OpenZeppelin Kontracts library(opens in a new tab), wey dey shuse one address as di owna of di kontract, and e dey give modifier to restrict access to only dat owna for some metod.
1contract Ownable {2 address public owner;34 constructor() internal {5 owner = msg.sender;6 }78 modifier onlyOwner() {9 require(owner == msg.sender, "Ownable: caller is not the owner");10 _;11 }12}Show mi evrytinKopy
To yus one buildin block laik dis for yor kontract, yu go first nid to import am, and den extend from am in yor own kontracts. Dis go allow yu yus di modifier wey di Ownabol
kontract don provide to sekure yor own funshons.
1import ".../Ownable.sol"; // Path to the imported library23contract MyContract is Ownable {4 // The following function can only be called by the owner5 function secured() onlyOwner public {6 msg.sender.transfer(1 ether);7 }8}Kopy
Anoda popular eksampol na SafeMath(opens in a new tab) abi DsMath(opens in a new tab). Dis na libraries (as e dey against to base kontracts) wey provide arithmetik funshons wit ovaflow sheks, wey di languaj nor dey provide. Na good praktis to yus one of dis libraries insted of native arithemetik operashons to guard yor kontract ovaflows, wey fit get bad konsekwens.
Levels
To make tins fit work well togeda and dey kompatibol, di Ethereum komunity don define some standards in di form of ERCs. Yu fit read more about dem for di standards sekshon.
Wen yu dey add one ERC as part of yor kontracts, na good idia to dey look for standard implementashons pass to dey try roll out yor own. Plenti smart kontract libraries inklude implementashons for di ERCs wey popular pass. For eksampol, di afailabol ERC20 fungibol token standard wey efribody sabi for HQ20(opens in a new tab), DappSys(opens in a new tab) and OpenZeppelin(opens in a new tab). In adishon, some ERCs also provide kanonical implementashons as part of di ERC imsef.
Im worth to menshon sey some ERCs nor bi standalone, but dem adishons to oda ERCs. For eksampol, ERC2612(opens in a new tab) dey add one ekstenshon to ERC20 to impruf di way to yus am.
Hau to add one labrary
Make yu always refer to di dokumentashon of di library wey yu inklud for spesifik instrukshon on hau to inklude am in yor projet. Dem don pakaj mome Solidity kontract libraries to dey yus npm
, so yu fit just npm install
dem. Plenti tools wey dey kompile kontracts go look yor node_modules
for smart kontract libraries, so yu fit do di followin:
1// This will load the @openzeppelin/contracts library from your node_modules2import "@openzeppelin/contracts/token/ERC721/ERC721.sol";34contract MyNFT is ERC721 {5 constructor() ERC721("MyNFT", "MNFT") public { }6}Kopy
Nor mata di metod yu yus, wen yu dey inklude library, always dey kip one eye on di languaj vashon. For eksampol, yu nor fit yus one library for Solidity 0.6 if yu dey write yor kontracts in Solidity 0.5.
Wen yu fit yus am
To dey yus one smart kontract library for yor project get plenti benefits. Numba one, im dey save yu taim as im dey provide yu wit ready-to-yus buildin blocks wey yu fit inklude in yor system, pass to dey code dem yorsef.
Sekurity na ogbonge plus to am. Open source smart kontract libraries dey skrutinaize wella. Bikos plenti projects dipend on dem, strong insentiv dey by di komunity to kip dem onda konstant review. Im komon wella to dey find errors in aplikashon code pass inside kontract libraries wey yu fit yus again. Some libraries sef dey get external audit(opens in a new tab) for adishonal sekurity.
But, to dey yus smart kontract libraries get risk say yu fit inklude code wey yu nor sabi wella for yur project. Im fit dey tempt yu to import one kontract and inklude am direct into yor projet, but witout one good ondastandin of wetin dat kontract dey do, yu fit dey introdus one issue wella in yor system due to one bihavior yu nor ekspet. Always make sure to read di dokumentashon of di code yu dey import, and den review di code imsef bifor yu make am part of yor project!
Last, wen yu dey diside on weda to inklude one library, make yu konsida hau yu dey yus am. Di one wey pipol dey yus pass get benefits to get one large komunity and more eyes to dey look into am for issues. Sekurity suppose bi yor primary fokus wen yu dey build wit smart kontracts!
Tools resembol
OpenZeppelin Kontracts - Library wey popular pass to dey sekure smart kontract divelopment.
DappSys - Safe, simpol, flexibol building-blocks for smart-kontracts.
HQ20 - One Solidity project wit kontracts, libraries and eksampol to helep yu build aplikashons wey dem don distribute for real world.
thirdweb Solidity SDK - Dey provide di tools wey dey nid to build kustom smart kontracts betta
Related tutorials
- Di sekurity konsiderashons for Ethereum divelopas – Dis one na tutorial on sekurity konsiderashons wen dem dey build smart kontracts, inkludin library yus.
- Ondastand di ERC-20 token smart kontract -Tutorial on di ERC20 standard, wey dem plenti libraries dey provide.
Further reading
Know a community resource wey fit helped you? Edit this page and add it!