diff --git a/package-lock.json b/package-lock.json index eeb1ad7..73e418f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "realtoken-dashboard-v2", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 05cbddf..ddd43c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "realtoken-dashboard-v2", - "version": "2.0.0", + "version": "2.0.1", "scripts": { "dev": "next dev", "build": "next build", diff --git a/src/hooks/useInitStore.ts b/src/hooks/useInitStore.ts index 4b00d21..d6aa44e 100644 --- a/src/hooks/useInitStore.ts +++ b/src/hooks/useInitStore.ts @@ -4,6 +4,7 @@ import { useSelector } from 'react-redux' import { useWeb3React } from '@web3-react/core' import { fetchCurrenciesRates } from 'src/store/features/currencies/currenciesSlice' +import { selectRealtokens } from 'src/store/features/realtokens/realtokensSelector' import { fetchRealtokens } from 'src/store/features/realtokens/realtokensSlice' import { selectCleanedAddressList } from 'src/store/features/settings/settingsSelector' import { @@ -21,6 +22,7 @@ export default function useInitStore() { const dispatch = useAppDispatch() const addressList = useSelector(selectCleanedAddressList) + const realtokens = useSelector(selectRealtokens) const { account } = useWeb3React() useEffect(() => { @@ -40,9 +42,11 @@ export default function useInitStore() { useEffect(() => { if (addressList.length) { - dispatch(fetchWallets()) + if (realtokens.length) { + dispatch(fetchWallets(realtokens)) + } } else { dispatch(resetWallets()) } - }, [addressList.length, addresses, dispatch]) + }, [realtokens, addresses, dispatch]) } diff --git a/src/repositories/rmm.repository.ts b/src/repositories/rmm.repository.ts index 6bb7683..ec48d0d 100644 --- a/src/repositories/rmm.repository.ts +++ b/src/repositories/rmm.repository.ts @@ -22,6 +22,7 @@ function mergeWalletsPositions(wallets: RmmPosition[]) { if (existingPosition) { existingPosition.amount += position.amount + existingPosition.debt += position.debt } else { acc.push({ ...position }) } diff --git a/src/repositories/subgraphs/queries/levinswap.queries.ts b/src/repositories/subgraphs/queries/levinswap.queries.ts index afd52e2..f51ebfd 100644 --- a/src/repositories/subgraphs/queries/levinswap.queries.ts +++ b/src/repositories/subgraphs/queries/levinswap.queries.ts @@ -1,14 +1,18 @@ import { gql } from '@apollo/client' +import { Realtoken } from 'src/store/features/realtokens/realtokensSelector' import { useCacheWithLocalStorage } from 'src/utils/useCache' import { LevinSwapClient } from '../clients' -export async function getLevinSwapBalances(addressList: string[]) { +export async function getLevinSwapBalances( + addressList: string[], + realtokens: Realtoken[] +) { const result = await executeQuery( addressList.map((item) => item.toLowerCase()) ) - return formatBalances(result.data.users) + return formatBalances(result.data.users, realtokens) } const executeQuery = useCacheWithLocalStorage( @@ -64,7 +68,10 @@ interface LevinSwapResult { }[] } -function formatBalances(users: LevinSwapResult['users']) { +function formatBalances( + users: LevinSwapResult['users'], + realtokens: Realtoken[] +) { return users.map((user) => { const balances: Record = {} user.liquidityPositions.forEach((position) => { @@ -79,11 +86,19 @@ function formatBalances(users: LevinSwapResult['users']) { (balances[token1] ?? 0) + parseFloat(position.pair.reserve1) * share }) + const isRealtoken = (address: string) => + realtokens.find((item) => + [ + item.gnosisContract?.toLowerCase(), + item.ethereumContract?.toLowerCase(), + ].includes(address.toLowerCase()) + ) + return { address: user.id, balances: Object.entries(balances).map(([address, amount]) => ({ token: address, - amount, + amount: amount / 10 ** (isRealtoken(address) ? 18 : 0), })), } }) diff --git a/src/repositories/wallets.repository.ts b/src/repositories/wallets.repository.ts index 4c1f4f5..2a9013c 100644 --- a/src/repositories/wallets.repository.ts +++ b/src/repositories/wallets.repository.ts @@ -1,3 +1,5 @@ +import { Realtoken } from 'src/store/features/realtokens/realtokensSelector' + import { getLevinSwapBalances } from './subgraphs/queries/levinswap.queries' import { getRealtokenBalances } from './subgraphs/queries/realtoken.queries' import { getRmmBalances } from './subgraphs/queries/rmm.queries' @@ -17,16 +19,21 @@ interface WalletBalance { export type WalletBalances = Record export const WalletsRepository = { - getBalances: async (addressList: string[]): Promise => - getWalletsBalances(addressList), + getBalances: async ( + addressList: string[], + realtokens: Realtoken[] + ): Promise => getWalletsBalances(addressList, realtokens), } -async function getWalletsBalances(addressList: string[]) { +async function getWalletsBalances( + addressList: string[], + realtokens: Realtoken[] +) { const [realtokenBalances, rmmBalances, levinSwapBalances] = await Promise.all( [ getRealtokenBalances(addressList), getRmmBalances(addressList), - getLevinSwapBalances(addressList), + getLevinSwapBalances(addressList, realtokens), ] ) diff --git a/src/store/features/wallets/walletsSlice.ts b/src/store/features/wallets/walletsSlice.ts index f0100bc..9a445d1 100644 --- a/src/store/features/wallets/walletsSlice.ts +++ b/src/store/features/wallets/walletsSlice.ts @@ -7,6 +7,7 @@ import { } from 'src/repositories/rmm.repository' import { AppDispatch, RootState } from 'src/store/store' +import { Realtoken } from '../realtokens/realtokensSelector' import { selectCleanedAddressList } from '../settings/settingsSelector' interface WalletsInitialStateType { @@ -41,7 +42,7 @@ const rmmPositionsChanged = createAction( const balancesIsLoading = createAction(isLoadingDispatchType) // THUNKS -export function fetchWallets() { +export function fetchWallets(realtokens: Realtoken[]) { return async (dispatch: AppDispatch, getState: () => RootState) => { const state = getState() const isLoading = state.wallets.isLoading @@ -51,7 +52,7 @@ export function fetchWallets() { dispatch({ type: isLoadingDispatchType, payload: true }) try { const [balances, rmmPositions] = await Promise.all([ - WalletsRepository.getBalances(addressList), + WalletsRepository.getBalances(addressList, realtokens), RmmRepository.getPositions(addressList), ]) dispatch({ type: balancesChangedDispatchType, payload: balances })