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
createdDeclaration
Swift
public static let shared: DexCalculationService
-
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 givenToken
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 givenToken
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)
-
The
TokenAmount
expected to be returned for the suppliedXTZAmount
, given the dex contract xtzPool and tokenPool.Declaration
Swift
public func xtzToTokenExpectedReturn(xtzToSell: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> TokenAmount?
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 givenToken
that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
TokenAmount
containing the amount the user can expect in return for their XTZ -
The minimum possible
TokenAmount
returned, taking into account slippage.Declaration
Swift
public func xtzToTokenMinimumReturn(tokenAmount: TokenAmount, slippage: Double) -> TokenAmount?
Parameters
tokenAmount
The
TokenAmount
returned fromxtzToTokenExpectedReturn()
.slippage
A double value between 0 and 1, indicating the maximum percentage of slippage a user will accept.
Return Value
TokenAmount
containing the minimum amount the user can expect in return for their XTZ -
Calculate the
XTZAmount
required in order to receive the suppliedTokenAmount
.Declaration
Swift
public func xtzToTokenRequiredXtzFor(tokenAmount: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> XTZAmount?
Parameters
tokenAmount
The
TokenAmount
the user wants to receive.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 givenToken
that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
XTZAmount
containing the amount of XTZ required in order to recieve the amount of token.
-
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 givenToken
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 requestedToken
-
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 givenToken
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 requestedToken
-
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 givenToken
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 requestedToken
-
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 givenToken
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.
-
The
XTZAmount
expected to be returned for the suppliedTokenAmount
, given the dex contracts xtzPool and tokenPool.Declaration
Swift
public func tokenToXtzExpectedReturn(tokenToSell: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> XTZAmount?
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 givenToken
that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx. -
The minimum possible
XTZAmount
returned, taking into account slippage.Declaration
Parameters
xtzAmount
The
XTZAmount
returned fromtokenToXtzExpectedReturn()
.slippage
A double value between 0 and 1, indicating the maximum percentage of slippage a user will accept.
Return Value
XTZAmount
containing the minimum amount the user can expect in return for their Token -
Calculate the
TokenAmount
required in order to receive the suppliedXTZAmount
.Declaration
Swift
public func tokenToXtzRequiredTokenFor(xtzAmount: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> TokenAmount?
Parameters
xtzAmount
The
XTZAmount
the user wants to receive.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 givenToken
that the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
TokenAmount
containing the amount ofToken
required in order to recieve the amount of XTZ.
-
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 givenToken
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 givenToken
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 givenToken
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 givenToken
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 givenToken
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 givenToken
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 givenToken
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.
-
Calculate the amount of liquidity tokens a user can expect back for an amount of XTZ and Token
Declaration
Swift
public func addLiquidityReturn(xtzToDeposit: XTZAmount, xtzPool: XTZAmount, totalLiquidity: TokenAmount, slippage: Double, dex: DipDupExchangeName) -> (expected: TokenAmount, minimum: TokenAmount)?
Parameters
xtzToDeposit
The XTZ to send to the dex contract
tokenToDeposit
The Token to send to the dex contract
totalLiquidity
The total liquidity already in the contract
slippage
Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
TokenAmount
an amount of Liquidity token you will receive -
Calculate the amount of Token that is required to send along side your XTZ
Declaration
Swift
public func addLiquidityTokenRequired(xtzToDeposit: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> TokenAmount?
Parameters
xtzToDeposit
The amount of XTZ to send
xtzPool
The XTZ currently held in the dex contract
tokenPool
The Token currently held in the dex contract
Return Value
TokenAmount
The amount of token required to send with the given amount of XTZ -
Calculate the amount of XTZ that is required to send along side your Token
Declaration
Swift
public func addLiquidityXtzRequired(tokenToDeposit: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> XTZAmount?
Parameters
tokenToDeposit
The amount of Token to send
xtzPool
The XTZ currently held in the dex contract
tokenPool
The Token currently held in the dex contract
Return Value
XTZAmount
The amount of XTZ required to send with the given amount of Token
-
Calculate the amount of token a user would revice back if they burned X liquidity
Declaration
Swift
public func removeLiquidityTokenReceived(liquidityBurned: TokenAmount, totalLiquidity: TokenAmount, tokenPool: TokenAmount, slippage: Double) -> (expected: TokenAmount, minimum: TokenAmount)?
Parameters
liquidityBurned
The amount of liquidity to burn
totalLiquidity
The totla liquidity held in the dex contract
tokenPool
The total token held in the dex contract
slippage
Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
TokenAmount
The amount of Token that would be returned -
Calculate the amount of XTZ a user would revice back if they burned X liquidity
Declaration
Swift
public func removeLiquidityXtzReceived(liquidityBurned: TokenAmount, totalLiquidity: TokenAmount, xtzPool: XTZAmount, slippage: Double, dex: DipDupExchangeName) -> (expected: XTZAmount, minimum: XTZAmount)?
Parameters
liquidityBurned
The amount of liquidity to burn
totalLiquidity
The totla liquidity held in the dex contract
xtzPool
The total XTZ held in the dex contract
slippage
Percentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
XTZAmount
The amount of XTZ that would be returned
-
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