简体   繁体   中英

Questions about ABI in Solidity

I'm studying Uniswapv2 codes and I got stucked with ABI. 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?
    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. What is right?

You can use short form, like your example, without full ABI.

ABI for web3js is JSON form. Its used for simply call all existed functions and parameters. If you use many function and parameters -- JSON ABI much better for use.

You used short hacked version with selector - this is not good readable style of code.

1-

I believe token.call(abi.encodeWithSelector(SELECTOR, to, value)); token is another contract instance and called inside a contract. call is used to execute code of another contract

2-

The ABI encodes information about smart contracts' functions and events. It acts as an interface between EVM-level bytecode and high-level smart contract program code. To interact with a smart contract deployed on the Ethereum blockchain, external programs require an ABI and the address of the smart contract. The ABI consists of the following:

• All function names

• Input and output types of functions

• 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.bin: This contains the hex representation of binary of the smart contract code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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