UNPKG

@orca-so/whirlpool-sdk

Version:

Whirlpool SDK for the Orca protocol.

58 lines (57 loc) 3.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deriveATA = exports.resolveOrCreateATA = void 0; const spl_token_1 = require("@solana/spl-token"); const deserialize_token_account_1 = require("./deserialize-token-account"); const helpers_1 = require("./helpers"); const token_instructions_1 = require("./token-instructions"); /** * IMPORTANT: wrappedSolAmountIn should only be used for input/source token that * could be SOL. This is because when SOL is the output, it is the end * destination, and thus does not need to be wrapped with an amount. * * @param connection Solana connection class * @param ownerAddress The user's public key * @param tokenMint Token mint address * @param wrappedSolAmountIn Optional. Only use for input/source token that could be SOL * @returns */ function resolveOrCreateATA(connection, ownerAddress, tokenMint, wrappedSolAmountIn = new spl_token_1.u64(0)) { return __awaiter(this, void 0, void 0, function* () { if (!tokenMint.equals(spl_token_1.NATIVE_MINT)) { const ataAddress = yield deriveATA(ownerAddress, tokenMint); const tokenAccountInfo = yield connection.getAccountInfo(ataAddress); const tokenAccount = (0, deserialize_token_account_1.deserializeTokenAccount)(tokenAccountInfo === null || tokenAccountInfo === void 0 ? void 0 : tokenAccountInfo.data); if (tokenAccount) { return Object.assign({ address: ataAddress }, helpers_1.emptyInstruction); } const createAtaInstruction = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, tokenMint, ataAddress, ownerAddress, ownerAddress); return { address: ataAddress, instructions: [createAtaInstruction], cleanupInstructions: [], signers: [], }; } else { const accountRentExempt = yield connection.getMinimumBalanceForRentExemption(spl_token_1.AccountLayout.span); return (0, token_instructions_1.createWSOLAccountInstructions)(ownerAddress, wrappedSolAmountIn, accountRentExempt); } }); } exports.resolveOrCreateATA = resolveOrCreateATA; function deriveATA(ownerAddress, tokenMint) { return __awaiter(this, void 0, void 0, function* () { return yield spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, tokenMint, ownerAddress); }); } exports.deriveATA = deriveATA;