KyberSwap Docs
  • Introduction to KyberSwap
  • Getting Started
    • Quickstart
      • FAQ
    • Supported Exchanges And Networks
    • Foundational Topics
      • Decentralized Finance
        • Tokens
        • Stablecoins
        • DEX/DeFi Aggregator
        • Slippage
        • Price Impact
        • Zap
        • Maximal Extractable Value (MEV)
      • Decentralized Technologies
        • Wallets
        • Dapps
        • RPC
        • Oracles
        • On-Chain vs Off-Chain Data
      • Other Valuable Resources
  • KyberSwap Solutions
    • KyberSwap Interface
      • User Guides
        • Connect Your Wallet
        • Switching Networks
        • Instantly Swap At Superior Rates
        • Swap At Your Preferred Rates
        • Add Your Favourite Tokens
        • Get Crypto With Fiat
        • Bridge Your Assets Across Multiple Chains
        • Swap Between Different Tokens Across Chains
      • Profiles
        • Profile Creation
        • Profile Customization
        • Sync Profile Across Devices
      • Notifications
        • Notification Center
    • KyberSwap Aggregator
      • Concepts
        • Dynamic Trade Routing
      • User Guides
        • Instantly Swap At Superior Rates
      • Developer Guides
        • Execute A Swap With The Aggregator API
        • Upgrading To APIv1
      • Aggregator API Specification
        • EVM Swaps
        • Permit
      • Contracts
        • Aggregator Contract Addresses
      • DEX IDs
      • Subgraphs
      • FAQ
    • KyberSwap Zap as a Service
      • KyberSwap Zap as a Service (ZaaS) API
        • ZaaS HTTP API
        • ZaaS GRPC API
      • KyberSwap Zap Liquidity Widget
      • Zap Fee Model
      • Zap's Supported Chains/Dexes
      • Zap's Deployed Contract Addresses
      • Zap's DEX IDs
    • KyberSwap Widget
      • Developer Guides
        • Integrating The KyberSwap Widget
        • Customizing The KyberSwap Widget
      • iFrame Alternative
      • Widget/iFrame Fee
    • KyberSwap Liquidity Widget
      • Integrating The KyberSwap Liquidity Widget
    • Limit Order
      • Concepts
        • Off-Chain Relay, On-Chain Settlement
        • Gasless Cancellation
      • User Guides
        • Swap At Your Preferred Rates
        • Update Limit Orders
        • Cancel Limit Orders
      • Developer Guides
        • Create Limit Order
        • Gasless Cancel
        • Hard Cancel
        • Fill Limit Order
      • Contracts
        • Limit Order Contract Addresses
      • Limit Order API Specification
        • General APIs
        • Maker APIs
        • Taker APIs
      • FAQ
    • KyberSwap OnChain Price Service
    • Fee Schedule
  • Governance
    • KyberDAO
      • User Guides
        • Participating in KyberDAO
        • Staking
        • Voting
        • Stake KNC And Enjoy Gas Savings
      • Fees to KyberDAO
      • KyberDAO Operator MultiSig
      • Contracts
        • KyberDAO Contract Repo
        • KyberDAO Contract Addresses
      • FAQ - Others
    • KNC Token
      • KNC Tokenomics & Utility
      • Gas Refund Program
      • KNC Contract Addresses
  • Security
    • Audits
  • Reference
    • Legacy
      • KyberSwap Classic
        • Concepts
          • Programmable Pricing Curves
          • Dynamic Auto-Adjusting Fees
          • Virtual Balances
          • Protocol Fees
        • Contracts
          • Classic Contract Repo
          • Classic Contract Addresses
          • Classic Contract Farming Addresses
      • KyberSwap Elastic
        • Concepts
          • Concentrated Liquidity
          • Reinvestment Curve
          • Tick-Range Mechanism
          • Pool Process Flows
          • Anti-Sniping Mechanism
          • Tick-Based Farming
          • Elastic Zap
          • TWAP Oracle
          • Elastic APR Calculations
        • Contracts
          • Elastic Contract Repo
          • Elastic Contract Addresses
          • Elastic Farming Contract Addresses
          • Elastic Zap Contract Addresses
          • Elastic Core Contracts
          • Elastic Core Libraries
          • Elastic Periphery Core Contracts
          • Elastic Peripheral Library Contracts
          • Elastic Peripheral Base Contracts
        • Subgraphs
      • Whitepapers
      • Audits
      • KyberAI
        • KyberScore
        • Concepts
        • On-Chain Indicators
          • Number Of Trades
          • Trading Volume
          • Netflow To Whale Wallets
          • Netflow To CEX
          • Number Of Transfers
          • Volume Of Transfers
          • Number Of Holders
          • Top Holders
        • Technical Indicators
          • Live Charts
          • Support & Resistance Levels
          • Live Trades
          • Funding Rate On CEX
          • Liquidations On CEX
        • Liquidity Analysis
      • Elastic Legacy
        • Elastic Legacy Contract Repo
        • Elastic Legacy Contract Addresses
        • Elastic Legacy Farming Contract Addresses
        • Remove Elastic Legacy Liquidity
      • Protocol
        • Overview
        • Smart Contract Architecture
        • Trust and Security Model
      • Integrations
        • Getting Started
        • Use Cases
        • Integration Types
        • Smart Contracts
        • Ethers JS
        • RESTful API
        • Slippage Rate Protection
        • Price Feed Security
        • Contract Events
        • Platform Fees
      • Reserves
        • Getting Started
          • Overview
          • Why Develop On Kyber
          • Create New Reserve
          • Existing Reserves
          • Customising Existing Reserves
        • Development Guides
          • Fed Price Reserve
          • Automated Price Reserve
          • Reserves with Ganache
          • Orderbook Reserve
        • Operations
          • Listing Policies
          • Reserve IDs
          • Reserve Rebates
          • Sanity Rates
      • Addresses
        • Introduction
        • Mainnet
        • Kovan
        • Rinkeby
        • Ropsten
      • API/ABI
        • Introduction
        • RESTful API
          • RESTful API Overview
          • RESTful API
        • Core Smart Contracts
          • IKyberNetworkProxy
          • KyberNetworkProxy
          • IKyberNetwork
          • ISimpleKyberProxy
          • IKyberMatchingEngine
          • KyberMatchingEngine
          • IKyberHint
          • KyberHintHandler
          • IKyberHintHandler
          • IKyberFeeHandler
          • IKyberStaking
          • KyberStaking
          • IKyberDao
          • KyberDao
          • IKyberStorage
          • KyberStorage
          • IKyberHistory
          • KyberHistory
          • IKyberReserve
          • KyberReserve
          • ConversionRates
          • LiquidityConversionRates
          • EpochUtils
          • IEpochUtils
          • KyberFeeHandler
        • Contract ABIs
          • ABIs
        • Code Snippets
          • Token Quantity Conversion
        • Misc Contracts
          • KyberNetwork
          • ConversionRatesInterface
          • PermissionGroups
          • SanityRates
          • Withdrawable
          • OrderbookReserveInterface
          • OrderbookReserveLister
    • KyberSwap Operator MultiSig
    • Permitable Tokens
    • Third-Party Integrations
    • KyberSwap Analytics
    • KyberSwap App
    • GitHub
    • KyberSwap Analytics
    • KyberSwap Blog
    • Kyber Network Press Kit
  • Socials
    • X
    • Discord
    • Telegram
    • LinkedIn
    • Reddit
    • Instagram
    • Tik Tok
  • Support
    • KyberSwap Help Center
    • Complaints Handling Process
Powered by GitBook
On this page
  • BaseSplitCodeFactory​
  • getCreationCodeContracts()​
  • getCreationCode()​
  • CodeDeployer​
  • FullMath​
  • mulDivFloor()​
  • mulDivCeiling()​
  • LinkedList​
  • Struct: Data​
  • init()​
  • insert()​
  • remove()​
  • LiqDeltaMath​
  • applyLiquidityDelta()​
  • MathConstants​
  • QtyDeltaMath​
  • getQtysForInitialLockup()​
  • calcRequiredQty0()​
  • calcRequiredQty1()​
  • getQty0FromBurnRTokens()​
  • getQty1FromBurnRTokens()​
  • divCeiling()​
  • QuadMath​
  • getSmallerRootOfQuadEqn()​
  • sqrt()​
  • ReinvestmentMath​
  • calcRMintQty()​
  • SafeCast​
  • toUint32()​
  • toInt128()​
  • toUint128()​
  • revToUint128()​
  • toUint160()​
  • toInt256()​
  • revToInt256()​
  • revToUint256()​
  • SwapMath​
  • computeSwapStep()​
  • calcReachAmount()​
  • estimateIncrementalLiquidity()​
  • calcIncrementalLiquidity()​
  • calcFinalPrice()​
  • calcReturnedAmount()​
  • TickMath​
  • Constants​
  • getSqrtRatioAtTick()​
  • getTickAtSqrtRatio()​
  • getMaxNumberTicks()​

Was this helpful?

  1. Reference
  2. Legacy
  3. KyberSwap Elastic
  4. Contracts

Elastic Core Libraries

PreviousElastic Core ContractsNextElastic Periphery Core Contracts

Last updated 2 years ago

Was this helpful?

BaseSplitCodeFactory

Inherited by the Factory contract. Main purpose is to hold the Kyberswap Elastic Pool creation code in a separate address, since its creation code is close to the bytecode size limit of 24kB.

Taken from . The only modification made is the unchecked keyword for sol 0.8 compatibility.

getCreationCodeContracts()

Returns the 2 addresses where the creation code of the contract created by this factory is stored.

getCreationCode()

Returns the creation code of the contract this factory creates.

CodeDeployer

Taken from . Imported and used by the contract to handle deployment.

FullMath

Taken from - Remco Bloeman. Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits.

mulDivFloor()

Returns (a * b / denominator) rounded down.

Input
Type
Explanation

a

uint256

multiplicand

b

uint256

multiplier

denominator

uint256

divisor

A doubly linked list to be used for tick management.

Field
Type
Explanation

previous

int24

previous tick

next

int24

next tick

Initializes the LinkedList with the lowestValue and highestValue, where

  • lowestValue.previous = lowestValue

  • lowestValue.next = highestValue

  • highestValue.previous = lowestValue

  • highestValue.next = highestValue

Field
Type
Explanation

self

mapping(int24 => Data)

A mapping of int24 values to the Data struct

lowestValue

int24

lowest value

highestValue

int24

highest value

Inserts a new value into the LinkedList, given an existing lower value. The new value to be inserted should not be an existing value. Also, the condition lowerValue < newValue < lowerValue.next should be satisfied.

Field
Type
Explanation

self

mapping(int24 => Data)

A mapping of int24 values to the Data struct

newValue

int24

value to be inserted

lowerValue

int24

highest existing value in the linked list that is < newValue

Removes an existing value from the LinkedList. Returns the next lowest value (existingValue.previous).

Field
Type
Explanation

self

mapping(int24 => Data)

A mapping of int24 values to the Data struct

removedValue

int24

value to be removed

Contains a function to assist with the addition of signed liquidityDelta to unsigned liquidity.

Adds or remove uint128 liquidityDelta to uint128 liquidity

Field
Type
Explanation

liquidity

uint128

Liquidity to be adjusted

liquidityDelta

int128

quantity change to be applied

isAddLiquidity

bool

true = add liquidity, false = remove liquidity

Contains constants commonly used by multiple files.

Contains functions for calculating token0 and token1 quantites from differences in prices or from burning reinvestment tokens

Calculate the token0 and token1 quantities needed for unlocking the pool given an initial price and liquidity.

Input

Input Field
Type
Explanation

initialSqrtP

uint160

initial sqrt price raised by 2**96

liquidity

uint128

initial liquidity. should be MIN_LIQUIDITY = 100000

Output

Return Field
Type
Explanation

qty0

uint256

token0 quantity required

qty1

uint256

token1 quantity required

Calculates the token0 quantity between 2 sqrt prices for a given liquidity quantity.

Note that the function assumes that upperSqrtP > lowerSqrtP.

Field
Type
Explanation

lowerSqrtP

uint160

the lower sqrt price

upperSqrtP

uint128

the upper sqrt price

liquidity

int128

liquidity quantity

isAddLiquidity

bool

true = add liquidity, false = remove liquidity

Type
Explanation

int256

token0 qty required for position with liquidity between the 2 sqrt prices

Generally, if the return value > 0, it will be transferred into the pool. Conversely, if the return value < 0, it will be transferred out of the pool.

Calculates the token1 quantity between 2 sqrt prices for a given liquidity quantity.

Note that the function assumes that upperSqrtP > lowerSqrtP.

Field
Type
Explanation

lowerSqrtP

uint160

the lower sqrt price

upperSqrtP

uint128

the upper sqrt price

liquidity

int128

liquidity quantity

isAddLiquidity

bool

true = add liquidity, false = remove liquidity

Type
Explanation

int256

token0 qty required for position with liquidity between the 2 sqrt prices

Generally, if the return value > 0, it will be transferred into the pool. Conversely, if the return value < 0, it will be transferred out of the pool.

Calculates the token0 quantity to be sent to the user for a given amount of reinvestment tokens to be burnt.

Input Field
Type
Explanation

sqrtP

uint160

the current sqrt price

liquidity

uint128

expected change in reinvestment liquidity due to the burning of reinvestment tokens

Calculates the token1 quantity to be sent to the user for a given amount of reinvestment tokens to be burnt.

Input Field
Type
Explanation

sqrtP

uint160

the current sqrt price

liquidity

uint128

expected change in reinvestment liquidity due to the burning of reinvestment tokens

Returns ceil(x / y). y should not be zero.

Given a variant of the quadratic equation ax2−2bx+c−0ax^2 - 2bx + c - 0ax2−2bx+c−0 where aaa, bbb and c>0c > 0c>0, calculate the smaller root via the quadratic formula.

Returns b−b2−aca\frac{b - \sqrt{b^2 - ac}}{a}ab−b2−ac​​

Input Field
Type
Explanation

a

uint256

b

uint256

c

uint256

Contains a helper function to calculate reinvestment tokens to be minted given an increase in reinvestment liquidity.

Given the difference between reinvestL and reinvestLLast, calculate how many reinvestment tokens are to be minted.

rMintQty=rTotalSupply∗reinvestL−reinvestLLastreinvestLLast∗baseLbaseL+reinvestLrMintQty = rTotalSupply * \frac{reinvestL - reinvestL_{Last}}{reinvestL_{Last}} * \frac{baseL}{baseL + reinvestL}rMintQty=rTotalSupply∗reinvestLLast​reinvestL−reinvestLLast​​∗baseL+reinvestLbaseL​

Input Field
Type
Explanation

reinvestL

uint256

latest reinvestment liquidity value. Should be >= reinvestLLast

reinvestLLast

uint256

reinvestmentLiquidityLast value

baseL

uint256

active base liquidity

rTotalSupply

uint256

total supply of reinvestment token

Contains methods for safely casting between different types.

Casts a uint256 to a uint32. Reverts on overflow.

Casts a uint128 to a int128. Reverts on overflow.

Casts a uint256 to a uint128. Reverts on overflow.

Given int128 y, returns uint128 z = -y.

Casts a uint256 to a uint160. Reverts on overflow.

Casts a uint256 to a int256. Reverts on overflow.

Cast a uint256 to a int256 and reverses the sign. Reverts on overflow.

Given int256 y, returns uint256 z = -y.

Computes the actual swap input / output amounts to be deducted or added, the swap fee to be collected and the resulting price.

Field
Type
Explanation

liquidity

uint256

active base liquidity + reinvestment liquidity

currentSqrtP

uint160

current sqrt price

targetSqrtP

uint160

sqrt price limit nextSqrtP can take

feeInBps

uint256

swap fee in basis points

specifiedAmount

int256

amount remaining to be used for the swap

isExactInput

bool

true if specifiedAmount refers to input amount, false if specifiedAmount refers to output amount

isToken0

bool

true if specifiedAmount is in token0, false if specifiedAmount is in token1

Field
Type
Explanation

usedAmount

int256

actual amount to be used for the swap. >= 0 if isExactInput = true, <= 0 if isExactInput = false

returnedAmount

int256

output qty (<= 0) to be accumulated if isExactInput = true, input qty (>= 0) if isExactInput = false

deltaL

uint256

collected swap fee, to be incremented to reinvest liquidity

nextSqrtP

uint160

new sqrt price after the computed swap step

nextSqrtP should not exceed targetSqrtP.

Calculates the amount needed to reach targetSqrtP from currentSqrtP. Note that currentSqrtP and targetSqrtP are casted from uint160 to uint256 as they are multiplied by TWO_BPS (20_000) or feeInBps.

The mathematical formulas are provided below for reference.

isExactInput
isToken0
Formula

true

true

true

false

false

true

false

false

Note that while cases 1 and 3 and cases 2 and 4 are mathematically equivalent, the implementation differs by performing a double negation for the exact output cases. It takes the difference of pn\sqrt{p_n}pn​​ and pc\sqrt{p_c}pc​​ in the numerator (>0), then performing a second negation.

Input Field
Type
Formula Variable
Explanation

liquidity

uint256

active base liquidity + reinvestment liquidity

currentSqrtP

uint160

current sqrt price

targetSqrtP

uint160

sqrt price limit nextSqrtP can take

feeInBps

uint256

swap fee in basis points

isExactInput

bool

N.A.

true / false if specified swap amount refers to input / output amount respectively

isToken0

bool

N.A.

true / false if specified swap amount is in token0 / token1 respectively

Estimates deltaL, the swap fee to be collected based on amountSpecified. This is called only for the final swap step, where the next (temporary) tick will not be crossed.

In the case where exact input is specified, the formula is rather straightforward.

isToken0
Formula

true

false

In the case where exact output is specified, a quadratic equation has to be solved. The desired result is the smaller root of the quadratic equation.

isToken0
Formula

true

false

Input Field
Type
Formula Variable
Explanation

absDelta

uint256

liquidity

uint256

active base liquidity + reinvestment liquidity

currentSqrtP

uint160

current sqrt price

feeInBps

uint256

swap fee in basis points

isExactInput

bool

N.A.

true / false if specified swap amount refers to input / output amount respectively

isToken0

bool

N.A.

true / false if specified swap amount is in token0 / token1 respectively

Calculates deltaL, the swap fee to be collected based on amountSpecified. This is called for an intermediate swap step, where the next (temporary) tick will be crossed.

The mathematical formulas are provided below for reference.

isExactInput
isToken0
Formula

true

true

true

false

false

true

false

false

Inputs

Input Field
Type
Formula Variable
Explanation

absDelta

uint256

liquidity

uint256

active base liquidity + reinvestment liquidity

currentSqrtP

uint160

current sqrt price

nextSqrtP

uint160

next sqrt price

isExactInput

bool

N.A.

true / false if specified swap amount refers to input / output amount respectively

isToken0

bool

N.A.

true / false if specified swap amount is in token0 / token1 respectively

Calculates the sqrt price of the final swap step where the next (temporary) tick will not be crossed.

The mathematical formulas are provided below for reference.

isExactInput
isToken0
Formula

true

true

true

false

false

true

false

false

Input

Input Field
Type
Formula Variable
Explanation

absDelta

uint256

liquidity

uint256

active base liquidity + reinvestment liquidity

deltaL

uint256

collected swap fee

currentSqrtP

uint160

current sqrt price

isExactInput

bool

N.A.

true / false if specified swap amount refers to input / output amount respectively

isToken0

bool

N.A.

true / false if specified swap amount is in token0 / token1 respectively

The mathematical formulas are provided below for reference.

The formula is actually the same, with the difference being made to the operands to ensure the price difference is non-negative.

isExactInput
Formula

true

false

L+ΔLpn−Lpc\frac{L+\Delta{L}}{\sqrt{p_n}} - \frac{L}{\sqrt{p_c}}pn​​L+ΔL​−pc​​L​

Input Field
Type
Formula Variable
Explanation

liquidity

uint256

active base liquidity + reinvestment liquidity

currentSqrtP

uint160

current sqrt price

nextSqrtP

uint160

next sqrt price

deltaL

uint256

collected swap fee

isExactInput

bool

N.A.

true / false if specified swap amount refers to input / output amount respectively

isToken0

bool

N.A.

true / false if specified swap amount is in token0 / token1 respectively

Field
Type
Value
Explanation

MIN_TICK

int24

-887272

MAX_TICK

int24

887272

MIN_SQRT_RATIO

uint160

4295128739

getSqrtRatioAtTick(MIN_TICK)

MAX_SQRT_RATIO

uint160

1461446703485210103287273052203988822378723970342

getSqrtRatioAtTick(MAX_TICK)

Given a int24 tick, calculates 1.0001tick∗296{\sqrt{1.0001^{tick}} * 2^{96}}1.0001tick​∗296.

Given a square root price ratio uint160 sqrtP, calculates the greatest tick such that getSqrtRatioAtTick(tick) <= sqrtP.

Note that MIN_SQRT_RATIO <= sqrtP <= MAX_SQRT_RATIO, otherwise the function will revert.

Used to calculate the maximum liquidity allowable per tick. This function calculates the maximum number of ticks that can be inserted into the LinkedList, given a tickDistance.

Field
Type
Explanation

_tickDistance

int24

Ticks can only be initialized at multiples of this value.

mulDivCeiling()

Similar to , but rounded up.

LinkedList

Struct: Data

init()

insert()

remove()

Note that no removal is performed if removedValue happens to be the lowestValue or highestValue passed in .

LiqDeltaMath

applyLiquidityDelta()

MathConstants

QtyDeltaMath

getQtysForInitialLockup()

calcRequiredQty0()

Input

Output

calcRequiredQty1()

Input

Output

getQty0FromBurnRTokens()

getQty1FromBurnRTokens()

divCeiling()

QuadMath

getSmallerRootOfQuadEqn()

sqrt()

Unchanged from . Calculates the square root of a value using the Babylonian method.

ReinvestmentMath

calcRMintQty()

SafeCast

toUint32()

toInt128()

toUint128()

revToUint128()

toUint160()

toInt256()

revToInt256()

revToUint256()

SwapMath

Contains the logic needed for computing swap input / output amounts and fees. The primary function to look at is , as it is where the bulk of the swap flow logic is in, and where calls to the other functions in the library are made.

computeSwapStep()

Inputs

Outputs

Note

calcReachAmount()

(>0)

(>0)

(<0)

(<0)

estimateIncrementalLiquidity()

usedAmount, absolute value of usedAmount (actual amount used for swap)

calcIncrementalLiquidity()

||

usedAmount, absolute value of usedAmount (actual amount used for swap)

calcFinalPrice()

||

usedAmount, absolute value of usedAmount (actual amount used for swap)

calcReturnedAmount()

Calculates returnedAmount for the function. Rounds down when isExactInput = true (calculating output < 0) so that we avoid sending too much. Conversely, rounds up when isExactInput = false to ensure sufficient input > 0 will be received.

isToken0 = true

isToken0 = false

TickMath

Contains functions for computing square root prices from ticks and vice versa. Adapted from .

Constants

Minimum possible tick =

Minimum possible tick =

getSqrtRatioAtTick()

getTickAtSqrtRatio()

getMaxNumberTicks()

2∗BPS∗L(pc−pn)pc(2∗BPS∗pn−feepc)\frac{2*BPS*L(\sqrt{p_c} - \sqrt{p_n})}{\sqrt{p_c}(2*BPS*\sqrt{p_n} - fee\sqrt{p_c})}pc​​(2∗BPS∗pn​​−feepc​​)2∗BPS∗L(pc​​−pn​​)​
2∗BPS∗pc∗L(pn−pc)(2∗BPS∗pc−feepn)\frac{2*BPS*\sqrt{p_c}*L(\sqrt{p_n} - \sqrt{p_c})}{(2*BPS*\sqrt{p_c} - fee\sqrt{p_n})}(2∗BPS∗pc​​−feepn​​)2∗BPS∗pc​​∗L(pn​​−pc​​)​
−2∗BPS∗L(pn−pc)pc(2∗BPS∗pn−feepc)-\frac{2*BPS*L(\sqrt{p_n} - \sqrt{p_c})}{\sqrt{p_c}(2*BPS*\sqrt{p_n} - fee\sqrt{p_c})}−pc​​(2∗BPS∗pn​​−feepc​​)2∗BPS∗L(pn​​−pc​​)​
−2∗BPS∗pc∗L(pc−pn)(2∗BPS∗pc−feepn)-\frac{2*BPS*\sqrt{p_c}*L(\sqrt{p_c} - \sqrt{p_n})}{(2*BPS*\sqrt{p_c} - fee\sqrt{p_n})}−(2∗BPS∗pc​​−feepn​​)2∗BPS∗pc​​∗L(pc​​−pn​​)​
LLL
pc\sqrt{p_c}pc​​
pn\sqrt{p_n}pn​​
feefeefee
delta∗fee∗pc2∗BPS\frac{delta*fee*\sqrt{p_c}}{2*BPS}2∗BPSdelta∗fee∗pc​​​
delta∗fee2∗BPS∗pc\frac{delta*fee}{2*BPS*\sqrt{p_c}}2∗BPS∗pc​​delta∗fee​
fee∗(ΔL)2−2[(BPS−fee)∗L−BPS∗delta∗pc]ΔL+fee∗L∗delta∗pc=0fee*(\Delta{L})^2-2[(BPS-fee)*L-BPS*delta*\sqrt{p_c}]\Delta{L}+fee*L*delta*\sqrt{p_c}=0fee∗(ΔL)2−2[(BPS−fee)∗L−BPS∗delta∗pc​​]ΔL+fee∗L∗delta∗pc​​=0
fee∗(ΔL)2−2[(BPS−fee)∗L−BPS∗deltapc]ΔL+fee∗L∗deltapc=0fee*(\Delta{L})^2-2[(BPS-fee)*L-\frac{BPS*delta}{\sqrt{p_c}}]\Delta{L}+\frac{fee*L*delta}{\sqrt{p_c}}=0fee∗(ΔL)2−2[(BPS−fee)∗L−pc​​BPS∗delta​]ΔL+pc​​fee∗L∗delta​=0
deltadeltadelta
∥∥∥∥∥∥
∥∥∥∥∥∥
LLL
pc\sqrt{p_c}pc​​
feefeefee
pn∗(Lpc+∥delta∥)−L\sqrt{p_n}*(\frac{L}{\sqrt{p_c}}+\|delta\|)-Lpn​​∗(pc​​L​+∥delta∥)−L
(L∗pc)+∥delta∥pn−L\frac{(L*\sqrt{p_c})+\|delta\|}{\sqrt{p_n}}-Lpn​​(L∗pc​​)+∥delta∥​−L
pn∗(Lpc−∥delta∥)−L\sqrt{p_n}*(\frac{L}{\sqrt{p_c}}-\|delta\|)-Lpn​​∗(pc​​L​−∥delta∥)−L
(L∗pc)−∥delta∥pn−L\frac{(L*\sqrt{p_c})-\|delta\|}{\sqrt{p_n}}-Lpn​​(L∗pc​​)−∥delta∥​−L
deltadeltadelta
∥∥∥∥∥∥
∥∥∥∥∥∥
LLL
pc\sqrt{p_c}pc​​
pn\sqrt{p_n}pn​​
(L+ΔL)pcL+∥delta∥pc\frac{(L+\Delta{L})\sqrt{p_c}}{L+\|delta\|\sqrt{p_c}}L+∥delta∥pc​​(L+ΔL)pc​​​
Lpc+∥delta∥L+ΔL\frac{L\sqrt{p_c}+\|delta\|}{L+\Delta{L}}L+ΔLLpc​​+∥delta∥​
(L+ΔL)pcL−∥delta∥pc\frac{(L+\Delta{L})\sqrt{p_c}}{L-\|delta\|\sqrt{p_c}}L−∥delta∥pc​​(L+ΔL)pc​​​
Lpc−∥delta∥L+ΔL\frac{L\sqrt{p_c}-\|delta\|}{L+\Delta{L}}L+ΔLLpc​​−∥delta∥​
deltadeltadelta
∥∥∥∥∥∥
∥∥∥∥∥∥
LLL
ΔLΔLΔL
pc\sqrt{p_c}pc​​
ΔLpn−L(pc−pn)\Delta{L}\sqrt{p_n}-L(\sqrt{p_c}-\sqrt{p_n})ΔLpn​​−L(pc​​−pn​​)
ΔLpn+L(pn−pc)\Delta{L}\sqrt{p_n}+L(\sqrt{p_n}-\sqrt{p_c})ΔLpn​​+L(pn​​−pc​​)
LLL
pc\sqrt{p_c}pc​​
pn\sqrt{p_n}pn​​
ΔLΔLΔL
log1.00012−128{log_{1.0001}2^{-128}}log1.0001​2−128
log1.00012128{log_{1.0001}2^{128}}log1.0001​2128
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
DMMv1
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Uniswap V3's TickMath library
​
​
​
​
​
Balancer Labs's solidity utils repo
​
​
​
Balancer Labs's solidity utils repo
​
Mathemagic finale: muldiv
​
BaseSplitCodeFactory
mulDivFloor
init()
computeSwapStep
computeSwapStep