简体   繁体   English

UniswapV3 position 铸币算法无法根据我传入的参数获得正确的矿池地址

[英]UniswapV3 position minting algorithm can't get correct pool address based on params I've passed in

Uniswap V3 PoolAddress.computeAddress computes different address comparing to the one I get from UniswapV3poolFactory.getPool . Uniswap V3 PoolAddress.computeAddress计算的地址与我从UniswapV3poolFactory.getPool获得的地址不同。 And in my code it is ok, I can get pool address from this mapping, but uniswap contracts uses PoolAddress library in order to get pool address.在我的代码中没关系,我可以从这个映射中获取池地址,但是 uniswap 合约使用PoolAddress库来获取池地址。

When I try to mint new position using mint function from NonfungiblePositionManager.sol this function calls addLiquidity from LiquidityManagement.sol and this function has call to the PoolAddress.sol library method computeAddress . When I try to mint new position using mint function from NonfungiblePositionManager.sol this function calls addLiquidity from LiquidityManagement.sol and this function has call to the PoolAddress.sol library method computeAddress . And there it throws because after computing pool address it tries to call methods from pool but it can't cause address is wrong.它在那里抛出,因为在计算pool地址之后它尝试从pool调用方法,但它不能导致地址错误。

I've tried to change the order of the tokens that I pass to the mint function but the order is correct, I've also logged all of the data that is related to pool address computation in uniswap contracts using hardhat local chain and it is the same to the one I used for getPool mapping.我尝试更改传递给mint function 的令牌的顺序,但顺序是正确的,我还使用安全帽本地链记录了与 uniswap 合约中的池地址计算相关的所有数据,它是与我用于getPool映射的那个相同。 The only thing that still can cause wrong computations on my opinion is the POOL_INIT_CODE_HASH constant in PoolAddress library, but I haven't changed that.在我看来,唯一仍然会导致错误计算的是PoolAddress库中的POOL_INIT_CODE_HASH常量,但我没有改变它。 All mentioned methods I'll post below the text along with links to uniswap repos.我将在文本下方发布所有提到的方法以及指向 uniswap 存储库的链接。 Compiler version that I've used for all unsiwap contracts is 0.7.6 .我用于所有 unsiwap 合约的编译器版本是0.7.6 If anyone got any idea how to solve this, please, let me know.如果有人知道如何解决这个问题,请告诉我。

v3-core v3-核心

v3-periphery v3-外围

NonfungiblePositionManager.sol NonfungiblePositionManager.sol

LiquidityManagement.sol 流动性管理.sol

PoolAddress.sol 池地址.sol

NonfungiblePositionManager.sol

function mint(MintParams calldata params)
    external
    payable
    override
    checkDeadline(params.deadline)
    returns (
      uint256 tokenId,
      uint128 liquidity,
      uint256 amount0,
      uint256 amount1
    )
  {
    IUniswapV3Pool pool;
    

// it throws
---> (liquidity, amount0, amount1, pool) = addLiquidity(
      AddLiquidityParams({
        token0: params.token0,
        token1: params.token1,
        fee: params.fee,
        recipient: address(this),
        tickLower: params.tickLower,
        tickUpper: params.tickUpper,
        amount0Desired: params.amount0Desired,
        amount1Desired: params.amount1Desired,
        amount0Min: params.amount0Min,
        amount1Min: params.amount1Min
      })
    );
    

    _mint(params.recipient, (tokenId = _nextId++));

    bytes32 positionKey = PositionKey.compute(address(this), params.tickLower, params.tickUpper);
    (, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128, , ) = pool.positions(positionKey);
    
    // idempotent set
    uint80 poolId = cachePoolKey(
      address(pool),
      PoolAddress.PoolKey({token0: params.token0, token1: params.token1, fee: params.fee})
    );
    
    _positions[tokenId] = Position({
      nonce: 0,
      operator: address(0),
      poolId: poolId,
      tickLower: params.tickLower,
      tickUpper: params.tickUpper,
      liquidity: liquidity,
      feeGrowthInside0LastX128: feeGrowthInside0LastX128,
      feeGrowthInside1LastX128: feeGrowthInside1LastX128,
      tokensOwed0: 0,
      tokensOwed1: 0
    });
    
    emit IncreaseLiquidity(tokenId, liquidity, amount0, amount1);
  }
LiquidityManagement.sol

function addLiquidity(AddLiquidityParams memory params)
    internal
    returns (
      uint128 liquidity,
      uint256 amount0,
      uint256 amount1,
      IUniswapV3Pool pool
    )
  {
    PoolAddress.PoolKey memory poolKey = PoolAddress.PoolKey({
      token0: params.token0,
      token1: params.token1,
      fee: params.fee
    });
    

    // here is the computation of pool address
---> pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));

    // all subsequent operations fails 

    // compute the liquidity amount
    {
      (uint160 sqrtPriceX96, , , , , , ) = pool.slot0();
      console.log("liquidity 4");
      uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(params.tickLower);
      console.log("liquidity 5");
      uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(params.tickUpper);
      console.log("liquidity 6");

      liquidity = LiquidityAmounts.getLiquidityForAmounts(
        sqrtPriceX96,
        sqrtRatioAX96,
        sqrtRatioBX96,
        params.amount0Desired,
        params.amount1Desired
      );
      console.log("liquidity 7");
    }

    (amount0, amount1) = pool.mint(
      params.recipient,
      params.tickLower,
      params.tickUpper,
      liquidity,
      abi.encode(MintCallbackData({poolKey: poolKey, payer: msg.sender}))
    );
    

    require(amount0 >= params.amount0Min && amount1 >= params.amount1Min, "Price slippage check");
  }
PoolAddress.sol

  bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;


  function computeAddress(address factory, PoolKey memory key) internal view returns (address pool) {
    require(key.token0 < key.token1);
    pool = address(
      // uint160(
      uint256(
        keccak256(
          abi.encodePacked(
            hex"ff",
            factory,
            keccak256(abi.encode(key.token0, key.token1, key.fee)),
            POOL_INIT_CODE_HASH
          )
        )
      )
      // )
    );
  }

暂无
暂无

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

相关问题 当我的余额比原始余额减少 10% 时,如何打破这个 UniswapV3 机器人的循环 - How do i break the loop for this UniswapV3 bot when my balance has decreased by 10% from original balance 如果我使用参数创建了多链节点,那么任何人都可以连接,现在可以更改它吗? - If I've created a multichain node using params anyone can connect can i change it now? 铸造后我可以更新 NFT 的元数据吗? - Can I update NFT's metadata after minting? 铸币完成后如何获得交易收据 - How to get a transaction receipt once minting is done 我一直在尝试实现 DApp 浏览器,但无法理解移动应用程序如何与浏览器通信 - I've been trying to implement a DApp browser, but can't understand how the Mobile application is able to communicate with the browser 允许某人输入他们的年薪并获得财务建议的 Solidity 代码。 我无法输入薪水并根据输入获得结果 - Solidity code that allows someone to input their yearly salary and get financial advice. I can't input salary and get result based on the input 我无法通过QBitNinjaClient获取交易结果 - I can't get Transaction Result with QBitNinjaClient 如何在 RIDE 中从 tx.sender 获取字符串地址? - How can I get string address from tx.sender in RIDE? 在浏览器或chrome扩展程序中确认交易之前如何获取web3合约地址? - How can I get the web3 contract address before confirming transaction in the browser or chrome extension? 如何使用 @solana/web3.js 从 Solana 中的自定义令牌中删除铸币权限? - How do I remove the minting authority from my custom token in Solana using @solana/web3.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM