Skip to content

Commit

Permalink
Claim inactive farms
Browse files Browse the repository at this point in the history
  • Loading branch information
vkulinich-cl committed Nov 16, 2023
1 parent d8d2cea commit a0fb1dc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
81 changes: 81 additions & 0 deletions src/api/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export const useGlobalFarm = (id: u32) => {
return useQuery(QUERY_KEYS.globalFarm(id), getGlobalFarm(api, id))
}

export const useInactiveYieldFarms = (poolIds: (AccountId32 | string)[]) => {
const api = useApiPromise()
return useQueries({
queries: poolIds.map((poolId) => ({
queryKey: QUERY_KEYS.inactiveYieldFarms(poolId),
queryFn: getInctiveYieldFarms(api, poolId),
})),
})
}

export const useFarms = (poolIds: Array<AccountId32 | string>) => {
const activeYieldFarms = useActiveYieldFarms(poolIds)

Expand Down Expand Up @@ -72,6 +82,35 @@ export const useFarms = (poolIds: Array<AccountId32 | string>) => {
)
}

export const useInactiveFarms = (poolIds: Array<AccountId32 | string>) => {
const activeYieldFarms = useInactiveYieldFarms(poolIds)

const data = activeYieldFarms.reduce(
(acc, farm) => (farm.data ? [...acc, ...farm.data] : acc),
[] as FarmIds[],
)

const globalFarms = useGlobalFarms(data.map((id) => id.globalFarmId))
const yieldFarms = useYieldFarms(data)

return useQueryReduce(
[globalFarms, yieldFarms, ...activeYieldFarms] as const,
(globalFarms, yieldFarms, ...activeYieldFarms) => {
const farms =
activeYieldFarms.flat(2).map((af) => {
const globalFarm = globalFarms?.find((gf) =>
af.globalFarmId.eq(gf.id),
)
const yieldFarm = yieldFarms?.find((yf) => af.yieldFarmId.eq(yf.id))
if (!globalFarm || !yieldFarm) return undefined
return { globalFarm, yieldFarm }
}) ?? []

return farms.filter(isNotNil)
},
)
}

export const getYieldFarms = (api: ApiPromise, ids: FarmIds[]) => async () => {
const reqs = ids.map(({ poolId, globalFarmId, yieldFarmId }) =>
api.query.xykWarehouseLM.yieldFarm(poolId, globalFarmId, yieldFarmId),
Expand Down Expand Up @@ -124,6 +163,48 @@ export const getActiveYieldFarms =
return data
}

export const getInctiveYieldFarms =
(api: ApiPromise, poolId: AccountId32 | string) => async () => {
const allGlobalFarms = await api.query.xykWarehouseLM.globalFarm.entries()
allGlobalFarms.map((globalFarm) => globalFarm[0].keys)

const globalFarmsIds = allGlobalFarms.map(([key]) => {
const [id] = key.args
return id.toString()
})

const globalFarms = await Promise.all(
globalFarmsIds.map((globalFarmId) =>
api.query.xykWarehouseLM.yieldFarm.entries(poolId, globalFarmId),
),
)

const stoppedFarms = globalFarms.reduce<
{
poolId: AccountId32
globalFarmId: u32
yieldFarmId: u32
}[]
>((acc, [globalFarm]) => {
if (globalFarm) {
const yieldFarm = globalFarm[1].unwrap()

const isStopped = yieldFarm.state.isStopped

if (isStopped)
acc.push({
poolId: globalFarm[0].args[0],
globalFarmId: globalFarm[0].args[1],
yieldFarmId: yieldFarm.id,
})
}

return acc
}, [])

return stoppedFarms
}

export const getGlobalFarms = (api: ApiPromise, ids: u32[]) => async () => {
const reqs = ids.map((id) => api.query.xykWarehouseLM.globalFarm(id))
const res = await Promise.all(reqs)
Expand Down
17 changes: 12 additions & 5 deletions src/utils/farms/claiming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useUsdPeggedAsset } from "api/asset"
import { useAssetDetailsList } from "api/assetDetails"
import { useBestNumber } from "api/chain"
import { DepositNftType } from "api/deposits"
import { useFarms } from "api/farms"
import { useFarms, useInactiveFarms } from "api/farms"
import { usePools } from "api/pools"
import { useSpotPrices } from "api/spotPrice"
import BigNumber from "bignumber.js"
Expand Down Expand Up @@ -46,21 +46,28 @@ export const useClaimableAmount = (
const farms = useFarms(
pool?.address ? [pool.address] : pools.data?.map((p) => p.address) ?? [],
)

const inactiveFarms = useInactiveFarms(
pool?.address ? [pool.address] : pools.data?.map((p) => p.address) ?? [],
)

const allFarms = [...(farms.data ?? []), ...(inactiveFarms.data ?? [])]

const usd = useUsdPeggedAsset()

const api = useApiPromise()
const accountResolver = getAccountResolver(api.registry)

const assetIds = [
...new Set(farms.data?.map((i) => i.globalFarm.rewardCurrency.toString())),
...new Set(allFarms?.map((i) => i.globalFarm.rewardCurrency.toString())),
]

const assetList = useAssetDetailsList(assetIds)

const usdSpotPrices = useSpotPrices(assetIds, usd.data?.id)

const accountAddresses =
farms.data
allFarms
?.map(
({ globalFarm }) =>
[
Expand Down Expand Up @@ -99,12 +106,12 @@ export const useClaimableAmount = (
assetList.data ?? [],
)

const { globalFarms, yieldFarms } = createMutableFarmEntries(farms.data ?? [])
const { globalFarms, yieldFarms } = createMutableFarmEntries(allFarms ?? [])

const rewardSum = deposits
?.map((record) =>
record.deposit.yieldFarmEntries.map((farmEntry) => {
const aprEntry = farms.data?.find(
const aprEntry = allFarms?.find(
(i) =>
i.globalFarm.id.eq(farmEntry.globalFarmId) &&
i.yieldFarm.id.eq(farmEntry.yieldFarmId),
Expand Down
4 changes: 4 additions & 0 deletions src/utils/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export const QUERY_KEYS = {
"activeYieldFarms",
poolId.toString(),
],
inactiveYieldFarms: (poolId: AccountId32 | string) => [
"activeYieldFarms",
poolId.toString(),
],
globalFarm: (id: u32) => [QUERY_KEY_PREFIX, "globalFarm", id.toString()],
yieldFarm: (ids: {
poolId: AccountId32 | string
Expand Down

0 comments on commit a0fb1dc

Please sign in to comment.