Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 730b628

Browse files
authored
token-js: expose unpack methods (#3471)
* feat: expose unpack methods * default program id Co-authored-by: Arrowana <[email protected]>
1 parent d9a284f commit 730b628

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

token/js/src/state/account.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,18 @@ export async function getMinimumBalanceForRentExemptAccountWithExtensions(
157157
return await connection.getMinimumBalanceForRentExemption(accountLen, commitment);
158158
}
159159

160-
function unpackAccount(info: AccountInfo<Buffer> | null, address: PublicKey, programId: PublicKey) {
160+
/**
161+
* Unpacks a token account
162+
* @param info the token account on-chain account
163+
* @param address TokenAccount
164+
* @param programId SPL Token program account
165+
* @returns
166+
*/
167+
export function unpackAccount(
168+
info: AccountInfo<Buffer> | null,
169+
address: PublicKey,
170+
programId: PublicKey = TOKEN_PROGRAM_ID
171+
): Account {
161172
if (!info) throw new TokenAccountNotFoundError();
162173
if (!info.owner.equals(programId)) throw new TokenInvalidAccountOwnerError();
163174
if (info.data.length < ACCOUNT_SIZE) throw new TokenInvalidAccountSizeError();

token/js/src/state/mint.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { struct, u32, u8 } from '@solana/buffer-layout';
22
import { bool, publicKey, u64 } from '@solana/buffer-layout-utils';
3-
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
3+
import { AccountInfo, Commitment, Connection, PublicKey } from '@solana/web3.js';
44
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../constants';
55
import {
66
TokenAccountNotFoundError,
@@ -77,6 +77,21 @@ export async function getMint(
7777
programId = TOKEN_PROGRAM_ID
7878
): Promise<Mint> {
7979
const info = await connection.getAccountInfo(address, commitment);
80+
return unpackMint(info, address, programId);
81+
}
82+
83+
/**
84+
* Unpacks a mint
85+
* @param info the mint on-chain account
86+
* @param address Mint
87+
* @param programId SPL Token program account
88+
* @returns
89+
*/
90+
export function unpackMint(
91+
info: AccountInfo<Buffer> | null,
92+
address: PublicKey,
93+
programId: PublicKey = TOKEN_PROGRAM_ID
94+
): Mint {
8095
if (!info) throw new TokenAccountNotFoundError();
8196
if (!info.owner.equals(programId)) throw new TokenInvalidAccountOwnerError();
8297
if (info.data.length < MINT_SIZE) throw new TokenInvalidAccountSizeError();

0 commit comments

Comments
 (0)