-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bridge-ui-v2): account balance (#14159)
Co-authored-by: Korbinian <KorbinianK@users.noreply.github.com>
- Loading branch information
1 parent
9531a75
commit 081be64
Showing
12 changed files
with
181 additions
and
17 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
53 changes: 52 additions & 1 deletion
53
packages/bridge-ui-v2/src/components/AmountInput/AmountInput.svelte
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
9 changes: 9 additions & 0 deletions
9
packages/bridge-ui-v2/src/components/LoadingText/LoadingText.svelte
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,9 @@ | ||
<script lang="ts"> | ||
import { classNames } from '$libs/util/classNames'; | ||
export let mask = 'Loading'; | ||
let classes = classNames('animate-pulse blur-sm font-bold', $$props.class); | ||
</script> | ||
|
||
<span class={classes}>{mask}</span> |
Empty file.
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,49 @@ | ||
import { getContract } from '@wagmi/core'; | ||
import { zeroAddress } from 'viem'; | ||
|
||
import { tokenVaultABI } from '$abi'; | ||
import { chainContractsMap } from '$libs/chain'; | ||
import { getLogger } from '$libs/util/logger'; | ||
|
||
import { isETH } from './tokens'; | ||
import type { Token } from './types'; | ||
|
||
const log = getLogger('token:getAddress'); | ||
|
||
export async function getAddress(token: Token, srcChainId?: number, destChainId?: number) { | ||
if (!srcChainId) return; | ||
|
||
// Get the address for the token on the source chain | ||
let address = token.addresses[srcChainId]; | ||
|
||
// If the token isn't ETH or has no address... | ||
if (!isETH(token) && (!address || address === zeroAddress)) { | ||
if (!destChainId) return; | ||
|
||
// Find the address on the destination chain instead. We are | ||
// most likely on Taiko chain and the token hasn't yet been | ||
// deployed on it. | ||
const destChainTokenAddress = token.addresses[destChainId]; | ||
|
||
// Get the TokenVault contract on the source chain. The idea is to find | ||
// the bridged address for the token on the destination chain if it's been | ||
// deployed there. This is registered in the TokenVault contract, | ||
// cacnonicalToBridged mapping. | ||
const srcTokenVaultContract = getContract({ | ||
abi: tokenVaultABI, | ||
address: chainContractsMap[srcChainId].tokenVaultAddress, | ||
}); | ||
|
||
try { | ||
address = await srcTokenVaultContract.read.canonicalToBridged([BigInt(destChainId), destChainTokenAddress]); | ||
|
||
log(`Bridged address for ${token.symbol} is "${address}"`); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
throw Error(`Failed to get address for ${token.symbol} on chain ${srcChainId}`, { cause: error }); | ||
} | ||
} | ||
|
||
return address; | ||
} |
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 { fetchBalance } from '@wagmi/core'; | ||
import type { Address } from 'abitype'; | ||
import { zeroAddress } from 'viem'; | ||
|
||
import { getAddress } from './getAddress'; | ||
import { isETH } from './tokens'; | ||
import type { Token } from './types'; | ||
|
||
export async function getBalance(token: Token, userAddress: Address, srcChainId?: number, destChainId?: number) { | ||
if (isETH(token)) { | ||
return fetchBalance({ address: userAddress }); | ||
} | ||
|
||
// We are dealing with an ERC20 token. We need to first find out its address | ||
// on the current chain in order to fetch the balance. | ||
const tokenAddress = await getAddress(token, srcChainId, destChainId); | ||
|
||
if (!tokenAddress || tokenAddress === zeroAddress) return null; | ||
|
||
// Wagmi is an excellent library 😊 | ||
return fetchBalance({ | ||
address: userAddress, | ||
chainId: srcChainId, | ||
token: tokenAddress, | ||
}); | ||
} |
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,6 @@ | ||
export { checkMintable } from './checkMintable'; | ||
export { getAddress } from './getAddress'; | ||
export { getBalance } from './getBalance'; | ||
export { mint } from './mint'; | ||
export * from './tokens'; | ||
export * from './types'; |
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,3 @@ | ||
export function truncateString(str: string, maxlength: number, strBoundary = '…') { | ||
return str.length > maxlength ? `${str.substring(0, maxlength)}${strBoundary}` : str; | ||
} |