diff --git a/src/context/OwnedTokenAccounts.tsx b/src/context/OwnedTokenAccounts.tsx index 914c13a4..d9f6578a 100644 --- a/src/context/OwnedTokenAccounts.tsx +++ b/src/context/OwnedTokenAccounts.tsx @@ -136,35 +136,38 @@ export const OwnedTokenAccountsProvider: React.FC = ({ children }) => { } ;(async () => { - const filters = getOwnedTokenAccountsFilter(pubKey) setLoading(true) try { // @ts-expect-error we know what we're doing - const resp = await connection._rpcRequest('getProgramAccounts', [ - TOKEN_PROGRAM_ID.toBase58(), + const resp = await connection._rpcRequest('getTokenAccountsByOwner', [ + pubKey.toBase58(), + { + programId: TOKEN_PROGRAM_ID.toBase58(), + }, { commitment: connection.commitment, - filters, }, ]) const _ownedTokenAccounts = {} - resp.result?.forEach(({ account, pubkey }) => { - const accountInfo = AccountLayout.decode(bs58.decode(account.data)) - const initialAccount = convertAccountInfoToLocalStruct( - accountInfo, - new PublicKey(pubkey), - ) - const mint = initialAccount.mint.toString() - if (_ownedTokenAccounts[mint]) { - _ownedTokenAccounts[mint].push(initialAccount) - } else { - _ownedTokenAccounts[mint] = [initialAccount] - } - if (!subscriptionsRef.current[pubkey]) { - // Subscribe to the SPL token account updates only if no subscription exists for this token. - subscribeToTokenAccount(new PublicKey(pubkey)) - } - }) + if (resp?.result?.value) { + resp.result.value.forEach(({ account, pubkey }) => { + const accountInfo = AccountLayout.decode(bs58.decode(account.data)) + const initialAccount = convertAccountInfoToLocalStruct( + accountInfo, + new PublicKey(pubkey), + ) + const mint = initialAccount.mint.toString() + if (_ownedTokenAccounts[mint]) { + _ownedTokenAccounts[mint].push(initialAccount) + } else { + _ownedTokenAccounts[mint] = [initialAccount] + } + if (!subscriptionsRef.current[pubkey]) { + // Subscribe to the SPL token account updates only if no subscription exists for this token. + subscribeToTokenAccount(new PublicKey(pubkey)) + } + }) + } setOwnedTokenAccounts(_ownedTokenAccounts) } catch (err) { pushNotification({ diff --git a/src/hooks/Serum/useSettleFunds.tsx b/src/hooks/Serum/useSettleFunds.tsx index aa533d3c..458436f9 100644 --- a/src/hooks/Serum/useSettleFunds.tsx +++ b/src/hooks/Serum/useSettleFunds.tsx @@ -30,8 +30,10 @@ export const useSettleFunds = ( useOwnedTokenAccounts() const openOrders = useSerumOpenOrderAccounts(serumMarketAddress, true) const serumMarket = serumMarkets[serumMarketAddress]?.serumMarket - const baseMintAddress = serumMarket.baseMintAddress.toString() - const quoteMintAddress = serumMarket.quoteMintAddress.toString() + const baseMintAddress = + serumMarket?.baseMintAddress && serumMarket.baseMintAddress.toString() + const quoteMintAddress = + serumMarket?.quoteMintAddress && serumMarket.quoteMintAddress.toString() const baseTokenAccounts = ownedTokenAccounts[baseMintAddress] ?? [] const quoteTokenAccounts = ownedTokenAccounts[quoteMintAddress] ?? [] const { pubKey: baseTokenAccountKey } = getHighestAccount(baseTokenAccounts) @@ -52,7 +54,7 @@ export const useSettleFunds = ( await createAssociatedTokenAccountInstruction({ payer: pubKey, owner: pubKey, - mintPublicKey: serumMarket.baseMintAddress, + mintPublicKey: serumMarket?.baseMintAddress, }) transaction.add(createOptAccountTx)