Handle token accounts with invalid mints
This commit is contained in:
parent
a515840db1
commit
9643de889b
|
@ -5,6 +5,10 @@ export const TOKEN_PROGRAM_ID = new PublicKey(
|
|||
'TokenSVp5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o',
|
||||
);
|
||||
|
||||
export const WRAPPED_SOL_MINT = new PublicKey(
|
||||
'So11111111111111111111111111111111111111111',
|
||||
);
|
||||
|
||||
const LAYOUT = BufferLayout.union(BufferLayout.u8('instruction'));
|
||||
LAYOUT.addVariant(
|
||||
0,
|
||||
|
|
|
@ -12,7 +12,7 @@ import {
|
|||
getOwnedTokenAccounts,
|
||||
transferTokens,
|
||||
} from './tokens';
|
||||
import { TOKEN_PROGRAM_ID } from './tokens/instructions';
|
||||
import { TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT } from './tokens/instructions';
|
||||
import {
|
||||
ACCOUNT_LAYOUT,
|
||||
parseMintData,
|
||||
|
@ -142,7 +142,24 @@ export function useBalanceInfo(publicKey) {
|
|||
let [mintInfo, mintInfoLoaded] = useAccountInfo(mint);
|
||||
let { name, symbol } = useTokenName(mint);
|
||||
|
||||
if (accountInfoLoaded && mint && mintInfoLoaded) {
|
||||
if (!accountInfoLoaded) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mint && mint.equals(WRAPPED_SOL_MINT)) {
|
||||
return {
|
||||
amount,
|
||||
decimals: 9,
|
||||
mint,
|
||||
owner,
|
||||
tokenName: 'Wrapped SOL',
|
||||
tokenSymbol: 'SOL',
|
||||
valid: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (mint && mintInfoLoaded) {
|
||||
try {
|
||||
let { decimals } = parseMintData(mintInfo.data);
|
||||
return {
|
||||
amount,
|
||||
|
@ -151,21 +168,34 @@ export function useBalanceInfo(publicKey) {
|
|||
owner,
|
||||
tokenName: name,
|
||||
tokenSymbol: symbol,
|
||||
initialized: true,
|
||||
valid: true,
|
||||
};
|
||||
} else if (accountInfoLoaded && !mint) {
|
||||
} catch (e) {
|
||||
return {
|
||||
amount,
|
||||
decimals: 0,
|
||||
mint,
|
||||
owner,
|
||||
tokenName: 'Invalid',
|
||||
tokenSymbol: 'INVALID',
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!mint) {
|
||||
return {
|
||||
amount: accountInfo?.lamports ?? 0,
|
||||
decimals: 9,
|
||||
mint: null,
|
||||
owner: publicKey,
|
||||
tokenName: 'Solana',
|
||||
tokenName: 'SOL',
|
||||
tokenSymbol: 'SOL',
|
||||
initialized: false,
|
||||
valid: true,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function useWalletSelector() {
|
||||
|
|
Loading…
Reference in New Issue