Skip to content

Commit

Permalink
Merge pull request #450 from galacticcouncil/rococo
Browse files Browse the repository at this point in the history
Rococo -> production
  • Loading branch information
vkulinich-cl authored Nov 20, 2023
2 parents 9f064e5 + e882daf commit e52c882
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 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
4 changes: 2 additions & 2 deletions src/sections/provider/ProviderSelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function ProviderSelectModal(props: {
const { rpcList, addRpc, removeRpc } = useRpcStore()

const form = useForm<{ address: string }>({
defaultValues: { address: "" },
defaultValues: { address: "wss://" },
mode: "onChange",
})

Expand All @@ -44,7 +44,7 @@ export function ProviderSelectModal(props: {
const relayParentNumber = relay.unwrap().relayParentNumber

if (relayParentNumber.toNumber()) {
addRpc(`wss://${value.address}`)
addRpc(value.address)
form.reset()
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/sections/provider/ProviderSelectModal.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WsProvider } from "@polkadot/rpc-provider"

export const connectWsProvider = (url: string) => {
return new Promise<WsProvider>(function (resolve, reject) {
const provider = new WsProvider(`wss://${url}`)
const provider = new WsProvider(url)

provider.on("connected", () => {
resolve(provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SContainer, SInput } from "./ProviderInput.styled"
import { ChangeEvent, ReactNode } from "react"
import { ReactComponent as PlusIcon } from "assets/icons/PlusIcon.svg"
import { Icon } from "components/Icon/Icon"
import { Text } from "components/Typography/Text/Text"
import { SErrorMessage } from "components/AddressInput/AddressInput.styled"
import { useTranslation } from "react-i18next"

Expand All @@ -27,9 +26,6 @@ export const ProviderInput = ({
<>
<SContainer error={!!error}>
<Icon sx={{ color: "white", opacity: 0.4 }} icon={<PlusIcon />} />
<Text color="white" sx={{ opacity: 0.4 }} fs={12}>
wss://
</Text>
<SInput
name={name}
value={value}
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) => [
"inactiveYieldFarms",
poolId.toString(),
],
globalFarm: (id: u32) => [QUERY_KEY_PREFIX, "globalFarm", id.toString()],
yieldFarm: (ids: {
poolId: AccountId32 | string
Expand Down

0 comments on commit e52c882

Please sign in to comment.