Skip to content

Commit

Permalink
use pot free balance
Browse files Browse the repository at this point in the history
  • Loading branch information
vkulinich-cl committed Jun 6, 2024
1 parent e90c21f commit b3c08c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 75 deletions.
19 changes: 18 additions & 1 deletion src/api/accountBalances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AccountId32 } from "@polkadot/types/interfaces"
import { NATIVE_ASSET_ID, useApiPromise } from "utils/api"
import { useQuery } from "@tanstack/react-query"
import { useQueries, useQuery } from "@tanstack/react-query"
import { QUERY_KEYS } from "utils/queryKeys"
import { ApiPromise } from "@polkadot/api"
import { Maybe, undefinedNoop } from "utils/helpers"
Expand All @@ -20,6 +20,23 @@ export const useAccountBalances = (id: Maybe<AccountId32 | string>) => {
)
}

export const useAccountsBalances = (addresses: string[]) => {
const { api } = useApiPromise()

return useQueries({
queries: addresses.map((address) => ({
queryKey: QUERY_KEYS.accountBalances(address),
queryFn: async () => {
const data = await getAccountBalances(api, address)()

return {...data, address}
},
enabled: !!address,
})),
})

}

const getAccountBalances =
(api: ApiPromise, accountId: AccountId32 | string) => async () => {
const [tokens, native] = await Promise.all([
Expand Down
63 changes: 0 additions & 63 deletions src/api/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { useApiPromise } from "utils/api"
import { getAccountResolver } from "utils/farms/claiming/accountResolver"
import { isNotNil, useQueryReduce } from "utils/helpers"
import { QUERY_KEYS } from "utils/queryKeys"
import { PROVIDERS, useProviderRpcUrlStore } from "./provider"
import { u8aToHex } from "@polkadot/util"
import { decodeAddress } from "@polkadot/util-crypto"
import { BN_0, TREASURY_WALLET } from "utils/constants"
import request, { gql } from "graphql-request"

export const useYieldFarms = (ids: FarmIds[]) => {
const { api } = useApiPromise()
Expand Down Expand Up @@ -244,61 +239,3 @@ export interface FarmIds {
globalFarmId: u32
yieldFarmId: u32
}

export const useFarmPotTransfers = (potAddresses: string[]) => {
const preference = useProviderRpcUrlStore()
const rpcUrl = preference.rpcUrl ?? import.meta.env.VITE_PROVIDER_URL
const selectedProvider = PROVIDERS.find(
(provider) => new URL(provider.url).hostname === new URL(rpcUrl).hostname,
)

const indexerUrl =
selectedProvider?.indexerUrl ?? import.meta.env.VITE_INDEXER_URL

return useQueries({
queries: potAddresses.map((potAddress) => ({
queryKey: QUERY_KEYS.potTransfers(potAddress),
queryFn: async () => {
const transfers = await getTransfers(indexerUrl, potAddress)
const sum = transfers.events.reduce((acc, transfer) => {
if (
transfer.args.to.slice(0, 26) !== transfer.args.from.slice(0, 26)
) {
return acc.plus(transfer.args.amount)
}

return acc
}, BN_0)
return { amount: sum.toString(), potAddress }
},
enabled: !!potAddress,
})),
})
}

const getTransfers = async (indexerUrl: string, address: string) => {
const potAddress = u8aToHex(decodeAddress(address))
const treasuryAddress = u8aToHex(decodeAddress(TREASURY_WALLET))

return {
...(await request<{
events: Array<{ args: { to: string; from: string; amount: string } }>
}>(
indexerUrl,
gql`
query FarmTransfers($potAddress: String!) {
events(
where: {
name_in: ["Balances.Transfer", "Tokens.Transfer"]
args_jsonContains: { to: $potAddress }
}
orderBy: block_height_ASC
) {
args
}
}
`,
{ potAddress, treasuryAddress },
)),
}
}
3 changes: 0 additions & 3 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ export const ORMLVEST = "ormlvest"

// asset type
export const ASSET_TYPE_TOKEN = "Token"

export const TREASURY_WALLET =
"bXj4uMHTyQyvNCLHKBv6ztwkPSx8tgsrxuFtAFfWDYntXtohw"
28 changes: 20 additions & 8 deletions src/utils/farms/apr.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as liquidityMining from "@galacticcouncil/math-liquidity-mining"
import { AccountId32 } from "@polkadot/types/interfaces/runtime"
import { PalletLiquidityMiningLoyaltyCurve } from "@polkadot/types/lookup"
import { useAccountsBalances } from "api/accountBalances"
import { useBestNumber } from "api/chain"
import { useFarmPotTransfers, useFarms } from "api/farms"
import { useFarms } from "api/farms"
import BigNumber from "bignumber.js"
import { secondsInYear } from "date-fns"
import { NATIVE_ASSET_ID } from "utils/api"
import { BLOCK_TIME, BN_0, BN_1, BN_QUINTILL } from "utils/constants"
import { isNotNil, useQueryReduce } from "utils/helpers"

Expand All @@ -20,7 +22,7 @@ export const useAPR = (poolId: AccountId32 | string) => {
poolFarms.data?.map((farm) => farm.globalFarmPotAddress).filter(isNotNil) ??
[]

const potTransfers = useFarmPotTransfers(potAddresses)
const balances = useAccountsBalances(potAddresses)

return useQueryReduce(
[bestNumber, poolFarms] as const,
Expand Down Expand Up @@ -107,13 +109,23 @@ export const useAPR = (poolId: AccountId32 | string) => {
.times(yieldPerPeriod)
.div(maxRewardPerPeriod)

const potMaxBalance = BigNumber(
potTransfers.find(
(potTransfer) =>
potTransfer.data?.potAddress === globalFarmPotAddress,
)?.data?.amount ?? 0,
const rewardCurrency = globalFarm.rewardCurrency.toString()
const accountBalance = balances.find(
(balance) => balance.data?.address === globalFarmPotAddress,
)

const maxReward = accountBalance
? rewardCurrency === NATIVE_ASSET_ID
? accountBalance.data?.native.data.free.toBigNumber()
: accountBalance.data?.balances
.find((balance) => balance.id.toString() === rewardCurrency)
?.data.free.toBigNumber()
: undefined

const potMaxBalance = maxReward
? distributedRewards.plus(maxReward)
: undefined

return {
minApr,
apr,
Expand All @@ -122,7 +134,7 @@ export const useAPR = (poolId: AccountId32 | string) => {
potMaxBalance,
fullness,
estimatedEndBlock: estimatedEndBlock,
assetId: globalFarm.rewardCurrency,
assetId: rewardCurrency,
currentPeriod,
loyaltyCurve,
...farm,
Expand Down

0 comments on commit b3c08c7

Please sign in to comment.