Maverick Docs
  • Introducing Maverick
  • Getting Started
    • 🖥️The Maverick V2 UI
    • 👛Connect a Wallet
    • ⛓️Choose a Network
    • 🪙Approving Tokens
  • Guides
    • 🤝Traders
      • How to Make a Swap
    • 💰Liquidity Providers
      • Understanding Liquidity Provision
      • Understanding Modes
      • How to Add Liquidity
      • How to Deploy a New Pool
      • How to Check Position Balances
      • How to Manage Liquidity in a Pool
      • How to Migrate from V1 to V2
      • Understanding Permanent Loss
      • Liquidity Strategies
    • ⚡Incentives
      • Understanding Boosted Positions
      • Understanding Incentives
      • How to Join a Boosted Position
      • How to Create a Boosted Position
      • How to Manage a Boosted Position
      • How to Add Incentives
    • 🎡veFlywheel
      • veFlywheel Basics
      • Guide to veFlywheel Emissions
      • How to Vote to Direct Emissions
    • 👩‍🏫Advanced Tutorials
      • Single-Sided Incentives
  • Technical Reference
    • Contract Addresses
      • V1 Contract Addresses
      • V2 Contract Addresses
    • Maverick V1
      • V1 Whitepaper
      • V1 Contracts
        • Router
        • Pool
        • Factory
        • SlimRouter
    • Maverick V2
      • V2 Whitepaper
      • V2 Contracts
        • Maverick V2 Common Contracts
          • base
            • IMulticall
            • IPayableMulticall
            • Multicall
            • PayableMulticall
          • interfaces
            • IMaverickV2AddLiquidityCallback
            • IMaverickV2Factory
            • IMaverickV2FactoryAdmin
            • IMaverickV2FlashLoanCallback
            • IMaverickV2Pool
            • IMaverickV2PoolAdmin
            • IMaverickV2SwapCallback
          • libraries
            • ArrayOperations
            • Constants
            • Math
            • PoolLib
            • TickMath
            • TransferLib
        • Maverick V2 AMM Contracts
          • poollib
            • Bin
            • Delta
            • Deployer
            • DeployerPermissioned
            • SwapMath
            • Twa
          • MaverickV2Factory
          • MaverickV2Pool
          • MaverickV2PoolPermissioned
        • Maverick V2 Reward Contracts
          • interfaces
            • IMaverickV2IncentiveMatcher
            • IMaverickV2IncentiveMatcherFactory
            • IMaverickV2Reward
            • IMaverickV2RewardFactory
            • IMaverickV2RewardRouter
            • IMaverickV2RewardVault
            • IMaverickV2VotingEscrowBase
            • IMaverickV2VotingEscrow
            • IMaverickV2VotingEscrowFactory
            • IMaverickV2VotingEscrowLens
            • IMaverickV2VotingEscrowWSync
          • libraries
            • IncentiveMatcherDeployer
            • RewardDeployer
            • VotingEscrowDeployer
            • VotingEscrowWSyncDeployer
          • rewardbase
            • IRewardAccounting
            • RewardAccounting
          • votingescrowbase
            • HistoricalBalance
            • IHistoricalBalance
            • ILegacyVeMav
            • VotingEscrow
          • MaverickV2IncentiveMatcher
          • MaverickV2IncentiveMatcherFactory
          • MaverickV2Reward
          • MaverickV2RewardFactory
          • MaverickV2RewardRouter
          • MaverickV2RewardVault
          • MaverickV2VotingEscrow
          • MaverickV2VotingEscrowFactory
          • MaverickV2VotingEscrowLens
          • MaverickV2VotingEscrowWSync
        • Maverick V2 Supplemental Contracts
          • base
            • Checks
            • IChecks
            • IMigrateBins
            • MigrateBins
          • boostedpositionbase
            • BoostedPositionBase
            • IBoostedPositionBase
            • ImmutableArrayGetter
          • interfaces
            • IMaverickV2BoostedPosition
            • IMaverickV2BoostedPositionFactory
            • IMaverickV2LiquidityManager
            • IMaverickV2PoolLens
            • IMaverickV2Position
            • IMaverickV2Quoter
            • IMaverickV2Router
            • IPositionImage
          • libraries
            • BoostedPositionDeployerDynamic
            • BoostedPositionDeployerStatic
            • BytesLib
            • LiquidityUtilities
            • PackLib
            • Path
            • PoolInspection
          • liquiditybase
            • ArgPacker
            • IArgPacker
          • paymentbase
            • IERC20PermitAllowed
            • IPayment
            • ISelfPermit
            • IState
            • IWETH9
            • Payment
            • SelfPermit
            • State
          • positionbase
            • INft
            • Nft
          • routerbase
            • CallbackOperations
            • ExactOutputSlim
            • ICallbackOperations
            • IExactOutputSlim
            • IPushOperations
            • IRouterErrors
            • PushOperations
          • MaverickV2BoostedPositionDynamic
          • MaverickV2BoostedPositionFactory
          • MaverickV2BoostedPositionStatic
          • MaverickV2LiquidityManager
          • MaverickV2PoolLens
          • MaverickV2Position
          • MaverickV2Quoter
          • MaverickV2Router
          • PositionImage
      • V2 API Documentation
      • V2 Testnet Guide
        • Get Sepolia ETH & Test Tokens
        • How to Create a Boosted Position in V2
        • How to Add & Match Incentives
        • How to Create a veFlywheel
    • Development FAQ
    • Finding LP Balances
  • MAV token
    • MAV Token & Tokenomics
    • veMAV & MAV Staking
    • How to Stake MAV
    • Syncing V1 veMAV
    • Airdrop Information
      • Maverick Ecosystem Rewards Pre-Season Airdrop
      • Maverick Governance & S1 Airdrops
  • Further Information
    • Frequently Asked Questions
    • Glossary
    • Security
    • Maverick Wallets
    • Dune Analytics
    • Rebasing Tokens
    • Restricted Territories
    • Delphi Digital Report
Powered by GitBook
On this page
  1. Technical Reference
  2. Maverick V2
  3. V2 Contracts
  4. Maverick V2 AMM Contracts
  5. poollib

SwapMath

Functions

_amountToBinNetOfProtocolFee

Internal function to calculate the amount after deducting the protocol fee.

function _amountToBinNetOfProtocolFee(uint256 deltaInErc, uint256 feeBasis, uint256 protocolFeeD3)
    private
    pure
    returns (uint256 amount);

Parameters

Name
Type
Description

deltaInErc

uint256

Input delta in ERC token.

feeBasis

uint256

Fee basis for calculating the fee amount.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol in three-decimal format. e.g. fee of 100 is 10% protocol fee rate.

Returns

Name
Type
Description

amount

uint256

The amount after deducting the protocol fee.

_remainingBinInputSpaceGivenOutput

Internal function to calculate the remaining input space given the output.

function _remainingBinInputSpaceGivenOutput(uint256 binLiquidity, uint256 output, uint256 sqrtPrice, bool tokenAIn)
    private
    pure
    returns (uint256 binAmountIn);

Parameters

Name
Type
Description

binLiquidity

uint256

The current liquidity in the bin.

output

uint256

The desired output amount.

sqrtPrice

uint256

The current square root price.

tokenAIn

bool

True if the swap input is token A.

Returns

Name
Type
Description

binAmountIn

uint256

The input amount that can be accommodated in the bin.

computeEndPrice

Compute end price of a swap as well as the approximate end price in the tick domain. The resulting output fraction tick part is written to the input newDelta object.

Ain: endSqrtP = in / L + sqrtP

Bin: endSqrtP = 1 / (in / L + 1 / sqrtP) = L / (in + L / sqrtP)

fractional Tick: (endSqrtP - lowerSqrtP) / (upperSqrtP - lowerSqrtP)

function computeEndPrice(
    Delta.Instance memory delta,
    Delta.Instance memory newDelta,
    IMaverickV2Pool.TickData memory tickData
) internal pure;

computeSwapExactIn

Calculate swap data for an exact input swap.

function computeSwapExactIn(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountIn,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);

Parameters

Name
Type
Description

sqrtPrice

uint256

Current price.

tickData

IMaverickV2Pool.TickData

Reserve and liquidity values of the bin.

amountIn

uint256

Desired input amount.

tokenAIn

bool

True if the swap input is token A.

fee

uint256

Ratio that the swapper pays in D18 format.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol.

Returns

Name
Type
Description

delta

Delta.Instance

Swap data delta for the exact input swap.

computeSwapExactOut

Calculate swap data for an exact output swap.

function computeSwapExactOut(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountOut,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);

Parameters

Name
Type
Description

sqrtPrice

uint256

Current price.

tickData

IMaverickV2Pool.TickData

Reserve and liquidity values of the bin.

amountOut

uint256

Desired output amount.

tokenAIn

bool

True if the swap input is token A.

fee

uint256

Ratio that the swapper pays in D18 format.

protocolFeeD3

uint256

Proportion of the fee that goes to the protocol.

Returns

Name
Type
Description

delta

Delta.Instance

Swap data delta for the exact output swap.

PreviousDeployerPermissionedNextTwa

Last updated 1 year ago