以太坊ABI简述
ABI 是什么
ABI
(Application Binray Interface):应用程序二进制接口,描述了应用程序和操作系统之间、一个应用和它的库或应用的组成部分之间的底层接口。
以太坊中的ABI
web3js 通过以太坊智能合约的 json 接口 (ABI) 创建一个 JavaScript 对象,用来在 js 代码中描述各种函数和事件的组成。
生成ABI
solcjs --abi xxx.sol
函数的ABI
- name(string):function 名称
- type(string):'function' || 'construct' || 'fallback'
inputs(array):function 的输入参数
- name(string):参数名
- type(string):参数的 data type
- components(array):如果输入的参数是 tuple(struct) type 才会有这个参数,描述 struct 中的参数类型
- outputs(array):function 的返回值,和 inputs 使用同样的表示方式。没有返回值时为 []
- payable(boolean):function 是否可以接收 Ether,默认为 false
- constant(boolean):function 是否会改写区块链状态
- stateMutability(string):'pure'(不会读写区块链状态)|| 'view'(只读不写区块链状态)|| 'payable'(会改变区块链状态,且可接收 Ether)|| 'nonpayable'(可改变区块链状态,不接收 Ether)
事件的ABI
- name(string):event 名称
- type(string):'event'
inputs(array):输入参数
- name(string):参数名
- type(string):参数的 data type
- components(array):如果输入的参数是 tuple(struct) type 才会有这个参数,描述 struct 中包含的信息类型
- indexed(boolean):参数是否被定义为 indexed
- anonymous(boolean):event 是否被定义为 anonymous