Skip to content

sambacha/foundry-tutorial

Repository files navigation

Foundry Tutorial

foundry - forge foundry - cast


Overview

UniswapV3 Liquidity Testing

forge test -f $ETH_RPC_URL -vvv --fork-block-number 14327540

Test Pattern: Console Emit

  // from src/test/UniswapV3.sol
  emit log_named_uint("amount0 (USDC)", amount0);
  emit log_named_uint("amount1 (ETH)", amount1);

  emit log("------- amount0/amount1 for liquidity at range [lower, upper] at LOWER price");
  emit log_named_uint("lowerPrice (ETH/USDC)", token0Price(sqrtPriceLowerX96));

Example

$ forge test -f $ETH_RPC_URL -vvv --fork-block-number 14327540
Compiling...
No files changed, compilation skipped

Running 1 test for UniswapV3Test.json:UniswapV3Test
[PASS] testExample() (gas: 36365)
Logs:
  ------- amount0/amount1 for liquidity at range [lower, upper] at CURRENT price
  currentPrice (ETH/USDC): 378261998000000
  amount0 (USDC): 9409580115
  amount1 (ETH): 5698544927171011234
  ------- amount0/amount1 for liquidity at range [lower, upper] at LOWER price
  lowerPrice (ETH/USDC): 189073881000000
  amount0 (USDC): 30718029649
  amount1 (ETH): 0
  ------- amount0/amount1 for liquidity at range [lower, upper] at UPPER price
  upperPrice (ETH/USDC): 566703047000000
  amount0 (USDC): 0
  amount1 (ETH): 10055111697431443031

License

Foundry is released under MIT/Apache-20 by @gakonst
Solidity contracts are Copyright 2021 by @MrToph