Skip to content

normanrichardson/uniswap_testing

Repository files navigation

Testing the uniswap smart contracts

This builds on the tutorials for a single swap and providing liquidity. Tests are run using hardhat and forking the Mainnet.

Tips

  • It was easier to work with existing pairs of assets. Experimentation is required to see if you can create your own pool with new ERC20 tokens. There may be some understanding to gain about how Uniswap works by doing this.

  • It was easier to fork Mainnet than say Rinkeby as

    • it is easier to find existing asset addresses on Mainnet.
    • it is easier to find out what pools exist and their parameters.
  • Each pool has a defined "tick spacing" (see for example the defaults here). The tick spacing is used to discretize the price/liquidity space. If a liquidity position is requested that does not cover an integer number of tick spacings, the transaction reverts with no error message. This applies to all ticklower and tickupper, so using tickLower: TickMath.MIN_TICK, tickUpper: TickMath.MAX_TICK, does not actually work for the 3000 pool.

  • Each ERC20 token has its own precision, see the decimals function. Stablecoins such as DAI and USDC have a different number of decimals, so 1 token of DAI is not equal to 1 token on USDC.

  • In providing liquidity there is a bug in the example code. When decreasing the liquidity (via the nonfungiblePositionManager.decreaseLiquidity) the tokens are not transferred to the calling contract, but rather placed in the tokens owed field of the position. So before trying to transfer tokens to the owner, the tokens need to be collected.

Set up

  1. Set up an Alchemy account

Create an alchemy account. Create an app on alchemy that uses the ethereum chain and mainnet network. Get the api key for the alchemy app. This api key will give you access to fork Mainnet.

  1. Clone and navigate to a local working directory

  2. Create a .env file

$touch .env

The file should contain:

ALCHEMY_URL=https://eth-mainnet.alchemyapi.io/v2/{ API KEY }
  1. Install dependencies
$npm install
  1. Compile the solidity contract using hardhat
$npx hardhat compile
  1. Run tests using hardhat
$npx hardhat test

Useful resources

About

Testing uniswap swapping and liquidity positions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published