1

I wrote the following function to swap DAI to an ERC20 token on Rinkeby. It worked perfectly for some time and then it started to revert without any message when trying to swap DAI to UNI on Rinkeby. There is a pool available. I check that first. And the function still works if I try to swap DAI to WETH. So I assume the error must be linked somehow to the DAI/UNI pool. Here are the addresses used on Rinkeby:

DAI "0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa" UNI "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984" Pooladdress: 0x1dB796Ed3113a7EDfF2d0ad272705A3fd4642f9d

Any ideas?

// 4. Initial buy of token to be traded
// As long as there are funds of baseCurrency available you can invest in token
  function initialAssetBuy(address _tokenAddress, uint _value) external onlyOwner() {
      require(block.timestamp >= contributionEnd, "Contributtion period not yet passed");
      require(getTokenBalance(DAI) > 0, "No more token of baseCurrency available in this contract");
      require(_value > 0, "Each registered asset must get a value of baseCurrency");
      require(_tokenAddress != DAI, "You can not trade baseCurrency into baseCurrency");

      (bool _success, uint24 _poolFee) = _uniswapV3PoolExists(DAI, _tokenAddress);
      require(_success, "Pool does not exist");

      uint _amountIn = _value;

      ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
        tokenIn: DAI,
        tokenOut: _tokenAddress,
        fee: _poolFee,
        recipient: address(this),
        deadline: block.timestamp + 15,
        amountIn: _amountIn,
        amountOutMinimum: 1,
        sqrtPriceLimitX96: 0
      });

      // Approve the SwapRouter contract for the amount of DAI
      // I first made an error to approve this contract - that's wrong, the DAI was already tranfered to this contract in contribute()
      IERC20(DAI).approve(address(SwapRouter), _amountIn);

      uint _amountOut = SwapRouter.exactInputSingle(params);

      uint _price = uint(_amountIn / _amountOut);


      assets[nextAssetId] = Asset(nextAssetId, getTokenSticker(_tokenAddress), _tokenAddress, _price, 0);
      nextAssetId ++;

      currentState = State.TRADING;

  }
2
  • Do the pool have enough liquidity? Can you provide a link to a failed transaction?
    – Ismael
    Jan 27, 2022 at 5:56
  • I also thought that it reverts because of missing liquidity. But the pool has a little amount for both tokens (DAI and UNI). Here the hash of the reverted TX on Rinkeby 0x342a7d0b67cc5d6c873f10e0c99435e7d935cefeb38ebda85d6d571cb28cb757 AND the pool address 0x1dB796Ed3113a7EDfF2d0ad272705A3fd4642f9d Jan 27, 2022 at 17:13

1 Answer 1

1

After some tests I finally come to the conclusion the the tx reverted because the pool did not have enough liquidity. There was some liquidity in the pool for both tokens which confused me first. But the level of liquidity was too low.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.