diff --git a/.github/workflows/ipfs-deploy.yml b/.github/workflows/ipfs-deploy.yml index d5ff33395..79237d2d1 100644 --- a/.github/workflows/ipfs-deploy.yml +++ b/.github/workflows/ipfs-deploy.yml @@ -44,13 +44,13 @@ jobs: - name: Build all projects env: NODE_OPTIONS: "--max_old_space_size=8192" - run: NEXT_PUBLIC_HOST="ipfs" pnpm run build --filter='!berajs-docs' --filter='!storybook' --filter='!apps/perp' + run: NEXT_PUBLIC_HOST="ipfs" pnpm run build --filter='packages/*' --filter='apps/hub' --filter='apps/honey' - - name: Deploy Berps - run: cd apps/perp && fleek sites deploy + # - name: Deploy Berps + # run: cd apps/perp && fleek sites deploy - - name: Deploy Bend - run: cd apps/lend && fleek sites deploy + # - name: Deploy Bend + # run: cd apps/lend && fleek sites deploy - name: Deploy Hub run: cd apps/hub && fleek sites deploy diff --git a/.github/workflows/ipfs-static-build-test.yml b/.github/workflows/ipfs-static-build-test.yml index 3a85d6e9d..fc79b2255 100644 --- a/.github/workflows/ipfs-static-build-test.yml +++ b/.github/workflows/ipfs-static-build-test.yml @@ -39,6 +39,6 @@ jobs: PERPS_OCT_KEY: ${{ secrets.PERPS_OCT_KEY }} CHARTING_ACCESS_TOKEN: ${{ secrets.CHARTING_ACCESS_TOKEN }} NODE_OPTIONS: "--max_old_space_size=8192" - run: NEXT_PUBLIC_HOST="ipfs" pnpm run build --filter='!berajs-docs' --filter='!storybook' --filter='!apps/perp' + run: NEXT_PUBLIC_HOST="ipfs" pnpm run build --filter='packages/*' --filter='apps/hub' --filter='apps/honey' \ No newline at end of file diff --git a/packages/berajs/src/abi/lendPoolImplementationAbi.js b/packages/berajs/src/abi/bend/lendPoolImplementationAbi.js similarity index 100% rename from packages/berajs/src/abi/lendPoolImplementationAbi.js rename to packages/berajs/src/abi/bend/lendPoolImplementationAbi.js diff --git a/packages/berajs/src/abi/lendRewardHelperAbi.js b/packages/berajs/src/abi/bend/lendRewardHelperAbi.js similarity index 100% rename from packages/berajs/src/abi/lendRewardHelperAbi.js rename to packages/berajs/src/abi/bend/lendRewardHelperAbi.js diff --git a/packages/berajs/src/abi/lendUiDataProviderAbi.js b/packages/berajs/src/abi/bend/lendUiDataProviderAbi.js similarity index 100% rename from packages/berajs/src/abi/lendUiDataProviderAbi.js rename to packages/berajs/src/abi/bend/lendUiDataProviderAbi.js diff --git a/packages/berajs/src/abi/multiswapAbi.js b/packages/berajs/src/abi/bend/multiswapAbi.js similarity index 100% rename from packages/berajs/src/abi/multiswapAbi.js rename to packages/berajs/src/abi/bend/multiswapAbi.js diff --git a/packages/berajs/src/abi/bTokenAbi.ts b/packages/berajs/src/abi/berps/bTokenAbi.ts similarity index 100% rename from packages/berajs/src/abi/bTokenAbi.ts rename to packages/berajs/src/abi/berps/bTokenAbi.ts diff --git a/packages/berajs/src/abi/berpsErrorsAbi.ts b/packages/berajs/src/abi/berps/berpsErrorsAbi.ts similarity index 100% rename from packages/berajs/src/abi/berpsErrorsAbi.ts rename to packages/berajs/src/abi/berps/berpsErrorsAbi.ts diff --git a/packages/berajs/src/abi/borrowingAbi.ts b/packages/berajs/src/abi/berps/borrowingAbi.ts similarity index 100% rename from packages/berajs/src/abi/borrowingAbi.ts rename to packages/berajs/src/abi/berps/borrowingAbi.ts diff --git a/packages/berajs/src/abi/pythAbi.ts b/packages/berajs/src/abi/berps/pythAbi.ts similarity index 100% rename from packages/berajs/src/abi/pythAbi.ts rename to packages/berajs/src/abi/berps/pythAbi.ts diff --git a/packages/berajs/src/abi/pythErrorsAbi.ts b/packages/berajs/src/abi/berps/pythErrorsAbi.ts similarity index 100% rename from packages/berajs/src/abi/pythErrorsAbi.ts rename to packages/berajs/src/abi/berps/pythErrorsAbi.ts diff --git a/packages/berajs/src/abi/referralsAbi.ts b/packages/berajs/src/abi/berps/referralsAbi.ts similarity index 100% rename from packages/berajs/src/abi/referralsAbi.ts rename to packages/berajs/src/abi/berps/referralsAbi.ts diff --git a/packages/berajs/src/abi/tradingAbi.ts b/packages/berajs/src/abi/berps/tradingAbi.ts similarity index 100% rename from packages/berajs/src/abi/tradingAbi.ts rename to packages/berajs/src/abi/berps/tradingAbi.ts diff --git a/packages/berajs/src/abi/balancerVaultAbi.ts b/packages/berajs/src/abi/bex/balancerVaultAbi.ts similarity index 100% rename from packages/berajs/src/abi/balancerVaultAbi.ts rename to packages/berajs/src/abi/bex/balancerVaultAbi.ts diff --git a/packages/berajs/src/abi/wberaAbi.js b/packages/berajs/src/abi/bex/wberaAbi.js similarity index 100% rename from packages/berajs/src/abi/wberaAbi.js rename to packages/berajs/src/abi/bex/wberaAbi.js diff --git a/packages/berajs/src/abi/epochs.ts b/packages/berajs/src/abi/epochs.ts deleted file mode 100755 index 894d2b325..000000000 --- a/packages/berajs/src/abi/epochs.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * @deprecated - */ -export const EPOCHS_PRECOMPILE_ABI = [ - { - inputs: [ - { - internalType: "string", - name: "identifier", - type: "string", - }, - ], - name: "getCurrentEpoch", - outputs: [ - { - internalType: "int64", - name: "epochNumber", - type: "int64", - }, - { - internalType: "int64", - name: "startTimestamp", - type: "int64", - }, - { - internalType: "int64", - name: "endTimestamp", - type: "int64", - }, - ], - stateMutability: "view", - type: "function", - }, -] as const; diff --git a/packages/berajs/src/abi/governance-timelock.ts b/packages/berajs/src/abi/governance/governance-timelock.ts similarity index 100% rename from packages/berajs/src/abi/governance-timelock.ts rename to packages/berajs/src/abi/governance/governance-timelock.ts diff --git a/packages/berajs/src/abi/governance.ts b/packages/berajs/src/abi/governance/governance.ts similarity index 100% rename from packages/berajs/src/abi/governance.ts rename to packages/berajs/src/abi/governance/governance.ts diff --git a/packages/berajs/src/abi/honeyRouterAbi.js b/packages/berajs/src/abi/honey/honeyRouterAbi.js similarity index 100% rename from packages/berajs/src/abi/honeyRouterAbi.js rename to packages/berajs/src/abi/honey/honeyRouterAbi.js diff --git a/packages/berajs/src/abi/index.ts b/packages/berajs/src/abi/index.ts index 3b2783cfe..226e4f46a 100755 --- a/packages/berajs/src/abi/index.ts +++ b/packages/berajs/src/abi/index.ts @@ -1,24 +1,23 @@ -export * from "./staking"; -export * from "./epochs"; -export * from "./bgt"; -export * from "./governance"; -export * from "./governance-timelock"; -export * from "./rewards"; -export * from "./honeyRouterAbi"; -export * from "./multicall3Abi"; -export * from "./wberaAbi"; -export * from "./tradingAbi"; -export * from "./bTokenAbi"; -export * from "./lendPoolImplementationAbi"; -export * from "./lendUiDataProviderAbi"; -export * from "./lendRewardHelperAbi"; -export * from "./multiswapAbi"; -export * from "./referralsAbi"; -export * from "./pythAbi"; -export * from "./vault"; -export * from "./bgtStaker"; -export * from "./borrowingAbi"; -export * from "./berpsErrorsAbi"; -export * from "./pythErrorsAbi"; -export * from "./beraChef"; -export * from "./balancerVaultAbi"; +export * from "./pol/staking"; +export * from "./pol/bgt"; +export * from "./governance/governance"; +export * from "./governance/governance-timelock"; +export * from "./pol/rewards"; +export * from "./honey/honeyRouterAbi"; +export * from "./misc/multicall3Abi"; +export * from "./berps/tradingAbi"; +export * from "./berps/bTokenAbi"; +export * from "./bend/lendPoolImplementationAbi"; +export * from "./bend/lendUiDataProviderAbi"; +export * from "./bend/lendRewardHelperAbi"; +export * from "./bend/multiswapAbi"; +export * from "./berps/referralsAbi"; +export * from "./berps/pythAbi"; +export * from "./pol/reward-vault"; +export * from "./pol/bgtStaker"; +export * from "./berps/borrowingAbi"; +export * from "./berps/berpsErrorsAbi"; +export * from "./berps/pythErrorsAbi"; +export * from "./pol/beraChef"; +export * from "./bex/wberaAbi"; +export * from "./bex/balancerVaultAbi"; diff --git a/packages/berajs/src/abi/multicall3Abi.js b/packages/berajs/src/abi/misc/multicall3Abi.js similarity index 100% rename from packages/berajs/src/abi/multicall3Abi.js rename to packages/berajs/src/abi/misc/multicall3Abi.js diff --git a/packages/berajs/src/abi/beraChef.ts b/packages/berajs/src/abi/pol/beraChef.ts similarity index 100% rename from packages/berajs/src/abi/beraChef.ts rename to packages/berajs/src/abi/pol/beraChef.ts diff --git a/packages/berajs/src/abi/bgt.ts b/packages/berajs/src/abi/pol/bgt.ts similarity index 100% rename from packages/berajs/src/abi/bgt.ts rename to packages/berajs/src/abi/pol/bgt.ts diff --git a/packages/berajs/src/abi/bgtStaker.js b/packages/berajs/src/abi/pol/bgtStaker.ts similarity index 99% rename from packages/berajs/src/abi/bgtStaker.js rename to packages/berajs/src/abi/pol/bgtStaker.ts index da6761a9b..d7329b8f4 100755 --- a/packages/berajs/src/abi/bgtStaker.js +++ b/packages/berajs/src/abi/pol/bgtStaker.ts @@ -456,4 +456,4 @@ export const BGT_STAKER_ABI = [ { type: "error", name: "VaultAlreadyExists", inputs: [] }, { type: "error", name: "WithdrawAmountIsZero", inputs: [] }, { type: "error", name: "ZeroAddress", inputs: [] }, -]; +] as const; diff --git a/packages/berajs/src/abi/vault.ts b/packages/berajs/src/abi/pol/reward-vault.ts similarity index 100% rename from packages/berajs/src/abi/vault.ts rename to packages/berajs/src/abi/pol/reward-vault.ts diff --git a/packages/berajs/src/abi/rewards.js b/packages/berajs/src/abi/pol/rewards.js similarity index 100% rename from packages/berajs/src/abi/rewards.js rename to packages/berajs/src/abi/pol/rewards.js diff --git a/packages/berajs/src/abi/staking.js b/packages/berajs/src/abi/pol/staking.js similarity index 100% rename from packages/berajs/src/abi/staking.js rename to packages/berajs/src/abi/pol/staking.js diff --git a/packages/berajs/src/hooks/index.ts b/packages/berajs/src/hooks/index.ts index 046449677..e9ae773cf 100755 --- a/packages/berajs/src/hooks/index.ts +++ b/packages/berajs/src/hooks/index.ts @@ -1,25 +1,25 @@ -export * from "./useTokenInformation"; +export * from "./tokens/useTokenInformation"; export * from "./useTokens"; export * from "./useChainId"; -export * from "./usePollAllowances"; +export * from "./tokens/usePollAllowances"; export * from "./useContractWrite"; export * from "./transactions"; export * from "./modules"; -export * from "./usePollBalance"; -export * from "./usePollAllowance"; +export * from "./tokens/usePollBalance"; +export * from "./tokens/usePollAllowance"; export * from "./useOct"; -export * from "./usePollBeraBalance"; +export * from "./tokens/usePollBeraBalance"; export * from "./usePollTransactionCount"; export * from "./useGasData"; -export * from "./useTokenHoneyPrice"; -export * from "./useTokenHoneyPrices"; -export * from "./usePollWalletBalances"; +export * from "./tokens/useTokenHoneyPrice"; +export * from "./tokens/useTokenHoneyPrices"; +export * from "./tokens/usePollWalletBalances"; export * from "./usePollEstimateFeesPerGas"; -export * from "./useSubgraphTokenInformation"; -export * from "./useSubgraphTokenInformations"; +export * from "./tokens/useSubgraphTokenInformation"; +export * from "./tokens/useSubgraphTokenInformations"; export * from "./useValidatorList"; export * from "./useGaugesMetadata"; -export * from "./useTpsl"; +export * from "./modules/perps/useTpsl"; export * from "./usePrevious"; export * from "./useInterval"; export * from "./useGetVerifiedAbi"; diff --git a/packages/berajs/src/hooks/useTpsl.ts b/packages/berajs/src/hooks/modules/perps/useTpsl.ts similarity index 99% rename from packages/berajs/src/hooks/useTpsl.ts rename to packages/berajs/src/hooks/modules/perps/useTpsl.ts index e8137a0dd..50092a6b1 100644 --- a/packages/berajs/src/hooks/useTpsl.ts +++ b/packages/berajs/src/hooks/modules/perps/useTpsl.ts @@ -1,6 +1,6 @@ import BigNumber from "bignumber.js"; import { useCallback, useEffect, useState } from "react"; -import { usePrevious } from "./usePrevious"; +import { usePrevious } from "../../usePrevious"; function atLeastZero(value: string | BigNumber): string { return BigNumber(value).lt(0) ? "0" : value.toString(10); diff --git a/packages/berajs/src/hooks/usePollAllowance.ts b/packages/berajs/src/hooks/tokens/usePollAllowance.ts similarity index 100% rename from packages/berajs/src/hooks/usePollAllowance.ts rename to packages/berajs/src/hooks/tokens/usePollAllowance.ts diff --git a/packages/berajs/src/hooks/usePollAllowances.ts b/packages/berajs/src/hooks/tokens/usePollAllowances.ts similarity index 97% rename from packages/berajs/src/hooks/usePollAllowances.ts rename to packages/berajs/src/hooks/tokens/usePollAllowances.ts index 629f14c63..919740695 100755 --- a/packages/berajs/src/hooks/usePollAllowances.ts +++ b/packages/berajs/src/hooks/tokens/usePollAllowances.ts @@ -10,7 +10,7 @@ import { DefaultHookOptions, DefaultHookReturnType, } from "~/types/global"; -import { getAllowances } from "../actions/dex"; +import { getAllowances } from "../../actions/dex"; export type UsePollAllowancesArgs = { spender: string; diff --git a/packages/berajs/src/hooks/tokens/usePollBalance.ts b/packages/berajs/src/hooks/tokens/usePollBalance.ts new file mode 100755 index 000000000..5eabd1005 --- /dev/null +++ b/packages/berajs/src/hooks/tokens/usePollBalance.ts @@ -0,0 +1,99 @@ +import { + gasTokenDecimals, + gasTokenName, + gasTokenSymbol, + nativeTokenAddress, +} from "@bera/config"; +import useSWR from "swr"; +import { Address, erc20Abi, formatEther, formatUnits } from "viem"; +import { usePublicClient } from "wagmi"; + +import POLLING from "~/enum/polling"; +import { DefaultHookReturnType } from "~/types"; +import { BalanceToken } from "~/types/dex"; +import { useBeraJs } from "../../contexts"; +import { useTokenInformation } from "./useTokenInformation"; + +export const usePollBalance = ({ + address, + owner, +}: { + address: string | undefined; + owner?: Address | undefined; +}): Omit, "mutate"> & { + /** + * + * @deprecated you can use data instead + */ + useBalance: () => BalanceToken | undefined; +} => { + const publicClient = usePublicClient(); + const { account } = useBeraJs(); + const assetOwner = owner ?? account; + + const QUERY_KEY = + publicClient && assetOwner ? [assetOwner, address, "balance"] : null; + const { data: tokenInformation, isLoading: isLoadingTokenInformation } = + useTokenInformation({ address }); + + const isNativeToken = address === nativeTokenAddress; + + const { isLoading, data, error, ...rest } = useSWR( + QUERY_KEY, + async () => { + if (!publicClient) return undefined; + if (assetOwner && address) { + if (address !== nativeTokenAddress) { + return await publicClient.readContract({ + address: address as `0x${string}`, + abi: erc20Abi, + functionName: "balanceOf", + args: [assetOwner!], + }); + } + return await publicClient.getBalance({ + address: assetOwner!, + }); + } + return undefined; + }, + { + refreshInterval: POLLING.FAST, + }, + ); + + const formattedBalance = + !error && data + ? address === nativeTokenAddress + ? formatEther(data as bigint) + : tokenInformation && !isLoadingTokenInformation + ? formatUnits(data, tokenInformation.decimals) + : undefined + : undefined; + + const tokenBalance = { + balance: data ?? 0n, + formattedBalance: formattedBalance ?? "0", + address: address as `0x${string}`, + decimals: isNativeToken + ? gasTokenDecimals + : tokenInformation?.decimals ?? 0, + symbol: isNativeToken ? gasTokenSymbol : tokenInformation?.symbol ?? "", + name: isNativeToken ? gasTokenName : tokenInformation?.name ?? "", + } satisfies BalanceToken; + + const useBalance = () => { + return tokenBalance; + }; + + return { + ...rest, + refresh: () => { + rest.mutate(); + }, + isLoading: isLoading || isNativeToken ? isLoadingTokenInformation : false, + error: error || !isNativeToken ? error : undefined, + data: tokenBalance, + useBalance, + }; +}; diff --git a/packages/berajs/src/hooks/usePollBeraBalance.ts b/packages/berajs/src/hooks/tokens/usePollBeraBalance.ts similarity index 100% rename from packages/berajs/src/hooks/usePollBeraBalance.ts rename to packages/berajs/src/hooks/tokens/usePollBeraBalance.ts diff --git a/packages/berajs/src/hooks/usePollWalletBalances.ts b/packages/berajs/src/hooks/tokens/usePollWalletBalances.ts similarity index 95% rename from packages/berajs/src/hooks/usePollWalletBalances.ts rename to packages/berajs/src/hooks/tokens/usePollWalletBalances.ts index a98cbe4a3..f9fd5fa8e 100755 --- a/packages/berajs/src/hooks/usePollWalletBalances.ts +++ b/packages/berajs/src/hooks/tokens/usePollWalletBalances.ts @@ -9,9 +9,9 @@ import { DefaultHookOptions, DefaultHookReturnType, type Token, -} from ".."; -import { useBeraJs } from "../contexts"; -import { useTokens } from "./useTokens"; +} from "../.."; +import { useBeraJs } from "../../contexts"; +import { useTokens } from "../useTokens"; export type UsePollWalletBalancesArgs = { externalTokenList?: Token[]; diff --git a/packages/berajs/src/hooks/useSubgraphTokenInformation.ts b/packages/berajs/src/hooks/tokens/useSubgraphTokenInformation.ts similarity index 96% rename from packages/berajs/src/hooks/useSubgraphTokenInformation.ts rename to packages/berajs/src/hooks/tokens/useSubgraphTokenInformation.ts index 08b65c834..3e1b1acdb 100644 --- a/packages/berajs/src/hooks/useSubgraphTokenInformation.ts +++ b/packages/berajs/src/hooks/tokens/useSubgraphTokenInformation.ts @@ -3,7 +3,7 @@ import useSWR, { mutate } from "swr"; import { getTokenHoneyPrice } from "~/actions/honey"; import POLLING from "~/enum/polling"; import { DefaultHookOptions, DefaultHookReturnType } from "~/types/global"; -import { Token, useBeraJs } from ".."; +import { Token, useBeraJs } from "../.."; import { getSubgraphTokenInformation } from "~/actions/shared/getSubgraphTokenInformation"; /** diff --git a/packages/berajs/src/hooks/useSubgraphTokenInformations.ts b/packages/berajs/src/hooks/tokens/useSubgraphTokenInformations.ts similarity index 97% rename from packages/berajs/src/hooks/useSubgraphTokenInformations.ts rename to packages/berajs/src/hooks/tokens/useSubgraphTokenInformations.ts index 8791f6592..0e4c7ee6c 100644 --- a/packages/berajs/src/hooks/useSubgraphTokenInformations.ts +++ b/packages/berajs/src/hooks/tokens/useSubgraphTokenInformations.ts @@ -2,7 +2,7 @@ import useSWR, { mutate } from "swr"; import POLLING from "~/enum/polling"; import { DefaultHookOptions, DefaultHookReturnType } from "~/types/global"; -import { useBeraJs } from ".."; +import { useBeraJs } from "../.."; import { getSubgraphTokenInformations, SubgraphTokenInformations, diff --git a/packages/berajs/src/hooks/useTokenHoneyPrice.ts b/packages/berajs/src/hooks/tokens/useTokenHoneyPrice.ts similarity index 96% rename from packages/berajs/src/hooks/useTokenHoneyPrice.ts rename to packages/berajs/src/hooks/tokens/useTokenHoneyPrice.ts index f3fb89f26..224fcf961 100755 --- a/packages/berajs/src/hooks/useTokenHoneyPrice.ts +++ b/packages/berajs/src/hooks/tokens/useTokenHoneyPrice.ts @@ -3,7 +3,7 @@ import useSWR, { mutate } from "swr"; import { getTokenHoneyPrice } from "~/actions/honey"; import POLLING from "~/enum/polling"; import { DefaultHookOptions, DefaultHookReturnType } from "~/types/global"; -import { useBeraJs } from ".."; +import { useBeraJs } from "../.."; /** * diff --git a/packages/berajs/src/hooks/useTokenHoneyPrices.ts b/packages/berajs/src/hooks/tokens/useTokenHoneyPrices.ts similarity index 96% rename from packages/berajs/src/hooks/useTokenHoneyPrices.ts rename to packages/berajs/src/hooks/tokens/useTokenHoneyPrices.ts index 7c04b2d9a..a0c200eed 100755 --- a/packages/berajs/src/hooks/useTokenHoneyPrices.ts +++ b/packages/berajs/src/hooks/tokens/useTokenHoneyPrices.ts @@ -3,7 +3,7 @@ import useSWR, { mutate } from "swr"; import { TokenHoneyPrices, getTokenHoneyPrices } from "~/actions/honey"; import POLLING from "~/enum/polling"; import { DefaultHookOptions, DefaultHookReturnType } from "~/types/global"; -import { useBeraJs } from ".."; +import { useBeraJs } from "../.."; /** * diff --git a/packages/berajs/src/hooks/useTokenInformation.ts b/packages/berajs/src/hooks/tokens/useTokenInformation.ts similarity index 88% rename from packages/berajs/src/hooks/useTokenInformation.ts rename to packages/berajs/src/hooks/tokens/useTokenInformation.ts index 591cf688a..901e0f076 100755 --- a/packages/berajs/src/hooks/useTokenInformation.ts +++ b/packages/berajs/src/hooks/tokens/useTokenInformation.ts @@ -1,5 +1,5 @@ import useSWRImmutable from "swr/immutable"; -import { Address, isAddress } from "viem"; +import { isAddress } from "viem"; import { usePublicClient } from "wagmi"; import { getTokenInformation } from "~/actions"; @@ -8,7 +8,13 @@ import { DefaultHookReturnType, Token, useBeraJs, -} from ".."; +} from "../.."; +import { + gasTokenDecimals, + gasTokenName, + gasTokenSymbol, + nativeTokenAddress, +} from "@bera/config"; export type UseTokenInformationResponse = DefaultHookReturnType< Token | undefined diff --git a/packages/berajs/src/hooks/usePollBalance.ts b/packages/berajs/src/hooks/usePollBalance.ts deleted file mode 100755 index 93b5b6dd9..000000000 --- a/packages/berajs/src/hooks/usePollBalance.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { multicallAddress, nativeTokenAddress } from "@bera/config"; -import useSWR from "swr"; -import useSWRImmutable from "swr/immutable"; -import { erc20Abi, formatEther, formatUnits, getAddress } from "viem"; -import { usePublicClient } from "wagmi"; - -import { multicall3Abi } from "~/abi"; -import POLLING from "~/enum/polling"; -import { useBeraJs } from "../contexts"; -import { BalanceToken } from "~/types/dex"; -import { DefaultHookReturnType } from ".."; - -interface Call { - abi: typeof erc20Abi; - address: `0x${string}`; - functionName: string; - args: any[]; -} - -export const usePollBalance = ({ - address, - owner, -}: { - address: string | undefined; - owner?: string | undefined; -}): DefaultHookReturnType & { - /** - * - * @deprecated you can use data instead - */ - useBalance: () => BalanceToken | undefined; -} => { - const publicClient = usePublicClient(); - const { account } = useBeraJs(); - const assetOwner = owner ?? account; - - const QUERY_KEY = - publicClient && address ? [assetOwner, address, "balance"] : null; - - // TODO: it would be better if we used useSWRImmutable here for token info, and a dynamic query for balance - const { isLoading, data, ...rest } = useSWR( - QUERY_KEY, - async () => { - if (!publicClient) return undefined; - if (assetOwner && address) { - if (address !== nativeTokenAddress) { - const call: Call[] = [ - { - address: address as `0x${string}`, - abi: erc20Abi, - functionName: "balanceOf", - args: [assetOwner], - }, - { - address: address as `0x${string}`, - abi: erc20Abi, - functionName: "symbol", - args: [], - }, - { - address: address as `0x${string}`, - abi: erc20Abi, - functionName: "name", - args: [], - }, - { - address: address as `0x${string}`, - abi: erc20Abi, - functionName: "decimals", - args: [], - }, - ]; - const result = await publicClient.multicall({ - contracts: call, - multicallAddress: multicallAddress, - allowFailure: false, - }); - - const balance: BalanceToken = { - balance: result[0] as bigint, - formattedBalance: formatUnits( - result[0] as bigint, - (result[3] as number) ?? 18, - ), - address: getAddress(address), - decimals: result[3] as number, - symbol: result[1] as string, - name: result[2] as string, - }; - return balance; - } - const call = { - address: multicallAddress, - abi: multicall3Abi, - functionName: "getEthBalance", - args: [assetOwner], - }; - const result = await publicClient.readContract(call); - return { - balance: result as bigint, - formattedBalance: formatEther((result as bigint) ?? 0n), - address, - decimals: 18, - symbol: "BERA", - name: "Berachain", - } satisfies BalanceToken; - } - return undefined; - }, - { - refreshInterval: POLLING.FAST, - }, - ); - - const useBalance = () => { - const { data = undefined } = useSWRImmutable(QUERY_KEY); - return data; - }; - - return { - ...rest, - isLoading, - data, - refresh: () => rest.mutate(), - useBalance, - }; -};