Skip to content

Commit

Permalink
Fix loading apr
Browse files Browse the repository at this point in the history
  • Loading branch information
vkulinich-cl committed Dec 9, 2024
1 parent 0c9c4fc commit d921064
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 59 deletions.
143 changes: 87 additions & 56 deletions src/api/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,51 +114,51 @@ const getActiveFarms =
return await Promise.all(activeFarmIds)
}

const getStoppedFarms = (deposits: TDeposit[], activeFarms: TFarmIds[]) =>
deposits.reduce<TFarmIds[]>((result, deposit) => {
const missingEntries = deposit.data.yieldFarmEntries.filter((entry) => {
const isActive = activeFarms.some(
(activeFarm) =>
activeFarm.poolId === deposit.data.ammPoolId &&
activeFarm.yieldFarmId === entry.yieldFarmId &&
activeFarm.globalFarmId === entry.globalFarmId,
)
return !isActive
})

missingEntries.forEach((entry) => {
const isAlreadyInResult = result.some(
(item) =>
item.yieldFarmId === entry.yieldFarmId &&
item.poolId === deposit.data.ammPoolId &&
item.globalFarmId === entry.globalFarmId,
)

if (!isAlreadyInResult) {
result.push({
yieldFarmId: entry.yieldFarmId,
poolId: deposit.data.ammPoolId,
globalFarmId: entry.globalFarmId,
isActive: false,
})
}
})

return result
}, [])

const getFarmsData =
(
api: ApiPromise,
balanceClient: BalanceClient,
activeFarms: TFarmIds[],
getAsset: (id: string) => TAsset,
deposits: TDeposit[],
isXyk: boolean = false,
) =>
async () => {
const accountResolver = getAccountResolver(api.registry)

const stoppedFarms = deposits.reduce<TFarmIds[]>((result, deposit) => {
const missingEntries = deposit.data.yieldFarmEntries.filter((entry) => {
const isActive = activeFarms.some(
(activeFarm) =>
activeFarm.poolId === deposit.data.ammPoolId &&
activeFarm.yieldFarmId === entry.yieldFarmId &&
activeFarm.globalFarmId === entry.globalFarmId,
)
return !isActive
})

missingEntries.forEach((entry) => {
const isAlreadyInResult = result.some(
(item) =>
item.yieldFarmId === entry.yieldFarmId &&
item.poolId === deposit.data.ammPoolId &&
item.globalFarmId === entry.globalFarmId,
)

if (!isAlreadyInResult) {
result.push({
yieldFarmId: entry.yieldFarmId,
poolId: deposit.data.ammPoolId,
globalFarmId: entry.globalFarmId,
isActive: false,
})
}
})

return result
}, [])

const farmsData = [...activeFarms, ...stoppedFarms].map(async (farm) => {
const farmsData = activeFarms.map(async (farm) => {
const { isActive, globalFarmId, yieldFarmId, poolId } = farm
const yieldFarmRaw = isXyk
? await api.query.xykWarehouseLM.yieldFarm(
Expand Down Expand Up @@ -266,67 +266,98 @@ export const useOmnipoolFarms = (ids: string[]) => {
const { account } = useAccount()
const { api, balanceClient, isLoaded } = useRpcProvider()
const { getAssetWithFallback } = useAssets()
const { data, isSuccess: isAccountAssets } = useAccountAssets()
const { data } = useAccountAssets()

const { omnipoolDeposits } = data ?? {}
const { omnipoolDeposits = [] } = data ?? {}

const { data: activeFarms, isSuccess: isActiveFarms } = useQuery(
QUERY_KEYS.omnipoolActiveFarms,
getActiveFarms(api, ids),
{ enabled: !!ids.length && isLoaded, staleTime: millisecondsInHour },
)

return useQuery(
QUERY_KEYS.omnipoolFarms(account?.address),
activeFarms && omnipoolDeposits
? getFarmsData(
api,
balanceClient,
activeFarms,
getAssetWithFallback,
omnipoolDeposits,
)
const stoppedFarms = useMemo(
() => getStoppedFarms(omnipoolDeposits, activeFarms ?? []),
[activeFarms, omnipoolDeposits],
)

const { data: stoppedFarmsData = [] } = useQuery(
QUERY_KEYS.stoppedOmnipoolFarms(account?.address),
getFarmsData(api, balanceClient, stoppedFarms, getAssetWithFallback),
{
enabled:
isActiveFarms &&
isLoaded &&
!!account?.address &&
!!stoppedFarms.length,
staleTime: millisecondsInHour,
},
)

const { data: activeFarmsData = [], isLoading } = useQuery(
QUERY_KEYS.omnipoolFarms,
activeFarms
? getFarmsData(api, balanceClient, activeFarms, getAssetWithFallback)
: undefinedNoop,
{
enabled: isActiveFarms && isLoaded && isAccountAssets,
select,
enabled: isActiveFarms && isLoaded,
staleTime: millisecondsInHour,
},
)

return { data: select([...activeFarmsData, ...stoppedFarmsData]), isLoading }
}

export const useXYKFarms = (ids: string[]) => {
const { account } = useAccount()
const { api, balanceClient, isLoaded } = useRpcProvider()
const { getAssetWithFallback } = useAssets()
const { data, isSuccess: isAccountAssets } = useAccountAssets()
const { data } = useAccountAssets()

const { xykDeposits } = data ?? {}
const { xykDeposits = [] } = data ?? {}

const { data: activeFarms, isSuccess: isActiveFarms } = useQuery(
QUERY_KEYS.xykActiveFarms,
getActiveFarms(api, ids, true),
{ enabled: !!ids.length && isLoaded, staleTime: millisecondsInHour },
)

return useQuery(
QUERY_KEYS.xykFarms(account?.address),
activeFarms && xykDeposits
const stoppedFarms = useMemo(
() => getStoppedFarms(xykDeposits, activeFarms ?? []),
[activeFarms, xykDeposits],
)

const { data: stoppedFarmsData = [] } = useQuery(
QUERY_KEYS.stoppedXykFarms(account?.address),
getFarmsData(api, balanceClient, stoppedFarms, getAssetWithFallback, true),
{
enabled:
isActiveFarms &&
isLoaded &&
!!account?.address &&
!!stoppedFarms.length,
staleTime: millisecondsInHour,
},
)

const { data: activeFarmsData = [], isLoading } = useQuery(
QUERY_KEYS.xykFarms,
activeFarms
? getFarmsData(
api,
balanceClient,
activeFarms,
getAssetWithFallback,
xykDeposits,
true,
)
: undefinedNoop,
{
enabled: isActiveFarms && isAccountAssets && isLoaded,
select,
enabled: isActiveFarms && isLoaded,
staleTime: millisecondsInHour,
},
)

return { data: select([...activeFarmsData, ...stoppedFarmsData]), isLoading }
}

export const useFarmCurrentPeriod = () => {
Expand Down
12 changes: 9 additions & 3 deletions src/utils/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ export const QUERY_KEYS = {
poolId?.toString(),
],
omnipoolActiveFarms: ["omnipoolActiveFarms"],
omnipoolFarms: (address?: string) => [
omnipoolFarms: [QUERY_KEY_PREFIX, "omnipoolFarms"],
stoppedOmnipoolFarms: (address?: string) => [
QUERY_KEY_PREFIX,
"omnipoolFarms",
"stoppedOmnipoolFarms",
address,
],
xykActiveFarms: ["xykActiveFarms"],
xykFarms: (address?: string) => [QUERY_KEY_PREFIX, "xykFarms", address],
xykFarms: [QUERY_KEY_PREFIX, "xykFarms"],
stoppedXykFarms: (address?: string) => [
QUERY_KEY_PREFIX,
"stoppedXykFarms",
address,
],
totalIssuances: ["totalIssuances"],
reserves: (id: Maybe<string | u32>, address: Maybe<AccountId32 | string>) => [
QUERY_KEY_PREFIX,
Expand Down

0 comments on commit d921064

Please sign in to comment.