UNPKG

@marinade.finance/kamino-sdk

Version:
134 lines 6.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findAtaBalance = exports.removeBudgetAndAtaIxns = exports.checkIfAccountExists = exports.isWsolInfoInvalid = exports.createWsolAtaIfMissing = exports.getAtasWithCreateIxnsIfMissing = exports.createAtaIfMissingIx = exports.getComputeBudgetAndPriorityFeeIxns = exports.decodeSerializedTransaction = exports.MAX_ACCOUNTS_PER_TRANSACTION = void 0; const web3_js_1 = require("@solana/web3.js"); const decimal_js_1 = __importDefault(require("decimal.js")); const tokenUtils_1 = require("./tokenUtils"); const spl_token_1 = require("@solana/spl-token"); const utils_1 = require("./utils"); exports.MAX_ACCOUNTS_PER_TRANSACTION = 64; const decodeSerializedTransaction = (tx) => { if (!tx) { return undefined; } return web3_js_1.Transaction.from(Buffer.from(tx, 'base64')); }; exports.decodeSerializedTransaction = decodeSerializedTransaction; const getComputeBudgetAndPriorityFeeIxns = (units, priorityFeeLamports) => { const ixns = []; ixns.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units })); if (priorityFeeLamports && priorityFeeLamports.gt(0)) { const unitPrice = priorityFeeLamports.mul(10 ** 6).div(units); ixns.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: BigInt(unitPrice.floor().toString()) })); } return ixns; }; exports.getComputeBudgetAndPriorityFeeIxns = getComputeBudgetAndPriorityFeeIxns; const createAtaIfMissingIx = async (connection, mint, owner) => { const ata = await (0, tokenUtils_1.getAssociatedTokenAddress)(mint, owner); const doesAtaExist = Boolean(await checkIfAccountExists(connection, ata)); const createIxn = !doesAtaExist ? (0, tokenUtils_1.createAssociatedTokenAccountInstruction)(owner, ata, owner, mint) : undefined; return createIxn; }; exports.createAtaIfMissingIx = createAtaIfMissingIx; const getAtasWithCreateIxnsIfMissing = async (connection, mints, owner) => { const requests = mints.map(async (mint) => { let createAtaIx = await (0, exports.createAtaIfMissingIx)(connection, mint, owner); if (createAtaIx) { return createAtaIx; } }); const result = (await Promise.all(requests.filter((x) => x !== undefined))).filter((ix) => ix !== undefined); return result; }; exports.getAtasWithCreateIxnsIfMissing = getAtasWithCreateIxnsIfMissing; const createWsolAtaIfMissing = async (connection, amount, owner, method = 'deposit') => { const createIxns = []; const closeIxns = []; const wsolAta = await spl_token_1.Token.getAssociatedTokenAddress(tokenUtils_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, spl_token_1.NATIVE_MINT, owner); const solDeposit = amount.toNumber(); const wsolAtaAccountInfo = await connection.getAccountInfo(wsolAta); // This checks if we need to create it if ((0, exports.isWsolInfoInvalid)(wsolAtaAccountInfo)) { createIxns.push(spl_token_1.Token.createAssociatedTokenAccountInstruction(tokenUtils_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, spl_token_1.NATIVE_MINT, wsolAta, owner, owner)); } let uiAmount = 0; try { if (wsolAtaAccountInfo != null) { const tokenBalance = await (0, exports.findAtaBalance)(connection, wsolAta); uiAmount = tokenBalance === null ? 0 : tokenBalance; } } catch (err) { console.log('Err Token Balance', err); } if (solDeposit !== null && solDeposit > uiAmount && method === 'deposit') { createIxns.push(web3_js_1.SystemProgram.transfer({ fromPubkey: owner, toPubkey: wsolAta, lamports: BigInt((0, utils_1.collToLamportsDecimal)(new decimal_js_1.default(solDeposit - uiAmount), tokenUtils_1.DECIMALS_SOL) .floor() .toString()), })); } if (createIxns.length > 0) { createIxns.push( // Sync Native instruction. @solana/spl-token will release it soon. Here use the raw instruction temporally. new web3_js_1.TransactionInstruction({ keys: [ { pubkey: wsolAta, isSigner: false, isWritable: true, }, ], data: Buffer.from(new Uint8Array([17])), programId: spl_token_1.TOKEN_PROGRAM_ID, })); } closeIxns.push(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, wsolAta, new web3_js_1.PublicKey(owner), new web3_js_1.PublicKey(owner), [])); return { ata: wsolAta, createIxns, closeIxns, }; }; exports.createWsolAtaIfMissing = createWsolAtaIfMissing; const isWsolInfoInvalid = (wsolAtaAccountInfo) => { const res = wsolAtaAccountInfo === null || (wsolAtaAccountInfo !== null && wsolAtaAccountInfo.data.length === 0 && wsolAtaAccountInfo.owner.toString() === '11111111111111111111111111111111'); return res; }; exports.isWsolInfoInvalid = isWsolInfoInvalid; async function checkIfAccountExists(connection, account) { return (await connection.getAccountInfo(account)) != null; } exports.checkIfAccountExists = checkIfAccountExists; function removeBudgetAndAtaIxns(ixns, mints) { return ixns.filter((ixn) => { const { programId, keys } = ixn; if (programId.toString() === web3_js_1.ComputeBudgetProgram.programId.toString()) { return false; } if (programId.toString() === tokenUtils_1.ASSOCIATED_TOKEN_PROGRAM_ID.toString()) { const mint = keys[3]; return !mints.includes(mint.pubkey.toString()); } return true; }); } exports.removeBudgetAndAtaIxns = removeBudgetAndAtaIxns; const findAtaBalance = async (connection, ata) => { const res = await connection.getTokenAccountBalance(ata); if (res && res.value) { return res.value.uiAmount; } return null; }; exports.findAtaBalance = findAtaBalance; //# sourceMappingURL=transactions.js.map