DexCalculationService

public class DexCalculationService

Wrapper around the kukai-dex-calculations JS library for performing calculations: https://github.com/kukai-wallet/kukai-dex-calculations

  • Public shared instace to avoid having multiple copies of the underlying JSContext created

    Declaration

    Swift

    public static let shared: DexCalculationService

User flow functions

  • A helper function to create all the necessary calculations for a xtzToToken exchange, to perform the operation and display the info to the user in a confirmation screen.

    Declaration

    Swift

    public func calculateXtzToToken(xtzToSell: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, maxSlippage: Double, dex: DipDupExchangeName) -> DexSwapCalculationResult?

    Parameters

    xtzToSell

    The XTZAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex contract holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex contract holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    maxSlippage

    Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept

    Return Value

    DexSwapCalculationResult containing the results of all the necessary calculations.

  • A helper function to create all the necessary calculations for a tokenToXtz exchange, to perform the operation and display the info to the user in a confirmation screen.

    Declaration

    Swift

    public func calculateTokenToXTZ(tokenToSell: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, maxSlippage: Double, dex: DipDupExchangeName) -> DexSwapCalculationResult?

    Parameters

    tokenToSell

    The TokenAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex contract holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex contract holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    maxSlippage

    Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept

    Return Value

    DexSwapCalculationResult containing the results of all the necessary calculations.

  • A helper function to create all the necessary calculations for adding liquidity, with an XTZ input

    Declaration

    Swift

    public func calculateAddLiquidity(xtz: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, totalLiquidity: TokenAmount, maxSlippage: Double, dex: DipDupExchangeName) -> DexAddCalculationResult?

    Parameters

    xtz

    The amount of XTZ to deposit

    xtzPool

    The total XTZ held in the dex contract

    tokenPool

    The total token held in the dex contract

    totalLiquidity

    The ttotal liquidity held in the liquidity contract

    maxSlippage

    Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept

    Return Value

    (tokenRequired: TokenAmount, liquidity: TokenAmount) containing the results of all the necessary calculations.

  • A helper function to create all the necessary calculations for adding liquidity, with an Token input

    Declaration

    Swift

    public func calculateAddLiquidity(token: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, totalLiquidity: TokenAmount, maxSlippage: Double, dex: DipDupExchangeName) -> DexAddCalculationResult?

    Parameters

    token

    The amount of Token to deposit

    xtzPool

    The total XTZ held in the dex contract

    tokenPool

    The total token held in the dex contract

    totalLiquidity

    The ttotal liquidity held in the liquidity contract

    maxSlippage

    Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept

    Return Value

    (xtzRequired: XTZAmount, liquidity: TokenAmount) containing the results of all the necessary calculations.

  • A helper function to create all the necessary calculations for removing liquidity, to return everything the user will get out

    Declaration

    Swift

    public func calculateRemoveLiquidity(liquidityBurned: TokenAmount, totalLiquidity: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, maxSlippage: Double, dex: DipDupExchangeName) -> DexRemoveCalculationResult?

    Parameters

    liquidityBurned

    The amount of Liquidity tokens the user wants to burn or sell

    totalLiquidity

    The total volume of liquidity held in the contract

    xtzPool

    The xtz pool held in the dex contract

    tokenPool

    The token pool held in the dex contract

    maxSlippage

    Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept

    Return Value

    (xtz: XTZAmount, token: TokenAmount) containing the results of all the necessary calculations.

  • Declaration

    Swift

    public static func settings(forDex dex: DipDupExchangeName) -> (fee: Double, burn: Double, includeSubsidy: Bool)

XTZ To Token

XTZ To Token Rates

  • The exchange rate for a given trade, taking into account slippage and fees

    Declaration

    Swift

    public func xtzToTokenExchangeRate(xtzToSell: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    xtzToSell

    The XTZAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 XTZ to the requested Token

  • The exchange rate for a given trade, taking into account slippage and fees, formatted and truncated for easier display in the UI.

    Declaration

    Swift

    public func xtzToTokenExchangeRateDisplay(xtzToSell: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    xtzToSell

    The XTZAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 XTZ to the requested Token

  • Before a user has entered in an amount to trade, its useful to show them the base exchange rate, ignoring slippage.

    Declaration

    Swift

    public func xtzToTokenMarketRate(xtzPool: XTZAmount, tokenPool: TokenAmount) -> Decimal?

    Parameters

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 XTZ to the requested Token

  • Calcualte the percentage the price impact the given trade would incur. Since this is already taken into account for the other functions, this function returns in the scale of 0 - 100, for display purposes.

    Declaration

    Swift

    public func xtzToTokenPriceImpact(xtzToSell: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    xtzToSell

    The XTZAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the slippage percentage, 0 - 100.

Token to XTZ

XTZ To Token Rates

  • The exchange rate for a given trade, taking into account slippage and fees

    Declaration

    Swift

    public func tokenToXtzExchangeRate(tokenToSell: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    tokenToSell

    The TokenAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 of the given Token to XTZ

  • The exchange rate for a given trade, taking into account slippage and fees, formatted and truncated for easier display in the UI.

    Declaration

    Swift

    public func tokenToXtzExchangeRateDisplay(tokenToSell: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    tokenToSell

    The TokenAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 of the given Token to XTZ

  • Before a user has entered in an amount to trade, its useful to show them the base exchange rate, ignoring slippage.

    Declaration

    Swift

    public func tokenToXtzMarketRate(xtzPool: XTZAmount, tokenPool: TokenAmount) -> Decimal?

    Parameters

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the exchange rate from 1 of the given Token to XTZ

  • Calcualte the percentage slippage the given trade would incur. Since this is already taken into account for the other functions, this function returns in the scale of 0 - 100, for display purposes.

    Declaration

    Swift

    public func tokenToXtzPriceImpact(tokenToSell: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> Decimal?

    Parameters

    tokenToSell

    The TokenAmount to sell.

    xtzPool

    The XTZAmount representing the current pool of XTZ that the dex holds. Can be fetched with xxxxx.

    tokenPool

    The TokenAmount representing the current pool of the given Token that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.

    Return Value

    Decimal containing the slippage percentage, 0 - 100.

Add Liquidity

Remove Liquidity

Misc

  • Estimate the APY of liquidity baking contract, as it has a known income.

    Declaration

    Swift

    public func estimateLiquidityBakingAPY(xtzPool: XTZAmount) -> Decimal?

    Parameters

    xtzPool

    The total XTZ held in the dex contract

    Return Value

    Decimal The estimated percentage APY