@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
128 lines (127 loc) • 5.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.zeroBalance = exports.transferOrDestroyZeroCoin = exports.coinFromBalance = exports.coinIntoBalance = exports.getPools = exports.getPercentageAmount = exports.parsePool = exports.getEstimatedAmountIncludingSlippage = exports.sqrtPriceX64ToPrice = exports.priceToSqrtPriceX64 = exports.priceToTick = exports.getLiquidityParams = exports.toUnsignedTick = exports.asUintN = void 0;
const library_1 = require("../library");
const types_1 = require("../types");
const clmm_1 = require("./clmm");
/**
* Converts a bigint to an unsigned integer of the specified number of bits.
* @param {bigint} int - The bigint to convert.
* @param {number} bits - The number of bits to use in the conversion. Defaults to 32 bits.
* @returns {string} - Returns the converted unsigned integer as a string.
*/
function asUintN(int, bits = 32) {
return BigInt.asUintN(bits, BigInt(int)).toString();
}
exports.asUintN = asUintN;
/**
* Converts given value to unsigned number
* @param tickValue The value to be converted
* @returns unsigned number
*/
function toUnsignedTick(tickValue) {
return Number(asUintN(BigInt(tickValue)).toString());
}
exports.toUnsignedTick = toUnsignedTick;
/**
* Returns the the bits of lower, upper tick and liquidity
* to be passed as input to provide liquidity/mint contract call
*/
function getLiquidityParams(pool, lowerPrice, upperPrice, coinAmounts, slippage // should be in range 0 to 1
) {
const currentSqrtPriceX64 = new types_1.BN(pool.current_sqrt_price);
const lowerPriceX64 = priceToSqrtPriceX64(pool, lowerPrice);
const upperPriceX64 = priceToSqrtPriceX64(pool, upperPrice);
const lowerTick = clmm_1.TickMath.sqrtPriceX64ToTickIndex(lowerPriceX64);
const upperTick = clmm_1.TickMath.sqrtPriceX64ToTickIndex(upperPriceX64);
const liquidity = clmm_1.ClmmPoolUtil.estimateLiquidityFromCoinAmounts(currentSqrtPriceX64, lowerTick, upperTick, coinAmounts).toNumber();
const minCoinAmounts = {
coinA: new types_1.BN(getPercentageAmount(coinAmounts.coinA.toString(), slippage, false).toFixed()),
coinB: new types_1.BN(getPercentageAmount(coinAmounts.coinB.toString(), slippage, false).toFixed())
};
return {
lowerTick,
upperTick,
lowerPriceX64,
upperPriceX64,
lowerPrice,
upperPrice,
liquidity,
coinAmounts,
minCoinAmounts
};
}
exports.getLiquidityParams = getLiquidityParams;
function priceToTick(pool, price) {
const priceSqrtX64 = priceToSqrtPriceX64(pool, price);
return clmm_1.TickMath.sqrtPriceX64ToTickIndex(new types_1.BN(priceSqrtX64));
}
exports.priceToTick = priceToTick;
function priceToSqrtPriceX64(pool, price) {
return clmm_1.TickMath.priceToSqrtPriceX64((0, clmm_1.d)(price), pool.coin_a.decimals, pool.coin_b.decimals);
}
exports.priceToSqrtPriceX64 = priceToSqrtPriceX64;
function sqrtPriceX64ToPrice(pool, sqrtPriceX64) {
return new types_1.BigNumber(clmm_1.TickMath.sqrtPriceX64ToPrice(new types_1.BN(sqrtPriceX64), pool.coin_a.decimals, pool.coin_b.decimals).toString());
}
exports.sqrtPriceX64ToPrice = sqrtPriceX64ToPrice;
function getEstimatedAmountIncludingSlippage(amount, slippage, byAmountIn) {
return byAmountIn
? amount.minus(amount.multipliedBy(slippage.dividedBy(100)))
: amount.plus(amount.multipliedBy(slippage.dividedBy(100)));
}
exports.getEstimatedAmountIncludingSlippage = getEstimatedAmountIncludingSlippage;
function parsePool(pool) {
return {
id: pool.id,
coinA: pool.coin_a.address,
coinB: pool.coin_b.address,
coinADecimals: pool.coin_a.decimals,
coinBDecimals: pool.coin_b.decimals,
name: pool.name
};
}
exports.parsePool = parsePool;
function getPercentageAmount(number, percentage, upside) {
return (0, library_1.bigNumber)(number).times((0, library_1.bigNumber)(upside ? 1 + percentage : 1 - percentage));
}
exports.getPercentageAmount = getPercentageAmount;
function getPools(pools, name) {
return pools.filter(pool => pool.name == name);
}
exports.getPools = getPools;
function coinIntoBalance(tx, coinType, coinObject) {
return tx.moveCall({
target: `0x2::coin::into_balance`,
typeArguments: [coinType],
arguments: [coinObject]
})[0];
}
exports.coinIntoBalance = coinIntoBalance;
function coinFromBalance(tx, coinType, balance) {
return tx.moveCall({
target: `0x2::coin::from_balance`,
typeArguments: [coinType],
arguments: [balance]
})[0];
}
exports.coinFromBalance = coinFromBalance;
function transferOrDestroyZeroCoin(tx, coinType, coin, to, pkg) {
// if the pkg is not provided use the mainnet spot package to make the call
// TODO: we should always provide package as if this function is being run on testnet and
// the pkg is not provided, we will be having issues
tx.moveCall({
target: `${pkg || "0x406f52151e7dd65addd93b0bdad7989e82aec20c3ae6971954a5140f14a59e4b"}::utils::transfer_coin`,
typeArguments: [coinType],
arguments: [coin, tx.pure.address(to)]
});
}
exports.transferOrDestroyZeroCoin = transferOrDestroyZeroCoin;
function zeroBalance(tx, coinType) {
return tx.moveCall({
target: `0x2::balance::zero`,
typeArguments: [coinType],
arguments: []
})[0];
}
exports.zeroBalance = zeroBalance;
;