需要匹配最佳的池子,那需要列出所有关于这个代币的池子交易对,需要怎么获取?
目前只会通过工厂合约 getPair 获取,那这样也只是单个,不知道其余池子的配对合约
Pancake的交易对都是从Factory合约创建出来的,也可以从Factory合约获取,Facotory合约代码在这里
https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/PancakeFactory.sol
关键代码:
mapping(address => mapping(address => address)) public getPair;
address[] public allPairs;
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
constructor(address _feeToSetter) public {
feeToSetter = _feeToSetter;
}
function allPairsLength() external view returns (uint) {
return allPairs.length;
}
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);
}
从上面的代码可见,allPairs就是所有的交易对,另外,也可以通过PairCreated事件获取。