Skip to content

Commit

Permalink
chore: use fees as percentage as it's easier to type for users
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Oct 19, 2023
1 parent c3e98f0 commit 2cd6c51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
15 changes: 8 additions & 7 deletions cmd/loadtest/uniswapv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ var uniswapV3LoadTestCmd = &cobra.Command{

func checkFlags() error {
// Check pool fees.
switch fees := int64(*uniswapv3LoadTestParams.PoolFees); fees {
case int64(uniswapv3loadtest.StableTier), int64(uniswapv3loadtest.StandardTier), int64(uniswapv3loadtest.ExoticTier):
switch fees := *uniswapv3LoadTestParams.PoolFees; fees {
case float64(uniswapv3loadtest.StableTier), float64(uniswapv3loadtest.StandardTier), float64(uniswapv3loadtest.ExoticTier):
// Fees are correct, do nothing.
default:
return fmt.Errorf("UniswapV3 only supports a few pool tiers which are stable (0.05%%): %d, standard (0.3%%): %d, and exotic (1%%): %d",
int64(uniswapv3loadtest.StableTier), int64(uniswapv3loadtest.StandardTier), int64(uniswapv3loadtest.ExoticTier))
return fmt.Errorf("UniswapV3 only supports a few pool tiers which are stable: %f%%, standard: %f%%, and exotic: %f%%",
float64(uniswapv3loadtest.StableTier), float64(uniswapv3loadtest.StandardTier), float64(uniswapv3loadtest.ExoticTier))
}

// Check swap amount input.
Expand Down Expand Up @@ -99,7 +99,8 @@ func validateUrl(input string) (*url.URL, error) {

type params struct {
UniswapFactoryV3, UniswapMulticall, UniswapProxyAdmin, UniswapTickLens, UniswapNFTLibDescriptor, UniswapNonfungibleTokenPositionDescriptor, UniswapUpgradeableProxy, UniswapNonfungiblePositionManager, UniswapMigrator, UniswapStaker, UniswapQuoterV2, UniswapSwapRouter, WETH9, UniswapPoolToken0, UniswapPoolToken1 *string
PoolFees, SwapAmountInput *uint64
PoolFees *float64
SwapAmountInput *uint64
}

func init() {
Expand All @@ -124,7 +125,7 @@ func init() {
params.UniswapPoolToken1 = uniswapV3LoadTestCmd.Flags().String("uniswap-pool-token-1-address", "", "The address of a pre-deployed ERC20 contract used in the Uniswap pool Token0 // Token1")

// Pool and swap parameters.
params.PoolFees = uniswapV3LoadTestCmd.Flags().Uint64P("pool-fees", "f", uniswapv3loadtest.PoolFees.Uint64(), "Trading fees charged on each swap or trade made within a UniswapV3 liquidity pool (e.g. 3000 means 0.3%)")
params.PoolFees = uniswapV3LoadTestCmd.Flags().Float64P("pool-fees", "f", float64(uniswapv3loadtest.StandardTier), "Trading fees charged on each swap or trade made within a UniswapV3 liquidity pool (e.g. 0.3 means 0.3%)")
params.SwapAmountInput = uniswapV3LoadTestCmd.Flags().Uint64P("swap-amount", "a", uniswapv3loadtest.SwapAmountInput.Uint64(), "The amount of inbound token given as swap input")

uniswapv3LoadTestParams = *params
Expand Down Expand Up @@ -155,7 +156,7 @@ func initUniswapV3Loadtest(ctx context.Context, c *ethclient.Client, tops *bind.
}

log.Debug().Msg("🎱 Deploying UniswapV3 liquidity pool...")
fees := big.NewInt(int64(*uniswapv3LoadTestParams.PoolFees))
fees := uniswapv3loadtest.PercentageToUniswapFeeTier(*uniswapv3LoadTestParams.PoolFees)
poolConfig = *uniswapv3loadtest.NewPool(token0, token1, fees)
if err = uniswapv3loadtest.SetupLiquidityPool(ctx, c, tops, cops, uniswapV3Config, poolConfig, recipient, blockUntilSuccessful); err != nil {
return
Expand Down
28 changes: 15 additions & 13 deletions cmd/loadtest/uniswapv3/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ import (
"github.com/rs/zerolog/log"
)

type feeTier int64

// Only a few fee tiers are possible in UniswapV3.
// https://docs.uniswap.org/concepts/protocol/fees
var (
StableTier feeTier = 50000 // 0.05%
StandardTier feeTier = 3000 // 0.3%
ExoticTier feeTier = 100 // 1%
)

var (
// Trading fees charged on each swap or trade made within a UniswapV3 liquidity pool.
PoolFees = big.NewInt(int64(StandardTier))

// Reserve of a token in a UniswapV3 pool.
poolReserveForOneToken = big.NewInt(1_000_000_000_000)

Expand All @@ -38,6 +25,21 @@ var (
// The maximum tick that may be passed to `getSqrtRatioAtTick` computed from log base 1.0001 of 2**128.
const maxTick = 887272

type feeTier float64

var (
// Only a few fee tiers are possible in UniswapV3. They are represented in percentage.
// https://uniswapv3book.com/docs/milestone_5/swap-fees/#accruing-swap-fees
StableTier feeTier = 0.05 // 500
StandardTier feeTier = 0.3 // 3_000
ExoticTier feeTier = 1 // 10_000
)

// PercentageToUniswapFeeTier takes a percentage and returns the corresponding UniswapV3 fee tier.
func PercentageToUniswapFeeTier(p float64) *big.Int {
return big.NewInt(int64(p * 100000))
}

// PoolConfig represents the configuration of a UniswapV3 pool.
type PoolConfig struct {
Token0, Token1 ContractConfig[uniswapv3.Swapper]
Expand Down

0 comments on commit 2cd6c51

Please sign in to comment.