Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain ABI for ethernet intelligent contract

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to obtain ABI in Ethernet Square Intelligent contract". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ABI (Application Binary Interface)

If you understand API, it's easy to understand ABI. To put it simply, API is the interface between programs and programs. This interface contains programs that provide external access to functions, variables, and so on. ABI is also the interface between programs, but the program is compiled binary code. So it's the same interface, but it conveys information in binary format. So ABI describes how to transfer binary information between decode/encode programs. The following figure takes Linux as an example to describe the relationship between API, ABI, and programs in Linux.

Compile and deploy smart contracts

Before the Ethereum smart contract can be used by everyone, it must be deployed to the blockchain.

From the code of a smart contract to the use of a smart contract, there are probably several steps:

Write code for smart contracts (usually in Solidity)

The code that compiles the smart contract becomes bytecode (binary code) that can be executed on the EVM. At the same time, the ABI of the intelligent contract can be obtained by compiling.

To deploy a smart contract, you actually store the bytecode on the chain (through a transaction) and get an address that belongs to the contract.

If you want to write a program to call the smart contract, send the message to the address of the contract (again through a transaction). According to the input information, the Ethereum node selects which function in the contract to execute and the parameters to enter.

And how do you know what function this smart contract provides and what parameters should be passed in? This information is recorded in the ABI of the smart contract!

Ethereum Smart contract ABI

The Ethereum smart contract ABI is represented by an array that contains several Function or Event in JSON format. According to the latest Solidity file:

Function

There are 7 parameters:

Name:a string,function name

Type:a string, "function", "constructor", or "fallback"

The parameters entered by inputs:an array,function, including:

Name:a string, parameter name

Type:a string, the data type (e.g. Uint256) of the parameter

Components:an array, which is only available if the input parameter is tuple (struct) type. Describe the types of parameters contained in struct

The return value of outputs:an array,function, expressed in the same way as inputs. If there is no return value to ignore, the value is []

Whether payable:true,function can accept Ether. Default is false.

Whether constant:true,function will overwrite the status of block chain, instead, it will be false.

StateMutability:a string, whose value may be one of the following: "pure" (unable to read or write block chain status), "view" (read-only but not write block chain status), "payable" and "nonpayable" (will change the block chain state, and if the acceptable Ether is "payable", it is regarded as "nonpayable")

If you look closely, you will find that the contents described by the two parameters payable and constant seem to be included in the stateMutability.

It is also true that in Solidity v0.4.16, the key words of constant, which modifies function, is divided into view (neither reads from nor writes to the state) and pure (does not modify the state), and Type Checker is forced to check from v0.4.17. Constant is only used to modify variable that cannot be modified. The parameter stateMutability is added to ABI to uniformly indicate that payable and constant are currently reserved for backward compatibility. Detailed content and discussion of this change can be found in: https://github.com/ethereum/solidity/issues/992

Event

There are 4 parameters:

Name: the name of a string,event

Type: a string,always "event"

Inputs: an array, input parameters, including:

Name: a string, parameter name

Type: a string, data type (e.g. Uint256) of the parameter

Components: an array, which is only available if the input parameter is tuple (struct) type. Describe the types of information contained in struct

Indexed: true. If this parameter is defined as indexed, it will be false instead.

Anonymous: true, if event is defined as anonymous

To update the smart contract status, you need to send a transaction,transaction and wait for verification, so the update contract status is out of sync and the return value cannot be obtained immediately. Using Event, you can record the relevant information to Log after the status update is successful, and notify the DApp that listens to the Event or any program that applies the interface. Each transaction has a corresponding Log.

So to put it simply, Event can be used to: 1. Get the return value of function update contract status 2. It can also be used as another storage space for the contract.

The parameters of Event are as follows: indexed, and others without indexed. With indexed parameters, you can use filter, such as the same Event, and I can choose to listen only to transactions sent from a specific address. The information for each Log is also divided into two parts: Topics (array up to 4 in length) and Data. Parameters with indexed will store the Topics where Log exists, and others will store Data. If it is defined as anonymous, the Topics [0] in the following example will not be produced, whose value is hash of Event signature as the ID of this Event.

Event Set (address indexed _ from, uint value)

Take a simple smart contract as an example.

This smart contract includes:

Data: a modifiable state variable that automatically produces a data () function that can only be read

Set (): a function that modifies the value of data

Set (): an event that records Log every time a data is modified

Smart contract Source Code:

Pragma solidity ^ 0.4.20 position contract SimpleStorage {uint public data; event Set (address indexed _ from, uint value); function set (uint x) public {data = x; Set (msg.sender, x);}}

Smart contract ABI:

[{"constant": true, "inputs": [], "name": "data", "outputs": [{"name": "," type ":" uint256 "}]," payable ": false," stateMutability ":" view "," type ":" function "}, {" anonymous ": false "inputs": [{"indexed": true, "name": "_ from", "type": "address"}, {"indexed": false, "name": "value", "type": "uint256"}], "name": "Set", "type": "event"}, {"constant": false, "inputs": [{"name": "x" "type": "uint256"}], "name": "set", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}] get Ethereum smart contract ABISolidity Compiler

You can use Solidity Compiler to get the contract ABI, and I use the JavaScript version of Compiler as an example.

Installation:

Npm install solc-g

Obtain the contract ABI:

Solcjs simpleStorage.sol-abi

Will generate a simpleStorage_sol_SimpleStorage.abi file, which is the contract ABI content.

You can also obtain the binary code of the contract:

Solcjs your_contract.sol-bin

Remix

Similarly, using Solidity Compiler, you can also use Remix. The complete ABI can be seen in the Details of the contract. You can specify the Compiler version in Settings.

Etherscan

Many well-known contracts put the contract source code on Etherscan for verification, and you can see the h contract ABI at the same time.

In addition, Etherscan provides API, which can be used to obtain a verified contract ABI.

This is the end of the content of "how to get ABI in Ethernet Square Smart contract". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report