header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

Simple Introduction to Solidity|Lecture 14: Abstract Contracts and Interfaces

2022-07-18 22:00
Read this article in 9 Minutes
Taking the interface contract of ERC721 as an example to introduce the abstract contract and interface in solidity, so as to better understand the ERC721 standard.
Original title: " A minimalist introduction to Solidity: 14. Abstract contracts and interfaces 》
Original author: 0xAA   


I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist "Getting Started" is for novices (programmers can find other tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


Here First, we use the ERC721 interface contract as an example to introduce the abstract contract (abstract) and interface (interface) in solidity to help you better understand the ERC721 standard.


Abstract contract


If there is at least one unimplemented function in a smart contract, namely If a function lacks the content in {}, the contract must be marked as abstract, otherwise the compilation will report an error; in addition, the unrealized function needs to be added virtual so that the sub-contract can be rewritten. Take our previous insertion sort contract as an example. If we haven’t figured out how to implement the insertion sort function, we can mark the contract as abstract and ask others to write it later.



Interface


An interface is similar to an abstract contract, but it does not implement any functionality. Rules for interfaces:


- cannot contain state variables

- cannot contain constructors

- cannot inherit from interfaces Other contracts outside

- All functions must be external and cannot have a function body

- Contracts that inherit interfaces must implement all functions defined by the interface

< br>

Although the interface does not implement any function, it is very important. Interfaces are the skeleton of a smart contract, defining what the contract does and how to trigger them: if a smart contract implements a certain interface (like ERC20 or ERC721), other Dapps and smart contracts know how to interact with it. Because the interface provides two important information:


- the bytes4 selector of each function in the contract, and the function name based on their function signature (each parameter type).

- interface id (see EIP165 for more information)


In addition, the interface is equivalent to the contract ABI (Application Binary Interface) and can be mutually Conversion: Compile the interface to get the ABI of the contract, and use the abi-to-sol tool to convert the ABI json file into an interface sol file.


We take the ERC721 interface contract IERC721 as an example, which defines 3 events and 9 functions, and all NFTs of the ERC721 standard implement these functions. We can see that the difference between an interface and a regular contract is that each function ends with ; instead of the function body { }.



IERC721 event


IERC721 contains 3 events, among which Transfer and Approval events are also available in ERC20.


- Transfer event: released during transfer, recording the Token sending address from, receiving address to and tokenid.


- Approval event: Released during authorization, recording authorization address owner, authorized address approved and tokenid`.


- ApprovalForAll event: Released during batch authorization, recording the issuing address owner of batch authorization, the authorized address operator and approved or not.


IERC721 function 


balanceOf: Returns the NFT holding balance of an address.


ownerOf: Returns the owner of a tokenId.


transferFrom: ordinary transfer, the parameters are transfer address from, receiving address to and tokenId.


safeTransferFrom: safe transfer (if the recipient is a contract address, it will be required to implement the ERC721Receiver interface). The parameters are transfer-out address from, receiving address to and tokenId.


approve: Authorize another address to use your NFT. The parameters are the authorized address approve and tokenId.


getApproved: Query which address the tokenId is approved to.


setApprovalForAll: Batch authorize the series of NFTs you hold to an address operator.


isApprovedForAll: Query whether the NFT of an address has been authorized in batches to another operator address.


safeTransferFrom: An overloaded function for safe transfer, the parameter contains data.


When to use the interface?


If we know that a contract implements the IERC721 interface, we can interact with it without knowing its specific code implementation.


Boring Ape BAYC belongs to ERC721 Token , realizing the function of IERC721 interface. We don’t need to know its source code, we only need to know its contract address, and we can interact with it using the IERC721 interface, such as using balanceOf() to query the BAYC balance of an address, and using safeTransferFrom() to transfer BAYC.



Summary


In this lecture, I introduced the abstract contract (`abstract`) and interface (`interface `), they can both write templates and reduce code redundancy. We also covered the `ERC721` interface contract `IERC721` and how it can be used to interact with the boring ape `BAYC` contract.


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
This platform has fully integrated the Farcaster protocol. If you have a Farcaster account, you canLogin to comment
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit