UNPKG

@bbachain/spl-token-swap

Version:

JavaScript/TypeScript bindings for the Solana Program Library (SPL) Token Swap program.

168 lines (167 loc) 6.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenSwap = exports.tokenSwapBeet = void 0; const beet = __importStar(require("@bbachain/beet")); const beetBBA = __importStar(require("@bbachain/beet-bbachain")); const web3 = __importStar(require("@bbachain/web3.js")); const types_1 = require("../types"); const __1 = require(".."); /** * @category userTypes */ exports.tokenSwapBeet = new beet.FixableBeetArgsStruct([ ['accountDiscriminator', beet.u8], ['isInitialized', beet.bool], ['bumpSeed', beet.u8], ['tokenProgramId', beetBBA.publicKey], ['tokenA', beetBBA.publicKey], ['tokenB', beetBBA.publicKey], ['poolMint', beetBBA.publicKey], ['tokenAMint', beetBBA.publicKey], ['tokenBMint', beetBBA.publicKey], ['poolFeeAccount', beetBBA.publicKey], ['fees', types_1.feesBeet], ['swapCurve', types_1.swapCurveBeet], ], 'TokenSwap'); /** * Holds the data for the {@link TokenSwap} Account and provides de/serialization * functionality for that data * * @category Accounts */ class TokenSwap { constructor(isInitialized, bumpSeed, tokenProgramId, tokenA, tokenB, poolMint, tokenAMint, tokenBMint, poolFeeAccount, fees, swapCurve) { this.isInitialized = isInitialized; this.bumpSeed = bumpSeed; this.tokenProgramId = tokenProgramId; this.tokenA = tokenA; this.tokenB = tokenB; this.poolMint = poolMint; this.tokenAMint = tokenAMint; this.tokenBMint = tokenBMint; this.poolFeeAccount = poolFeeAccount; this.fees = fees; this.swapCurve = swapCurve; } /** * Creates a {@link TokenSwap} instance from the provided args. */ static fromArgs(args) { return new TokenSwap(args.isInitialized, args.bumpSeed, args.tokenProgramId, args.tokenA, args.tokenB, args.poolMint, args.tokenAMint, args.tokenBMint, args.poolFeeAccount, args.fees, args.swapCurve); } /** * Deserializes the {@link TokenSwap} from the data of the provided {@link web3.AccountInfo}. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it. */ static fromAccountInfo(accountInfo, offset = 0) { return TokenSwap.deserialize(accountInfo.data, offset); } /** * Retrieves the account info from the provided address and deserializes * the {@link TokenSwap} from its data. * * @throws Error if no account info is found at the address or if deserialization fails */ static async fromAccountAddress(connection, address, commitmentOrConfig) { const accountInfo = await connection.getAccountInfo(address, commitmentOrConfig); if (accountInfo == null) { throw new Error(`Unable to find TokenSwap account at ${address}`); } return TokenSwap.fromAccountInfo(accountInfo, 0)[0]; } /** * Provides a {@link web3.Connection.getProgramAccounts} config builder, * to fetch accounts matching filters that can be specified via that builder. * * @param programId - the program that owns the accounts we are filtering */ static gpaBuilder(programId = new web3.PublicKey(__1.PROGRAM_ADDRESS)) { return beetBBA.GpaBuilder.fromStruct(programId, exports.tokenSwapBeet); } /** * Deserializes the {@link TokenSwap} from the provided data Buffer. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it. */ static deserialize(buf, offset = 0) { const [raw, offset2] = exports.tokenSwapBeet.deserialize(buf, offset); return [TokenSwap.fromArgs(raw), offset2]; } /** * Serializes the {@link TokenSwap} into a Buffer. * @returns a tuple of the created Buffer and the offset up to which the buffer was written to store it. */ serialize() { return exports.tokenSwapBeet.serialize({ accountDiscriminator: 1, ...this, }); } /** * Returns the byteSize of a {@link Buffer} holding the serialized data of * {@link TokenSwap} for the provided args. * * @param args need to be provided since the byte size for this account * depends on them */ static byteSize(args) { const instance = TokenSwap.fromArgs(args); return exports.tokenSwapBeet.toFixedFromValue({ accountDiscriminator: 1, ...instance, }).byteSize; } /** * Fetches the minimum balance needed to exempt an account holding * {@link TokenSwap} data from rent * * @param args need to be provided since the byte size for this account * depends on them * @param connection used to retrieve the rent exemption information */ static async getMinimumBalanceForRentExemption(args, connection, commitment) { return connection.getMinimumBalanceForRentExemption(TokenSwap.byteSize(args), commitment); } /** * Returns a readable version of {@link TokenSwap} properties * and can be used to convert to JSON and/or logging */ pretty() { return { isInitialized: this.isInitialized, bumpSeed: this.bumpSeed, tokenProgramId: this.tokenProgramId.toBase58(), tokenA: this.tokenA.toBase58(), tokenB: this.tokenB.toBase58(), poolMint: this.poolMint.toBase58(), tokenAMint: this.tokenAMint.toBase58(), tokenBMint: this.tokenBMint.toBase58(), poolFeeAccount: this.poolFeeAccount.toBase58(), fees: this.fees, swapCurve: this.swapCurve, }; } } exports.TokenSwap = TokenSwap;