Skip to content

Commit

Permalink
Merge pull request #472 from mithraiclabs/fix/owned-token-accounts
Browse files Browse the repository at this point in the history
Fix/owned token accounts
  • Loading branch information
evanpipta authored Jul 29, 2021
2 parents fd736ac + 4ce6f9a commit 8a47ccd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
45 changes: 24 additions & 21 deletions src/context/OwnedTokenAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/Serum/useSettleFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -52,7 +54,7 @@ export const useSettleFunds = (
await createAssociatedTokenAccountInstruction({
payer: pubKey,
owner: pubKey,
mintPublicKey: serumMarket.baseMintAddress,
mintPublicKey: serumMarket?.baseMintAddress,
})

transaction.add(createOptAccountTx)
Expand Down

0 comments on commit 8a47ccd

Please sign in to comment.