Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Call update cake apr once instead of per farm when initial load #10774

Open
wants to merge 44 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9f9ca34
perf: Call update cake apr once instead of per farm when initial load
memoyil Oct 2, 2024
a56ec31
fix: Caches and remove poolinfo from masterchef cache
memoyil Oct 2, 2024
c4ce34d
perf: Use string key
memoyil Oct 2, 2024
56178d4
perf: Use string instead of function for query key
memoyil Oct 3, 2024
a7fa9d0
fix: Inline functions and avoid fetching pools again when cakeapr und…
memoyil Oct 3, 2024
faa4566
chore: Remove async and braces
memoyil Oct 3, 2024
131f8ba
perf: Dont fetch masterchef sanity check if there is no valid pool
memoyil Oct 3, 2024
73b124c
chore: Remove unused unset cache
memoyil Oct 3, 2024
60ebb10
perf: Use native assign
memoyil Oct 4, 2024
2edf749
fix: Make v2 calls retryable
memoyil Oct 12, 2024
b742f79
chore: Use getbalance
memoyil Oct 12, 2024
cda7690
perf: Use hash
memoyil Oct 16, 2024
391788e
perf: Make reduce efficent
memoyil Oct 16, 2024
eedc06c
perf: Make reduce efficent
memoyil Oct 16, 2024
8c4031d
perf: Make account fetcher reduce more efficent
memoyil Oct 16, 2024
e06b51b
perf: Make fetchers reduce efficient
memoyil Oct 16, 2024
19e99a0
perf: Make chain reduce efficent
memoyil Oct 16, 2024
5777621
chore: Upgrade jotai
memoyil Oct 16, 2024
9990847
fix: Build
memoyil Oct 16, 2024
1c5f188
fix: Recursive update
memoyil Oct 16, 2024
7d31a19
chore: Remove redundant getter
memoyil Oct 16, 2024
7916136
chore: Comment out update
memoyil Oct 16, 2024
7e4c57b
fix: Atom update
memoyil Oct 17, 2024
3887444
fix: Atom update
memoyil Oct 17, 2024
f152b8a
fix: Give precedence to poolapr
memoyil Oct 17, 2024
6ea516e
fix: Add Check
memoyil Oct 17, 2024
2ad4474
fix: Remove condition
memoyil Oct 17, 2024
dbb9eb5
fix: Add back condition
memoyil Oct 17, 2024
49b8e85
chore: Disable data fetch
memoyil Oct 17, 2024
ee899d4
chore: Use entries
memoyil Oct 17, 2024
0f31c80
fix: Build
memoyil Oct 17, 2024
12acd1e
fix: Wait
memoyil Oct 17, 2024
ae18cd2
chore: Remove query
memoyil Oct 17, 2024
2ba37a0
chore: Upgrade nextjs
memoyil Oct 17, 2024
62117f5
chore: Remove farm fetch
memoyil Oct 17, 2024
d5dbc2a
fix: Use query
memoyil Oct 17, 2024
12d5350
chore: Upgrade tanstack query
memoyil Oct 17, 2024
322ea02
chore: Experiment mock
memoyil Oct 17, 2024
9edb4d9
fix: Revert query
memoyil Oct 17, 2024
67cec37
Revert "chore: Upgrade tanstack query"
memoyil Oct 17, 2024
5be0888
fix: Add back fetch
memoyil Oct 17, 2024
082a3d0
fix: mock response
memoyil Oct 17, 2024
84ac021
fix: Mock explorer result
memoyil Oct 17, 2024
412eb61
fix: mock with timeout
memoyil Oct 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/aptos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@vanilla-extract/recipes": "^0.5.0",
"bignumber.js": "^9.0.0",
"dayjs": "^1.11.10",
"jotai": "^2.4.3",
"jotai": "^2.10.1",
"lodash": "^4.17.21",
"next": "catalog:",
"next-seo": "^5.15.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@wagmi/core": "^0.10.11",
"@wormhole-foundation/wormhole-connect": "0.1.7",
"ethers": "^5.0.0",
"jotai": "^2.4.3",
"jotai": "^2.10.1",
"lodash": "^4.17.21",
"next": "catalog:",
"next-themes": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion apps/gamification/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"easymde": "2.16.1",
"graphql": "^16.8.1",
"graphql-request": "5.0.0",
"jotai": "^2.4.3",
"jotai": "^2.10.1",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"next": "catalog:",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"fast-json-stable-stringify": "^2.1.0",
"graphql": "^16.8.1",
"graphql-request": "5.0.0",
"jotai": "^2.4.3",
"jotai": "^2.10.1",
"js-cookie": "^3.0.1",
"lightweight-charts": "^4.0.1",
"local-storage": "^2.0.0",
Expand All @@ -120,7 +120,7 @@
"next-seo": "^5.15.0",
"next-themes": "^0.2.1",
"nuqs": "^1.17.4",
"openapi-fetch": "^0.9.7",
"openapi-fetch": "^0.12.2",
"polished": "^4.2.2",
"qs": "^6.0.0",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/hooks/useUserAcknowledgement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAccount } from 'wagmi'
export function useUserAcknowledgement(id: string) {
const { address } = useAccount()
const atom = useMemo(
() => atomWithStorage(`pcs_user_ack_${id}_${address}`, false, undefined, { unstable_getOnInit: true }),
() => atomWithStorage(`pcs_user_ack_${id}_${address}`, false, undefined, { getOnInit: true }),
[id, address],
)
const [userACK, setUserACK] = useAtom(atom)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/hooks/useZksyncAirDropAutoPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const zksyncAutoPopup = atomWithStorage<{ [account: string]: boolean }>(
'pcs:zksync-airdrop-auto-popup-v2',
{},
undefined,
{ unstable_getOnInit: true },
{ getOnInit: true },
)

const useAutoPopup = (): [boolean, (value: boolean) => void] => {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/pages/usdv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import styled from 'styled-components'

import DisclaimerModal from 'components/DisclaimerModal'

const usdvDisclaimer = atomWithStorage('pcs:usdv-disclaimer-accept', false, undefined, { unstable_getOnInit: true })
const usdvDisclaimer = atomWithStorage('pcs:usdv-disclaimer-accept', false, undefined, { getOnInit: true })

let initialized = false

Expand Down
49 changes: 28 additions & 21 deletions apps/web/src/state/farmsV4/state/accountPositions/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AppState } from 'state'
import { safeGetAddress } from 'utils'
import { publicClient } from 'utils/viem'
import { Address, erc20Abi, zeroAddress } from 'viem'
import { getBalanceNumber } from '@pancakeswap/utils/formatBalance'
import { StablePoolInfo, V2PoolInfo } from '../type'
import { StableLPDetail, V2LPDetail } from './type'

Expand Down Expand Up @@ -136,10 +137,7 @@ export const getAccountV2LpDetails = async (

const validLpTokens = lpTokens.filter((token) => token.chainId === chainId)

const bCakeWrapperAddresses = validReserveTokens.map((tokens) => {
const lpAddress = getV2LiquidityToken(tokens).address
return getBCakeWrapperAddress(lpAddress, chainId)
})
const bCakeWrapperAddresses = validLpTokens.map((token) => getBCakeWrapperAddress(token.address, chainId))

const balanceCalls = validLpTokens.map((token) => {
return {
Expand All @@ -152,15 +150,13 @@ export const getAccountV2LpDetails = async (
const farmingCalls = bCakeWrapperAddresses.reduce(
(acc, address) => {
if (!address || address === '0x') return acc
return [
...acc,
{
abi: v2BCakeWrapperABI,
address,
functionName: 'userInfo',
args: [account] as const,
} as const,
]
acc.push({
abi: v2BCakeWrapperABI,
address,
functionName: 'userInfo',
args: [account] as const,
})
return acc
},
[] as Array<{
abi: typeof v2BCakeWrapperABI
Expand Down Expand Up @@ -203,16 +199,26 @@ export const getAccountV2LpDetails = async (
])

const farming = bCakeWrapperAddresses.reduce((acc, address) => {
if (!address || address === '0x') return [...acc, undefined]
const { result } = _farming.shift() ?? { result: undefined }
return [...acc, result]
if (!address || address === '0x') {
acc.push(undefined)
} else {
const { result } = _farming.shift() ?? { result: undefined }
acc.push(result)
}
return acc
}, [] as Array<readonly [bigint, bigint, bigint, bigint, bigint] | undefined>)

return balances
.map((result, index) => {
const { result: _balance = 0n, status } = result
// LP not exist
if (status === 'failure') return undefined
if (status === 'failure') {
if (reserves[index].status === 'failure' && totalSupplies[index].status === 'failure') {
return undefined
}
throw new Error(
`Mismatch between balances, reserves, and supplies: Token: ${validLpTokens[index].address} Balance (${status}), Reserve (${reserves[index].status}), Supply (${totalSupplies[index].status})`,
)
}

const nativeBalance = CurrencyAmount.fromRawAmount(validLpTokens[index], _balance)
const farmingInfo = farming[index]
Expand All @@ -221,7 +227,7 @@ export const getAccountV2LpDetails = async (
let farmingBoostedAmount = CurrencyAmount.fromRawAmount(validLpTokens[index], '0')
if (farmingInfo) {
farmingBalance = CurrencyAmount.fromRawAmount(validLpTokens[index], farmingInfo[0].toString())
farmingBoosterMultiplier = new BigNumber(Number(farmingInfo[2])).div(1000000000000).toNumber()
farmingBoosterMultiplier = getBalanceNumber(new BigNumber(Number(farmingInfo[2])), 12)
farmingBoostedAmount = CurrencyAmount.fromRawAmount(validLpTokens[index], farmingInfo[3].toString())
}
const tokens = validReserveTokens[index]
Expand Down Expand Up @@ -256,7 +262,7 @@ export const getAccountV2LpDetails = async (
protocol: Protocol.V2,
}
})
.filter((r) => typeof r !== 'undefined') as V2LPDetail[]
.filter(Boolean) as V2LPDetail[]
}

export const getStablePairDetails = async (
Expand All @@ -270,7 +276,8 @@ export const getStablePairDetails = async (
if (!account || !client || !validStablePairs.length) return []

const bCakeWrapperAddresses = validStablePairs.reduce((acc, pair) => {
return [...acc, getBCakeWrapperAddress(pair.lpAddress, chainId)]
acc.push(getBCakeWrapperAddress(pair.lpAddress, chainId))
return acc
}, [] as Array<Address>)

const balanceCalls = validStablePairs.map((pair) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const useAccountV2LpDetails = (chainIds: number[], account?: Address | nu
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
refetchInterval: false,
// Prevents re-fetching while the data is still fresh
staleTime: SLOW_INTERVAL,
} satisfies UseQueryOptions<V2LPDetail[]>
Expand Down
9 changes: 6 additions & 3 deletions apps/web/src/state/farmsV4/state/extendPools/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ export const DEFAULT_QUERIES = {

export const extendPoolsQueryAtom = atom<ExtendPoolsQuery>(DEFAULT_QUERIES)

export const extendPoolsAtom = atom([] as PoolInfo[], (get, set, values: PoolInfo[]) => {
// remove duplicates pools with farmPoolsAtom
export const extendPoolsAtom = atom<PoolInfo[]>([])

export const updateExtendPoolsAtom = atom(null, (get, set, values: PoolInfo[]) => {
const farms = get(farmPoolsAtom)
const currentPools = get(extendPoolsAtom)

const newData = values.filter(
(pool) => !farms.some((farm) => isAddressEqual(farm.lpAddress, pool.lpAddress) && farm.protocol === pool.protocol),
)

set(extendPoolsAtom, [...get(extendPoolsAtom), ...newData])
set(extendPoolsAtom, [...currentPools, ...newData])
})

interface PoolsOfPositionType {
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/state/farmsV4/state/extendPools/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Token } from '@pancakeswap/swap-sdk-core'
import { computePoolAddress, DEPLOYER_ADDRESSES, FeeAmount } from '@pancakeswap/v3-sdk'
import { useQuery } from '@tanstack/react-query'
import { useAtom } from 'jotai'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
import memoize from 'lodash/memoize'
import { useCallback, useState } from 'react'
import { Address } from 'viem/accounts'
import type { PoolInfo } from '../type'
import { DEFAULT_QUERIES, extendPoolsAtom, ExtendPoolsQuery, extendPoolsQueryAtom } from './atom'
import { DEFAULT_QUERIES, extendPoolsAtom, ExtendPoolsQuery, extendPoolsQueryAtom, updateExtendPoolsAtom } from './atom'
import { fetchExplorerPoolInfo, fetchExplorerPoolsList } from './fetcher'

const RESET_QUERY_KEYS = ['protocols', 'orderBy', 'chains', 'pools', 'tokens'] as Array<keyof ExtendPoolsQuery>

export const useExtendPools = () => {
const [query, _setQuery] = useAtom(extendPoolsQueryAtom)
const [extendPools, updateExtendPools] = useAtom(extendPoolsAtom)
const updateExtendPools = useSetAtom(updateExtendPoolsAtom)
const extendPools = useAtomValue(extendPoolsAtom)
const [pageEnd, setPageEnd] = useState(false)
const fetchPoolList = useCallback(
async (newQuery: Partial<ExtendPoolsQuery>) => {
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/state/farmsV4/state/farmPools/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,20 @@ import { atom } from 'jotai'
import { PoolInfo } from '../type'

export const farmPoolsAtom = atom<PoolInfo[]>([])

export const updateFarmPoolAtom = atom(null, (get, set, newPool: PoolInfo) => {
const pools = get(farmPoolsAtom)
const updatedPools = pools.map((pool) => {
const isMatchingPool = newPool.stableSwapAddress
? pool.chainId === newPool.chainId &&
pool.lpAddress === newPool.lpAddress &&
pool.stableSwapAddress === newPool.stableSwapAddress
: pool.chainId === newPool.chainId && pool.lpAddress === newPool.lpAddress
if (isMatchingPool) {
return newPool
}
return pool
})

set(farmPoolsAtom, updatedPools)
})
Loading
Loading