Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 1x 1x 1x | import { ParsedInstruction, ParsedTransactionWithMeta, PublicKey } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { TokenInfo } from './types';
import { TOKENS } from './constants';
interface TokenBalance {
mint: string;
accountIndex: number;
uiTokenAmount: {
decimals: number;
};
}
export class TokenInfoExtractor {
private readonly defaultSolInfo: TokenInfo = {
mint: TOKENS.SOL,
amount: 0,
decimals: 9,
};
constructor(private readonly txWithMeta: ParsedTransactionWithMeta) {}
public extractSPLTokenInfo(): Map<string, TokenInfo> {
const splTokenAddresses = new Map<string, TokenInfo>();
const allAccountKeys = this.txWithMeta.transaction.message.accountKeys;
this.processPostTokenBalances(splTokenAddresses, allAccountKeys);
this.processInstructions(splTokenAddresses);
this.fillMissingTokenInfo(splTokenAddresses);
return splTokenAddresses;
}
private processPostTokenBalances(tokenMap: Map<string, TokenInfo>, accountKeys: Array<{ pubkey: PublicKey }>): void {
(this.txWithMeta.meta?.postTokenBalances || []).forEach((accountInfo) => {
Iif (accountInfo.mint) {
const accountKey = accountKeys[accountInfo.accountIndex].pubkey.toBase58();
tokenMap.set(accountKey, {
mint: accountInfo.mint.toString(),
amount: 0,
decimals: accountInfo.uiTokenAmount.decimals,
});
}
});
}
private processInstructions(tokenMap: Map<string, TokenInfo>): void {
const processInstruction = (instr: ParsedInstruction) => {
Iif (!instr.programId.equals(TOKEN_PROGRAM_ID)) return;
const { source, destination } = instr.parsed?.info || {};
Iif (!source && !destination) return;
const emptyTokenInfo = { mint: '', amount: 0, decimals: 0 };
Iif (source && !tokenMap.has(source)) {
tokenMap.set(source, emptyTokenInfo);
}
Iif (destination && !tokenMap.has(destination)) {
tokenMap.set(destination, emptyTokenInfo);
}
};
// Process main and inner instructions
this.getAllInstructions().forEach((instruction) => {
processInstruction(instruction as ParsedInstruction);
});
}
private getAllInstructions(): ParsedInstruction[] {
const mainInstructions = this.txWithMeta.transaction.message.instructions;
const innerInstructions = (this.txWithMeta.meta?.innerInstructions || []).flatMap((set) => set.instructions);
return [...mainInstructions, ...innerInstructions] as ParsedInstruction[];
}
private fillMissingTokenInfo(tokenMap: Map<string, TokenInfo>): void {
tokenMap.forEach((info, account) => {
Iif (!info.mint) {
tokenMap.set(account, this.defaultSolInfo);
}
});
}
public extractTokenInfo(): Map<string, TokenInfo> {
try {
const tokenMap = new Map<string, TokenInfo>();
this.getPostTokenBalances().forEach((balance) => {
Iif (balance.mint) {
const mintAddress = balance.mint.toString();
tokenMap.set(mintAddress, {
mint: mintAddress,
amount: 0,
decimals: balance.uiTokenAmount.decimals,
});
}
});
Iif (!tokenMap.has(TOKENS.SOL)) {
tokenMap.set(TOKENS.SOL, this.defaultSolInfo);
}
return tokenMap;
} catch (error) {
throw this.formatError('extract token info', error);
}
}
public extractDecimals(): Map<string, number> {
try {
const decimalsMap = new Map<string, number>();
this.getPostTokenBalances().forEach((balance) => {
Iif (balance.mint) {
decimalsMap.set(balance.mint.toString(), balance.uiTokenAmount.decimals);
}
});
Iif (!decimalsMap.has(TOKENS.SOL)) {
decimalsMap.set(TOKENS.SOL, this.defaultSolInfo.decimals);
}
return decimalsMap;
} catch (error) {
throw this.formatError('extract decimals', error);
}
}
public getDecimals(mint: string): number {
return (
this.getPostTokenBalances().find((balance) => balance.mint === mint)?.uiTokenAmount.decimals ??
(mint === TOKENS.SOL ? this.defaultSolInfo.decimals : 0)
);
}
public validateTokenInfo(requiredMints: PublicKey[]): void {
const balances = this.getPostTokenBalances();
const missingMints = requiredMints.filter((mint) => {
const mintStr = mint.toString();
return !balances.some((balance) => balance.mint.toString() === mintStr) && mint.toBase58() !== TOKENS.SOL;
});
Iif (missingMints.length > 0) {
throw this.formatError(
'validate token info',
`Missing token info for mints: ${missingMints.map((m) => m.toString()).join(', ')}`
);
}
}
private getPostTokenBalances(): TokenBalance[] {
return this.txWithMeta.meta?.postTokenBalances || [];
}
private formatError(operation: string, error: unknown): string {
return `Failed to ${operation}: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
}
|