简体   繁体   English

关于 Solidity 中 ABI 的问题

[英]Questions about ABI in Solidity

I'm studying Uniswapv2 codes and I got stucked with ABI.我正在研究 Uniswapv2 代码,但我被 ABI 困住了。 https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol

  1. Why use ABI with call method, even if we can call transfer function from interface directly?即使我们可以直接从接口调用transfer function,为什么还要使用带有call方法的 ABI?
    bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));
.
.
.
    function _safeTransfer(address token, address to, uint value) private {
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
    }
  1. I've read a lot about ABI, and I'm confused about some says that it is JSON format and some says it is byte form.我已经阅读了很多关于 ABI 的内容,我对有些人说它是 JSON 格式而有些人说它是字节格式感到困惑。 What is right?什么是对的?

You can use short form, like your example, without full ABI.您可以使用简短的形式,如您的示例,无需完整的 ABI。

ABI for web3js is JSON form. web3js 的 ABI 是 JSON 形式。 Its used for simply call all existed functions and parameters.它用于简单地调用所有现有的函数和参数。 If you use many function and parameters -- JSON ABI much better for use.如果您使用许多 function 和参数 -- JSON ABI 更适合使用。

You used short hacked version with selector - this is not good readable style of code.您使用了带有选择器的短黑客版本 - 这不是很好的可读代码风格。

1- 1-

I believe token.call(abi.encodeWithSelector(SELECTOR, to, value));我相信token.call(abi.encodeWithSelector(SELECTOR, to, value)); token is another contract instance and called inside a contract. token 是另一个合约实例,在合约内部调用。 call is used to execute code of another contract call用于执行另一个合约的代码

2- 2-

The ABI encodes information about smart contracts' functions and events. ABI 对有关智能合约功能和事件的信息进行编码。 It acts as an interface between EVM-level bytecode and high-level smart contract program code.它充当 EVM 级字节码和高级智能合约程序代码之间的接口。 To interact with a smart contract deployed on the Ethereum blockchain, external programs require an ABI and the address of the smart contract.为了与部署在以太坊区块链上的智能合约进行交互,外部程序需要 ABI 和智能合约的地址。 The ABI consists of the following: ABI 由以下部分组成:

• All function names • 所有 function 名称

• Input and output types of functions • 输入和output类功能

• All event names and their parameters • 所有事件名称及其参数

Contract has hex representation and binary representation:合约有十六进制表示和二进制表示:

  • Contract.abi: This contains the ABI of the smart contract in JSON format. Contract.abi:这包含 JSON 格式的智能合约的 ABI。

  • Contract.bin: This contains the hex representation of binary of the smart contract code. Contract.bin:这包含智能合约代码二进制的十六进制表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM