header-langage
简体中文
繁體中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Solidity Minimalist Primer | Lecture 17: Library Contracts Stand on the shoulders of Giants

Read this article in 7 Minutes
This article introduces the library functions in Solidity using the library function String referenced in ERC721 as an example.
原文标题:《 Solidity 极简入门: 17. 库合约 站在巨人的肩膀上 》
Original author: 0xAa   


I've been relearning Solidity recently to consolidate the details and also write a "Solidity Minimality Primer" for kids to use (programming geeks can find another tutorial) and update 1-3 sessions every week.


All open source code and tutorial github:github.com/AmazingAng/WTFSolidity


In this session, we'll introduce the library contracts in Solidity using the library contract String referenced in ERC721 as an example and summarize the library functions commonly used in Solidity.


Library function


Library functions are a special kind of contract that exists primarily to increase the reusability of Solidity code and reduce gas fees. Library contracts are generally a collection of useful functions (library functions), created by the great god or project side, we stand on the shoulders of giants, can use on the line.


  Library Contract: Standing on the shoulders of giants


The main differences between it and ordinary contracts are as follows:


A state variable cannot be inherited or inherited

Cannot receive ether

It cannot be destroyed.


The String library contracts


The String library contract is a code library for converting Uint256 types into corresponding String types. Example code is as follows:    



He basically consists of two functions, toString() to convert uint256 toString and toHexString() to convert uint256 to hexadecimal and toString.


How to use library contracts


We use the String library function toHexString() to demonstrate two ways to use functions from library contracts.


1. Using the using for directive:


Instruction & have spent using A for B;   Can be used with additional library functions (slave & NBSP; A) to any type (B). After the instruction is added, functions in library A are automatically added as members of variables of type B and can be called directly. Note: This variable is passed to the function as the first argument when called:



2. Call the library function by the name of the library contract:



我们部署合约并输入 170 测试一下,两种方法均能返回正确的 16 进制 string 「0xaa」。证明我们调用库函数成功!   


  The library function was successfully called

conclusion


In this session we'll introduce the Library in Solidity using the Library function String referenced in ERC721 as an example. 99% of developers don't need to write their own library contracts, they just need to write them. We just need to know which library contracts to use in which cases. The common ones are:


String: Convert Uint256 to String

Address: Determines whether an Address is a contract Address

Create2: Secure use of Create2 EVM Opcode

Arrays: the array-related library function & NBSP;  


The original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit