UNPKG

@symmetry-hq/baskets-sdk

Version:

Software Development Kit for interacting with Symmetry Baskets Program

128 lines (127 loc) 8.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccountLayout = exports.AccountState = exports.TokenOwnerOffCurveError = exports.TokenError = exports.syncNativeInstructionData = exports.TokenInstruction = exports.NATIVE_MINT_2022 = exports.NATIVE_MINT = exports.ASSOCIATED_TOKEN_PROGRAM_ID = exports.TOKEN_2022_PROGRAM_ID = exports.TOKEN_PROGRAM_ID = void 0; exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction; exports.getAssociatedTokenAddressSync = getAssociatedTokenAddressSync; exports.createSyncNativeInstruction = createSyncNativeInstruction; const web3_js_1 = require("@solana/web3.js"); const buffer_layout_1 = require("@solana/buffer-layout"); const buffer_layout_utils_1 = require("@solana/buffer-layout-utils"); /** Address of the SPL Token program */ exports.TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'); /** Address of the SPL Token 2022 program */ exports.TOKEN_2022_PROGRAM_ID = new web3_js_1.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'); /** Address of the SPL Associated Token Account program */ exports.ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'); /** Address of the special mint for wrapped native SOL in spl-token */ exports.NATIVE_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111112'); /** Address of the special mint for wrapped native SOL in spl-token-2022 */ exports.NATIVE_MINT_2022 = new web3_js_1.PublicKey('9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP'); function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) { return buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, Buffer.alloc(0), programId, associatedTokenProgramId); } function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) { if (!allowOwnerOffCurve && !web3_js_1.PublicKey.isOnCurve(owner.toBuffer())) throw new TokenOwnerOffCurveError(); const [address] = web3_js_1.PublicKey.findProgramAddressSync([owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId); return address; } function buildAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, instructionData, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) { const keys = [ { pubkey: payer, isSigner: true, isWritable: true }, { pubkey: associatedToken, isSigner: false, isWritable: true }, { pubkey: owner, isSigner: false, isWritable: false }, { pubkey: mint, isSigner: false, isWritable: false }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false }, { pubkey: programId, isSigner: false, isWritable: false }, ]; return new web3_js_1.TransactionInstruction({ keys, programId: associatedTokenProgramId, data: instructionData, }); } var TokenInstruction; (function (TokenInstruction) { TokenInstruction[TokenInstruction["InitializeMint"] = 0] = "InitializeMint"; TokenInstruction[TokenInstruction["InitializeAccount"] = 1] = "InitializeAccount"; TokenInstruction[TokenInstruction["InitializeMultisig"] = 2] = "InitializeMultisig"; TokenInstruction[TokenInstruction["Transfer"] = 3] = "Transfer"; TokenInstruction[TokenInstruction["Approve"] = 4] = "Approve"; TokenInstruction[TokenInstruction["Revoke"] = 5] = "Revoke"; TokenInstruction[TokenInstruction["SetAuthority"] = 6] = "SetAuthority"; TokenInstruction[TokenInstruction["MintTo"] = 7] = "MintTo"; TokenInstruction[TokenInstruction["Burn"] = 8] = "Burn"; TokenInstruction[TokenInstruction["CloseAccount"] = 9] = "CloseAccount"; TokenInstruction[TokenInstruction["FreezeAccount"] = 10] = "FreezeAccount"; TokenInstruction[TokenInstruction["ThawAccount"] = 11] = "ThawAccount"; TokenInstruction[TokenInstruction["TransferChecked"] = 12] = "TransferChecked"; TokenInstruction[TokenInstruction["ApproveChecked"] = 13] = "ApproveChecked"; TokenInstruction[TokenInstruction["MintToChecked"] = 14] = "MintToChecked"; TokenInstruction[TokenInstruction["BurnChecked"] = 15] = "BurnChecked"; TokenInstruction[TokenInstruction["InitializeAccount2"] = 16] = "InitializeAccount2"; TokenInstruction[TokenInstruction["SyncNative"] = 17] = "SyncNative"; TokenInstruction[TokenInstruction["InitializeAccount3"] = 18] = "InitializeAccount3"; TokenInstruction[TokenInstruction["InitializeMultisig2"] = 19] = "InitializeMultisig2"; TokenInstruction[TokenInstruction["InitializeMint2"] = 20] = "InitializeMint2"; TokenInstruction[TokenInstruction["GetAccountDataSize"] = 21] = "GetAccountDataSize"; TokenInstruction[TokenInstruction["InitializeImmutableOwner"] = 22] = "InitializeImmutableOwner"; TokenInstruction[TokenInstruction["AmountToUiAmount"] = 23] = "AmountToUiAmount"; TokenInstruction[TokenInstruction["UiAmountToAmount"] = 24] = "UiAmountToAmount"; TokenInstruction[TokenInstruction["InitializeMintCloseAuthority"] = 25] = "InitializeMintCloseAuthority"; TokenInstruction[TokenInstruction["TransferFeeExtension"] = 26] = "TransferFeeExtension"; TokenInstruction[TokenInstruction["ConfidentialTransferExtension"] = 27] = "ConfidentialTransferExtension"; TokenInstruction[TokenInstruction["DefaultAccountStateExtension"] = 28] = "DefaultAccountStateExtension"; TokenInstruction[TokenInstruction["Reallocate"] = 29] = "Reallocate"; TokenInstruction[TokenInstruction["MemoTransferExtension"] = 30] = "MemoTransferExtension"; TokenInstruction[TokenInstruction["CreateNativeMint"] = 31] = "CreateNativeMint"; TokenInstruction[TokenInstruction["InitializeNonTransferableMint"] = 32] = "InitializeNonTransferableMint"; TokenInstruction[TokenInstruction["InterestBearingMintExtension"] = 33] = "InterestBearingMintExtension"; TokenInstruction[TokenInstruction["CpiGuardExtension"] = 34] = "CpiGuardExtension"; TokenInstruction[TokenInstruction["InitializePermanentDelegate"] = 35] = "InitializePermanentDelegate"; TokenInstruction[TokenInstruction["TransferHookExtension"] = 36] = "TransferHookExtension"; // ConfidentialTransferFeeExtension = 37, // WithdrawalExcessLamports = 38, TokenInstruction[TokenInstruction["MetadataPointerExtension"] = 39] = "MetadataPointerExtension"; })(TokenInstruction || (exports.TokenInstruction = TokenInstruction = {})); exports.syncNativeInstructionData = (0, buffer_layout_1.struct)([(0, buffer_layout_1.u8)('instruction')]); function createSyncNativeInstruction(account, programId = exports.TOKEN_PROGRAM_ID) { const keys = [{ pubkey: account, isSigner: false, isWritable: true }]; const data = Buffer.alloc(exports.syncNativeInstructionData.span); exports.syncNativeInstructionData.encode({ instruction: TokenInstruction.SyncNative }, data); return new web3_js_1.TransactionInstruction({ keys, programId, data }); } class TokenError extends Error { constructor(message) { super(message); } } exports.TokenError = TokenError; /** Thrown if the owner of a token account is a PDA (Program Derived Address) */ class TokenOwnerOffCurveError extends TokenError { constructor() { super(...arguments); this.name = 'TokenOwnerOffCurveError'; } } exports.TokenOwnerOffCurveError = TokenOwnerOffCurveError; /** Token account state as stored by the program */ var AccountState; (function (AccountState) { AccountState[AccountState["Uninitialized"] = 0] = "Uninitialized"; AccountState[AccountState["Initialized"] = 1] = "Initialized"; AccountState[AccountState["Frozen"] = 2] = "Frozen"; })(AccountState || (exports.AccountState = AccountState = {})); exports.AccountLayout = (0, buffer_layout_1.struct)([ (0, buffer_layout_utils_1.publicKey)('mint'), (0, buffer_layout_utils_1.publicKey)('owner'), (0, buffer_layout_utils_1.u64)('amount'), (0, buffer_layout_1.u32)('delegateOption'), (0, buffer_layout_utils_1.publicKey)('delegate'), (0, buffer_layout_1.u8)('state'), (0, buffer_layout_1.u32)('isNativeOption'), (0, buffer_layout_utils_1.u64)('isNative'), (0, buffer_layout_utils_1.u64)('delegatedAmount'), (0, buffer_layout_1.u32)('closeAuthorityOption'), (0, buffer_layout_utils_1.publicKey)('closeAuthority'), ]);