-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib: refactor fetching of wallet token balances using Redux (#447)
* contrib: add reselect dependency (same version as the one used by rtk-query) Enables memoization of Redux selectors * contrib: add selectors directory w/ selectWalletAddress + selectWalletPublicKey memoized selector * contrib: refactor token balances fetching using Redux - remove useWatchWalletBalance hook in favor of a fetchWalletTokenBalances thunk action. - remove props drilling from the App component to all component which may need to fetch token balances by using the fetchWalletTokenBalances thunk action. - the same pattern can be applied to most custom hooks in the code base. it allows separating business logic from the UI, better architecture which also improves testability. it will have a good impact on rendering performance with wider adoption in the code base. - clean-up walletBalances actions: - use type inference - define constant action type & use it in the matching reducer - use concise definition of action creators: a simple function returning an action object * contrib: fix wallet token balances should reset when user disconnects * contrib: fix fetchWalletTokenBalances typing issue
- Loading branch information
1 parent
6fea0c4
commit adbc617
Showing
22 changed files
with
171 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { BN } from '@coral-xyz/anchor'; | ||
import { NATIVE_MINT } from '@solana/spl-token'; | ||
|
||
import { SOL_DECIMALS } from '@/constant'; | ||
import { selectWalletPublicKey } from '@/selectors/wallet'; | ||
import type { Dispatch, RootState } from '@/store/store'; | ||
import type { TokenSymbol } from '@/types'; | ||
import { findATAAddressSync, nativeToUi } from '@/utils'; | ||
|
||
import { setWalletTokenBalances } from './walletBalances'; | ||
|
||
export const fetchWalletTokenBalances = | ||
() => async (dispatch: Dispatch, getState: () => RootState) => { | ||
const connection = window.adrena.mainConnection; | ||
const walletPublicKey = selectWalletPublicKey(getState()); | ||
|
||
if (!walletPublicKey || !connection) { | ||
dispatch(setWalletTokenBalances(null)); | ||
return; | ||
} | ||
|
||
const tokens = [ | ||
...window.adrena.client.tokens, | ||
window.adrena.client.alpToken, | ||
window.adrena.client.adxToken, | ||
{ mint: NATIVE_MINT, symbol: 'SOL' }, | ||
]; | ||
|
||
const balances = await Promise.all( | ||
tokens.map(async ({ mint }) => { | ||
const ata = findATAAddressSync(walletPublicKey, mint); | ||
|
||
// in case of SOL, consider both SOL in the wallet + WSOL in ATA | ||
if (mint.equals(NATIVE_MINT)) { | ||
try { | ||
const [wsolBalance, solBalance] = await Promise.all([ | ||
// Ignore ATA error if any, consider there are 0 WSOL | ||
connection.getTokenAccountBalance(ata).catch(() => null), | ||
connection.getBalance(walletPublicKey), | ||
]); | ||
|
||
return ( | ||
wsolBalance?.value.uiAmount ?? | ||
0 + nativeToUi(new BN(solBalance), SOL_DECIMALS) | ||
); | ||
} catch { | ||
// Error loading info | ||
return null; | ||
} | ||
} | ||
|
||
try { | ||
const balance = await connection.getTokenAccountBalance(ata); | ||
return balance.value.uiAmount; | ||
} catch { | ||
// Cannot find ATA | ||
return null; | ||
} | ||
}), | ||
); | ||
|
||
dispatch( | ||
setWalletTokenBalances( | ||
balances.reduce((acc, balance, index) => { | ||
acc[tokens[index].symbol] = balance; | ||
|
||
return acc; | ||
}, {} as Record<TokenSymbol, number | null>), | ||
), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { TokenSymbol } from '@/types'; | ||
|
||
export const SET_TOKEN_BALANCES_ACTION_TYPE = 'setTokenBalances' as const; | ||
|
||
export const setWalletTokenBalances = ( | ||
balances: Record<TokenSymbol, number | null> | null, | ||
) => ({ | ||
type: SET_TOKEN_BALANCES_ACTION_TYPE, | ||
payload: balances, | ||
}); | ||
|
||
export type WalletBalancesActions = ReturnType<typeof setWalletTokenBalances>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.