UNPKG

kamino-sdk-beta

Version:

Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol

145 lines 7.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSOLMint = exports.DECIMALS_SOL = exports.SOL_MINTS = exports.TOKEN_PROGRAM_ID = exports.ASSOCIATED_TOKEN_PROGRAM_ID = void 0; exports.getAssociatedTokenAddressAndData = getAssociatedTokenAddressAndData; exports.getAssociatedTokenAddress = getAssociatedTokenAddress; exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction; exports.createAddExtraComputeUnitsIx = createAddExtraComputeUnitsIx; exports.createTransactionWithExtraBudget = createTransactionWithExtraBudget; exports.assignBlockInfoToTransaction = assignBlockInfoToTransaction; exports.sendTransactionWithLogs = sendTransactionWithLogs; exports.getStrategyPriceRangeOrca = getStrategyPriceRangeOrca; exports.getStrategyPriceRangeRaydium = getStrategyPriceRangeRaydium; exports.getStrategyPriceRangeMeteora = getStrategyPriceRangeMeteora; exports.getMeteoraPriceLowerUpper = getMeteoraPriceLowerUpper; exports.getPriceLowerUpper = getPriceLowerUpper; exports.getTokenNameFromCollateralInfo = getTokenNameFromCollateralInfo; exports.getMintDecimals = getMintDecimals; exports.removeBudgetAndAtaIxns = removeBudgetAndAtaIxns; const web3_js_1 = require("@solana/web3.js"); const utils_1 = require("./utils"); const whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk"); const meteora_1 = require("./meteora"); const spl_token_1 = require("@solana/spl-token"); exports.ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'); exports.TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'); exports.SOL_MINTS = [ new web3_js_1.PublicKey('So11111111111111111111111111111111111111111'), new web3_js_1.PublicKey('So11111111111111111111111111111111111111112'), ]; exports.DECIMALS_SOL = 9; async function getAssociatedTokenAddressAndData(connection, mint, owner, programId = exports.TOKEN_PROGRAM_ID) { const ata = await getAssociatedTokenAddress(mint, owner, true, programId); const data = await connection.getAccountInfo(ata); return [ata, data]; } function getAssociatedTokenAddress(mint, owner, allowOwnerOffCurve = true, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) { return (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, allowOwnerOffCurve, programId, associatedTokenProgramId); } function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) { const keys = [ { pubkey: payer, isSigner: true, isWritable: true }, { pubkey: associatedToken, isSigner: false, isWritable: true }, { pubkey: owner, isSigner: false, isWritable: false }, { pubkey: mint, isSigner: false, isWritable: false }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false }, { pubkey: programId, isSigner: false, isWritable: false }, ]; return new web3_js_1.TransactionInstruction({ keys, programId: associatedTokenProgramId, data: Buffer.alloc(0), }); } function createAddExtraComputeUnitsIx(units) { return web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units }); } function createTransactionWithExtraBudget(extraUnits = 400000) { return new web3_js_1.Transaction().add(createAddExtraComputeUnitsIx(extraUnits)); } async function assignBlockInfoToTransaction(connection, transaction, payer) { const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(); transaction.recentBlockhash = blockhash; transaction.feePayer = payer; transaction.lastValidBlockHeight = lastValidBlockHeight; return transaction; } async function sendTransactionWithLogs(connection, tx, payer, signers, commitment = 'processed', skipPreflight = false) { const txn = await assignBlockInfoToTransaction(connection, tx, payer); try { const res = await (0, web3_js_1.sendAndConfirmTransaction)(connection, txn, signers, { commitment: commitment, skipPreflight: skipPreflight, }); return res; } catch (e) { console.log('ERROR:', e); await (0, utils_1.sleep)(5000); // @ts-ignore const sig = e.toString().split(' failed ')[0].split('Transaction ')[1]; const res = await connection.getTransaction(sig, { commitment: 'confirmed' }); if (res && res.meta) { console.log('Txn', res.meta.logMessages); } return null; } } 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.equals(mint)).length > 0; }; exports.isSOLMint = isSOLMint; async function getMintDecimals(connection, mint) { if ((0, exports.isSOLMint)(mint)) { return exports.DECIMALS_SOL; } const acc = await connection.getAccountInfo(mint); if (!acc) { throw new Error(`Mint ${mint.toBase58()} not found`); } const mintInfo = (0, spl_token_1.unpackMint)(mint, acc, acc.owner); return mintInfo.decimals; } function removeBudgetAndAtaIxns(ixns, mints) { return ixns.filter((ixn) => { const { programId, keys } = ixn; if (programId.equals(web3_js_1.ComputeBudgetProgram.programId)) { return false; } if (programId.equals(exports.ASSOCIATED_TOKEN_PROGRAM_ID)) { const mint = keys[3]; return !mints.includes(mint.pubkey); } return true; }); } //# sourceMappingURL=tokenUtils.js.map