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
JSContextcreatedDeclaration
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
xtzToSellThe
XTZAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex contract holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex contract holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.maxSlippagePercentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
DexSwapCalculationResultcontaining 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
tokenToSellThe
TokenAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex contract holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex contract holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.maxSlippagePercentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
DexSwapCalculationResultcontaining 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
xtzThe amount of XTZ to deposit
xtzPoolThe total XTZ held in the dex contract
tokenPoolThe total token held in the dex contract
totalLiquidityThe ttotal liquidity held in the liquidity contract
maxSlippagePercentage (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
tokenThe amount of Token to deposit
xtzPoolThe total XTZ held in the dex contract
tokenPoolThe total token held in the dex contract
totalLiquidityThe ttotal liquidity held in the liquidity contract
maxSlippagePercentage (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
liquidityBurnedThe amount of Liquidity tokens the user wants to burn or sell
totalLiquidityThe total volume of liquidity held in the contract
xtzPoolThe xtz pool held in the dex contract
tokenPoolThe token pool held in the dex contract
maxSlippagePercentage (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
TokenAmountexpected 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
xtzToSellThe
XTZAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
TokenAmountcontaining the amount the user can expect in return for their XTZ - 
                  
                  
The minimum possible
TokenAmountreturned, taking into account slippage.Declaration
Swift
public func xtzToTokenMinimumReturn(tokenAmount: TokenAmount, slippage: Double) -> TokenAmount?Parameters
tokenAmountThe
TokenAmountreturned fromxtzToTokenExpectedReturn().slippageA double value between 0 and 1, indicating the maximum percentage of slippage a user will accept.
Return Value
TokenAmountcontaining the minimum amount the user can expect in return for their XTZ - 
                  
                  
Calculate the
XTZAmountrequired in order to receive the suppliedTokenAmount.Declaration
Swift
public func xtzToTokenRequiredXtzFor(tokenAmount: TokenAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> XTZAmount?Parameters
tokenAmountThe
TokenAmountthe user wants to receive.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
XTZAmountcontaining 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
xtzToSellThe
XTZAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining 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
xtzToSellThe
XTZAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining 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
xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining 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
xtzToSellThe
XTZAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining the slippage percentage, 0 - 100. 
- 
                  
                  
The
XTZAmountexpected 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
tokenToSellThe
TokenAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx. - 
                  
                  
The minimum possible
XTZAmountreturned, taking into account slippage.Declaration
Parameters
xtzAmountThe
XTZAmountreturned fromtokenToXtzExpectedReturn().slippageA double value between 0 and 1, indicating the maximum percentage of slippage a user will accept.
Return Value
XTZAmountcontaining the minimum amount the user can expect in return for their Token - 
                  
                  
Calculate the
TokenAmountrequired in order to receive the suppliedXTZAmount.Declaration
Swift
public func tokenToXtzRequiredTokenFor(xtzAmount: XTZAmount, xtzPool: XTZAmount, tokenPool: TokenAmount, dex: DipDupExchangeName) -> TokenAmount?Parameters
xtzAmountThe
XTZAmountthe user wants to receive.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
TokenAmountcontaining the amount ofTokenrequired 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
tokenToSellThe
TokenAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining the exchange rate from 1 of the givenTokento 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
tokenToSellThe
TokenAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining the exchange rate from 1 of the givenTokento 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
xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining the exchange rate from 1 of the givenTokento 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
tokenToSellThe
TokenAmountto sell.xtzPoolThe
XTZAmountrepresenting the current pool of XTZ that the dex holds. Can be fetched with xxxxx.tokenPoolThe
TokenAmountrepresenting the current pool of the givenTokenthat the dex holds. Must have the same number of decimalPlaces as the token it represents. Can be fetched with xxxxx.Return Value
Decimalcontaining 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
xtzToDepositThe XTZ to send to the dex contract
tokenToDepositThe Token to send to the dex contract
totalLiquidityThe total liquidity already in the contract
slippagePercentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
TokenAmountan 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
xtzToDepositThe amount of XTZ to send
xtzPoolThe XTZ currently held in the dex contract
tokenPoolThe Token currently held in the dex contract
Return Value
TokenAmountThe 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
tokenToDepositThe amount of Token to send
xtzPoolThe XTZ currently held in the dex contract
tokenPoolThe Token currently held in the dex contract
Return Value
XTZAmountThe 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
liquidityBurnedThe amount of liquidity to burn
totalLiquidityThe totla liquidity held in the dex contract
tokenPoolThe total token held in the dex contract
slippagePercentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
TokenAmountThe 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
liquidityBurnedThe amount of liquidity to burn
totalLiquidityThe totla liquidity held in the dex contract
xtzPoolThe total XTZ held in the dex contract
slippagePercentage (must be between 0 and 1) of maximum amount of slippage the user is willing to accept
Return Value
XTZAmountThe 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
xtzPoolThe total XTZ held in the dex contract
Return Value
DecimalThe estimated percentage APY 
Install in Dash