Skip to content

Commit

Permalink
Merge pull request #111 from berachain/refactor/berajs
Browse files Browse the repository at this point in the history
Refactor/berajs
  • Loading branch information
bearpong authored Nov 4, 2024
2 parents d6e87a4 + b097b6c commit 992db73
Show file tree
Hide file tree
Showing 40 changed files with 157 additions and 214 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ipfs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ipfs-static-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 0 additions & 34 deletions packages/berajs/src/abi/epochs.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
47 changes: 23 additions & 24 deletions packages/berajs/src/abi/index.ts
Original file line number Diff line number Diff line change
@@ -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";
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 11 additions & 11 deletions packages/berajs/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
99 changes: 99 additions & 0 deletions packages/berajs/src/hooks/tokens/usePollBalance.ts
Original file line number Diff line number Diff line change
@@ -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<DefaultHookReturnType<BalanceToken | undefined>, "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,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "../..";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "../..";

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -8,7 +8,13 @@ import {
DefaultHookReturnType,
Token,
useBeraJs,
} from "..";
} from "../..";
import {
gasTokenDecimals,
gasTokenName,
gasTokenSymbol,
nativeTokenAddress,
} from "@bera/config";

export type UseTokenInformationResponse = DefaultHookReturnType<
Token | undefined
Expand Down
Loading

0 comments on commit 992db73

Please sign in to comment.