UNPKG

@frakters/nft-lending-v2

Version:

Client library for interacting with nft lenging solana program

207 lines (206 loc) 9.54 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.findOrCreateAccountByMint = exports.ensureWrappedAccount = exports.createTokenAccount = exports.createMint = exports.createAssociatedTokenAccountInstruction = exports.createUninitializedAccount = exports.createUninitializedMint = exports.createTempMemoryAccount = exports.DEFAULT_TEMP_MEM_SPACE = exports.ensureSplAccount = exports.approve = exports.TokenAccountParser = void 0; const spl_token_1 = require("@solana/spl-token"); const web3_js_1 = require("@solana/web3.js"); const ids_1 = require("./utils/ids"); const ids_2 = require("./utils/ids"); const deserialize_1 = require("./utils/deserialize"); // import { TokenAccountParser } from '../contexts/accounts/parsesrs'; exports.TokenAccountParser = (pubKey, info) => { // Sometimes a wrapped sol account gets closed, goes to 0 length, // triggers an update over wss which triggers this guy to get called // since your UI already logged that pubkey as a token account. Check for length. if (info.data.length > 0) { const buffer = Buffer.from(info.data); const data = deserialize_1.deserializeAccount(buffer); const details = { pubkey: pubKey, account: Object.assign({}, info), info: data, }; return details; } }; function approve(instructions, cleanupInstructions, account, owner, amount, autoRevoke = true, // if delegate is not passed ephemeral transfer authority is used delegate, existingTransferAuthority) { const tokenProgram = ids_1.TOKEN_PROGRAM_ID; const transferAuthority = existingTransferAuthority || web3_js_1.Keypair.generate(); //const delegateKey = delegate ?? transferAuthority.publicKey; instructions.push(spl_token_1.Token.createApproveInstruction(tokenProgram, account, delegate !== null && delegate !== void 0 ? delegate : transferAuthority.publicKey, owner, [], amount)); if (autoRevoke) { cleanupInstructions.push(spl_token_1.Token.createRevokeInstruction(tokenProgram, account, owner, [])); } return transferAuthority; } exports.approve = approve; function ensureSplAccount(instructions, cleanupInstructions, toCheck, payer, amount, signers) { // if (!toCheck.info.isNative) { // return toCheck.pubkey; // } const account = createUninitializedAccount(instructions, payer, amount, signers); instructions.push(spl_token_1.Token.createInitAccountInstruction(ids_1.TOKEN_PROGRAM_ID, ids_1.WRAPPED_SOL_MINT, account, payer)); cleanupInstructions.push(spl_token_1.Token.createCloseAccountInstruction(ids_1.TOKEN_PROGRAM_ID, account, payer, payer, [])); return account; } exports.ensureSplAccount = ensureSplAccount; exports.DEFAULT_TEMP_MEM_SPACE = 65548; function createTempMemoryAccount(instructions, payer, signers, owner, space = exports.DEFAULT_TEMP_MEM_SPACE) { const account = web3_js_1.Keypair.generate(); instructions.push(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: account.publicKey, // 0 will evict/close account since it cannot pay rent lamports: 0, space: space, programId: owner, })); signers.push(account); return account.publicKey; } exports.createTempMemoryAccount = createTempMemoryAccount; function createUninitializedMint(instructions, payer, amount, signers) { const account = web3_js_1.Keypair.generate(); instructions.push(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: account.publicKey, lamports: amount, space: spl_token_1.MintLayout.span, programId: ids_1.TOKEN_PROGRAM_ID, })); signers.push(account); return account.publicKey; } exports.createUninitializedMint = createUninitializedMint; function createUninitializedAccount(instructions, payer, amount, signers) { const account = web3_js_1.Keypair.generate(); instructions.push(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: account.publicKey, lamports: amount, space: spl_token_1.AccountLayout.span, programId: ids_1.TOKEN_PROGRAM_ID, })); signers.push(account); return account.publicKey; } exports.createUninitializedAccount = createUninitializedAccount; function createAssociatedTokenAccountInstruction(instructions, associatedTokenAddress, payer, walletAddress, splTokenMintAddress) { const keys = [ { pubkey: payer, isSigner: true, isWritable: true, }, { pubkey: associatedTokenAddress, isSigner: false, isWritable: true, }, { pubkey: walletAddress, isSigner: false, isWritable: false, }, { pubkey: splTokenMintAddress, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: ids_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: ids_1.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, data: Buffer.from([]), })); } exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction; function createMint(instructions, payer, mintRentExempt, decimals, owner, freezeAuthority, signers) { const account = createUninitializedMint(instructions, payer, mintRentExempt, signers); instructions.push(spl_token_1.Token.createInitMintInstruction(ids_1.TOKEN_PROGRAM_ID, account, decimals, owner, freezeAuthority)); return account; } exports.createMint = createMint; function createTokenAccount(instructions, payer, accountRentExempt, mint, owner, signers) { const account = createUninitializedAccount(instructions, payer, accountRentExempt, signers); instructions.push(spl_token_1.Token.createInitAccountInstruction(ids_1.TOKEN_PROGRAM_ID, mint, account, owner)); return account; } exports.createTokenAccount = createTokenAccount; function ensureWrappedAccount(instructions, cleanupInstructions, toCheck, payer, amount, signers) { // if (toCheck && !toCheck.info.isNative) { // return toCheck.pubkey; // } const TOKEN_PROGRAM_ID = ids_2.programIds().token; const account = web3_js_1.Keypair.generate(); instructions.push(web3_js_1.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: account.publicKey, lamports: amount, space: spl_token_1.AccountLayout.span, programId: TOKEN_PROGRAM_ID, })); instructions.push(spl_token_1.Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, ids_1.WRAPPED_SOL_MINT, account.publicKey, payer)); cleanupInstructions.push(spl_token_1.Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, account.publicKey, payer, payer, [])); signers.push(account); return account.publicKey.toBase58(); } exports.ensureWrappedAccount = ensureWrappedAccount; // TODO: check if one of to accounts needs to be native sol ... if yes unwrap it ... function findOrCreateAccountByMint(payer, owner, instructions, cleanupInstructions, accountRentExempt, mint, // use to identify same type signers, connection) { return __awaiter(this, void 0, void 0, function* () { const accountToFind = mint.toBase58(); const ownerKey = owner.toBase58(); const account = yield connection.getAccountInfo(mint, 'confirmed'); // const account = cache // .byParser(TokenAccountParser) // .map(id => cache.get(id)) // .find( // acc => // acc !== undefined && // acc.info.mint.toBase58() === accountToFind && // acc.info.owner.toBase58() === ownerKey && // (excluded === undefined || !excluded.has(acc.pubkey)), // ); const isWrappedSol = accountToFind === ids_1.WRAPPED_SOL_MINT.toBase58(); let toAccount; if (account && !isWrappedSol) { toAccount = mint; } else { // creating depositor pool account toAccount = createTokenAccount(instructions, payer, accountRentExempt, mint, owner, signers); if (isWrappedSol) { cleanupInstructions.push(spl_token_1.Token.createCloseAccountInstruction(ids_1.TOKEN_PROGRAM_ID, toAccount, payer, payer, [])); } } return toAccount; }); } exports.findOrCreateAccountByMint = findOrCreateAccountByMint;