0

defi | pair 的 token0 和 token1

 1 year ago
source link: https://benpaodewoniu.github.io/2022/06/30/defi27/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

defi | pair 的 token0 和 token1

看一下工厂合约的 pairtoken0token1 的细节。

function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'Pancake: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'Pancake: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'Pancake: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(PancakePair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IPancakePair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
}
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);

这句话的意思是地址更小的放在 token0

看下面的例子

AVXL = "0xBD29490383edFd560426C3B63d01534408bC2da6"
BNB = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
AVXL_BNB_pair = 0x0cA8Eafd9b851228c0C7A65a43aded02B964dfD6
token0 = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"
token1 = "0xbd29490383edfd560426c3b63d01534408bc2da6"

其中 token0 < token1,但是如果使用 Web3.toChecksumAddress(address) 进行转化的话。

Web3.toChecksumAddress(token0) > Web3.toChecksumAddress(token1)

所以,比较大小的时候,双方比较的地址要么都是小写,要么,都是大写,不能转化为 ETH 大小写兼并的模式。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK