@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
96 lines • 5.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSOLMint = exports.DECIMALS_SOL = exports.SOL_MINTS = void 0;
exports.getAssociatedTokenAddressAndAccount = getAssociatedTokenAddressAndAccount;
exports.getAssociatedTokenAddress = getAssociatedTokenAddress;
exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction;
exports.createComputeUnitLimitIx = createComputeUnitLimitIx;
exports.getStrategyPriceRangeOrca = getStrategyPriceRangeOrca;
exports.getStrategyPriceRangeRaydium = getStrategyPriceRangeRaydium;
exports.getStrategyPriceRangeMeteora = getStrategyPriceRangeMeteora;
exports.getMeteoraPriceLowerUpper = getMeteoraPriceLowerUpper;
exports.getPriceLowerUpper = getPriceLowerUpper;
exports.getTokenNameFromCollateralInfo = getTokenNameFromCollateralInfo;
exports.getMintDecimals = getMintDecimals;
const kit_1 = require("@solana/kit");
const whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk");
const meteora_1 = require("./meteora");
const token_1 = require("@solana-program/token");
const token_2022_1 = require("@solana-program/token-2022");
const system_1 = require("@solana-program/system");
const compute_budget_1 = require("@solana-program/compute-budget");
exports.SOL_MINTS = [
(0, kit_1.address)('So11111111111111111111111111111111111111111'),
(0, kit_1.address)('So11111111111111111111111111111111111111112'),
];
exports.DECIMALS_SOL = 9;
async function getAssociatedTokenAddressAndAccount(connection, mint, owner, programId = token_1.TOKEN_PROGRAM_ADDRESS) {
const ata = await getAssociatedTokenAddress(mint, owner, programId);
const account = await (0, token_2022_1.fetchMaybeToken)(connection, ata);
return [ata, account.exists ? account : null];
}
async function getAssociatedTokenAddress(mint, owner, programId = token_1.TOKEN_PROGRAM_ADDRESS, associatedTokenProgramId = token_2022_1.ASSOCIATED_TOKEN_PROGRAM_ADDRESS) {
const [ata] = await (0, token_2022_1.findAssociatedTokenPda)({
mint,
owner,
tokenProgram: programId,
}, { programAddress: associatedTokenProgramId });
return ata;
}
function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = token_1.TOKEN_PROGRAM_ADDRESS, associatedTokenProgramId = token_2022_1.ASSOCIATED_TOKEN_PROGRAM_ADDRESS) {
return (0, token_2022_1.getCreateAssociatedTokenInstruction)({
mint,
owner,
ata: associatedToken,
payer: payer,
tokenProgram: programId,
systemProgram: system_1.SYSTEM_PROGRAM_ADDRESS,
}, { programAddress: associatedTokenProgramId });
}
function createComputeUnitLimitIx(units = 400000) {
return (0, compute_budget_1.getSetComputeUnitLimitInstruction)({ units });
}
function getStrategyPriceRangeOrca(tickLowerIndex, tickUpperIndex, strategy, poolPrice) {
const { priceLower, priceUpper } = getPriceLowerUpper(tickLowerIndex, tickUpperIndex, Number(strategy.tokenAMintDecimals.toString()), Number(strategy.tokenBMintDecimals.toString()));
const strategyOutOfRange = poolPrice.lt(priceLower) || poolPrice.gt(priceUpper);
return { priceLower, poolPrice, priceUpper, strategyOutOfRange };
}
function getStrategyPriceRangeRaydium(tickLowerIndex, tickUpperIndex, tickCurrent, tokenADecimals, tokenBDecimals) {
const { priceLower, priceUpper } = getPriceLowerUpper(tickLowerIndex, tickUpperIndex, tokenADecimals, tokenBDecimals);
const poolPrice = (0, whirlpool_sdk_1.tickIndexToPrice)(tickCurrent, tokenADecimals, tokenBDecimals);
const strategyOutOfRange = poolPrice.lt(priceLower) || poolPrice.gt(priceUpper);
return { priceLower, poolPrice, priceUpper, strategyOutOfRange };
}
function getStrategyPriceRangeMeteora(priceLower, priceUpper, activeBinId, binStep, decimalsA, decimalsB) {
const poolPrice = (0, meteora_1.getPriceOfBinByBinIdWithDecimals)(activeBinId, binStep, decimalsA, decimalsB);
const strategyOutOfRange = poolPrice.lt(priceLower) || poolPrice.gt(priceUpper);
return { priceLower, poolPrice, priceUpper, strategyOutOfRange };
}
function getMeteoraPriceLowerUpper(tickLowerIndex, tickUpperIndex, tokenAMintDecimals, tokenBMintDecimals, binStep) {
const priceLower = (0, meteora_1.getPriceOfBinByBinIdWithDecimals)(tickLowerIndex, binStep, tokenAMintDecimals, tokenBMintDecimals);
const priceUpper = (0, meteora_1.getPriceOfBinByBinIdWithDecimals)(tickUpperIndex, binStep, tokenAMintDecimals, tokenBMintDecimals);
return { priceLower, priceUpper };
}
function getPriceLowerUpper(tickLowerIndex, tickUpperIndex, tokenAMintDecimals, tokenBMintDecimals) {
const priceLower = (0, whirlpool_sdk_1.tickIndexToPrice)(tickLowerIndex, Number(tokenAMintDecimals.toString()), Number(tokenBMintDecimals.toString()));
const priceUpper = (0, whirlpool_sdk_1.tickIndexToPrice)(tickUpperIndex, Number(tokenAMintDecimals.toString()), Number(tokenBMintDecimals.toString()));
return { priceLower, priceUpper };
}
function getTokenNameFromCollateralInfo(collateralInfo) {
return String.fromCharCode(...collateralInfo.name.filter((x) => x > 0));
}
const isSOLMint = (mint) => {
return exports.SOL_MINTS.filter((m) => m === mint).length > 0;
};
exports.isSOLMint = isSOLMint;
async function getMintDecimals(rpc, mint) {
if ((0, exports.isSOLMint)(mint)) {
return exports.DECIMALS_SOL;
}
const acc = await (0, token_2022_1.fetchMaybeMint)(rpc, mint);
if (!acc.exists) {
throw new Error(`Mint ${mint} not found`);
}
return acc.data.decimals;
}
//# sourceMappingURL=tokenUtils.js.map
;