Skip to content

Commit

Permalink
Merge pull request #27 from dhedge/feat/add-use-pools-dynamic-hook
Browse files Browse the repository at this point in the history
Feat/add use pools dynamic hook
  • Loading branch information
artsiomYavorski authored Feb 27, 2024
2 parents 7789065 + 8deebec commit c68dc19
Show file tree
Hide file tree
Showing 61 changed files with 1,613 additions and 465 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lodash.uniqby": "^4.7.0"
},
"devDependencies": {
"@date-fns/utc": "^1.1.1",
"@headlessui/react": "^1.7.18",
"@heroicons/react": "^2.1.1",
"@nx/devkit": "^17.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-ui-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dhedge/core-ui-kit",
"version": "2.0.8",
"version": "2.0.9",
"description": "Core UI Kit",
"type": "module",
"main": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/core-ui-kit/src/hooks/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { usePoolFees } from './use-pool-fees'
export { useSynthetixV3AssetBalance } from './synthetixV3/use-synthetix-v3-asset-balance'
export { useTotalFundValueMutable } from './synthetixV3/use-total-funds-value-mutable'
export { useInvalidatePoolContractData } from './use-invalidate-pool-contract-data'
export { usePoolsDynamic } from './multicall'
2 changes: 1 addition & 1 deletion packages/core-ui-kit/src/hooks/pool/multicall/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { usePoolStatic } from './use-pool.static'
export { usePoolDynamic } from './use-pool.dynamic'
export { usePoolManagerStatic } from './use-pool-manager.static'
export { usePoolManagerDynamic } from './use-pool-manager.dynamic'
export { usePoolsDynamic } from './use-pools.dynamic'
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
import { PoolManagerLogicAbi } from 'abi'
import { PoolLogicAbi, PoolManagerLogicAbi } from 'abi'
import { AddressZero } from 'const'
import { useManagerLogicAddress } from 'hooks/pool/use-manager-logic-address'
import { useContractReadErrorLogging, useReadContracts } from 'hooks/web3'
import type { MulticallReturnType, PoolContractCallParams } from 'types'
import {
useAccount,
useContractReadErrorLogging,
useReadContracts,
} from 'hooks/web3'
import type {
Address,
MulticallReturnType,
PoolContractCallParams,
} from 'types'
import { isZeroAddress } from 'utils'

const getContracts = ({ address, chainId }: PoolContractCallParams) =>
type GetContractsParams = PoolContractCallParams & {
managerLogicAddress: Address
account: Address
}

const getContracts = ({
address,
chainId,
managerLogicAddress,
account,
}: GetContractsParams) =>
[
{
address,
address: managerLogicAddress,
abi: PoolManagerLogicAbi,
functionName: 'getFundComposition',
chainId,
},
{
address,
abi: PoolLogicAbi,
functionName: 'getExitRemainingCooldown',
chainId,
args: [account],
},
] as const

type Data = MulticallReturnType<ReturnType<typeof getContracts>>

const selector = (data: Data) => ({
getFundComposition: data[0].result,
getExitRemainingCooldown: data[1].result,
})

export const usePoolManagerDynamic = ({
address,
chainId,
}: PoolContractCallParams) => {
const { account = AddressZero } = useAccount()
const managerLogicAddress = useManagerLogicAddress({ address, chainId })

const result = useReadContracts({
contracts: getContracts({
address: managerLogicAddress ?? AddressZero,
address,
chainId,
managerLogicAddress: managerLogicAddress ?? AddressZero,
account,
}),
query: {
enabled: !!managerLogicAddress && !isZeroAddress(managerLogicAddress),
Expand Down
75 changes: 0 additions & 75 deletions packages/core-ui-kit/src/hooks/pool/multicall/use-pool.dynamic.ts

This file was deleted.

98 changes: 98 additions & 0 deletions packages/core-ui-kit/src/hooks/pool/multicall/use-pools.dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import chunk from 'lodash.chunk'

import { PoolLogicAbi } from 'abi'
import { AddressZero, DEFAULT_CHAIN_ID } from 'const'
import { useTradingPanelPoolConfigs } from 'hooks/state'
import {
useAccount,
useContractReadErrorLogging,
useReadContracts,
} from 'hooks/web3'
import type {
Address,
ContractFunctionReturnType,
DynamicPoolContractData,
PoolContractAccountCallParams,
} from 'types'

const getPoolContracts = ({
account,
chainId,
address,
}: PoolContractAccountCallParams) =>
[
{
address,
abi: PoolLogicAbi,
functionName: 'balanceOf',
chainId,
args: [account ?? AddressZero],
},
{
address,
abi: PoolLogicAbi,
functionName: 'tokenPrice',
chainId,
args: [],
},
{
address,
abi: PoolLogicAbi,
functionName: 'getFundSummary',
chainId,
},
] as const

type GetFundSummary = ContractFunctionReturnType<
typeof PoolLogicAbi,
'view',
'getFundSummary'
>

type PoolsMap = Record<Address, DynamicPoolContractData>

const getContracts = (pools: PoolContractAccountCallParams[]) =>
pools.flatMap(getPoolContracts)

const POOL_CHUNK_SIZE = getPoolContracts({
account: AddressZero,
chainId: DEFAULT_CHAIN_ID,
address: AddressZero,
}).length

export const usePoolsDynamic = () => {
const { account = AddressZero } = useAccount()
const pools = useTradingPanelPoolConfigs()

const result = useReadContracts({
contracts: getContracts(pools.map((pool) => ({ ...pool, account }))),
query: {
select: (data) =>
chunk(data, POOL_CHUNK_SIZE).reduce<PoolsMap>(
(acc, [balanceOf, tokenPrice, getFundSummary], index) => {
const poolAddress = pools?.[index]?.address ?? AddressZero
const summary = getFundSummary?.result as GetFundSummary

return {
...acc,
[poolAddress]: {
userBalance: balanceOf?.result?.toString(),
tokenPrice: tokenPrice?.result?.toString(),
totalValue: summary?.totalFundValue?.toString(),
totalSupply: summary?.totalSupply?.toString(),
isPrivateVault: summary?.privatePool,
performanceFee: summary?.performanceFeeNumerator?.toString(),
streamingFee: summary?.managerFeeNumerator?.toString(),
entryFee: summary?.entryFeeNumerator?.toString(),
},
}
},
{},
),
},
})

useContractReadErrorLogging({ error: result.error, status: result.status })

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ describe('usePoolTokenPriceMutable', () => {
chainId,
disabled: false,
})
expect(result.current).toEqual(1000000000000000000n)
expect(result.current).toEqual('1000000000000000000')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const usePoolTokenPriceMutable = ({
address,
chainId,
disabled,
}: PoolTokenPriceParams): bigint | undefined => {
}: PoolTokenPriceParams): string | undefined => {
const { data: [poolManagerLogic, totalSupply] = [] } = useReadContracts({
contracts: [
{
Expand Down Expand Up @@ -62,11 +62,9 @@ export const usePoolTokenPriceMutable = ({
: null

return totalFundValueMutable && totalSupplyWithManagerFee
? BigInt(
new BigNumber(totalFundValueMutable)
.dividedBy(totalSupplyWithManagerFee.toFixed())
.shiftedBy(DEFAULT_PRECISION)
.toFixed(0),
)
? new BigNumber(totalFundValueMutable)
.dividedBy(totalSupplyWithManagerFee.toFixed())
.shiftedBy(DEFAULT_PRECISION)
.toFixed(0)
: undefined
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { usePoolDynamic, usePoolManagerDynamic } from './multicall'
import { usePoolManagerDynamic } from './multicall'
import { useTradingPanelPoolConfig } from '../state'
import { useInvalidateOnBlock } from '../web3'

export const useInvalidatePoolContractData = () => {
const { address, chainId } = useTradingPanelPoolConfig()
const { queryKey: poolDynamicKey } = usePoolDynamic({
address,
chainId,
})

const { queryKey: poolManagerDynamicKey } = usePoolManagerDynamic({
address,
chainId,
})

useInvalidateOnBlock({ queryKey: poolDynamicKey })
useInvalidateOnBlock({ queryKey: poolManagerDynamicKey })
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const usePoolCompositionWithFraction = ({
return formatPoolComposition({
composition: poolComposition,
vaultTokensAmount: shiftBy(new BigNumber(vaultTokensAmount || 0)),
totalSupply: totalSupply.toString(),
totalSupply,
})
}, [vaultTokensAmount, poolComposition, totalSupply])
}
Loading

0 comments on commit c68dc19

Please sign in to comment.