dexter-calculations
Advanced tools
Comparing version 1.0.1 to 1.2.0
@@ -0,1 +1,12 @@ | ||
### 2020-01-25 -- v1.2.0 | ||
* Fix null bug related to tokenToTokenTokenInput. | ||
### 2020-01-25 -- v1.1.1 | ||
* Fix errors in documentation around decimals. | ||
* Replace slippage tests with price_impact tests. | ||
### 2020-01-07 -- v1.1.0 | ||
* Replace slippage calculations with price impact calculations. | ||
* Support token to token calculations. | ||
### 2020-12-02 -- v1.0.0 | ||
@@ -2,0 +13,0 @@ * First release. |
@@ -7,72 +7,76 @@ /** | ||
/** | ||
* Calculate the amount of token sold for a given XTZ input and the pool state of Dexter | ||
* for the dexter xtzToToken entrypoint. | ||
* Calculate the amount of token sold for a given XTZ input and Dexter's two pool | ||
* values for the dexter xtzToToken entrypoint. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
*/ | ||
export function xtzToTokenTokenOutput(xtzIn: number, xtzPool: number, tokenPool: number): (bigInt.BigIntegerStatic | null); | ||
export function xtzToTokenTokenOutput(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* Calculate the amount of XTZ you must pay in in order to receive a target | ||
* amount of token for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* amount of token for a given in the two Dexter pools. tokenOut is considered the | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of XTZ the user must send to xtzToToken to get the tokenOut amount. | ||
*/ | ||
export function xtzToTokenXtzInput(tokenOut: number, xtzPool: number, tokenPool: number, decimals: number): (bigInt.BigIntegerStatic | null); | ||
export function xtzToTokenXtzInput(tokenOut: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* Calculate the exchange rate for an XTZ to Token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* to the liquidity providers and the penalty for trade size. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function xtzToTokenExchangeRate(xtzIn: number, xtzPool: number, tokenPool: number): (number | null); | ||
export function xtzToTokenExchangeRate(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Same as xtzToTokenExchangeRate but adjusted for the decimal places. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function xtzToTokenExchangeRateForDisplay(xtzIn: number, xtzPool: number, tokenPool: number, decimals: any): (number | null); | ||
export function xtzToTokenExchangeRateForDisplay(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the xtzToToken market rate for a give Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
export function xtzToTokenMarketRate(xtzPool: number, tokenPool: number): (number | null); | ||
export function xtzToTokenMarketRate(xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the xtzToToken market rate for a give Dexter contract. The market | ||
* rate is an idea number that doesn't include fees. In practice, this rate | ||
* cannot be executed. | ||
* Calculate the xtzToToken price impact for a given Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
export function xtzToTokenSlippage(xtzIn: number, xtzPool: number, tokenPool: number): (number | null); | ||
export function xtzToTokenPriceImpact(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. | ||
* Calculate the minimum token out to be sent to Dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the xtzToToken entrypoint. | ||
*/ | ||
export function xtzToTokenMinimumTokenOutput(tokenOut: number, allowedSlippage: number): (bigInt.BigIntegerStatic | null); | ||
export function xtzToTokenMinimumTokenOutput(tokenOut: (bigInt.BigIntegerStatic | number | string), allowedSlippage: number): (bigInt.BigIntegerStatic | null); | ||
/** | ||
@@ -82,6 +86,6 @@ * Calculate the fee that liquidity providers, as a whole and not individually, | ||
* | ||
* @param {number} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to the dexter liquidity providers. | ||
*/ | ||
export function totalLiquidityProviderFee(xtzIn: number): (number | null); | ||
export function totalLiquidityProviderFee(xtzIn: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
@@ -91,6 +95,6 @@ * Calculate the fee that a single liquidity provider will receive for a given amount of | ||
* | ||
* @param {number} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to an individual dexter liquidity provider. | ||
*/ | ||
export function liquidityProviderFee(xtzIn: number, totalLiquidity: any, userLiquidity: any): (number | null); | ||
export function liquidityProviderFee(xtzIn: (bigInt.BigIntegerStatic | number | string), totalLiquidity: any, userLiquidity: any): (number | null); | ||
/** | ||
@@ -103,23 +107,23 @@ * ============================================================================= | ||
* Get the amount of XTZ sold for a given token input and the pool state of Dexter | ||
* for the dexter tokenToXtz entrypoint. | ||
* for the Dexter tokenToXtz entrypoint. | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of XTZ that Dexter will send to the :to | ||
* address in the dexter tokenToXtz entrypoint. | ||
*/ | ||
export function tokenToXtzXtzOutput(tokenIn: number, xtzPool: number, tokenPool: number): (bigInt.BigIntegerStatic | null); | ||
export function tokenToXtzXtzOutput(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* Calculate the amount of token you must pay in in order to receive a target | ||
* amount of XTZ for a given Dexter pool state. xtzOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token the user must send to tokenToXtz to get the xtzOut amount. | ||
*/ | ||
export function tokenToXtzTokenInput(xtzOut: number, xtzPool: number, tokenPool: number, decimals: number): (bigInt.BigIntegerStatic | null); | ||
export function tokenToXtzTokenInput(xtzOut: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
@@ -129,46 +133,151 @@ * Calculate the exchange rate for a token to XTZ trade including the 0.3% fee given | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function tokenToXtzExchangeRate(tokenIn: number, xtzPool: number, tokenPool: number): (number | null); | ||
export function tokenToXtzExchangeRate(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Same as tokenToXtzExchangeRate but adjusted for the decimal places. | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function tokenToXtzExchangeRateForDisplay(tokenIn: number, xtzPool: number, tokenPool: number, decimals: any): (number | null); | ||
export function tokenToXtzExchangeRateForDisplay(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: any): (number | null); | ||
/** | ||
* Calculate the tokenToXtz market rate for a give Dexter contract. The market | ||
* Calculate the tokenToXtz market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
export function tokenToXtzMarketRate(xtzPool: number, tokenPool: number): (number | null); | ||
export function tokenToXtzMarketRate(xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the tokenToXtz price impact for a give Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
export function tokenToXtzSlippage(tokenIn: number, xtzPool: number, tokenPool: number): (number | null); | ||
export function tokenToXtzPriceImpact(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max xtzOut | ||
* and the max allowed slippage rate the user accepts. | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be greater than 0.00 and less than 1.00. | ||
* @param {(bigInt|number|string)} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToXtz entrypoint. | ||
*/ | ||
export function tokenToXtzMinimumXtzOutput(xtzOut: number, allowedSlippage: number): (bigInt.BigIntegerStatic | null); | ||
export function tokenToXtzMinimumXtzOutput(xtzOut: (bigInt.BigIntegerStatic | number | string), allowedSlippage: number): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* ============================================================================= | ||
* tokenToToken entrypoint functions | ||
* ============================================================================= | ||
*/ | ||
/** | ||
* The tokenToToken entyrpoint requires two dexter contracts. The documentation | ||
* refers to the input token as token X and the output token as token Y. | ||
*/ | ||
/** | ||
* Calculate the amount of token Y sold for token X input and the pool state of Dexter | ||
* for the dexter tokenToToken entrypoint. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token Y that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
*/ | ||
export function tokenToTokenTokenOutput(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* Calculate the amount of token X you must pay in in order to receive a target | ||
* amount of token Y for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token Y that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token X the user must send to tokenToToken to get the tokenOut amount. | ||
*/ | ||
export function tokenToTokenTokenInput(tokenOut: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string), decimals2: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* Calculate the exchange rate for a token to token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function tokenToTokenExchangeRate(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Same as tokenToTokenExchangeRate but adjusted for the decimal places. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
export function tokenToTokenExchangeRateForDisplay(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string), decimals2: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the tokenToToken market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - Number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - Number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
export function tokenToTokenMarketRate(xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), decimals: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string), decimals2: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the tokenToToken price impact for two Dexter contracts. Price | ||
* impact is measure of how much a trade will alter the future price. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - The amount of token X the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
export function tokenToTokenPriceImpact(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string), xtzPool2: (bigInt.BigIntegerStatic | number | string), tokenPool2: (bigInt.BigIntegerStatic | number | string)): (number | null); | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToToken entrypoint. | ||
*/ | ||
export function tokenToTokenMinimumTokenOutput(tokenOut: (bigInt.BigIntegerStatic | number | string), allowedSlippage: number): (bigInt.BigIntegerStatic | null); | ||
/** | ||
* ============================================================================= | ||
* addLiquidity entrypoint functions | ||
@@ -182,8 +291,8 @@ * ============================================================================= | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
*/ | ||
export function addLiquidityLiquidityCreated(xtzIn: number, xtzPool: number, totalLiquidity: number): (bigInt.BigIntegerStatic | null); | ||
export function addLiquidityLiquidityCreated(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), totalLiquidity: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
@@ -195,8 +304,8 @@ * For a given amount of xtzIn and the state of the Dexter xtz pool and token | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
*/ | ||
export function addLiquidityTokenIn(xtzIn: number, xtzPool: number, tokenPool: number): (bigInt.BigIntegerStatic | null); | ||
export function addLiquidityTokenIn(xtzIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
@@ -208,8 +317,8 @@ * For a given amount of tokenIn and the state of the Dexter xtz pool and token | ||
* | ||
* @param {number} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. | ||
* @param {number} tokenPool Token amount that Dexter holds. | ||
* @returns {{null|bigInt}} The amount of liquidity that the sender gains. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. | ||
* @param {(bigInt|number|string)} tokenPool Token amount that Dexter holds. | ||
* @returns {{bigInt|null}} The amount of liquidity that the sender gains. | ||
*/ | ||
export function addLiquidityXtzIn(tokenIn: number, xtzPool: number, tokenPool: number): {}; | ||
export function addLiquidityXtzIn(tokenIn: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): {}; | ||
/** | ||
@@ -225,8 +334,8 @@ * ============================================================================= | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} tokenPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of token that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} tokenPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of token that the sender gains. | ||
*/ | ||
export function removeLiquidityTokenOut(liquidityBurned: number, totalLiquidity: number, tokenPool: number): (null | bigInt.BigIntegerStatic); | ||
export function removeLiquidityTokenOut(liquidityBurned: (bigInt.BigIntegerStatic | number | string), totalLiquidity: (bigInt.BigIntegerStatic | number | string), tokenPool: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
/** | ||
@@ -237,8 +346,8 @@ * Calculate the amount of XTZ a sender would receive for burning a certain amount | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} xtzPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of XTZ that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} xtzPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of XTZ that the sender gains. | ||
*/ | ||
export function removeLiquidityXtzOut(liquidityBurned: number, totalLiquidity: number, xtzPool: number): (null | bigInt.BigIntegerStatic); | ||
export function removeLiquidityXtzOut(liquidityBurned: (bigInt.BigIntegerStatic | number | string), totalLiquidity: (bigInt.BigIntegerStatic | number | string), xtzPool: (bigInt.BigIntegerStatic | number | string)): (bigInt.BigIntegerStatic | null); | ||
import bigInt = require("big-integer"); |
@@ -5,2 +5,10 @@ var bigInt = require('big-integer'); | ||
/** | ||
* Many functions use {(bigInt|number|string)} as parameter. These parameters | ||
* are converted into bigInt from the big-integer package and are expected to | ||
* to be non-negative numbers. string should be a string encoded integer. If you | ||
* are interfacing this project from another programming language, you should | ||
* pass the value for the parameter in {(bigInt|number|string)} as a string to | ||
* avoid number size restrictions in JavaScript. | ||
*/ | ||
/** | ||
* ============================================================================= | ||
@@ -41,2 +49,12 @@ * Internal utility functions | ||
/** | ||
* Test if a bigInt is less than or equal to zero. | ||
* | ||
* @param {bigInt} x | ||
* @returns {boolean} x <= 0 | ||
*/ | ||
function leqZero(x) { | ||
return x.compare(bigInt.zero) <= 0; | ||
} | ||
; | ||
/** | ||
* Ceiling division. If the remainder is greater than zero, increment by one. | ||
@@ -63,8 +81,8 @@ * | ||
/** | ||
* Calculate the amount of token sold for a given XTZ input and the pool state of Dexter | ||
* for the dexter xtzToToken entrypoint. | ||
* Calculate the amount of token sold for a given XTZ input and Dexter's two pool | ||
* values for the dexter xtzToToken entrypoint. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
@@ -86,3 +104,3 @@ */ | ||
if (gtZero(xtzIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var numerator = xtzIn_.times(tokenPool).times(bigInt(997)); | ||
var numerator = xtzIn_.times(tokenPool_).times(bigInt(997)); | ||
var denominator = xtzPool_.times(bigInt(1000)).add(xtzIn_.times(bigInt(997))); | ||
@@ -99,9 +117,9 @@ return numerator.divide(denominator); | ||
* Calculate the amount of XTZ you must pay in in order to receive a target | ||
* amount of token for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* amount of token for a given in the two Dexter pools. tokenOut is considered the | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of XTZ the user must send to xtzToToken to get the tokenOut amount. | ||
@@ -143,7 +161,7 @@ */ | ||
* Calculate the exchange rate for an XTZ to Token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* to the liquidity providers and the penalty for trade size. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -175,5 +193,6 @@ */ | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -205,12 +224,28 @@ */ | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function xtzToTokenMarketRate(xtzPool, tokenPool) { | ||
if (xtzPool > 0 && tokenPool > 0) { | ||
return tokenPool / xtzPool; | ||
function xtzToTokenMarketRate(xtzPool, tokenPool, decimals) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
return (tokenPool__ / xtzPool__); | ||
} | ||
else { | ||
@@ -223,17 +258,33 @@ return null; | ||
/** | ||
* Calculate the xtzToToken market rate for a give Dexter contract. The market | ||
* rate is an idea number that doesn't include fees. In practice, this rate | ||
* cannot be executed. | ||
* Calculate the xtzToToken price impact for a given Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function xtzToTokenSlippage(xtzIn, xtzPool, tokenPool) { | ||
if (xtzIn > 0 && xtzPool > 0 && tokenPool > 0) { | ||
var exchangeRate = xtzToTokenExchangeRate(xtzIn, xtzPool, tokenPool); | ||
var marketRate = xtzToTokenMarketRate(xtzPool, tokenPool); | ||
return Math.abs(exchangeRate - marketRate) / marketRate; | ||
function xtzToTokenPriceImpact(xtzIn, xtzPool, tokenPool) { | ||
var xtzIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
try { | ||
xtzIn_ = bigInt(xtzIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(xtzIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var midPrice = tokenPool_ / xtzPool_; | ||
var tokensBought = xtzIn_.times(tokenPool_).divide(xtzIn_.plus(xtzPool_)); | ||
// if no tokens have been purchased then there is no price impact | ||
if (leqZero(tokensBought)) { | ||
return 0; | ||
} | ||
var exactQuote = midPrice * xtzIn_; | ||
return (exactQuote - tokensBought) / exactQuote; | ||
} | ||
else { | ||
@@ -246,6 +297,8 @@ return null; | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. | ||
* Calculate the minimum token out to be sent to Dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
@@ -272,3 +325,3 @@ * @returns {(bigInt|null)} The minimum token amount to send to the xtzToToken entrypoint. | ||
* | ||
* @param {number} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to the dexter liquidity providers. | ||
@@ -297,3 +350,3 @@ */ | ||
* | ||
* @param {number} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to an individual dexter liquidity provider. | ||
@@ -329,7 +382,7 @@ */ | ||
* Get the amount of XTZ sold for a given token input and the pool state of Dexter | ||
* for the dexter tokenToXtz entrypoint. | ||
* for the Dexter tokenToXtz entrypoint. | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of XTZ that Dexter will send to the :to | ||
@@ -365,8 +418,8 @@ * address in the dexter tokenToXtz entrypoint. | ||
* amount of XTZ for a given Dexter pool state. xtzOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token the user must send to tokenToXtz to get the xtzOut amount. | ||
@@ -410,5 +463,5 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -440,5 +493,5 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -468,14 +521,30 @@ */ | ||
/** | ||
* Calculate the tokenToXtz market rate for a give Dexter contract. The market | ||
* Calculate the tokenToXtz market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function tokenToXtzMarketRate(xtzPool, tokenPool) { | ||
if (xtzPool > 0 && tokenPool > 0) { | ||
return xtzPool / tokenPool; | ||
function tokenToXtzMarketRate(xtzPool, tokenPool, decimals) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
return (xtzPool__ / tokenPool__); | ||
} | ||
else { | ||
@@ -488,14 +557,33 @@ return null; | ||
/** | ||
* Calculate the tokenToXtz price impact for a give Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function tokenToXtzSlippage(tokenIn, xtzPool, tokenPool) { | ||
if (tokenIn > 0 && xtzPool > 0 && tokenPool > 0) { | ||
var exchangeRate = tokenToXtzExchangeRate(tokenIn, xtzPool, tokenPool); | ||
var marketRate = tokenToXtzMarketRate(xtzPool, tokenPool); | ||
return Math.abs(exchangeRate - marketRate) / marketRate; | ||
function tokenToXtzPriceImpact(tokenIn, xtzPool, tokenPool) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var midPrice = xtzPool_ / tokenPool_; | ||
var xtzBought = tokenIn_.times(xtzPool_).divide(tokenIn_.plus(tokenPool_)); | ||
// if no tokens have been purchased then there is no price impact | ||
if (leqZero(xtzBought)) { | ||
return 0; | ||
} | ||
var exactQuote = midPrice * tokenIn_; | ||
return (exactQuote - xtzBought) / exactQuote; | ||
} | ||
else { | ||
@@ -509,6 +597,8 @@ return null; | ||
* Calculate the minimum token out to be sent to dexter for a given max xtzOut | ||
* and the max allowed slippage rate the user accepts. | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be greater than 0.00 and less than 1.00. | ||
* @param {(bigInt|number|string)} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToXtz entrypoint. | ||
@@ -532,2 +622,305 @@ */ | ||
* ============================================================================= | ||
* tokenToToken entrypoint functions | ||
* ============================================================================= | ||
*/ | ||
/** | ||
* The tokenToToken entyrpoint requires two dexter contracts. The documentation | ||
* refers to the input token as token X and the output token as token Y. | ||
*/ | ||
/** | ||
* Calculate the amount of token Y sold for token X input and the pool state of Dexter | ||
* for the dexter tokenToToken entrypoint. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token Y that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
*/ | ||
function tokenToTokenTokenOutput(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var firstExchange = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); | ||
return xtzToTokenTokenOutput(firstExchange, xtzPool2_, tokenPool2_); | ||
} | ||
else { | ||
return null; | ||
} | ||
; | ||
} | ||
; | ||
/** | ||
* Calculate the amount of token X you must pay in in order to receive a target | ||
* amount of token Y for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token Y that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token X the user must send to tokenToToken to get the tokenOut amount. | ||
*/ | ||
function tokenToTokenTokenInput(tokenOut, xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var tokenOut_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
tokenOut_ = bigInt(tokenOut); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenOut_) && gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && geqZero(decimals2_)) { | ||
var xtzOut = xtzToTokenXtzInput(tokenOut_, xtzPool2_, tokenPool2_, decimals2_); | ||
if (xtzOut != null && gtZero(xtzOut)) { | ||
return tokenToXtzTokenInput(xtzOut, xtzPool_, tokenPool_, decimals_); | ||
} | ||
; | ||
return null; | ||
} | ||
else { | ||
return null; | ||
} | ||
; | ||
} | ||
; | ||
/** | ||
* Calculate the exchange rate for a token to token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
function tokenToTokenExchangeRate(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var firstExchangeRate = tokenToXtzExchangeRate(tokenIn_, xtzPool_, tokenPool_); // looks right | ||
if (firstExchangeRate === null) { | ||
return null; | ||
} | ||
; | ||
var xtzIn = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); // looks right | ||
if (xtzIn === null) { | ||
return null; | ||
} | ||
; | ||
var secondExchangeRate = xtzToTokenExchangeRate(xtzIn, xtzPool2_, tokenPool2_); | ||
if (secondExchangeRate === null) { | ||
return null; | ||
} | ||
; | ||
return firstExchangeRate * secondExchangeRate; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
; | ||
/** | ||
* Same as tokenToTokenExchangeRate but adjusted for the decimal places. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
function tokenToTokenExchangeRateForDisplay(tokenIn, xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && gtZero(decimals2_)) { | ||
var firstExchangeRate = tokenToXtzExchangeRateForDisplay(tokenIn_, xtzPool_, tokenPool_, decimals_); | ||
if (firstExchangeRate === null) { | ||
return null; | ||
} | ||
; | ||
var xtzIn = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); | ||
if (xtzIn === null) { | ||
return null; | ||
} | ||
; | ||
var secondExchangeRate = xtzToTokenExchangeRateForDisplay(xtzIn, xtzPool2_, tokenPool2_, decimals2_); | ||
if (secondExchangeRate === null) { | ||
return null; | ||
} | ||
; | ||
return firstExchangeRate * secondExchangeRate; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
; | ||
/** | ||
* Calculate the tokenToToken market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - Number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - Number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function tokenToTokenMarketRate(xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && geqZero(decimals2_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
var xtzPool2__ = xtzPool2_ * Math.pow(10, -6); | ||
var tokenPool2__ = tokenPool2_ * Math.pow(10, -(decimals2_)); | ||
return (xtzPool__ / tokenPool__) * (tokenPool2__ / xtzPool2__); | ||
} | ||
else { | ||
return null; | ||
} | ||
; | ||
} | ||
; | ||
/** | ||
* Calculate the tokenToToken price impact for two Dexter contracts. Price | ||
* impact is measure of how much a trade will alter the future price. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - The amount of token X the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function tokenToTokenPriceImpact(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var xtzBought = tokenIn_.times(xtzPool_).divide(tokenIn_.plus(tokenPool_)); | ||
var one = tokenToXtzPriceImpact(tokenIn, xtzPool, tokenPool); | ||
var two = xtzToTokenPriceImpact(xtzBought, xtzPool2, tokenPool); | ||
return (one + two) / 2.0; | ||
} | ||
else { | ||
return null; | ||
} | ||
; | ||
} | ||
; | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToToken entrypoint. | ||
*/ | ||
function tokenToTokenMinimumTokenOutput(tokenOut, allowedSlippage) { | ||
return xtzToTokenMinimumTokenOutput(tokenOut, allowSlippage); | ||
} | ||
; | ||
/** | ||
* ============================================================================= | ||
* addLiquidity entrypoint functions | ||
@@ -541,5 +934,5 @@ * ============================================================================= | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
@@ -582,5 +975,5 @@ */ | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
@@ -617,6 +1010,6 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. | ||
* @param {number} tokenPool Token amount that Dexter holds. | ||
* @returns {{null|bigInt}} The amount of liquidity that the sender gains. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. | ||
* @param {(bigInt|number|string)} tokenPool Token amount that Dexter holds. | ||
* @returns {{bigInt|null}} The amount of liquidity that the sender gains. | ||
*/ | ||
@@ -656,6 +1049,6 @@ function addLiquidityXtzIn(tokenIn, xtzPool, tokenPool) { | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} tokenPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of token that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} tokenPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of token that the sender gains. | ||
*/ | ||
@@ -690,6 +1083,6 @@ function removeLiquidityTokenOut(liquidityBurned, totalLiquidity, tokenPool) { | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} xtzPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of XTZ that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} xtzPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of XTZ that the sender gains. | ||
*/ | ||
@@ -726,3 +1119,3 @@ function removeLiquidityXtzOut(liquidityBurned, totalLiquidity, xtzPool) { | ||
xtzToTokenMarketRate: xtzToTokenMarketRate, | ||
xtzToTokenSlippage: xtzToTokenSlippage, | ||
xtzToTokenPriceImpact: xtzToTokenPriceImpact, | ||
xtzToTokenMinimumTokenOutput: xtzToTokenMinimumTokenOutput, | ||
@@ -737,4 +1130,12 @@ totalLiquidityProviderFee: totalLiquidityProviderFee, | ||
tokenToXtzMarketRate: tokenToXtzMarketRate, | ||
tokenToXtzSlippage: tokenToXtzSlippage, | ||
tokenToXtzPriceImpact: tokenToXtzPriceImpact, | ||
tokenToXtzMinimumXtzOutput: tokenToXtzMinimumXtzOutput, | ||
// tokenToToken | ||
tokenToTokenTokenOutput: tokenToTokenTokenOutput, | ||
tokenToTokenTokenInput: tokenToTokenTokenInput, | ||
tokenToTokenExchangeRate: tokenToTokenExchangeRate, | ||
tokenToTokenExchangeRateForDisplay: tokenToTokenExchangeRateForDisplay, | ||
tokenToTokenMarketRate: tokenToTokenMarketRate, | ||
tokenToTokenPriceImpact: tokenToTokenPriceImpact, | ||
tokenToTokenMinimumTokenOutput: tokenToTokenMinimumTokenOutput, | ||
// addLiquidity | ||
@@ -741,0 +1142,0 @@ addLiquidityLiquidityCreated: addLiquidityLiquidityCreated, |
586
index-raw.js
@@ -1,7 +0,1 @@ | ||
#IF MAIN | ||
var bigInt = require('big-integer'); | ||
#ELSE | ||
var bigInt=function(undefined){"use strict";var BASE=1e7,LOG_BASE=7,MAX_INT=9007199254740992,MAX_INT_ARR=smallToArray(MAX_INT),DEFAULT_ALPHABET="0123456789abcdefghijklmnopqrstuvwxyz";var supportsNativeBigInt=typeof BigInt==="function";function Integer(v,radix,alphabet,caseSensitive){if(typeof v==="undefined")return Integer[0];if(typeof radix!=="undefined")return+radix===10&&!alphabet?parseValue(v):parseBase(v,radix,alphabet,caseSensitive);return parseValue(v)}function BigInteger(value,sign){this.value=value;this.sign=sign;this.isSmall=false}BigInteger.prototype=Object.create(Integer.prototype);function SmallInteger(value){this.value=value;this.sign=value<0;this.isSmall=true}SmallInteger.prototype=Object.create(Integer.prototype);function NativeBigInt(value){this.value=value}NativeBigInt.prototype=Object.create(Integer.prototype);function isPrecise(n){return-MAX_INT<n&&n<MAX_INT}function smallToArray(n){if(n<1e7)return[n];if(n<1e14)return[n%1e7,Math.floor(n/1e7)];return[n%1e7,Math.floor(n/1e7)%1e7,Math.floor(n/1e14)]}function arrayToSmall(arr){trim(arr);var length=arr.length;if(length<4&&compareAbs(arr,MAX_INT_ARR)<0){switch(length){case 0:return 0;case 1:return arr[0];case 2:return arr[0]+arr[1]*BASE;default:return arr[0]+(arr[1]+arr[2]*BASE)*BASE}}return arr}function trim(v){var i=v.length;while(v[--i]===0);v.length=i+1}function createArray(length){var x=new Array(length);var i=-1;while(++i<length){x[i]=0}return x}function truncate(n){if(n>0)return Math.floor(n);return Math.ceil(n)}function add(a,b){var l_a=a.length,l_b=b.length,r=new Array(l_a),carry=0,base=BASE,sum,i;for(i=0;i<l_b;i++){sum=a[i]+b[i]+carry;carry=sum>=base?1:0;r[i]=sum-carry*base}while(i<l_a){sum=a[i]+carry;carry=sum===base?1:0;r[i++]=sum-carry*base}if(carry>0)r.push(carry);return r}function addAny(a,b){if(a.length>=b.length)return add(a,b);return add(b,a)}function addSmall(a,carry){var l=a.length,r=new Array(l),base=BASE,sum,i;for(i=0;i<l;i++){sum=a[i]-base+carry;carry=Math.floor(sum/base);r[i]=sum-carry*base;carry+=1}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}BigInteger.prototype.add=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.subtract(n.negate())}var a=this.value,b=n.value;if(n.isSmall){return new BigInteger(addSmall(a,Math.abs(b)),this.sign)}return new BigInteger(addAny(a,b),this.sign)};BigInteger.prototype.plus=BigInteger.prototype.add;SmallInteger.prototype.add=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.subtract(n.negate())}var b=n.value;if(n.isSmall){if(isPrecise(a+b))return new SmallInteger(a+b);b=smallToArray(Math.abs(b))}return new BigInteger(addSmall(b,Math.abs(a)),a<0)};SmallInteger.prototype.plus=SmallInteger.prototype.add;NativeBigInt.prototype.add=function(v){return new NativeBigInt(this.value+parseValue(v).value)};NativeBigInt.prototype.plus=NativeBigInt.prototype.add;function subtract(a,b){var a_l=a.length,b_l=b.length,r=new Array(a_l),borrow=0,base=BASE,i,difference;for(i=0;i<b_l;i++){difference=a[i]-borrow-b[i];if(difference<0){difference+=base;borrow=1}else borrow=0;r[i]=difference}for(i=b_l;i<a_l;i++){difference=a[i]-borrow;if(difference<0)difference+=base;else{r[i++]=difference;break}r[i]=difference}for(;i<a_l;i++){r[i]=a[i]}trim(r);return r}function subtractAny(a,b,sign){var value;if(compareAbs(a,b)>=0){value=subtract(a,b)}else{value=subtract(b,a);sign=!sign}value=arrayToSmall(value);if(typeof value==="number"){if(sign)value=-value;return new SmallInteger(value)}return new BigInteger(value,sign)}function subtractSmall(a,b,sign){var l=a.length,r=new Array(l),carry=-b,base=BASE,i,difference;for(i=0;i<l;i++){difference=a[i]+carry;carry=Math.floor(difference/base);difference%=base;r[i]=difference<0?difference+base:difference}r=arrayToSmall(r);if(typeof r==="number"){if(sign)r=-r;return new SmallInteger(r)}return new BigInteger(r,sign)}BigInteger.prototype.subtract=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.add(n.negate())}var a=this.value,b=n.value;if(n.isSmall)return subtractSmall(a,Math.abs(b),this.sign);return subtractAny(a,b,this.sign)};BigInteger.prototype.minus=BigInteger.prototype.subtract;SmallInteger.prototype.subtract=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.add(n.negate())}var b=n.value;if(n.isSmall){return new SmallInteger(a-b)}return subtractSmall(b,Math.abs(a),a>=0)};SmallInteger.prototype.minus=SmallInteger.prototype.subtract;NativeBigInt.prototype.subtract=function(v){return new NativeBigInt(this.value-parseValue(v).value)};NativeBigInt.prototype.minus=NativeBigInt.prototype.subtract;BigInteger.prototype.negate=function(){return new BigInteger(this.value,!this.sign)};SmallInteger.prototype.negate=function(){var sign=this.sign;var small=new SmallInteger(-this.value);small.sign=!sign;return small};NativeBigInt.prototype.negate=function(){return new NativeBigInt(-this.value)};BigInteger.prototype.abs=function(){return new BigInteger(this.value,false)};SmallInteger.prototype.abs=function(){return new SmallInteger(Math.abs(this.value))};NativeBigInt.prototype.abs=function(){return new NativeBigInt(this.value>=0?this.value:-this.value)};function multiplyLong(a,b){var a_l=a.length,b_l=b.length,l=a_l+b_l,r=createArray(l),base=BASE,product,carry,i,a_i,b_j;for(i=0;i<a_l;++i){a_i=a[i];for(var j=0;j<b_l;++j){b_j=b[j];product=a_i*b_j+r[i+j];carry=Math.floor(product/base);r[i+j]=product-carry*base;r[i+j+1]+=carry}}trim(r);return r}function multiplySmall(a,b){var l=a.length,r=new Array(l),base=BASE,carry=0,product,i;for(i=0;i<l;i++){product=a[i]*b+carry;carry=Math.floor(product/base);r[i]=product-carry*base}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}function shiftLeft(x,n){var r=[];while(n-- >0)r.push(0);return r.concat(x)}function multiplyKaratsuba(x,y){var n=Math.max(x.length,y.length);if(n<=30)return multiplyLong(x,y);n=Math.ceil(n/2);var b=x.slice(n),a=x.slice(0,n),d=y.slice(n),c=y.slice(0,n);var ac=multiplyKaratsuba(a,c),bd=multiplyKaratsuba(b,d),abcd=multiplyKaratsuba(addAny(a,b),addAny(c,d));var product=addAny(addAny(ac,shiftLeft(subtract(subtract(abcd,ac),bd),n)),shiftLeft(bd,2*n));trim(product);return product}function useKaratsuba(l1,l2){return-.012*l1-.012*l2+15e-6*l1*l2>0}BigInteger.prototype.multiply=function(v){var n=parseValue(v),a=this.value,b=n.value,sign=this.sign!==n.sign,abs;if(n.isSmall){if(b===0)return Integer[0];if(b===1)return this;if(b===-1)return this.negate();abs=Math.abs(b);if(abs<BASE){return new BigInteger(multiplySmall(a,abs),sign)}b=smallToArray(abs)}if(useKaratsuba(a.length,b.length))return new BigInteger(multiplyKaratsuba(a,b),sign);return new BigInteger(multiplyLong(a,b),sign)};BigInteger.prototype.times=BigInteger.prototype.multiply;function multiplySmallAndArray(a,b,sign){if(a<BASE){return new BigInteger(multiplySmall(b,a),sign)}return new BigInteger(multiplyLong(b,smallToArray(a)),sign)}SmallInteger.prototype._multiplyBySmall=function(a){if(isPrecise(a.value*this.value)){return new SmallInteger(a.value*this.value)}return multiplySmallAndArray(Math.abs(a.value),smallToArray(Math.abs(this.value)),this.sign!==a.sign)};BigInteger.prototype._multiplyBySmall=function(a){if(a.value===0)return Integer[0];if(a.value===1)return this;if(a.value===-1)return this.negate();return multiplySmallAndArray(Math.abs(a.value),this.value,this.sign!==a.sign)};SmallInteger.prototype.multiply=function(v){return parseValue(v)._multiplyBySmall(this)};SmallInteger.prototype.times=SmallInteger.prototype.multiply;NativeBigInt.prototype.multiply=function(v){return new NativeBigInt(this.value*parseValue(v).value)};NativeBigInt.prototype.times=NativeBigInt.prototype.multiply;function square(a){var l=a.length,r=createArray(l+l),base=BASE,product,carry,i,a_i,a_j;for(i=0;i<l;i++){a_i=a[i];carry=0-a_i*a_i;for(var j=i;j<l;j++){a_j=a[j];product=2*(a_i*a_j)+r[i+j]+carry;carry=Math.floor(product/base);r[i+j]=product-carry*base}r[i+l]=carry}trim(r);return r}BigInteger.prototype.square=function(){return new BigInteger(square(this.value),false)};SmallInteger.prototype.square=function(){var value=this.value*this.value;if(isPrecise(value))return new SmallInteger(value);return new BigInteger(square(smallToArray(Math.abs(this.value))),false)};NativeBigInt.prototype.square=function(v){return new NativeBigInt(this.value*this.value)};function divMod1(a,b){var a_l=a.length,b_l=b.length,base=BASE,result=createArray(b.length),divisorMostSignificantDigit=b[b_l-1],lambda=Math.ceil(base/(2*divisorMostSignificantDigit)),remainder=multiplySmall(a,lambda),divisor=multiplySmall(b,lambda),quotientDigit,shift,carry,borrow,i,l,q;if(remainder.length<=a_l)remainder.push(0);divisor.push(0);divisorMostSignificantDigit=divisor[b_l-1];for(shift=a_l-b_l;shift>=0;shift--){quotientDigit=base-1;if(remainder[shift+b_l]!==divisorMostSignificantDigit){quotientDigit=Math.floor((remainder[shift+b_l]*base+remainder[shift+b_l-1])/divisorMostSignificantDigit)}carry=0;borrow=0;l=divisor.length;for(i=0;i<l;i++){carry+=quotientDigit*divisor[i];q=Math.floor(carry/base);borrow+=remainder[shift+i]-(carry-q*base);carry=q;if(borrow<0){remainder[shift+i]=borrow+base;borrow=-1}else{remainder[shift+i]=borrow;borrow=0}}while(borrow!==0){quotientDigit-=1;carry=0;for(i=0;i<l;i++){carry+=remainder[shift+i]-base+divisor[i];if(carry<0){remainder[shift+i]=carry+base;carry=0}else{remainder[shift+i]=carry;carry=1}}borrow+=carry}result[shift]=quotientDigit}remainder=divModSmall(remainder,lambda)[0];return[arrayToSmall(result),arrayToSmall(remainder)]}function divMod2(a,b){var a_l=a.length,b_l=b.length,result=[],part=[],base=BASE,guess,xlen,highx,highy,check;while(a_l){part.unshift(a[--a_l]);trim(part);if(compareAbs(part,b)<0){result.push(0);continue}xlen=part.length;highx=part[xlen-1]*base+part[xlen-2];highy=b[b_l-1]*base+b[b_l-2];if(xlen>b_l){highx=(highx+1)*base}guess=Math.ceil(highx/highy);do{check=multiplySmall(b,guess);if(compareAbs(check,part)<=0)break;guess--}while(guess);result.push(guess);part=subtract(part,check)}result.reverse();return[arrayToSmall(result),arrayToSmall(part)]}function divModSmall(value,lambda){var length=value.length,quotient=createArray(length),base=BASE,i,q,remainder,divisor;remainder=0;for(i=length-1;i>=0;--i){divisor=remainder*base+value[i];q=truncate(divisor/lambda);remainder=divisor-q*lambda;quotient[i]=q|0}return[quotient,remainder|0]}function divModAny(self,v){var value,n=parseValue(v);if(supportsNativeBigInt){return[new NativeBigInt(self.value/n.value),new NativeBigInt(self.value%n.value)]}var a=self.value,b=n.value;var quotient;if(b===0)throw new Error("Cannot divide by zero");if(self.isSmall){if(n.isSmall){return[new SmallInteger(truncate(a/b)),new SmallInteger(a%b)]}return[Integer[0],self]}if(n.isSmall){if(b===1)return[self,Integer[0]];if(b==-1)return[self.negate(),Integer[0]];var abs=Math.abs(b);if(abs<BASE){value=divModSmall(a,abs);quotient=arrayToSmall(value[0]);var remainder=value[1];if(self.sign)remainder=-remainder;if(typeof quotient==="number"){if(self.sign!==n.sign)quotient=-quotient;return[new SmallInteger(quotient),new SmallInteger(remainder)]}return[new BigInteger(quotient,self.sign!==n.sign),new SmallInteger(remainder)]}b=smallToArray(abs)}var comparison=compareAbs(a,b);if(comparison===-1)return[Integer[0],self];if(comparison===0)return[Integer[self.sign===n.sign?1:-1],Integer[0]];if(a.length+b.length<=200)value=divMod1(a,b);else value=divMod2(a,b);quotient=value[0];var qSign=self.sign!==n.sign,mod=value[1],mSign=self.sign;if(typeof quotient==="number"){if(qSign)quotient=-quotient;quotient=new SmallInteger(quotient)}else quotient=new BigInteger(quotient,qSign);if(typeof mod==="number"){if(mSign)mod=-mod;mod=new SmallInteger(mod)}else mod=new BigInteger(mod,mSign);return[quotient,mod]}BigInteger.prototype.divmod=function(v){var result=divModAny(this,v);return{quotient:result[0],remainder:result[1]}};NativeBigInt.prototype.divmod=SmallInteger.prototype.divmod=BigInteger.prototype.divmod;BigInteger.prototype.divide=function(v){return divModAny(this,v)[0]};NativeBigInt.prototype.over=NativeBigInt.prototype.divide=function(v){return new NativeBigInt(this.value/parseValue(v).value)};SmallInteger.prototype.over=SmallInteger.prototype.divide=BigInteger.prototype.over=BigInteger.prototype.divide;BigInteger.prototype.mod=function(v){return divModAny(this,v)[1]};NativeBigInt.prototype.mod=NativeBigInt.prototype.remainder=function(v){return new NativeBigInt(this.value%parseValue(v).value)};SmallInteger.prototype.remainder=SmallInteger.prototype.mod=BigInteger.prototype.remainder=BigInteger.prototype.mod;BigInteger.prototype.pow=function(v){var n=parseValue(v),a=this.value,b=n.value,value,x,y;if(b===0)return Integer[1];if(a===0)return Integer[0];if(a===1)return Integer[1];if(a===-1)return n.isEven()?Integer[1]:Integer[-1];if(n.sign){return Integer[0]}if(!n.isSmall)throw new Error("The exponent "+n.toString()+" is too large.");if(this.isSmall){if(isPrecise(value=Math.pow(a,b)))return new SmallInteger(truncate(value))}x=this;y=Integer[1];while(true){if(b&1===1){y=y.times(x);--b}if(b===0)break;b/=2;x=x.square()}return y};SmallInteger.prototype.pow=BigInteger.prototype.pow;NativeBigInt.prototype.pow=function(v){var n=parseValue(v);var a=this.value,b=n.value;var _0=BigInt(0),_1=BigInt(1),_2=BigInt(2);if(b===_0)return Integer[1];if(a===_0)return Integer[0];if(a===_1)return Integer[1];if(a===BigInt(-1))return n.isEven()?Integer[1]:Integer[-1];if(n.isNegative())return new NativeBigInt(_0);var x=this;var y=Integer[1];while(true){if((b&_1)===_1){y=y.times(x);--b}if(b===_0)break;b/=_2;x=x.square()}return y};BigInteger.prototype.modPow=function(exp,mod){exp=parseValue(exp);mod=parseValue(mod);if(mod.isZero())throw new Error("Cannot take modPow with modulus 0");var r=Integer[1],base=this.mod(mod);if(exp.isNegative()){exp=exp.multiply(Integer[-1]);base=base.modInv(mod)}while(exp.isPositive()){if(base.isZero())return Integer[0];if(exp.isOdd())r=r.multiply(base).mod(mod);exp=exp.divide(2);base=base.square().mod(mod)}return r};NativeBigInt.prototype.modPow=SmallInteger.prototype.modPow=BigInteger.prototype.modPow;function compareAbs(a,b){if(a.length!==b.length){return a.length>b.length?1:-1}for(var i=a.length-1;i>=0;i--){if(a[i]!==b[i])return a[i]>b[i]?1:-1}return 0}BigInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall)return 1;return compareAbs(a,b)};SmallInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=Math.abs(this.value),b=n.value;if(n.isSmall){b=Math.abs(b);return a===b?0:a>b?1:-1}return-1};NativeBigInt.prototype.compareAbs=function(v){var a=this.value;var b=parseValue(v).value;a=a>=0?a:-a;b=b>=0?b:-b;return a===b?0:a>b?1:-1};BigInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(this.sign!==n.sign){return n.sign?1:-1}if(n.isSmall){return this.sign?-1:1}return compareAbs(a,b)*(this.sign?-1:1)};BigInteger.prototype.compareTo=BigInteger.prototype.compare;SmallInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall){return a==b?0:a>b?1:-1}if(a<0!==n.sign){return a<0?-1:1}return a<0?1:-1};SmallInteger.prototype.compareTo=SmallInteger.prototype.compare;NativeBigInt.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var a=this.value;var b=parseValue(v).value;return a===b?0:a>b?1:-1};NativeBigInt.prototype.compareTo=NativeBigInt.prototype.compare;BigInteger.prototype.equals=function(v){return this.compare(v)===0};NativeBigInt.prototype.eq=NativeBigInt.prototype.equals=SmallInteger.prototype.eq=SmallInteger.prototype.equals=BigInteger.prototype.eq=BigInteger.prototype.equals;BigInteger.prototype.notEquals=function(v){return this.compare(v)!==0};NativeBigInt.prototype.neq=NativeBigInt.prototype.notEquals=SmallInteger.prototype.neq=SmallInteger.prototype.notEquals=BigInteger.prototype.neq=BigInteger.prototype.notEquals;BigInteger.prototype.greater=function(v){return this.compare(v)>0};NativeBigInt.prototype.gt=NativeBigInt.prototype.greater=SmallInteger.prototype.gt=SmallInteger.prototype.greater=BigInteger.prototype.gt=BigInteger.prototype.greater;BigInteger.prototype.lesser=function(v){return this.compare(v)<0};NativeBigInt.prototype.lt=NativeBigInt.prototype.lesser=SmallInteger.prototype.lt=SmallInteger.prototype.lesser=BigInteger.prototype.lt=BigInteger.prototype.lesser;BigInteger.prototype.greaterOrEquals=function(v){return this.compare(v)>=0};NativeBigInt.prototype.geq=NativeBigInt.prototype.greaterOrEquals=SmallInteger.prototype.geq=SmallInteger.prototype.greaterOrEquals=BigInteger.prototype.geq=BigInteger.prototype.greaterOrEquals;BigInteger.prototype.lesserOrEquals=function(v){return this.compare(v)<=0};NativeBigInt.prototype.leq=NativeBigInt.prototype.lesserOrEquals=SmallInteger.prototype.leq=SmallInteger.prototype.lesserOrEquals=BigInteger.prototype.leq=BigInteger.prototype.lesserOrEquals;BigInteger.prototype.isEven=function(){return(this.value[0]&1)===0};SmallInteger.prototype.isEven=function(){return(this.value&1)===0};NativeBigInt.prototype.isEven=function(){return(this.value&BigInt(1))===BigInt(0)};BigInteger.prototype.isOdd=function(){return(this.value[0]&1)===1};SmallInteger.prototype.isOdd=function(){return(this.value&1)===1};NativeBigInt.prototype.isOdd=function(){return(this.value&BigInt(1))===BigInt(1)};BigInteger.prototype.isPositive=function(){return!this.sign};SmallInteger.prototype.isPositive=function(){return this.value>0};NativeBigInt.prototype.isPositive=SmallInteger.prototype.isPositive;BigInteger.prototype.isNegative=function(){return this.sign};SmallInteger.prototype.isNegative=function(){return this.value<0};NativeBigInt.prototype.isNegative=SmallInteger.prototype.isNegative;BigInteger.prototype.isUnit=function(){return false};SmallInteger.prototype.isUnit=function(){return Math.abs(this.value)===1};NativeBigInt.prototype.isUnit=function(){return this.abs().value===BigInt(1)};BigInteger.prototype.isZero=function(){return false};SmallInteger.prototype.isZero=function(){return this.value===0};NativeBigInt.prototype.isZero=function(){return this.value===BigInt(0)};BigInteger.prototype.isDivisibleBy=function(v){var n=parseValue(v);if(n.isZero())return false;if(n.isUnit())return true;if(n.compareAbs(2)===0)return this.isEven();return this.mod(n).isZero()};NativeBigInt.prototype.isDivisibleBy=SmallInteger.prototype.isDivisibleBy=BigInteger.prototype.isDivisibleBy;function isBasicPrime(v){var n=v.abs();if(n.isUnit())return false;if(n.equals(2)||n.equals(3)||n.equals(5))return true;if(n.isEven()||n.isDivisibleBy(3)||n.isDivisibleBy(5))return false;if(n.lesser(49))return true}function millerRabinTest(n,a){var nPrev=n.prev(),b=nPrev,r=0,d,t,i,x;while(b.isEven())b=b.divide(2),r++;next:for(i=0;i<a.length;i++){if(n.lesser(a[i]))continue;x=bigInt(a[i]).modPow(b,n);if(x.isUnit()||x.equals(nPrev))continue;for(d=r-1;d!=0;d--){x=x.square().mod(n);if(x.isUnit())return false;if(x.equals(nPrev))continue next}return false}return true}BigInteger.prototype.isPrime=function(strict){var isPrime=isBasicPrime(this);if(isPrime!==undefined)return isPrime;var n=this.abs();var bits=n.bitLength();if(bits<=64)return millerRabinTest(n,[2,3,5,7,11,13,17,19,23,29,31,37]);var logN=Math.log(2)*bits.toJSNumber();var t=Math.ceil(strict===true?2*Math.pow(logN,2):logN);for(var a=[],i=0;i<t;i++){a.push(bigInt(i+2))}return millerRabinTest(n,a)};NativeBigInt.prototype.isPrime=SmallInteger.prototype.isPrime=BigInteger.prototype.isPrime;BigInteger.prototype.isProbablePrime=function(iterations,rng){var isPrime=isBasicPrime(this);if(isPrime!==undefined)return isPrime;var n=this.abs();var t=iterations===undefined?5:iterations;for(var a=[],i=0;i<t;i++){a.push(bigInt.randBetween(2,n.minus(2),rng))}return millerRabinTest(n,a)};NativeBigInt.prototype.isProbablePrime=SmallInteger.prototype.isProbablePrime=BigInteger.prototype.isProbablePrime;BigInteger.prototype.modInv=function(n){var t=bigInt.zero,newT=bigInt.one,r=parseValue(n),newR=this.abs(),q,lastT,lastR;while(!newR.isZero()){q=r.divide(newR);lastT=t;lastR=r;t=newT;r=newR;newT=lastT.subtract(q.multiply(newT));newR=lastR.subtract(q.multiply(newR))}if(!r.isUnit())throw new Error(this.toString()+" and "+n.toString()+" are not co-prime");if(t.compare(0)===-1){t=t.add(n)}if(this.isNegative()){return t.negate()}return t};NativeBigInt.prototype.modInv=SmallInteger.prototype.modInv=BigInteger.prototype.modInv;BigInteger.prototype.next=function(){var value=this.value;if(this.sign){return subtractSmall(value,1,this.sign)}return new BigInteger(addSmall(value,1),this.sign)};SmallInteger.prototype.next=function(){var value=this.value;if(value+1<MAX_INT)return new SmallInteger(value+1);return new BigInteger(MAX_INT_ARR,false)};NativeBigInt.prototype.next=function(){return new NativeBigInt(this.value+BigInt(1))};BigInteger.prototype.prev=function(){var value=this.value;if(this.sign){return new BigInteger(addSmall(value,1),true)}return subtractSmall(value,1,this.sign)};SmallInteger.prototype.prev=function(){var value=this.value;if(value-1>-MAX_INT)return new SmallInteger(value-1);return new BigInteger(MAX_INT_ARR,true)};NativeBigInt.prototype.prev=function(){return new NativeBigInt(this.value-BigInt(1))};var powersOfTwo=[1];while(2*powersOfTwo[powersOfTwo.length-1]<=BASE)powersOfTwo.push(2*powersOfTwo[powersOfTwo.length-1]);var powers2Length=powersOfTwo.length,highestPower2=powersOfTwo[powers2Length-1];function shift_isSmall(n){return Math.abs(n)<=BASE}BigInteger.prototype.shiftLeft=function(v){var n=parseValue(v).toJSNumber();if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}if(n<0)return this.shiftRight(-n);var result=this;if(result.isZero())return result;while(n>=powers2Length){result=result.multiply(highestPower2);n-=powers2Length-1}return result.multiply(powersOfTwo[n])};NativeBigInt.prototype.shiftLeft=SmallInteger.prototype.shiftLeft=BigInteger.prototype.shiftLeft;BigInteger.prototype.shiftRight=function(v){var remQuo;var n=parseValue(v).toJSNumber();if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}if(n<0)return this.shiftLeft(-n);var result=this;while(n>=powers2Length){if(result.isZero()||result.isNegative()&&result.isUnit())return result;remQuo=divModAny(result,highestPower2);result=remQuo[1].isNegative()?remQuo[0].prev():remQuo[0];n-=powers2Length-1}remQuo=divModAny(result,powersOfTwo[n]);return remQuo[1].isNegative()?remQuo[0].prev():remQuo[0]};NativeBigInt.prototype.shiftRight=SmallInteger.prototype.shiftRight=BigInteger.prototype.shiftRight;function bitwise(x,y,fn){y=parseValue(y);var xSign=x.isNegative(),ySign=y.isNegative();var xRem=xSign?x.not():x,yRem=ySign?y.not():y;var xDigit=0,yDigit=0;var xDivMod=null,yDivMod=null;var result=[];while(!xRem.isZero()||!yRem.isZero()){xDivMod=divModAny(xRem,highestPower2);xDigit=xDivMod[1].toJSNumber();if(xSign){xDigit=highestPower2-1-xDigit}yDivMod=divModAny(yRem,highestPower2);yDigit=yDivMod[1].toJSNumber();if(ySign){yDigit=highestPower2-1-yDigit}xRem=xDivMod[0];yRem=yDivMod[0];result.push(fn(xDigit,yDigit))}var sum=fn(xSign?1:0,ySign?1:0)!==0?bigInt(-1):bigInt(0);for(var i=result.length-1;i>=0;i-=1){sum=sum.multiply(highestPower2).add(bigInt(result[i]))}return sum}BigInteger.prototype.not=function(){return this.negate().prev()};NativeBigInt.prototype.not=SmallInteger.prototype.not=BigInteger.prototype.not;BigInteger.prototype.and=function(n){return bitwise(this,n,function(a,b){return a&b})};NativeBigInt.prototype.and=SmallInteger.prototype.and=BigInteger.prototype.and;BigInteger.prototype.or=function(n){return bitwise(this,n,function(a,b){return a|b})};NativeBigInt.prototype.or=SmallInteger.prototype.or=BigInteger.prototype.or;BigInteger.prototype.xor=function(n){return bitwise(this,n,function(a,b){return a^b})};NativeBigInt.prototype.xor=SmallInteger.prototype.xor=BigInteger.prototype.xor;var LOBMASK_I=1<<30,LOBMASK_BI=(BASE&-BASE)*(BASE&-BASE)|LOBMASK_I;function roughLOB(n){var v=n.value,x=typeof v==="number"?v|LOBMASK_I:typeof v==="bigint"?v|BigInt(LOBMASK_I):v[0]+v[1]*BASE|LOBMASK_BI;return x&-x}function integerLogarithm(value,base){if(base.compareTo(value)<=0){var tmp=integerLogarithm(value,base.square(base));var p=tmp.p;var e=tmp.e;var t=p.multiply(base);return t.compareTo(value)<=0?{p:t,e:e*2+1}:{p:p,e:e*2}}return{p:bigInt(1),e:0}}BigInteger.prototype.bitLength=function(){var n=this;if(n.compareTo(bigInt(0))<0){n=n.negate().subtract(bigInt(1))}if(n.compareTo(bigInt(0))===0){return bigInt(0)}return bigInt(integerLogarithm(n,bigInt(2)).e).add(bigInt(1))};NativeBigInt.prototype.bitLength=SmallInteger.prototype.bitLength=BigInteger.prototype.bitLength;function max(a,b){a=parseValue(a);b=parseValue(b);return a.greater(b)?a:b}function min(a,b){a=parseValue(a);b=parseValue(b);return a.lesser(b)?a:b}function gcd(a,b){a=parseValue(a).abs();b=parseValue(b).abs();if(a.equals(b))return a;if(a.isZero())return b;if(b.isZero())return a;var c=Integer[1],d,t;while(a.isEven()&&b.isEven()){d=min(roughLOB(a),roughLOB(b));a=a.divide(d);b=b.divide(d);c=c.multiply(d)}while(a.isEven()){a=a.divide(roughLOB(a))}do{while(b.isEven()){b=b.divide(roughLOB(b))}if(a.greater(b)){t=b;b=a;a=t}b=b.subtract(a)}while(!b.isZero());return c.isUnit()?a:a.multiply(c)}function lcm(a,b){a=parseValue(a).abs();b=parseValue(b).abs();return a.divide(gcd(a,b)).multiply(b)}function randBetween(a,b,rng){a=parseValue(a);b=parseValue(b);var usedRNG=rng||Math.random;var low=min(a,b),high=max(a,b);var range=high.subtract(low).add(1);if(range.isSmall)return low.add(Math.floor(usedRNG()*range));var digits=toBase(range,BASE).value;var result=[],restricted=true;for(var i=0;i<digits.length;i++){var top=restricted?digits[i]:BASE;var digit=truncate(usedRNG()*top);result.push(digit);if(digit<top)restricted=false}return low.add(Integer.fromArray(result,BASE,false))}var parseBase=function(text,base,alphabet,caseSensitive){alphabet=alphabet||DEFAULT_ALPHABET;text=String(text);if(!caseSensitive){text=text.toLowerCase();alphabet=alphabet.toLowerCase()}var length=text.length;var i;var absBase=Math.abs(base);var alphabetValues={};for(i=0;i<alphabet.length;i++){alphabetValues[alphabet[i]]=i}for(i=0;i<length;i++){var c=text[i];if(c==="-")continue;if(c in alphabetValues){if(alphabetValues[c]>=absBase){if(c==="1"&&absBase===1)continue;throw new Error(c+" is not a valid digit in base "+base+".")}}}base=parseValue(base);var digits=[];var isNegative=text[0]==="-";for(i=isNegative?1:0;i<text.length;i++){var c=text[i];if(c in alphabetValues)digits.push(parseValue(alphabetValues[c]));else if(c==="<"){var start=i;do{i++}while(text[i]!==">"&&i<text.length);digits.push(parseValue(text.slice(start+1,i)))}else throw new Error(c+" is not a valid character")}return parseBaseFromArray(digits,base,isNegative)};function parseBaseFromArray(digits,base,isNegative){var val=Integer[0],pow=Integer[1],i;for(i=digits.length-1;i>=0;i--){val=val.add(digits[i].times(pow));pow=pow.times(base)}return isNegative?val.negate():val}function stringify(digit,alphabet){alphabet=alphabet||DEFAULT_ALPHABET;if(digit<alphabet.length){return alphabet[digit]}return"<"+digit+">"}function toBase(n,base){base=bigInt(base);if(base.isZero()){if(n.isZero())return{value:[0],isNegative:false};throw new Error("Cannot convert nonzero numbers to base 0.")}if(base.equals(-1)){if(n.isZero())return{value:[0],isNegative:false};if(n.isNegative())return{value:[].concat.apply([],Array.apply(null,Array(-n.toJSNumber())).map(Array.prototype.valueOf,[1,0])),isNegative:false};var arr=Array.apply(null,Array(n.toJSNumber()-1)).map(Array.prototype.valueOf,[0,1]);arr.unshift([1]);return{value:[].concat.apply([],arr),isNegative:false}}var neg=false;if(n.isNegative()&&base.isPositive()){neg=true;n=n.abs()}if(base.isUnit()){if(n.isZero())return{value:[0],isNegative:false};return{value:Array.apply(null,Array(n.toJSNumber())).map(Number.prototype.valueOf,1),isNegative:neg}}var out=[];var left=n,divmod;while(left.isNegative()||left.compareAbs(base)>=0){divmod=left.divmod(base);left=divmod.quotient;var digit=divmod.remainder;if(digit.isNegative()){digit=base.minus(digit).abs();left=left.next()}out.push(digit.toJSNumber())}out.push(left.toJSNumber());return{value:out.reverse(),isNegative:neg}}function toBaseString(n,base,alphabet){var arr=toBase(n,base);return(arr.isNegative?"-":"")+arr.value.map(function(x){return stringify(x,alphabet)}).join("")}BigInteger.prototype.toArray=function(radix){return toBase(this,radix)};SmallInteger.prototype.toArray=function(radix){return toBase(this,radix)};NativeBigInt.prototype.toArray=function(radix){return toBase(this,radix)};BigInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!==10)return toBaseString(this,radix,alphabet);var v=this.value,l=v.length,str=String(v[--l]),zeros="0000000",digit;while(--l>=0){digit=String(v[l]);str+=zeros.slice(digit.length)+digit}var sign=this.sign?"-":"";return sign+str};SmallInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!=10)return toBaseString(this,radix,alphabet);return String(this.value)};NativeBigInt.prototype.toString=SmallInteger.prototype.toString;NativeBigInt.prototype.toJSON=BigInteger.prototype.toJSON=SmallInteger.prototype.toJSON=function(){return this.toString()};BigInteger.prototype.valueOf=function(){return parseInt(this.toString(),10)};BigInteger.prototype.toJSNumber=BigInteger.prototype.valueOf;SmallInteger.prototype.valueOf=function(){return this.value};SmallInteger.prototype.toJSNumber=SmallInteger.prototype.valueOf;NativeBigInt.prototype.valueOf=NativeBigInt.prototype.toJSNumber=function(){return parseInt(this.toString(),10)};function parseStringValue(v){if(isPrecise(+v)){var x=+v;if(x===truncate(x))return supportsNativeBigInt?new NativeBigInt(BigInt(x)):new SmallInteger(x);throw new Error("Invalid integer: "+v)}var sign=v[0]==="-";if(sign)v=v.slice(1);var split=v.split(/e/i);if(split.length>2)throw new Error("Invalid integer: "+split.join("e"));if(split.length===2){var exp=split[1];if(exp[0]==="+")exp=exp.slice(1);exp=+exp;if(exp!==truncate(exp)||!isPrecise(exp))throw new Error("Invalid integer: "+exp+" is not a valid exponent.");var text=split[0];var decimalPlace=text.indexOf(".");if(decimalPlace>=0){exp-=text.length-decimalPlace-1;text=text.slice(0,decimalPlace)+text.slice(decimalPlace+1)}if(exp<0)throw new Error("Cannot include negative exponent part for integers");text+=new Array(exp+1).join("0");v=text}var isValid=/^([0-9][0-9]*)$/.test(v);if(!isValid)throw new Error("Invalid integer: "+v);if(supportsNativeBigInt){return new NativeBigInt(BigInt(sign?"-"+v:v))}var r=[],max=v.length,l=LOG_BASE,min=max-l;while(max>0){r.push(+v.slice(min,max));min-=l;if(min<0)min=0;max-=l}trim(r);return new BigInteger(r,sign)}function parseNumberValue(v){if(supportsNativeBigInt){return new NativeBigInt(BigInt(v))}if(isPrecise(v)){if(v!==truncate(v))throw new Error(v+" is not an integer.");return new SmallInteger(v)}return parseStringValue(v.toString())}function parseValue(v){if(typeof v==="number"){return parseNumberValue(v)}if(typeof v==="string"){return parseStringValue(v)}if(typeof v==="bigint"){return new NativeBigInt(v)}return v}for(var i=0;i<1e3;i++){Integer[i]=parseValue(i);if(i>0)Integer[-i]=parseValue(-i)}Integer.one=Integer[1];Integer.zero=Integer[0];Integer.minusOne=Integer[-1];Integer.max=max;Integer.min=min;Integer.gcd=gcd;Integer.lcm=lcm;Integer.isInstance=function(x){return x instanceof BigInteger||x instanceof SmallInteger||x instanceof NativeBigInt};Integer.randBetween=randBetween;Integer.fromArray=function(digits,base,isNegative){return parseBaseFromArray(digits.map(parseValue),parseValue(base||10),isNegative)};return Integer}();if(typeof module!=="undefined"&&module.hasOwnProperty("exports")){module.exports=bigInt}if(typeof define==="function"&&define.amd){define(function(){return bigInt})}; | ||
#END | ||
var dexterCalculations = (function (undefined) { | ||
@@ -11,2 +5,11 @@ "use strict"; | ||
/** | ||
* Many functions use {(bigInt|number|string)} as parameter. These parameters | ||
* are converted into bigInt from the big-integer package and are expected to | ||
* to be non-negative numbers. string should be a string encoded integer. If you | ||
* are interfacing this project from another programming language, you should | ||
* pass the value for the parameter in {(bigInt|number|string)} as a string to | ||
* avoid number size restrictions in JavaScript. | ||
*/ | ||
/** | ||
* ============================================================================= | ||
@@ -21,3 +24,3 @@ * Internal utility functions | ||
* @param {bigInt} x | ||
* @returns {boolean} x > 0 | ||
* @returns {boolean} x > 0 | ||
*/ | ||
@@ -49,2 +52,12 @@ function gtZero(x) { | ||
/** | ||
* Test if a bigInt is less than or equal to zero. | ||
* | ||
* @param {bigInt} x | ||
* @returns {boolean} x <= 0 | ||
*/ | ||
function leqZero(x) { | ||
return x.compare(bigInt.zero) <= 0; | ||
}; | ||
/** | ||
* Ceiling division. If the remainder is greater than zero, increment by one. | ||
@@ -71,8 +84,8 @@ * | ||
/** | ||
* Calculate the amount of token sold for a given XTZ input and the pool state of Dexter | ||
* for the dexter xtzToToken entrypoint. | ||
* Calculate the amount of token sold for a given XTZ input and Dexter's two pool | ||
* values for the dexter xtzToToken entrypoint. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
@@ -93,3 +106,3 @@ */ | ||
if (gtZero(xtzIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var numerator = xtzIn_.times(tokenPool).times(bigInt(997)); | ||
var numerator = xtzIn_.times(tokenPool_).times(bigInt(997)); | ||
var denominator = xtzPool_.times(bigInt(1000)).add(xtzIn_.times(bigInt(997))); | ||
@@ -104,9 +117,9 @@ return numerator.divide(denominator); | ||
* Calculate the amount of XTZ you must pay in in order to receive a target | ||
* amount of token for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* amount of token for a given in the two Dexter pools. tokenOut is considered the | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of XTZ the user must send to xtzToToken to get the tokenOut amount. | ||
@@ -126,3 +139,4 @@ */ | ||
return null; | ||
}; | ||
}; | ||
if (gtZero(tokenOut_) && gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_)) { | ||
@@ -146,7 +160,7 @@ // (xtzPool_ * tokenOut_ * 1000 * 10 ** decimals) / (tokenPool - tokenOut * (997 * 10 ** decimals)) | ||
* Calculate the exchange rate for an XTZ to Token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* to the liquidity providers and the penalty for trade size. | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -175,5 +189,6 @@ */ | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -202,11 +217,25 @@ */ | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function xtzToTokenMarketRate(xtzPool, tokenPool) { | ||
if (xtzPool > 0 && tokenPool > 0) { | ||
return tokenPool / xtzPool; | ||
function xtzToTokenMarketRate(xtzPool, tokenPool, decimals) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
return (tokenPool__ / xtzPool__); | ||
} else { | ||
@@ -218,26 +247,42 @@ return null; | ||
/** | ||
* Calculate the xtzToToken market rate for a give Dexter contract. The market | ||
* rate is an idea number that doesn't include fees. In practice, this rate | ||
* cannot be executed. | ||
* Calculate the xtzToToken price impact for a given Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ the sender will sell to Dexter in xtzToToken. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function xtzToTokenSlippage(xtzIn, xtzPool, tokenPool) { | ||
if (xtzIn > 0 && xtzPool > 0 && tokenPool > 0) { | ||
var exchangeRate = xtzToTokenExchangeRate(xtzIn, xtzPool, tokenPool); | ||
var marketRate = xtzToTokenMarketRate(xtzPool, tokenPool); | ||
return Math.abs(exchangeRate - marketRate) / marketRate; | ||
function xtzToTokenPriceImpact(xtzIn, xtzPool, tokenPool) { | ||
var xtzIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
try { | ||
xtzIn_ = bigInt(xtzIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(xtzIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var midPrice = tokenPool_ / xtzPool_; | ||
var tokensBought = xtzIn_.times(tokenPool_).divide(xtzIn_.plus(xtzPool_)); | ||
// if no tokens have been purchased then there is no price impact | ||
if (leqZero(tokensBought)) { | ||
return 0; | ||
} | ||
var exactQuote = midPrice * xtzIn_; | ||
return (exactQuote - tokensBought) / exactQuote; | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
}; | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. | ||
* Calculate the minimum token out to be sent to Dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
@@ -262,3 +307,3 @@ * @returns {(bigInt|null)} The minimum token amount to send to the xtzToToken entrypoint. | ||
* | ||
* @param {number} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to the dexter liquidity providers. | ||
@@ -284,3 +329,3 @@ */ | ||
* | ||
* @param {number} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - The amount of XTZ sold to dexter. Must be greater than zero. | ||
* @returns {(number|null)} The fee paid to an individual dexter liquidity provider. | ||
@@ -314,7 +359,7 @@ */ | ||
* Get the amount of XTZ sold for a given token input and the pool state of Dexter | ||
* for the dexter tokenToXtz entrypoint. | ||
* for the Dexter tokenToXtz entrypoint. | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of XTZ that Dexter will send to the :to | ||
@@ -346,8 +391,8 @@ * address in the dexter tokenToXtz entrypoint. | ||
* amount of XTZ for a given Dexter pool state. xtzOut is considered the | ||
* maximum amount a user may receive. Trade slippage must still be considered. | ||
* maximum amount a user may receive. The user may receive less because of slippage. | ||
* | ||
* @param {number} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {number} decimals - The number of decimals a token has. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzOut - The amount of token that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token the user must send to tokenToXtz to get the xtzOut amount. | ||
@@ -391,5 +436,5 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -418,5 +463,5 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
@@ -443,13 +488,27 @@ */ | ||
/** | ||
* Calculate the tokenToXtz market rate for a give Dexter contract. The market | ||
* Calculate the tokenToXtz market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals a token has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function tokenToXtzMarketRate(xtzPool, tokenPool) { | ||
if (xtzPool > 0 && tokenPool > 0) { | ||
return xtzPool / tokenPool; | ||
function tokenToXtzMarketRate(xtzPool, tokenPool, decimals) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
return (xtzPool__ / tokenPool__); | ||
} else { | ||
@@ -461,25 +520,44 @@ return null; | ||
/** | ||
* Calculate the tokenToXtz price impact for a give Dexter contract. Price | ||
* impact is a measure of how much a trade will alter the future price. | ||
* | ||
* @param {number} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The slippage rate as a float value. | ||
* @param {(bigInt|number|string)} tokenIn - The amount of Token the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function tokenToXtzSlippage(tokenIn, xtzPool, tokenPool) { | ||
if (tokenIn > 0 && xtzPool > 0 && tokenPool > 0) { | ||
var exchangeRate = tokenToXtzExchangeRate(tokenIn, xtzPool, tokenPool); | ||
var marketRate = tokenToXtzMarketRate(xtzPool, tokenPool); | ||
return Math.abs(exchangeRate - marketRate) / marketRate; | ||
function tokenToXtzPriceImpact(tokenIn, xtzPool, tokenPool) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_)) { | ||
var midPrice = xtzPool_ / tokenPool_; | ||
var xtzBought = tokenIn_.times(xtzPool_).divide(tokenIn_.plus(tokenPool_)); | ||
// if no tokens have been purchased then there is no price impact | ||
if (leqZero(xtzBought)) { | ||
return 0; | ||
} | ||
var exactQuote = midPrice * tokenIn_; | ||
return (exactQuote - xtzBought) / exactQuote; | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
}; | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max xtzOut | ||
* and the max allowed slippage rate the user accepts. | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {number} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be greater than 0.00 and less than 1.00. | ||
* @param {(bigInt|number|string)} xtzOut - XTZ out as calculated by tokenToXtzTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToXtz entrypoint. | ||
@@ -501,2 +579,285 @@ */ | ||
* ============================================================================= | ||
* tokenToToken entrypoint functions | ||
* ============================================================================= | ||
*/ | ||
/** | ||
* The tokenToToken entyrpoint requires two dexter contracts. The documentation | ||
* refers to the input token as token X and the output token as token Y. | ||
*/ | ||
/** | ||
* Calculate the amount of token Y sold for token X input and the pool state of Dexter | ||
* for the dexter tokenToToken entrypoint. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of token Y that Dexter will send to the :to address in the dexter xtzToToken entrypoint. | ||
*/ | ||
function tokenToTokenTokenOutput(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var firstExchange = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); | ||
return xtzToTokenTokenOutput(firstExchange, xtzPool2_, tokenPool2_); | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
/** | ||
* Calculate the amount of token X you must pay in in order to receive a target | ||
* amount of token Y for a given Dexter pool state. tokenOut is considered the | ||
* maximum amount a user may receive. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - The amount of token Y that a user wants to receive. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of token X the user must send to tokenToToken to get the tokenOut amount. | ||
*/ | ||
function tokenToTokenTokenInput(tokenOut, xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var tokenOut_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
tokenOut_ = bigInt(tokenOut); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenOut_) && gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && geqZero(decimals2_)) { | ||
var xtzOut = xtzToTokenXtzInput(tokenOut_, xtzPool2_, tokenPool2_, decimals2_); | ||
if (xtzOut != null && gtZero(xtzOut)) { | ||
return tokenToXtzTokenInput(xtzOut, xtzPool_, tokenPool_, decimals_); | ||
}; | ||
return null; | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
/** | ||
* Calculate the exchange rate for a token to token trade including the 0.3% fee given | ||
* to the liquidity providers and the penalty for large trades. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
function tokenToTokenExchangeRate(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var firstExchangeRate = tokenToXtzExchangeRate(tokenIn_, xtzPool_, tokenPool_); // looks right | ||
if (firstExchangeRate === null) { | ||
return null; | ||
}; | ||
var xtzIn = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); // looks right | ||
if (xtzIn === null) { | ||
return null; | ||
}; | ||
var secondExchangeRate = xtzToTokenExchangeRate(xtzIn, xtzPool2_, tokenPool2_); | ||
if (secondExchangeRate === null) { | ||
return null; | ||
}; | ||
return firstExchangeRate * secondExchangeRate; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Same as tokenToTokenExchangeRate but adjusted for the decimal places. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - Token X amount the sender sells to Dexter. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - The number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - The number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The exchange rate as a float number. | ||
*/ | ||
function tokenToTokenExchangeRateForDisplay(tokenIn, xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && gtZero(decimals2_)) { | ||
var firstExchangeRate = tokenToXtzExchangeRateForDisplay(tokenIn_, xtzPool_, tokenPool_, decimals_); | ||
if (firstExchangeRate === null) { | ||
return null; | ||
}; | ||
var xtzIn = tokenToXtzXtzOutput(tokenIn_, xtzPool_, tokenPool_); | ||
if (xtzIn === null) { | ||
return null; | ||
}; | ||
var secondExchangeRate = xtzToTokenExchangeRateForDisplay(xtzIn, xtzPool2_, tokenPool2_, decimals2_); | ||
if (secondExchangeRate === null) { | ||
return null; | ||
}; | ||
return firstExchangeRate * secondExchangeRate; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Calculate the tokenToToken market rate for a given Dexter contract. The market | ||
* rate is an ideal number that doesn't include fees or penalties. In practice, | ||
* this rate cannot be executed. This is used for displaying an exchange rate | ||
* without the trade size penalty (before a user enters an amount for display). | ||
* | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals - Number of decimals token X has. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} decimals2 - Number of decimals token Y has. Must be greater than or equal to zero. | ||
* @returns {(number|null)} The market rate as a float value. | ||
*/ | ||
function tokenToTokenMarketRate(xtzPool, tokenPool, decimals, xtzPool2, tokenPool2, decimals2) { | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var decimals_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
var decimals2_ = bigInt.zero; | ||
try { | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
decimals_ = bigInt(decimals); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
decimals2_ = bigInt(decimals2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(xtzPool_) && gtZero(tokenPool_) && geqZero(decimals_) && gtZero(xtzPool2_) && gtZero(tokenPool2_) && geqZero(decimals2_)) { | ||
var xtzPool__ = xtzPool_ * Math.pow(10, -6); | ||
var tokenPool__ = tokenPool_ * Math.pow(10, -(decimals_)); | ||
var xtzPool2__ = xtzPool2_ * Math.pow(10, -6); | ||
var tokenPool2__ = tokenPool2_ * Math.pow(10, -(decimals2_)); | ||
return (xtzPool__ / tokenPool__) * (tokenPool2__ / xtzPool2__); | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
/** | ||
* Calculate the tokenToToken price impact for two Dexter contracts. Price | ||
* impact is measure of how much a trade will alter the future price. | ||
* | ||
* @param {(bigInt|number|string)} tokenIn - The amount of token X the sender will sell to Dexter in tokenToXtz. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds for token X. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool2 - XTZ amount that Dexter holds for token Y. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool2 - Token amount that Dexter holds for token Y. Must be greater than zero. | ||
* @returns {(number|null)} - The price impact percentage as a float value. | ||
*/ | ||
function tokenToTokenPriceImpact(tokenIn, xtzPool, tokenPool, xtzPool2, tokenPool2) { | ||
var tokenIn_ = bigInt.zero; | ||
var xtzPool_ = bigInt.zero; | ||
var tokenPool_ = bigInt.zero; | ||
var xtzPool2_ = bigInt.zero; | ||
var tokenPool2_ = bigInt.zero; | ||
try { | ||
tokenIn_ = bigInt(tokenIn); | ||
xtzPool_ = bigInt(xtzPool); | ||
tokenPool_ = bigInt(tokenPool); | ||
xtzPool2_ = bigInt(xtzPool2); | ||
tokenPool2_ = bigInt(tokenPool2); | ||
} catch(err) { | ||
return null; | ||
}; | ||
if (gtZero(tokenIn_) && gtZero(xtzPool_) && gtZero(tokenPool_) && gtZero(xtzPool2_) && gtZero(tokenPool2_)) { | ||
var xtzBought = tokenIn_.times(xtzPool_).divide(tokenIn_.plus(tokenPool_)); | ||
var one = tokenToXtzPriceImpact(tokenIn, xtzPool, tokenPool); | ||
var two = xtzToTokenPriceImpact(xtzBought, xtzPool2, tokenPool); | ||
return (one + two) / 2.0; | ||
} else { | ||
return null; | ||
}; | ||
}; | ||
/** | ||
* Calculate the minimum token out to be sent to dexter for a given max tokenOut | ||
* and the max allowed slippage rate the user accepts. If the exchange rate | ||
* has lowered less than the user's allowed slippage at the time of execution, | ||
* then the trade will fail. | ||
* | ||
* @param {(bigInt|number|string)} tokenOut - Token out as calculated by xtzToTokenTokenOut. Must be greater than zero. | ||
* @param {number} allowedSlippage - Maximum slippage rate that a user will except for an exchange. Must be between 0.00 and 1.00. | ||
* @returns {(bigInt|null)} The minimum token amount to send to the tokenToToken entrypoint. | ||
*/ | ||
function tokenToTokenMinimumTokenOutput(tokenOut, allowedSlippage) { | ||
return xtzToTokenMinimumTokenOutput(tokenOut, allowSlippage); | ||
}; | ||
/** | ||
* ============================================================================= | ||
* addLiquidity entrypoint functions | ||
@@ -511,5 +872,5 @@ * ============================================================================= | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} totalLiquidity - Total amount of liquidity in a Dexter pool. Must be greater than or equal to zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
@@ -548,5 +909,5 @@ */ | ||
* | ||
* @param {number} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {number} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzIn - XTZ amount the sender gives to Dexter for liquidity. Must be greater than zero. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. Must be greater than zero. | ||
* @param {(bigInt|number|string)} tokenPool - Token amount that Dexter holds. Must be greater than zero. | ||
* @returns {(bigInt|null)} The amount of liquidity that the sender gains. | ||
@@ -579,6 +940,6 @@ */ | ||
* | ||
* @param {number} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {number} xtzPool - XTZ amount that Dexter holds. | ||
* @param {number} tokenPool Token amount that Dexter holds. | ||
* @returns {{null|bigInt}} The amount of liquidity that the sender gains. | ||
* @param {(bigInt|number|string)} tokenIn - Token amount the sender gives to Dexter for liquidity. | ||
* @param {(bigInt|number|string)} xtzPool - XTZ amount that Dexter holds. | ||
* @param {(bigInt|number|string)} tokenPool Token amount that Dexter holds. | ||
* @returns {{bigInt|null}} The amount of liquidity that the sender gains. | ||
*/ | ||
@@ -616,6 +977,6 @@ function addLiquidityXtzIn(tokenIn, xtzPool, tokenPool) { | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} tokenPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of token that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} tokenPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of token that the sender gains. | ||
*/ | ||
@@ -646,6 +1007,6 @@ function removeLiquidityTokenOut(liquidityBurned, totalLiquidity, tokenPool) { | ||
* | ||
* @param {number} liquidityBurned LQT that the sender burns. | ||
* @param {number} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {number} xtzPool amount of token that Dexter holds. | ||
* @returns {(null|bigInt)} The amount of XTZ that the sender gains. | ||
* @param {(bigInt|number|string)} liquidityBurned LQT that the sender burns. | ||
* @param {(bigInt|number|string)} totalLiquidity The total amount of liquidity in a Dexter exchange. | ||
* @param {(bigInt|number|string)} xtzPool amount of token that Dexter holds. | ||
* @returns {(bigInt|null)} The amount of XTZ that the sender gains. | ||
*/ | ||
@@ -679,3 +1040,3 @@ function removeLiquidityXtzOut(liquidityBurned, totalLiquidity, xtzPool) { | ||
xtzToTokenMarketRate: xtzToTokenMarketRate, | ||
xtzToTokenSlippage: xtzToTokenSlippage, | ||
xtzToTokenPriceImpact: xtzToTokenPriceImpact, | ||
xtzToTokenMinimumTokenOutput: xtzToTokenMinimumTokenOutput, | ||
@@ -691,4 +1052,13 @@ totalLiquidityProviderFee: totalLiquidityProviderFee, | ||
tokenToXtzMarketRate: tokenToXtzMarketRate, | ||
tokenToXtzSlippage: tokenToXtzSlippage, | ||
tokenToXtzPriceImpact: tokenToXtzPriceImpact, | ||
tokenToXtzMinimumXtzOutput: tokenToXtzMinimumXtzOutput, | ||
// tokenToToken | ||
tokenToTokenTokenOutput: tokenToTokenTokenOutput, | ||
tokenToTokenTokenInput: tokenToTokenTokenInput, | ||
tokenToTokenExchangeRate: tokenToTokenExchangeRate, | ||
tokenToTokenExchangeRateForDisplay: tokenToTokenExchangeRateForDisplay, | ||
tokenToTokenMarketRate: tokenToTokenMarketRate, | ||
tokenToTokenPriceImpact: tokenToTokenPriceImpact, | ||
tokenToTokenMinimumTokenOutput: tokenToTokenMinimumTokenOutput, | ||
@@ -695,0 +1065,0 @@ // addLiquidity |
{ | ||
"name": "dexter-calculations", | ||
"version": "1.0.1", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -8,4 +8,18 @@ import DexterCalculations from '../index'; | ||
const json = JSON.parse(fs.readFileSync('test/xtz_to_token.json')); | ||
json.map(v => expect(DexterCalculations.xtzToTokenTokenOutput(v['xtz_in'], v['xtz_pool'], v['token_pool'])).toEqual(bigInt(v['token_out']))); | ||
return expect(DexterCalculations.xtzToTokenTokenOutput(1000000, 1000000000, 250000)).toEqual(bigInt(249)); | ||
json.map(v => { | ||
expect(DexterCalculations.xtzToTokenTokenOutput(v['xtz_in'], v['xtz_pool'], v['token_pool'])).toEqual(bigInt(v['token_out'])) | ||
expect(DexterCalculations.xtzToTokenPriceImpact(v['xtz_in'], v['xtz_pool'], v['token_pool'])).toEqual(parseFloat(v['price_impact'])); | ||
}); | ||
expect(DexterCalculations.xtzToTokenTokenOutput(1000000, 1000000000, 250000)).toEqual(bigInt(249)); | ||
// too small | ||
expect(DexterCalculations.xtzToTokenTokenOutput(5, 100000, 10)).toEqual(bigInt(0)); | ||
expect(DexterCalculations.xtzToTokenTokenOutput(5000, 100000, 10)).toEqual(bigInt(0)); | ||
expect(DexterCalculations.xtzToTokenTokenOutput(10000, 100000, 10)).toEqual(bigInt(0)); | ||
// now enough | ||
expect(DexterCalculations.xtzToTokenTokenOutput(20000, 100000, 10)).toEqual(bigInt(1)); | ||
}); | ||
@@ -50,31 +64,45 @@ | ||
it("xtzToTokenMarketRate", () => { | ||
const marketRate = DexterCalculations.xtzToTokenMarketRate(34204881343, 39306268); | ||
expect(marketRate).toBeGreaterThan(0.0011491420655971372 - 0.0005); | ||
expect(marketRate).toBeLessThan(0.0011491420655971372 + 0.0005); | ||
expect(DexterCalculations.xtzToTokenMarketRate('1000000','500000000000000000000', '18')).toBe(500.00000000000006); | ||
expect(DexterCalculations.xtzToTokenMarketRate('144621788919','961208019', '8')).toBe(0.000066463568607795); | ||
expect(DexterCalculations.xtzToTokenMarketRate('20167031717','41063990114535450000', '18')).toBe(0.0020361940562586686); | ||
expect(DexterCalculations.xtzToTokenMarketRate('46296642164','110543540642', '6')).toBe(2.3877226398064355); | ||
expect(DexterCalculations.xtzToTokenMarketRate('58392357794','73989702350', '6')).toBe(1.2671127720347453); | ||
}); | ||
it("xtzToTokenSlippage", () => { | ||
const json = JSON.parse(fs.readFileSync('test/xtz_to_token.json')); | ||
json.map(v => { | ||
const slippage = DexterCalculations.xtzToTokenSlippage(v['xtz_in'], v['xtz_pool'], v['token_pool']); | ||
expect(slippage).toBeGreaterThan(parseFloat(v['slippage']) - 0.0005); | ||
expect(slippage).toBeLessThan(parseFloat(v['slippage']) + 0.0005); | ||
}); | ||
it("xtzToTokenPriceImpact", () => { | ||
// good | ||
const priceImpact0 = DexterCalculations.xtzToTokenPriceImpact(1000000, 29757960047, 351953939); | ||
expect(priceImpact0).toBe(0.00010314839572795817); | ||
// based on tzBTC test data | ||
const slippage = DexterCalculations.xtzToTokenSlippage(1000000, 34204881343, 39306268); | ||
expect(slippage).toBeGreaterThan(0.0036 - 0.0005); | ||
expect(slippage).toBeLessThan(0.0036 + 0.0005); | ||
// too small | ||
const priceImpact1 = DexterCalculations.xtzToTokenPriceImpact(5, 100000, 10); | ||
expect(priceImpact1).toBe(0); | ||
const priceImpact2 = DexterCalculations.xtzToTokenPriceImpact(20000, 100000, 10); | ||
expect(priceImpact2).toBe(0.5); | ||
// based on tzBTC test data | ||
const slippage2 = DexterCalculations.xtzToTokenSlippage(200000000, 34204881343, 39306268); | ||
expect(slippage2).toBeGreaterThan(0.0088 - 0.0005); | ||
expect(slippage2).toBeLessThan(0.0088 + 0.0005); | ||
const priceImpact3 = DexterCalculations.xtzToTokenPriceImpact(90000, 100000, 10); | ||
expect(priceImpact3).toBe(0.5555555555555556); | ||
const priceImpact4 = DexterCalculations.xtzToTokenPriceImpact(200000, 100000, 10); | ||
expect(priceImpact4).toBe(0.7); | ||
// based on USDtz test data | ||
const slippage3 = DexterCalculations.xtzToTokenSlippage(10000000, 3003226688, 668057425); | ||
expect(slippage3).toBeGreaterThan(0.0063 - 0.0005); | ||
expect(slippage3).toBeLessThan(0.0063 + 0.0005); | ||
}); | ||
// // based on tzBTC test data | ||
const priceImpact5 = DexterCalculations.xtzToTokenPriceImpact(1000000, 34204881343, 39306268); | ||
expect(priceImpact5).toBe(0.00012362753169542967); | ||
// // based on tzBTC test data | ||
const priceImpact6 = DexterCalculations.xtzToTokenPriceImpact(200000000, 34204881343, 39306268); | ||
expect(priceImpact6).toBe(0.005814829860627381); | ||
// // based on USDtz test data | ||
const priceImpact7 = DexterCalculations.xtzToTokenPriceImpact(10000000, 3003226688, 668057425); | ||
expect(priceImpact7).toBe(0.003318788783598409); | ||
}); | ||
it("xtzToTokenMinimumTokenOutput", () => { | ||
@@ -108,3 +136,6 @@ expect(DexterCalculations.xtzToTokenMinimumTokenOutput(10000, 0.05)).toEqual(bigInt(9500)); | ||
const json = JSON.parse(fs.readFileSync('test/token_to_xtz.json')); | ||
json.map(v => expect(DexterCalculations.tokenToXtzXtzOutput(v['token_in'], v['xtz_pool'], v['token_pool'])).toEqual(bigInt(v['xtz_out']))); | ||
json.map(v => { | ||
expect(DexterCalculations.tokenToXtzXtzOutput(v['token_in'], v['xtz_pool'], v['token_pool'])).toEqual(bigInt(v['xtz_out'])); | ||
expect(DexterCalculations.tokenToXtzPriceImpact(v['token_in'], v['xtz_pool'], v['token_pool'])).toEqual(parseFloat(v['price_impact'])); | ||
}); | ||
return expect(DexterCalculations.tokenToXtzXtzOutput(1000, 20000000, 1000)).toEqual(bigInt(9984977)); | ||
@@ -146,36 +177,33 @@ }); | ||
it("tokenToXtzMarketRate", () => { | ||
const marketRate = DexterCalculations.tokenToXtzMarketRate(39306268, 34204881343); | ||
expect(marketRate).toBeGreaterThan(0.0011491420655971372 - 0.0005); | ||
expect(marketRate).toBeLessThan(0.0011491420655971372 + 0.0005); | ||
expect(DexterCalculations.tokenToXtzMarketRate('1000000','500000000000000000000', '18')).toBe(0.0019999999999999996); | ||
expect(DexterCalculations.tokenToXtzMarketRate('144621788919','961208019', '8')).toBe(15045.836703428498); | ||
expect(DexterCalculations.tokenToXtzMarketRate('20167031717','41063990114535450000', '18')).toBe(491.11232641421907); | ||
expect(DexterCalculations.tokenToXtzMarketRate('46296642164','110543540642', '6')).toBe(0.4188091126367452); | ||
expect(DexterCalculations.tokenToXtzMarketRate('58392357794','73989702350', '6')).toBe(0.7891957385878037); | ||
}); | ||
it("tokenToXtzSlippage", () => { | ||
const json = JSON.parse(fs.readFileSync('test/token_to_xtz.json')); | ||
json.map(v => { | ||
const slippage = DexterCalculations.tokenToXtzSlippage(v['token_in'], v['xtz_pool'], v['token_pool']); | ||
expect(slippage).toBeGreaterThan(parseFloat(v['slippage']) - 0.0005); | ||
expect(slippage).toBeLessThan(parseFloat(v['slippage']) + 0.0005); | ||
}); | ||
it("tokenToXtzPriceImpact", () => { | ||
// // based on tzBTC test data | ||
const slippage = DexterCalculations.tokenToXtzSlippage(100000000, 3849181242, 44365061); | ||
expect(slippage).toBeGreaterThan(0.693 - 0.0005); | ||
expect(slippage).toBeLessThan(0.693 + 0.0005); | ||
const priceImpact = DexterCalculations.tokenToXtzPriceImpact(100000000, 3849181242, 44365061); | ||
expect(priceImpact).toBe(0.6926883784623051); | ||
// // based on tzBTC test data | ||
const slippage2 = DexterCalculations.tokenToXtzSlippage(40000000, 3849181242, 44365061); | ||
expect(slippage2).toBeGreaterThan(0.475 - 0.0005); | ||
expect(slippage2).toBeLessThan(0.475 + 0.0005); | ||
const priceImpact2 = DexterCalculations.tokenToXtzPriceImpact(40000000, 3849181242, 44365061); | ||
expect(priceImpact2).toBe(0.47412992462990605); | ||
// // based on USDtz test data | ||
const slippage3 = DexterCalculations.tokenToXtzSlippage(1000000, 2869840667, 699209512); | ||
expect(slippage3).toBeGreaterThan(0.0044 - 0.0005); | ||
expect(slippage3).toBeLessThan(0.0044 + 0.0005); | ||
const priceImpact3 = DexterCalculations.tokenToXtzPriceImpact(1000000, 2869840667, 699209512); | ||
expect(priceImpact3).toBe(0.0014283084378495926); | ||
// // based on USDtz test data | ||
const slippage4 = DexterCalculations.tokenToXtzSlippage(8000000000, 9563874659, 19868860091); | ||
expect(slippage4).toBeGreaterThan(0.2886 - 0.0005); | ||
expect(slippage4).toBeLessThan(0.2886 + 0.0005); | ||
const priceImpact4 = DexterCalculations.tokenToXtzPriceImpact(8000000000, 9563874659, 19868860091); | ||
expect(priceImpact4).toBe(0.28705874495878053); | ||
}); | ||
it("tokenToXtzMinimumTokenOutput", () => { | ||
@@ -189,2 +217,60 @@ expect(DexterCalculations.tokenToXtzMinimumXtzOutput(10000, 0.05)).toEqual(bigInt(9500)); | ||
describe('dexter-calculations for tokenToToken', () => { | ||
it("tokenToTokenTokenOutput", () => { | ||
// trade tzBTC for USDtz | ||
expect(DexterCalculations.tokenToTokenTokenOutput(200000000, 28044673426, 371387874, 9564874659, 19866789049)).toEqual(bigInt(10037651239)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(100000000, 28044673426, 371387874, 9564874659, 19866789049)).toEqual(bigInt(7593267334)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(50000000, 28044673426, 371387874, 9564874659, 19866789049)).toEqual(bigInt(5106286311)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(40000000, 28044673426, 371387874, 9564874659, 19866789049)).toEqual(bigInt(4387740132)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(25000000, 28044673426, 371387874, 9564874659, 19866789049)).toEqual(bigInt(3085278056)); | ||
// trade USDtz for tzBTC | ||
expect(DexterCalculations.tokenToTokenTokenOutput(1000000, 9564874659, 19866789049, 28044673426, 371387874)).toEqual(bigInt(6337)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(10000000, 9564874659, 19866789049, 28044673426, 371387874)).toEqual(bigInt(63332)); | ||
expect(DexterCalculations.tokenToTokenTokenOutput(15000000000, 9564874659, 19866789049, 28044673426, 371387874)).toEqual(bigInt(47324814)); | ||
}); | ||
it("tokenToTokenTokenInput", () => { | ||
expect(DexterCalculations.tokenToTokenTokenInput(10037651239, 28044673426, 371387874, 8, 9564874659, 19866789049, 6)).toEqual(bigInt(199999999)); | ||
expect(DexterCalculations.tokenToTokenTokenInput(7593267334, 28044673426, 371387874, 8, 9564874659, 19866789049, 6)).toEqual(bigInt(99999999)); | ||
expect(DexterCalculations.tokenToTokenTokenInput('10000000000000000000', '29738193925', '352189932', '8', '5252041472', '9521487897832635693', '18')).toEqual(null); | ||
}); | ||
it("tokenToTokenExchangeRate", () => { | ||
expect(DexterCalculations.tokenToTokenExchangeRate(100000000, 8000000000000, 100000000000, 1000000000000, 2000000000000)).toEqual(157.63079069); | ||
}); | ||
it("tokenToTokenExchangeRateForDisplay", () => { | ||
expect(DexterCalculations.tokenToTokenExchangeRateForDisplay(100000000, 8000000000000, 100000000000, 8, 1000000000000, 2000000000000, 6)).toEqual(15763.079069); | ||
expect(DexterCalculations.tokenToTokenExchangeRateForDisplay(500000, 8000000000000, 100000000000, 8, 1000000000000, 2000000000000, 6)).toEqual(15903.4322); | ||
}); | ||
it("tokenToTokenMarketRate", () => { | ||
expect(DexterCalculations.tokenToTokenMarketRate(8000000000000, 100000000000, 8, 1000000000000, 2000000000000, 6)).toEqual(16000); | ||
expect(DexterCalculations.tokenToTokenMarketRate(236000000, 1000000, 6, 100000000, 4200000000, 8)).toEqual(99.11999999999999); | ||
}); | ||
it("tokenToTokenPriceImpact", () => { | ||
// sell one tzBTC for ETHtz | ||
const priceImpact = DexterCalculations.tokenToTokenPriceImpact(100000000, 200000000000, 1000000000, 11000000000,22000000000000000000); | ||
expect(priceImpact).toBe(0.3569810253690187); | ||
// sell 0.025 tzBTC for ETHtz | ||
const priceImpact2 = DexterCalculations.tokenToTokenPriceImpact(2500000, 200000000000, 1000000000, 11000000000,22000000000000000000); | ||
expect(priceImpact2).toBe(0.022934153979000203); | ||
// sell 4 tzBTC for ETHtz | ||
const priceImpact3 = DexterCalculations.tokenToTokenPriceImpact(400000000, 200000000000, 1000000000, 11000000000,22000000000000000000); | ||
expect(priceImpact3).toBe(0.5621443546475393); | ||
// sell 20 wXTZ for USDtz, where 1 XTZ = 1.3 wXTZ | ||
const priceImpact4 = DexterCalculations.tokenToTokenPriceImpact(20000000, 1000000000, 1300000000, 22000000000, 48000000000); | ||
expect(priceImpact4).toBe(0.007920219041906122); | ||
}); | ||
}); | ||
describe('dexter-calculations for addLiquidity', () => { | ||
@@ -191,0 +277,0 @@ it("addLiquidityTokenIn", () => { |
@@ -7,3 +7,3 @@ [ | ||
"xtz_out": "9984977", | ||
"slippage": "0.5007" | ||
"price_impact": "0.5" | ||
}, | ||
@@ -15,3 +15,3 @@ { | ||
"xtz_out": "1813221", | ||
"slippage": "0.0933" | ||
"price_impact": "0.0909095" | ||
}, | ||
@@ -23,3 +23,3 @@ { | ||
"xtz_out": "197431", | ||
"slippage": "0.0128" | ||
"price_impact": "0.009905" | ||
}, | ||
@@ -31,3 +31,3 @@ { | ||
"xtz_out": "19920", | ||
"slippage": "0.0040" | ||
"price_impact": "0.001" | ||
}, | ||
@@ -39,3 +39,3 @@ { | ||
"xtz_out": "3861597", | ||
"slippage": "0.1955" | ||
"price_impact": "0.19354854166666666" | ||
}, | ||
@@ -47,3 +47,3 @@ { | ||
"xtz_out": "2073262", | ||
"slippage": "0.1063" | ||
"price_impact": "0.1039426724137931" | ||
}, | ||
@@ -55,3 +55,3 @@ { | ||
"xtz_out": "9584586", | ||
"slippage": "0.4807" | ||
"price_impact": "0.4799792524377031" | ||
}, | ||
@@ -63,3 +63,3 @@ { | ||
"xtz_out": "1908461", | ||
"slippage": "0.1005" | ||
"price_impact": "0.09813581538461533" | ||
}, | ||
@@ -71,4 +71,32 @@ { | ||
"xtz_out": "1291776", | ||
"slippage": "0.0762" | ||
} | ||
"price_impact": "0.07363705211204516" | ||
}, | ||
{ | ||
"xtz_pool": "29764960047", | ||
"token_pool": "351871420", | ||
"token_in": "110000000", | ||
"xtz_out": "7072654862", | ||
"price_impact": "0.2381615212467131" | ||
}, | ||
{ | ||
"xtz_pool": "5210275350", | ||
"token_pool": "9597584059140809000", | ||
"token_in": "12000000000000000000", | ||
"xtz_out": "2891055412", | ||
"price_impact": "0.5556177009089963" | ||
}, | ||
{ | ||
"xtz_pool": "5836211738", | ||
"token_pool": "32669725403", | ||
"token_in": "112233000", | ||
"xtz_out": "19921238", | ||
"price_impact": "0.003423663423264276" | ||
}, | ||
{ | ||
"xtz_pool": "169488009", | ||
"token_pool": "59084008", | ||
"token_in": "16700000", | ||
"xtz_out": "37261491", | ||
"price_impact": "0.22036312371298977" | ||
} | ||
] |
@@ -7,3 +7,3 @@ [ | ||
"token_out": "249", | ||
"slippage": "0.0040" | ||
"price_impact": "0.0040" | ||
}, | ||
@@ -15,3 +15,3 @@ { | ||
"token_out": "497", | ||
"slippage": "0.0059" | ||
"price_impact": "0.002" | ||
}, | ||
@@ -23,3 +23,3 @@ { | ||
"token_out": "1240", | ||
"slippage": "0.0079" | ||
"price_impact": "0.0056" | ||
}, | ||
@@ -31,3 +31,3 @@ { | ||
"token_out": "2467", | ||
"slippage": "0.0132" | ||
"price_impact": "0.01" | ||
}, | ||
@@ -39,3 +39,3 @@ { | ||
"token_out": "22665", | ||
"slippage": "0.0934" | ||
"price_impact": "0.09092" | ||
}, | ||
@@ -47,3 +47,3 @@ { | ||
"token_out": "83", | ||
"slippage": "0.0172" | ||
"price_impact": "0.005440000000000086" | ||
}, | ||
@@ -55,3 +55,3 @@ { | ||
"token_out": "2614", | ||
"slippage": "0.0902" | ||
"price_impact": "0.08746384479717814" | ||
}, | ||
@@ -63,3 +63,3 @@ { | ||
"token_out": "15", | ||
"slippage": "0.0131" | ||
"price_impact": "0.013157894736842174" | ||
}, | ||
@@ -71,3 +71,3 @@ { | ||
"token_out": "34", | ||
"slippage": "0.0316" | ||
"price_impact": "0.0031897926634769274" | ||
}, | ||
@@ -79,3 +79,3 @@ { | ||
"token_out": "100", | ||
"slippage": "0.01806" | ||
"price_impact": "0.008248232521602547" | ||
}, | ||
@@ -87,3 +87,3 @@ { | ||
"token_out": "1", | ||
"slippage": "0.3734" | ||
"price_impact": "0.3734335839598998" | ||
}, | ||
@@ -95,3 +95,3 @@ { | ||
"token_out": "6", | ||
"slippage": "0.1075" | ||
"price_impact": "0.10753596630055816" | ||
}, | ||
@@ -103,3 +103,3 @@ { | ||
"token_out": "968", | ||
"slippage": "0.0128" | ||
"price_impact": "0.009772277227722817" | ||
}, | ||
@@ -111,3 +111,3 @@ { | ||
"token_out": "166", | ||
"slippage": "0.1700" | ||
"price_impact": "0.1700" | ||
}, | ||
@@ -119,3 +119,3 @@ { | ||
"token_out": "143", | ||
"slippage": "0.1769" | ||
"price_impact": "0.17697841726618704" | ||
}, | ||
@@ -127,3 +127,3 @@ { | ||
"token_out": "204", | ||
"slippage": "0.3011" | ||
"price_impact": "0.29767565492188197" | ||
}, | ||
@@ -135,4 +135,32 @@ { | ||
"token_out": "315", | ||
"slippage": "0.2597" | ||
"price_impact": "0.2573743922204214" | ||
}, | ||
{ | ||
"xtz_pool": "29764960047", | ||
"token_pool": "351871420", | ||
"xtz_in": "324234000", | ||
"token_out": "3780429", | ||
"price_impact": "0.010775957553247652" | ||
}, | ||
{ | ||
"xtz_pool": "5210275350", | ||
"token_pool": "9597584059140809000", | ||
"xtz_in": "123000000", | ||
"token_out": "220697918454258351", | ||
"price_impact": "0.023062750735343186" | ||
}, | ||
{ | ||
"xtz_pool": "5836211738", | ||
"token_pool": "32669725403", | ||
"xtz_in": "1000000000", | ||
"token_out": "4766677438", | ||
"price_impact": "0.14627984606587005" | ||
}, | ||
{ | ||
"xtz_pool": "169488009", | ||
"token_pool": "59084008", | ||
"xtz_in": "34000000", | ||
"token_out": "9847431", | ||
"price_impact": "0.16708603729465865" | ||
} | ||
] |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
177221
14
3120
0