以太坊 ABI(Application Binary Interface)是一种用于以太坊智能合约的接口规范,用于定义合约的函数和事件。ABI 定义了函数的参数类型、返回类型以及函数的编码和解码规则,使得合约与外部世界进行交互更加方便和可靠。

以太坊 ABI 由两部分组成:函数 ABI 和事件 ABI。

1.函数ABI

函数 ABI 定义了合约的函数及其相关信息,包括函数的名称、参数类型和顺序、返回类型等。它使用 JSON 对象表示,常见字段包括:

constructorreceivefallback从来没有名称或输出。receive和fallback也没有输入。

向非支付功能发送非零以太币将导致交易失败。

2.事件ABI

事件 ABI 定义了合约中定义的事件及其相关信息,包括事件的名称、参数类型和顺序等。它也使用 JSON 对象表示,常见字段包括:

错误事件如下:

JSON数组中可能存在多个具有相同名称甚至相同签名的错误;例如,如果错误源于智能合约中的不同文件或从另一个智能合约中引用。对于ABI来说,只有错误名称本身是相关的,而不是定义错误的位置。

示例

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract Test {
    constructor() { b = hex"12345678901234567890123456789012"; }
    event Event(uint indexed a, bytes32 b);
    event Event2(uint indexed a, bytes32 b);
    error InsufficientBalance(uint256 available, uint256 required);
    function foo(uint a) public { emit Event(a, b); }
    bytes32 b;
}

将产生JSON格式:

[{
"type":"error",
"inputs": [{"name":"available","type":"uint256"},{"name":"required","type":"uint256"}],
"name":"InsufficientBalance"
}, {
"type":"event",
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"bytes32","indexed":false}],
"name":"Event"
}, {
"type":"event",
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"bytes32","indexed":false}],
"name":"Event2"
}, {
"type":"function",
"inputs": [{"name":"a","type":"uint256"}],
"name":"foo",
"outputs": []
}]

以太坊 ABI 既用于智能合约的编译和部署阶段,也用于在以太坊网络上与智能合约进行交互。通过使用 ABI,开发人员可以在不了解合约实现细节的情况下与合约进行交互,实现合约函数的调用和事件的监听。

请注意,ABI 是以太坊智能合约的一部分,它定义了合约与外部世界的接口规范,以实现合约的可访问性和互操作性。

3.示例

EOA 使用的ABI数据与合约间调用时调用者持有的接口是等价的,都是对合约函数签名的完整描述

调用者使用ABI数据生成message中的calldata, 代码中calldata数据就是通过ABI来生成的

ssl

合约接口

//SPDX-License-Identifier: UNLICENSED

// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.9;

// We import this library to be able to use console.log
import "hardhat/console.sol";


// This is the main building block for smart contracts.
contract Token {
    // Some string type variables to identify the token.
    string public name = "My Hardhat Token";
    string public symbol = "MHT";

    // The fixed amount of tokens stored in an unsigned integer type variable.
    uint256 public totalSupply = 1000000;

    // An address type variable is used to store ethereum accounts.
    address public owner;

    // A mapping is a key/value map. Here we store each account balance.
    mapping(address => uint256) balances;

    // The Transfer event helps off-chain aplications understand
    // what happens within your contract.
    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    /**
     * Contract initialization.
     */
    constructor() {
        // The totalSupply is assigned to the transaction sender, which is the
        // account that is deploying the contract.
        balances[msg.sender] = totalSupply;
        owner = msg.sender;
    }

    /**
     * A function to transfer tokens.
     *
     * The `external` modifier makes a function *only* callable from outside
     * the contract.
     */
    function transfer(address to, uint256 amount) external {
        // Check if the transaction sender has enough tokens.
        // If `require`'s first argument evaluates to `false` then the
        // transaction will revert.
        require(balances[msg.sender] >= amount, "Not enough tokens");

        // We can print messages and values using console.log, a feature of
        // Hardhat Network:
        console.log(
            "Transferring from %s to %s %s tokens",
            msg.sender,
            to,
            amount
        );

        // Transfer the amount.
        balances[msg.sender] -= amount;
        balances[to] += amount;

        // Notify off-chain applications of the transfer.
        emit Transfer(msg.sender, to, amount);
    }

    /**
     * Read only function to retrieve the token balance of a given account.
     *
     * The `view` modifier indicates that it doesn't modify the contract's
     * state, which allows us to call it without executing a transaction.
     */
    function balanceOf(address account) external view returns (uint256) {
        return balances[account];
    }
}

编译后的ABI

{
  "_format": "hh-sol-artifact-1",
  "contractName": "Token",
  "sourceName": "contracts/Token.sol",
  "abi": [
    {
      "inputs": [],
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "internalType": "address",
          "name": "_from",
          "type": "address"
        },
        {
          "indexed": true,
          "internalType": "address",
          "name": "_to",
          "type": "address"
        },
        {
          "indexed": false,
          "internalType": "uint256",
          "name": "_value",
          "type": "uint256"
        }
      ],
      "name": "Transfer",
      "type": "event"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "account",
          "type": "address"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "name",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "owner",
      "outputs": [
        {
          "internalType": "address",
          "name": "",
          "type": "address"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "symbol",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "totalSupply",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        }
      ],
      "name": "transfer",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ],
  "bytecode": "0x60806040526040518060400160405280601081526020017f4d79204861726468617420546f6b656e00000000000000000000000000000000815250600090816200004a9190620003ae565b506040518060400160405280600381526020017f4d4854000000000000000000000000000000000000000000000000000000000081525060019081620000919190620003ae565b50620f4240600255348015620000a657600080fd5b50600254600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555033600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555062000495565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620001b657607f821691505b602082108103620001cc57620001cb6200016e565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620002367fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620001f7565b620002428683620001f7565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006200028f6200028962000283846200025a565b62000264565b6200025a565b9050919050565b6000819050919050565b620002ab836200026e565b620002c3620002ba8262000296565b84845462000204565b825550505050565b600090565b620002da620002cb565b620002e7818484620002a0565b505050565b5b818110156200030f5762000303600082620002d0565b600181019050620002ed565b5050565b601f8211156200035e576200032881620001d2565b6200033384620001e7565b8101602085101562000343578190505b6200035b6200035285620001e7565b830182620002ec565b50505b505050565b600082821c905092915050565b6000620003836000198460080262000363565b1980831691505092915050565b60006200039e838362000370565b9150826002028217905092915050565b620003b98262000134565b67ffffffffffffffff811115620003d557620003d46200013f565b5b620003e182546200019d565b620003ee82828562000313565b600060209050601f83116001811462000426576000841562000411578287015190505b6200041d858262000390565b8655506200048d565b601f1984166200043686620001d2565b60005b82811015620004605784890151825560018201915060208501945060208101905062000439565b868310156200048057848901516200047c601f89168262000370565b8355505b6001600288020188555050505b505050505050565b61095780620004a56000396000f3fe608060405234801561001057600080fd5b50600436106100625760003560e01c806306fdde031461006757806318160ddd1461008557806370a08231146100a35780638da5cb5b146100d357806395d89b41146100f1578063a9059cbb1461010f575b600080fd5b61006f61012b565b60405161007c91906105d2565b60405180910390f35b61008d6101b9565b60405161009a919061060d565b60405180910390f35b6100bd60048036038101906100b8919061068b565b6101bf565b6040516100ca919061060d565b60405180910390f35b6100db610208565b6040516100e891906106c7565b60405180910390f35b6100f961022e565b60405161010691906105d2565b60405180910390f35b6101296004803603810190610124919061070e565b6102bc565b005b600080546101389061077d565b80601f01602080910402602001604051908101604052809291908181526020018280546101649061077d565b80156101b15780601f10610186576101008083540402835291602001916101b1565b820191906000526020600020905b81548152906001019060200180831161019457829003601f168201915b505050505081565b60025481565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6001805461023b9061077d565b80601f01602080910402602001604051908101604052809291908181526020018280546102679061077d565b80156102b45780601f10610289576101008083540402835291602001916102b4565b820191906000526020600020905b81548152906001019060200180831161029757829003601f168201915b505050505081565b80600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561033e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610335906107fa565b60405180910390fd5b6103626040518060600160405280602481526020016108fe60249139338484610477565b80600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546103b19190610849565b9250508190555080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610407919061087d565b925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8360405161046b919061060d565b60405180910390a35050565b6105138484848460405160240161049194939291906108b1565b6040516020818303038152906040527f8ef3f399000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050610519565b50505050565b60008151905060006a636f6e736f6c652e6c6f679050602083016000808483855afa5050505050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561057c578082015181840152602081019050610561565b60008484015250505050565b6000601f19601f8301169050919050565b60006105a482610542565b6105ae818561054d565b93506105be81856020860161055e565b6105c781610588565b840191505092915050565b600060208201905081810360008301526105ec8184610599565b905092915050565b6000819050919050565b610607816105f4565b82525050565b600060208201905061062260008301846105fe565b92915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006106588261062d565b9050919050565b6106688161064d565b811461067357600080fd5b50565b6000813590506106858161065f565b92915050565b6000602082840312156106a1576106a0610628565b5b60006106af84828501610676565b91505092915050565b6106c18161064d565b82525050565b60006020820190506106dc60008301846106b8565b92915050565b6106eb816105f4565b81146106f657600080fd5b50565b600081359050610708816106e2565b92915050565b6000806040838503121561072557610724610628565b5b600061073385828601610676565b9250506020610744858286016106f9565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061079557607f821691505b6020821081036107a8576107a761074e565b5b50919050565b7f4e6f7420656e6f75676820746f6b656e73000000000000000000000000000000600082015250565b60006107e460118361054d565b91506107ef826107ae565b602082019050919050565b60006020820190508181036000830152610813816107d7565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610854826105f4565b915061085f836105f4565b92508282039050818111156108775761087661081a565b5b92915050565b6000610888826105f4565b9150610893836105f4565b92508282019050808211156108ab576108aa61081a565b5b92915050565b600060808201905081810360008301526108cb8187610599565b90506108da60208301866106b8565b6108e760408301856106b8565b6108f460608301846105fe565b9594505050505056fe5472616e7366657272696e672066726f6d20257320746f20257320257320746f6b656e73a26469706673582212202b51450482cc8f627513f615b0689c408a2debabc4fa7d0f9a03e5a982e487f564736f6c63430008110033",
  "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100625760003560e01c806306fdde031461006757806318160ddd1461008557806370a08231146100a35780638da5cb5b146100d357806395d89b41146100f1578063a9059cbb1461010f575b600080fd5b61006f61012b565b60405161007c91906105d2565b60405180910390f35b61008d6101b9565b60405161009a919061060d565b60405180910390f35b6100bd60048036038101906100b8919061068b565b6101bf565b6040516100ca919061060d565b60405180910390f35b6100db610208565b6040516100e891906106c7565b60405180910390f35b6100f961022e565b60405161010691906105d2565b60405180910390f35b6101296004803603810190610124919061070e565b6102bc565b005b600080546101389061077d565b80601f01602080910402602001604051908101604052809291908181526020018280546101649061077d565b80156101b15780601f10610186576101008083540402835291602001916101b1565b820191906000526020600020905b81548152906001019060200180831161019457829003601f168201915b505050505081565b60025481565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6001805461023b9061077d565b80601f01602080910402602001604051908101604052809291908181526020018280546102679061077d565b80156102b45780601f10610289576101008083540402835291602001916102b4565b820191906000526020600020905b81548152906001019060200180831161029757829003601f168201915b505050505081565b80600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561033e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610335906107fa565b60405180910390fd5b6103626040518060600160405280602481526020016108fe60249139338484610477565b80600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546103b19190610849565b9250508190555080600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610407919061087d565b925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8360405161046b919061060d565b60405180910390a35050565b6105138484848460405160240161049194939291906108b1565b6040516020818303038152906040527f8ef3f399000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050610519565b50505050565b60008151905060006a636f6e736f6c652e6c6f679050602083016000808483855afa5050505050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561057c578082015181840152602081019050610561565b60008484015250505050565b6000601f19601f8301169050919050565b60006105a482610542565b6105ae818561054d565b93506105be81856020860161055e565b6105c781610588565b840191505092915050565b600060208201905081810360008301526105ec8184610599565b905092915050565b6000819050919050565b610607816105f4565b82525050565b600060208201905061062260008301846105fe565b92915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006106588261062d565b9050919050565b6106688161064d565b811461067357600080fd5b50565b6000813590506106858161065f565b92915050565b6000602082840312156106a1576106a0610628565b5b60006106af84828501610676565b91505092915050565b6106c18161064d565b82525050565b60006020820190506106dc60008301846106b8565b92915050565b6106eb816105f4565b81146106f657600080fd5b50565b600081359050610708816106e2565b92915050565b6000806040838503121561072557610724610628565b5b600061073385828601610676565b9250506020610744858286016106f9565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061079557607f821691505b6020821081036107a8576107a761074e565b5b50919050565b7f4e6f7420656e6f75676820746f6b656e73000000000000000000000000000000600082015250565b60006107e460118361054d565b91506107ef826107ae565b602082019050919050565b60006020820190508181036000830152610813816107d7565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610854826105f4565b915061085f836105f4565b92508282039050818111156108775761087661081a565b5b92915050565b6000610888826105f4565b9150610893836105f4565b92508282019050808211156108ab576108aa61081a565b5b92915050565b600060808201905081810360008301526108cb8187610599565b90506108da60208301866106b8565b6108e760408301856106b8565b6108f460608301846105fe565b9594505050505056fe5472616e7366657272696e672066726f6d20257320746f20257320257320746f6b656e73a26469706673582212202b51450482cc8f627513f615b0689c408a2debabc4fa7d0f9a03e5a982e487f564736f6c63430008110033",
  "linkReferences": {},
  "deployedLinkReferences": {}
}

blockchain

区块链基础知识 交易 递归长度前缀 (RLP) ABI Gas