Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonfungiblePositionManager.createAndInitializePoolIfNecessary throws EvmError: Revert #386

Open
Nlferu opened this issue May 25, 2024 · 0 comments

Comments

@Nlferu
Copy link

Nlferu commented May 25, 2024

Hello,

I have just encountered some very strange bug/issue? While playing with NonfungiblePositionManager contract. I have wrote solidity code that creates new ERC20 contract and then calling function dexCoin() from another my contract that takes this newly created ERC20 contract address and calls below:

INonfungiblePositionManager(i_nftPositionManager).createAndInitializePoolIfNecessary(coinAddress, i_wrappedNativeToken, FEE, INITIAL_PRICE);

I have then wrote some simple test using foundry to check if everything works and indeed it was until I ran same code on forked Ethereum Mainnet. So the problem occurs on every single forked mainnet I tested (Ethereum Mainnet, Polygon, Avalanche), there is no issue on testnets like Sepolia.

This is contract code I'm using to start calls chain:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Coin} from "./Coin.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ICoinDexer} from "./Interfaces/ICoinDexer.sol";
import {ICoinMinter} from "./Interfaces/ICoinMinter.sol";

contract CoinMinter is ICoinMinter, Ownable {
    /// @dev Constructor
    /// @notice Owner of this contract is DFM contract
    constructor() Ownable(msg.sender) {}

    /////////////////////// @notice MCM External Functions (Callable only by DFM contract) ///////////////////////

    /// @notice Deploys new ERC20  Token
    /// @param params ICoinMinter
    function mintCoinAndRequestDex(MintParams calldata params) external onlyOwner {
        /// @dev BUG HERE:
        Coin newCoin = new Coin(params);

        emit CoinMinted(address(newCoin), params.name, params.symbol);

        ICoinDexer(params.mcd).dex(address(newCoin), params.totalFunds, params.totalCoins);

        emit ICoinDexer.DexRequestReceived(address(newCoin));
    }
}

If i run my test on forked mainnet I'm getting those logs:

emit DexRequestReceived(token: Coin: [0xc3B785939FE923f3EA338Ae6793C99de2671b2cB])
    │   │   │   ├─ [23974] WETH9::deposit{value: 11000000000000000000}()
    │   │   │   │   ├─ emit Deposit(dst: CoinDexer: [0xaBC27F4fa9dae4829575A3DF7958b9d80872c8a8], wad: 11000000000000000000 [1.1e19])
    │   │   │   │   └─ ← [Stop] 
    │   │   │   ├─ [534] WETH9::balanceOf(CoinDexer: [0xaBC27F4fa9dae4829575A3DF7958b9d80872c8a8]) [staticcall]
    │   │   │   │   └─ ← [Return] 11000000000000000000 [1.1e19]
    │   │   │   ├─ emit Swapped_ETH_For_WETH(amount: 11000000000000000000 [1.1e19])
    │   │   │   ├─ [674] NonfungiblePositionManager::createAndInitializePoolIfNecessary(Coin: [0xc3B785939FE923f3EA338Ae6793C99de2671b2cB], WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], 3000, 7922816251426433759354395 [7.922e24])
    │   │   │   │   └─ ← [Revert] EvmError: Revert

But if i do this in contract above:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Coin} from "./Coin.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ICoinDexer} from "./Interfaces/ICoinDexer.sol";
import {ICoinMinter} from "./Interfaces/ICoinMinter.sol";

contract CoinMinter is ICoinMinter, Ownable {
    /// @dev Constructor
    /// @notice Owner of this contract is DFM contract
    constructor() Ownable(msg.sender) {}

    /////////////////////// @notice MCM External Functions (Callable only by DFM contract) ///////////////////////

    /// @notice Deploys new ERC20  Token
    /// @param params ICoinMinter
    function mintCoinAndRequestDex(MintParams calldata params) external onlyOwner {
        /// @dev BUG HERE:
        new Coin(params); /// @dev NOTCICE I JUST ADDED THIS LINE
        Coin newCoin = new Coin(params);

        emit CoinMinted(address(newCoin), params.name, params.symbol);

        ICoinDexer(params.mcd).dex(address(newCoin), params.totalFunds, params.totalCoins);

        emit ICoinDexer.DexRequestReceived(address(newCoin));
    }
}

so i just added this line new Coin(params); and i run same test without changing anything else in code it works perfectly fine:

emit DexRequestReceived(token: Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B])
    │   │   │   ├─ [23974] WETH9::deposit{value: 11000000000000000000}()
    │   │   │   │   ├─ emit Deposit(dst: CoinDexer: [0xaBC27F4fa9dae4829575A3DF7958b9d80872c8a8], wad: 11000000000000000000 [1.1e19])
    │   │   │   │   └─ ← [Stop] 
    │   │   │   ├─ [534] WETH9::balanceOf(CoinDexer: [0xaBC27F4fa9dae4829575A3DF7958b9d80872c8a8]) [staticcall]
    │   │   │   │   └─ ← [Return] 11000000000000000000 [1.1e19]
    │   │   │   ├─ emit Swapped_ETH_For_WETH(amount: 11000000000000000000 [1.1e19])
    │   │   │   ├─ [4649797] NonfungiblePositionManager::createAndInitializePoolIfNecessary(Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B], WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], 3000, 7922816251426433759354395 [7.922e24])
    │   │   │   │   ├─ [2666] UniswapV3Factory::getPool(Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B], WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], 3000) [staticcall]
    │   │   │   │   │   └─ ← [Return] 0x0000000000000000000000000000000000000000
    │   │   │   │   ├─ [4593296] UniswapV3Factory::createPool(Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B], WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], 3000)
    │   │   │   │   │   ├─ [4435593] → new <unknown>@0xecC6B9DA20A905e5e37bd6f10D8911EA8659f05d
    │   │   │   │   │   │   ├─ [734] UniswapV3Factory::parameters() [staticcall]
    │   │   │   │   │   │   │   └─ ← [Return] UniswapV3Factory: [0x1F98431c8aD98523631AE4a59f267346ea31F984], Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B], WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], 3000, 60
    │   │   │   │   │   │   └─ ← [Return] 22142 bytes of code
    │   │   │   │   │   ├─ emit PoolCreated(token0: Coin: [0x2758Cea4179785155297c7C795B881D8677Dd18B], token1: WETH9: [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2], fee: 3000, tickSpacing: 60, pool: 0xecC6B9DA20A905e5e37bd6f10D8911EA8659f05d)
    │   │   │   │   │   └─ ← [Return] 0xecC6B9DA20A905e5e37bd6f10D8911EA8659f05d

So to sum up the only thing that changed is that ERC20 contract address. I have noticed that createAndInitializePoolIfNecessary has problem with some certain addresses and only while testing on mainnets. I have even reproduced this error using different private key and creating additional unnecessary coin as above but it didnt solve issue, so i guess address generated was incorrect for some reason. Solution I showed works only for some private keys and if it pass on ethereum mainnet it will also pass on any other mainnet and testnet. Am I missing some additional requirements to create and initialize pool? Maybe it is just foundry bug and it is creating some corrupted addresses (I highly doubt)? Or it just needs some specific ERC20 address format.

If anyone has any idea how this can be fixed or can explain to me where is the problem I will be very grateful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant