Skip to content

Commit

Permalink
feat: disable fetching ethereum, levinswap and rmm v2 by default
Browse files Browse the repository at this point in the history
These features are used only by few peoples, but require many TheGraph requests
  • Loading branch information
jycssu-com committed Jul 15, 2024
1 parent c9d3f4e commit 1d5f75c
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 25 deletions.
49 changes: 49 additions & 0 deletions src/components/layouts/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Menu,
SegmentedControl,
Select,
Switch,
useMantineColorScheme,
} from '@mantine/core'
import { DatePickerInput } from '@mantine/dates'
Expand All @@ -30,11 +31,17 @@ import { setCookie } from 'cookies-next'
import { TransferDatabaseService } from 'src/repositories/transfers/TransferDatabase'
import {
selectUserCurrency,
selectUserIncludesEth,
selectUserIncludesLevinSwap,
selectUserIncludesRmmV2,
selectUserRentCalculation,
selectVersion,
} from 'src/store/features/settings/settingsSelector'
import {
userCurrencyChanged,
userIncludesEthChanged,
userIncludesLevinSwapChanged,
userIncludesRmmV2Changed,
userRentCalculationChanged,
} from 'src/store/features/settings/settingsSlice'
import { Currency } from 'src/types/Currencies'
Expand Down Expand Up @@ -230,6 +237,47 @@ const CurrencySelect: FC = () => {
)
}

const FetchDataSettings: FC = () => {
const { t } = useTranslation('common', { keyPrefix: 'settings' })
const dispatch = useDispatch()

const userIncludesEth = useSelector(selectUserIncludesEth)
const userIncludesLevinSwap = useSelector(selectUserIncludesLevinSwap)
const userIncludesRmmV2 = useSelector(selectUserIncludesRmmV2)

const setUserIncludesEth = (value: boolean) =>
dispatch(userIncludesEthChanged(value))
const setUserIncludesLevinSwap = (value: boolean) =>
dispatch(userIncludesLevinSwapChanged(value))
const setUserIncludesRmmV2 = (value: boolean) =>
dispatch(userIncludesRmmV2Changed(value))

return (
<>
<Switch
checked={userIncludesEth}
onChange={(event) => setUserIncludesEth(event.currentTarget.checked)}
label={t('includesEth')}
style={{ margin: '4px 8px' }}
/>
<Switch
checked={userIncludesLevinSwap}
onChange={(event) =>
setUserIncludesLevinSwap(event.currentTarget.checked)
}
label={t('includesLevinSwap')}
style={{ margin: '4px 8px' }}
/>
<Switch
checked={userIncludesRmmV2}
onChange={(event) => setUserIncludesRmmV2(event.currentTarget.checked)}
label={t('includesRmmV2')}
style={{ margin: '4px 8px' }}
/>
</>
)
}

const RefreshDataButton: FC = () => {
const { t } = useTranslation('common', { keyPrefix: 'settings' })
const [loading, setLoading] = useState(false)
Expand Down Expand Up @@ -283,6 +331,7 @@ export const SettingsMenu: FC = () => {
<RealtimeRentMenu />
<ColorSchemeMenuItem />
<Menu.Divider />
<FetchDataSettings />
<RefreshDataButton />
<Menu.Divider />
<Box
Expand Down
5 changes: 4 additions & 1 deletion src/i18next/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"realtime": "Realtime",
"global": "Global",
"date": "Date",
"dateFormat": "MMMM DD, YYYY"
"dateFormat": "MMMM DD, YYYY",
"includesEth": "Includes Ethereum",
"includesLevinSwap": "Includes LevinSwap",
"includesRmmV2": "Includes RMM V2"
},
"walletButton": {
"connectWallet": "Connect wallet",
Expand Down
5 changes: 4 additions & 1 deletion src/i18next/locales/fr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"realtime": "Temps réel",
"global": "Global",
"date": "Date",
"dateFormat": "DD MMMM YYYY"
"dateFormat": "DD MMMM YYYY",
"includesEth": "Inclure Ethereum",
"includesLevinSwap": "Inclure LevinSwap",
"includesRmmV2": "Inclure RMM V2"
},
"walletButton": {
"connectWallet": "Connecter mon portefeuille",
Expand Down
7 changes: 5 additions & 2 deletions src/repositories/rmm.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import { GnosisRpcProvider } from './RpcProvider'
import { RmmPosition, getRmmPositions } from './subgraphs/queries/rmm.queries'

export const RmmRepository = {
async getPositions(addressList: string[]) {
const result = await getRmmPositions(addressList)
async getPositions(
addressList: string[],
options?: { includesRmmV2?: boolean },
) {
const result = await getRmmPositions(addressList, options)
const merged = mergeWalletsPositions(result)
const stableRMM3 = await Promise.all(
addressList.map(getBalanceOfStableRMM3),
Expand Down
4 changes: 4 additions & 0 deletions src/repositories/subgraphs/queries/levinswap.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { LevinSwapClient } from '../clients'
export async function getLevinSwapBalances(
addressList: string[],
realtokens: RealToken[],
options: { includesLevinSwap?: boolean } = {},
) {
if (!options.includesLevinSwap) {
return []
}
const result = await executeQuery(
addressList.map((item) => item.toLowerCase()),
)
Expand Down
18 changes: 12 additions & 6 deletions src/repositories/subgraphs/queries/realtoken.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { useCacheWithLocalStorage } from 'src/utils/useCache'

import { EthereumClient, GnosisClient } from '../clients'

export async function getRealtokenBalances(addressList: string[]) {
export async function getRealtokenBalances(
addressList: string[],
options: { includesEth?: boolean } = {},
) {
const [gnosisResult, ethereumResult] = await executeQuery(
addressList.map((item) => item.toLowerCase()),
options,
)

return {
Expand All @@ -16,16 +20,18 @@ export async function getRealtokenBalances(addressList: string[]) {
}

const executeQuery = useCacheWithLocalStorage(
async (addressList: string[]) =>
async (addressList: string[], options: { includesEth?: boolean } = {}) =>
Promise.all([
GnosisClient().query<RealtokenResult>({
query: RealtokenQuery,
variables: { addressList },
}),
EthereumClient().query<RealtokenResult>({
query: RealtokenQuery,
variables: { addressList },
}),
options.includesEth
? EthereumClient().query<RealtokenResult>({
query: RealtokenQuery,
variables: { addressList },
})
: Promise.resolve({ data: { accounts: [] } }),
]),
{
duration: 1000 * 60 * 10, // 10 minutes
Expand Down
24 changes: 15 additions & 9 deletions src/repositories/subgraphs/queries/rmm.queries.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import { gql } from '@apollo/client'

import _keyBy from 'lodash/keyBy'

import { useCacheWithLocalStorage } from 'src/utils/useCache'

import { RMM2Client, RMM3Client, RMM3WrapperClient } from '../clients'

const WRAPPER_ADDRESS = '0xd3dff217818b4f33eb38a243158fbed2bbb029d3'
import { RMM2Client, RMM3WrapperClient } from '../clients'

export async function getRmmBalances(addressList: string[]) {
export async function getRmmBalances(
addressList: string[],
options: { includesRmmV2?: boolean } = {},
) {
const addresses = addressList.map((item) => item.toLowerCase())
const [resultRMM2, resultRMM3] = await Promise.all([
executeRMM2Query(addresses),
options.includesRmmV2
? executeRMM2Query(addresses)
: Promise.resolve({ data: { users: [] } }),
executeRMM3Query(addresses),
])
return formatBalances([...resultRMM2.data.users, ...resultRMM3.data.users])
}

export async function getRmmPositions(addressList: string[]) {
export async function getRmmPositions(
addressList: string[],
options: { includesRmmV2?: boolean } = {},
) {
const addresses = addressList.map((item) => item.toLowerCase())
const [resultRMM2, resultRMM3] = await Promise.all([
executeRMM2Query(addresses),
options.includesRmmV2
? executeRMM2Query(addresses)
: Promise.resolve({ data: { users: [] } }),
executeRMM3Query(addresses),
])
return formatPositions([...resultRMM2.data.users, ...resultRMM3.data.users])
Expand Down
21 changes: 17 additions & 4 deletions src/repositories/wallets.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ export const WalletsRepository = {
getBalances: async (
addressList: string[],
realtokens: RealToken[],
): Promise<WalletBalances> => getWalletsBalances(addressList, realtokens),
options: {
includesEth?: boolean
includesLevinSwap?: boolean
includesRmmV2?: boolean
} = {},
): Promise<WalletBalances> =>
getWalletsBalances(addressList, realtokens, options),
}

async function getWalletsBalances(
addressList: string[],
realtokens: RealToken[],
options: {
includesEth?: boolean
includesLevinSwap?: boolean
includesRmmV2?: boolean
} = {},
) {
const [realtokenBalances, rmmBalances, levinSwapBalances] = await Promise.all(
[
getRealtokenBalances(addressList),
getRmmBalances(addressList),
getLevinSwapBalances(addressList, realtokens),
getRealtokenBalances(addressList, { includesEth: options.includesEth }),
getRmmBalances(addressList, { includesRmmV2: options.includesRmmV2 }),
getLevinSwapBalances(addressList, realtokens, {
includesLevinSwap: options.includesLevinSwap,
}),
],
)

Expand Down
15 changes: 15 additions & 0 deletions src/store/features/settings/settingsSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ export const selectVersion = createSelector(
(state: RootState) => state.settings,
(state) => state.version,
)

export const selectUserIncludesEth = createSelector(
(state: RootState) => state.settings,
(state) => state.includesEth,
)

export const selectUserIncludesLevinSwap = createSelector(
(state: RootState) => state.settings,
(state) => state.includesLevinSwap,
)

export const selectUserIncludesRmmV2 = createSelector(
(state: RootState) => state.settings,
(state) => state.includesRmmV2,
)
57 changes: 57 additions & 0 deletions src/store/features/settings/settingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
const USER_LS_KEY = 'store:settings/user'
const USER_CURRENCY_LS_KEY = 'store:settings/userCurrency'
const USER_RENT_CALCULATION_LS_KEY = 'store:settings/userRentCalculation'
const USER_INCLUDES_ETH_LS_KEY = 'store:settings/includesEth'
const USER_INCLUDES_LEVIN_SWAP_LS_KEY = 'store:settings/includesLevinSwap'
const USER_INCLUDES_RMM_V2_LS_KEY = 'store:settings/includesRmmV2'

export interface User {
id: string
Expand All @@ -30,6 +33,9 @@ interface SettingsInitialStateType {
userCurrency: Currency
isInitialized: boolean
rentCalculation: RentCalculation
includesEth: boolean
includesLevinSwap: boolean
includesRmmV2: boolean
version?: string
}

Expand All @@ -41,6 +47,9 @@ const settingsInitialState: SettingsInitialStateType = {
date: new Date().getTime(),
},
isInitialized: false,
includesEth: false,
includesLevinSwap: false,
includesRmmV2: false,
}

// DISPATCH TYPE
Expand All @@ -49,6 +58,11 @@ export const userChangedDispatchType = 'settings/userChanged'
export const userCurrencyChangedDispatchType = 'settings/userCurrencyChanged'
export const userRentCalculationChangedDispatchType =
'settings/userRentCalculationChanged'
export const userIncludesEthChangedDispatchType = 'settings/includesEthChanged'
export const userIncludesLevinSwapChangedDispatchType =
'settings/includesLevinSwapChanged'
export const userIncludesRmmV2ChangedDispatchType =
'settings/includesRmmV2Changed'

// ACTIONS
export const initializeSettings = createAction(initializeSettingsDispatchType)
Expand All @@ -65,6 +79,15 @@ export const userRentCalculationChanged = createAction(
},
}),
)
export const userIncludesEthChanged = createAction<boolean>(
userIncludesEthChangedDispatchType,
)
export const userIncludesLevinSwapChanged = createAction<boolean>(
userIncludesLevinSwapChangedDispatchType,
)
export const userIncludesRmmV2Changed = createAction<boolean>(
userIncludesRmmV2ChangedDispatchType,
)

// THUNKS
export function setUserAddress(address: string) {
Expand Down Expand Up @@ -169,12 +192,41 @@ export const settingsReducers = createReducer(
state.rentCalculation = action.payload
localStorage.setItem(USER_RENT_CALCULATION_LS_KEY, action.payload.state)
})
.addCase(userIncludesEthChanged, (state, action) => {
state.includesEth = action.payload
localStorage.setItem(
USER_INCLUDES_ETH_LS_KEY,
action.payload.toString(),
)
})
.addCase(userIncludesLevinSwapChanged, (state, action) => {
state.includesLevinSwap = action.payload
localStorage.setItem(
USER_INCLUDES_LEVIN_SWAP_LS_KEY,
action.payload.toString(),
)
})
.addCase(userIncludesRmmV2Changed, (state, action) => {
state.includesRmmV2 = action.payload
localStorage.setItem(
USER_INCLUDES_RMM_V2_LS_KEY,
action.payload.toString(),
)
})
.addCase(initializeSettings, (state) => {
const user = localStorage.getItem(USER_LS_KEY)
const userCurrency = localStorage.getItem(USER_CURRENCY_LS_KEY)
const userRentCalculation = localStorage.getItem(
USER_RENT_CALCULATION_LS_KEY,
)
const userIncludesEth = localStorage.getItem(USER_INCLUDES_ETH_LS_KEY)
const userIncludesLevinSwap = localStorage.getItem(
USER_INCLUDES_LEVIN_SWAP_LS_KEY,
)
const userIncludesRmmV2 = localStorage.getItem(
USER_INCLUDES_RMM_V2_LS_KEY,
)

state.user = user ? JSON.parse(user) : undefined
state.userCurrency = userCurrency
? (userCurrency as Currency)
Expand All @@ -188,6 +240,11 @@ export const settingsReducers = createReducer(
state: RentCalculationState.Global,
date: new Date().getTime(),
}

state.includesEth = userIncludesEth === 'true'
state.includesLevinSwap = userIncludesLevinSwap === 'true'
state.includesRmmV2 = userIncludesRmmV2 === 'true'

const { publicRuntimeConfig } = getConfig() as {
publicRuntimeConfig?: { version: string }
}
Expand Down
Loading

0 comments on commit 1d5f75c

Please sign in to comment.