UNPKG

fbonds-core

Version:

Banx protocol sdk

149 lines (148 loc) 5.7 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.createAssociatedTokenAccountInstructionIdimpotent = exports.createAssociatedTokenAccountInstruction = exports.getTokenBalance = exports.findAssociatedTokenAddresSync = exports.findAssociatedTokenAddress = exports.createFakeWallet = void 0; const anchor_1 = require("@coral-xyz/anchor"); const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes"); const token_1 = require("@coral-xyz/anchor/dist/cjs/utils/token"); const spl_token_1 = require("@solana/spl-token"); class NodeWallet { constructor(payer) { this.payer = payer; } static local() { const payer = new anchor_1.web3.Keypair(); return new NodeWallet(payer); } get publicKey() { return this.payer.publicKey; } signTransaction(tx) { return __awaiter(this, void 0, void 0, function* () { return tx; }); } signAllTransactions(txs) { return __awaiter(this, void 0, void 0, function* () { return txs; }); } } //when we only want to view vaults, no need to connect a real wallet. const createFakeWallet = () => { return new NodeWallet(anchor_1.web3.Keypair.fromSecretKey(bytes_1.bs58.decode('48E3pdbxGinywypQjfw4pbyHkznw581G2ftteeBKDztjthUK32dLNbLaSPrZoJ94tBJuzn96MFGbkRkxvt9Rmv1G'))); }; exports.createFakeWallet = createFakeWallet; const findAssociatedTokenAddress = (walletAddress, tokenMintAddress) => __awaiter(void 0, void 0, void 0, function* () { return (yield anchor_1.web3.PublicKey.findProgramAddress([walletAddress.toBuffer(), spl_token_1.TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()], token_1.ASSOCIATED_PROGRAM_ID))[0]; }); exports.findAssociatedTokenAddress = findAssociatedTokenAddress; const findAssociatedTokenAddresSync = (walletAddress, tokenMintAddress) => (anchor_1.web3.PublicKey.findProgramAddressSync([walletAddress.toBuffer(), spl_token_1.TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()], token_1.ASSOCIATED_PROGRAM_ID))[0]; exports.findAssociatedTokenAddresSync = findAssociatedTokenAddresSync; const getTokenBalance = (pubkey, connection) => __awaiter(void 0, void 0, void 0, function* () { const balance = yield connection.getTokenAccountBalance(pubkey); return parseInt(balance.value.amount); }); exports.getTokenBalance = getTokenBalance; const createAssociatedTokenAccountInstruction = (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: anchor_1.web3.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false, }, { pubkey: anchor_1.web3.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; return [ new anchor_1.web3.TransactionInstruction({ keys, programId: token_1.ASSOCIATED_PROGRAM_ID, data: Buffer.from([]), }), ]; }; exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction; const createAssociatedTokenAccountInstructionIdimpotent = (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: anchor_1.web3.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false, }, { pubkey: anchor_1.web3.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; return [ new anchor_1.web3.TransactionInstruction({ keys, programId: token_1.ASSOCIATED_PROGRAM_ID, data: Buffer.from([1]), }), ]; }; exports.createAssociatedTokenAccountInstructionIdimpotent = createAssociatedTokenAccountInstructionIdimpotent;