-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f9bc7
commit a5a9620
Showing
18 changed files
with
146 additions
and
42 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { useFungibleTokenBalance } from './use-fungible-token-balance'; | ||
import { useEthAccountBalance } from './use-eth-account-balance'; | ||
import { useEthFTBalance } from './use-eth-ft-balance'; | ||
import { useHandleEthSubmit } from './use-handle-eth-submit'; | ||
|
||
export { useFungibleTokenBalance, useHandleEthSubmit }; | ||
export { useEthFTBalance, useEthAccountBalance, useHandleEthSubmit }; |
27 changes: 27 additions & 0 deletions
27
frontend/src/features/swap/hooks/eth/use-eth-account-balance.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { formatEther } from 'viem'; | ||
import { useBalance } from 'wagmi'; | ||
|
||
import { useEthAccount } from '@/hooks'; | ||
|
||
const withPrecision = (value: string) => { | ||
// simplest solution without rounding for now | ||
const digitsCount = 3; | ||
|
||
return value.slice(0, value.indexOf('.') + digitsCount + 1); | ||
}; | ||
|
||
function useEthAccountBalance() { | ||
const ethAccount = useEthAccount(); | ||
|
||
const { data, isPending } = useBalance({ | ||
address: ethAccount?.address, | ||
}); | ||
|
||
const { value } = data || {}; | ||
const formattedValue = data ? withPrecision(formatEther(data.value)) : undefined; | ||
const isLoading = isPending; | ||
|
||
return { value, formattedValue, isLoading }; | ||
} | ||
|
||
export { useEthAccountBalance }; |
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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { useFungibleTokenBalance as useEthBalance, useHandleEthSubmit } from './eth'; | ||
import { useEthFTBalance, useEthAccountBalance, useHandleEthSubmit } from './eth'; | ||
import { useBridge } from './use-bridge'; | ||
import { useSwapForm } from './use-swap-form'; | ||
import { useVaraConfig, useFungibleTokenBalance as useVaraBalance, useHandleVaraSubmit } from './vara'; | ||
import { useVaraConfig, useVaraAccountBalance, useVaraFTBalance, useHandleVaraSubmit } from './vara'; | ||
|
||
export { | ||
useEthBalance, | ||
useEthFTBalance, | ||
useEthAccountBalance, | ||
useHandleEthSubmit, | ||
useSwapForm, | ||
useVaraConfig, | ||
useVaraBalance, | ||
useVaraAccountBalance, | ||
useVaraFTBalance, | ||
useHandleVaraSubmit, | ||
useBridge, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { useFTAddresses } from './use-ft-addresses'; | ||
import { useFungibleTokenBalance } from './use-fungible-token-balance'; | ||
import { useHandleVaraSubmit } from './use-handle-vara-submit'; | ||
import { useVaraAccountBalance } from './use-vara-account-balance'; | ||
import { useVaraConfig } from './use-vara-config'; | ||
import { useVaraFTBalance } from './use-vara-ft-balance'; | ||
|
||
export { useVaraConfig, useFungibleTokenBalance, useHandleVaraSubmit, useFTAddresses }; | ||
export { useVaraConfig, useVaraFTBalance, useHandleVaraSubmit, useFTAddresses, useVaraAccountBalance }; |
24 changes: 24 additions & 0 deletions
24
frontend/src/features/swap/hooks/vara/use-derive-balances-all.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useApi } from '@gear-js/react-hooks'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { BALANCE_REFETCH_INTERVAL } from '../../consts'; | ||
|
||
function useDeriveBalancesAll(accountAddress: string | undefined) { | ||
const { api, isApiReady } = useApi(); | ||
|
||
const getDeriveBalancesAll = async () => { | ||
if (!isApiReady) throw new Error('API is not initialized'); | ||
if (!accountAddress) throw new Error('Account is not found'); | ||
|
||
return api.derive.balances.all(accountAddress); | ||
}; | ||
|
||
return useQuery({ | ||
queryKey: ['deriveBalancesAll', isApiReady, accountAddress], | ||
queryFn: getDeriveBalancesAll, | ||
enabled: isApiReady && Boolean(accountAddress), | ||
refetchInterval: BALANCE_REFETCH_INTERVAL, | ||
}); | ||
} | ||
|
||
export { useDeriveBalancesAll }; |
26 changes: 26 additions & 0 deletions
26
frontend/src/features/swap/hooks/vara/use-vara-account-balance.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useAccount, useBalanceFormat } from '@gear-js/react-hooks'; | ||
import { useMemo } from 'react'; | ||
|
||
import { useDeriveBalancesAll } from './use-derive-balances-all'; | ||
|
||
function useVaraAccountBalance() { | ||
const { account, isAccountReady } = useAccount(); | ||
const { getFormattedBalance } = useBalanceFormat(); | ||
|
||
const { data, isPending } = useDeriveBalancesAll(account?.address); | ||
const { freeBalance } = data || {}; | ||
const value = freeBalance?.toBigInt(); | ||
const formattedValue = value !== undefined ? getFormattedBalance(value).value : undefined; | ||
|
||
// cuz swap vara form is rendered by default without login and we have to handle empty balance state | ||
const isLoading = useMemo(() => { | ||
if (!isAccountReady) return true; | ||
if (!account) return false; | ||
|
||
return isPending; | ||
}, [account, isAccountReady, isPending]); | ||
|
||
return { value, formattedValue, isLoading }; | ||
} | ||
|
||
export { useVaraAccountBalance }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Values, FormattedValues } from './form'; | ||
import { UseBalance, UseHandleSubmit } from './hooks'; | ||
import { UseAccountBalance, UseFTBalance, UseHandleSubmit } from './hooks'; | ||
|
||
export type { UseBalance, UseHandleSubmit, Values, FormattedValues }; | ||
export type { UseAccountBalance, UseFTBalance, UseHandleSubmit, Values, FormattedValues }; |
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