@dahlia-labs/stableswap-sdk
Version:
SDK for Mobius's StableSwap program.
33 lines • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateSwapPrice = void 0;
const tslib_1 = require("tslib");
const token_utils_1 = require("@dahlia-labs/token-utils");
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
const _1 = require("./");
function min(a, b) {
return jsbi_1.default.greaterThanOrEqual(a, b) ? b : a;
}
function max(a, b) {
return jsbi_1.default.greaterThanOrEqual(a, b) ? a : b;
}
/**
* Gets the price of the second token in the swap, i.e. "Token 1", with respect to "Token 0".
*
* To get the price of "Token 0", use `.invert()` on the result of this function.
* @returns
*/
const calculateSwapPrice = (exchangeInfo) => {
const reserve0 = exchangeInfo.reserves[0];
const reserve1 = exchangeInfo.reserves[1];
// We try to get at least 16 decimal points of precision here
// Otherwise, we attempt to swap 1% of total supply of the pool
// or at most, $1
const inputAmountNum = max(jsbi_1.default.divide(jsbi_1.default.BigInt(10000000000000000), jsbi_1.default.exponentiate(token_utils_1.TEN, jsbi_1.default.BigInt(18 - reserve0.token.decimals))), min(jsbi_1.default.exponentiate(token_utils_1.TEN, jsbi_1.default.BigInt(reserve0.token.decimals)), reserve0.divide(100).quotient));
const inputAmount = new token_utils_1.TokenAmount(reserve0.token, inputAmountNum);
const outputAmount = (0, _1.calculateEstimatedSwapOutputAmount)(exchangeInfo, inputAmount);
const frac = outputAmount.outputAmountBeforeFees.divide(inputAmount.asFraction);
return new token_utils_1.Price(reserve0.token, reserve1.token, frac.denominator, frac.numerator);
};
exports.calculateSwapPrice = calculateSwapPrice;
//# sourceMappingURL=price.js.map