Liquidity mining encourages users to pledge tokens and pledge vouchers to liquidity mining contracts. For users, using DeFi will not only gain the original profits, but also obtain liquidity mining rewards. Inspired by liquidity mining, it has promoted users to become the LP of DeFi and promoted the rapid growth of DeFi.
首先调用UniswapV3Factory.getPool方法查看交易对是否已经创建,getPool函数是solidity自动为UniswapV3Factory合约中的状态变量getPool生成的外部函数,getPool的数据类型为:
contract UniswapV3Factory is IUniswapV3Factory,UniswapV3PoolDeployer,NoDelegateCall{
...
mapping(address=>mapping(address=>mapping(uint24=>address)))public override getPool;
...
}
使用3个map说明了v3版本使用(tokenA,tokenB,fee)来作为一个交易对的键,即相同代币,不同费率之间的流动池不一样。另外对于给定的tokenA和tokenB,会先将其地址排序,将地址值更小的放在前,这样方便后续交易池的查询和计算。
再来看UniswapV3Factory创建交易对的过程,实际上它是调用deploy函数完成交易对的创建:
function deploy(
address factory,
address token0,
address token1,
uint24 fee,
int24 tickSpacing
)internal returns(address pool){
parameters=Parameters({factory:factory,token0:token0,token1:token1,fee:fee,tickSpacing:tickSpacing});
pool=address(new UniswapV3Pool{salt:keccak256(abi.encode(token0,token1,fee))}());
delete parameters;
}
createAndInitializePoolIfNecessary如下:
function createAndInitializePoolIfNecessary(
address tokenA,
address tokenB,
uint24 fee,
uint160 sqrtPriceX96
)external payable returns(address pool){
pool=IUniswapV3Factory(factory).getPool(tokenA,tokenB,fee);
if(pool==address(0)){
pool=IUniswapV3Factory(factory).createPool(tokenA,tokenB,fee);
IUniswapV3Pool(pool).initialize(sqrtPriceX96);
}else{
(uint160 sqrtPriceX96Existing,,,,,,)=IUniswapV3Pool(pool).slot0();
if(sqrtPriceX96Existing==0){
IUniswapV3Pool(pool).initialize(sqrtPriceX96);
}
}