-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from berachain/refactor/berajs
Refactor/berajs
- Loading branch information
Showing
40 changed files
with
157 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/berajs/src/hooks/useTpsl.ts → ...berajs/src/hooks/modules/perps/useTpsl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.