Skip to content

Commit

Permalink
Merge pull request #23 from RealT-Community/develop
Browse files Browse the repository at this point in the history
Release 2.0.1
  • Loading branch information
jycssu-com authored Nov 22, 2023
2 parents cddc66a + 5d7a956 commit 30e34dc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "realtoken-dashboard-v2",
"version": "2.0.0",
"version": "2.0.1",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useInitStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,6 +22,7 @@ export default function useInitStore() {
const dispatch = useAppDispatch()

const addressList = useSelector(selectCleanedAddressList)
const realtokens = useSelector(selectRealtokens)
const { account } = useWeb3React()

useEffect(() => {
Expand All @@ -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])
}
1 change: 1 addition & 0 deletions src/repositories/rmm.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function mergeWalletsPositions(wallets: RmmPosition[]) {

if (existingPosition) {
existingPosition.amount += position.amount
existingPosition.debt += position.debt
} else {
acc.push({ ...position })
}
Expand Down
23 changes: 19 additions & 4 deletions src/repositories/subgraphs/queries/levinswap.queries.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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<string, number> = {}
user.liquidityPositions.forEach((position) => {
Expand All @@ -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),
})),
}
})
Expand Down
15 changes: 11 additions & 4 deletions src/repositories/wallets.repository.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,16 +19,21 @@ interface WalletBalance {
export type WalletBalances = Record<WalletType, WalletBalance[]>

export const WalletsRepository = {
getBalances: async (addressList: string[]): Promise<WalletBalances> =>
getWalletsBalances(addressList),
getBalances: async (
addressList: string[],
realtokens: Realtoken[]
): Promise<WalletBalances> => 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),
]
)

Expand Down
5 changes: 3 additions & 2 deletions src/store/features/wallets/walletsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -41,7 +42,7 @@ const rmmPositionsChanged = createAction<WalletRmmPosition[]>(
const balancesIsLoading = createAction<boolean>(isLoadingDispatchType)

// THUNKS
export function fetchWallets() {
export function fetchWallets(realtokens: Realtoken[]) {
return async (dispatch: AppDispatch, getState: () => RootState) => {
const state = getState()
const isLoading = state.wallets.isLoading
Expand All @@ -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 })
Expand Down

0 comments on commit 30e34dc

Please sign in to comment.