UNPKG

@kamino-finance/klend-sdk

Version:

Typescript SDK for interacting with the Kamino Lending (klend) protocol

116 lines 5.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAtasWithCreateIxnsIfMissing = void 0; exports.createAssociatedTokenAccountIdempotentInstruction = createAssociatedTokenAccountIdempotentInstruction; exports.getAssociatedTokenAddress = getAssociatedTokenAddress; exports.createAtasIdempotent = createAtasIdempotent; exports.getDepositWsolIxns = getDepositWsolIxns; exports.removeBudgetAndAtaIxns = removeBudgetAndAtaIxns; exports.getTokenAccountBalance = getTokenAccountBalance; exports.getTokenAccountBalanceDecimal = getTokenAccountBalanceDecimal; const spl_token_1 = require("@solana/spl-token"); const web3_js_1 = require("@solana/web3.js"); const decimal_js_1 = __importDefault(require("decimal.js")); /** * Create an idempotent create ATA instruction * Overrides the create ATA ix to use the idempotent version as the spl-token library does not provide this ix yet * @param owner - owner of the ATA * @param mint - mint of the ATA * @param payer - payer of the transaction * @param tokenProgram - optional token program address - spl-token if not provided * @param ata - optional ata address - derived if not provided * @returns The ATA address public key and the transaction instruction */ function createAssociatedTokenAccountIdempotentInstruction(owner, mint, payer = owner, tokenProgram = spl_token_1.TOKEN_PROGRAM_ID, ata) { let ataAddress = ata; if (!ataAddress) { ataAddress = getAssociatedTokenAddress(mint, owner, true, tokenProgram, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID); } const createUserTokenAccountIx = (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payer, ataAddress, owner, mint, tokenProgram, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID); return [ataAddress, createUserTokenAccountIx]; } function getAssociatedTokenAddress(mint, owner, allowOwnerOffCurve = true, programId = spl_token_1.TOKEN_PROGRAM_ID, associatedTokenProgramId = spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID) { if (!allowOwnerOffCurve && !web3_js_1.PublicKey.isOnCurve(owner.toBuffer())) throw new Error('Token owner off curve'); const [address] = web3_js_1.PublicKey.findProgramAddressSync([owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId); return address; } const getAtasWithCreateIxnsIfMissing = async (connection, user, mints) => { const atas = mints.map((x) => getAssociatedTokenAddress(x.mint, user, true, x.tokenProgram)); const accountInfos = await connection.getMultipleAccountsInfo(atas); const createAtaIxs = []; for (let i = 0; i < atas.length; i++) { if (!accountInfos[i]) { const { mint, tokenProgram } = mints[i]; const [ata, createIxn] = createAssociatedTokenAccountIdempotentInstruction(user, mint, user, tokenProgram); atas[i] = ata; createAtaIxs.push(createIxn); } } return { atas, createAtaIxs, }; }; exports.getAtasWithCreateIxnsIfMissing = getAtasWithCreateIxnsIfMissing; function createAtasIdempotent(user, mints) { const res = []; for (const mint of mints) { const [ata, createAtaIx] = createAssociatedTokenAccountIdempotentInstruction(user, mint.mint, user, mint.tokenProgram); res.push({ ata, createAtaIx, }); } return res; } function getDepositWsolIxns(owner, ata, amountLamports) { const ixns = []; ixns.push(web3_js_1.SystemProgram.transfer({ fromPubkey: owner, toPubkey: ata, lamports: amountLamports.toNumber(), })); ixns.push(new web3_js_1.TransactionInstruction({ keys: [ { pubkey: ata, isSigner: false, isWritable: true, }, ], data: Buffer.from(new Uint8Array([17])), programId: spl_token_1.TOKEN_PROGRAM_ID, })); return ixns; } 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(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID)) { const mint = keys[3]; return !mints.includes(mint.pubkey.toString()); } return true; }); } async function getTokenAccountBalance(provider, tokenAccount) { const tokenAccountBalance = await provider.connection.getTokenAccountBalance(tokenAccount); return Number(tokenAccountBalance.value.amount).valueOf(); } async function getTokenAccountBalanceDecimal(connection, mint, owner, tokenProgram = spl_token_1.TOKEN_PROGRAM_ID) { const ata = getAssociatedTokenAddress(mint, owner, true, tokenProgram); const accInfo = await connection.getAccountInfo(ata); if (accInfo === null) { return new decimal_js_1.default('0'); } const { value } = await connection.getTokenAccountBalance(ata); return new decimal_js_1.default(value.uiAmountString); } //# sourceMappingURL=ata.js.map