Skip to content

Commit

Permalink
fix(bridge-ui-v2): refresh ETH balance at the top right (#14539)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodino authored and phenix3443 committed Aug 22, 2023
1 parent 302aa30 commit 6f6ee6d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
23 changes: 13 additions & 10 deletions packages/bridge-ui-v2/src/components/Bridge/Amount.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import type { FetchBalanceResult } from '@wagmi/core';
import { t } from 'svelte-i18n';
import { formatUnits, parseUnits } from 'viem';
Expand All @@ -9,12 +8,13 @@
import { warningToast } from '$components/NotificationToast';
import { checkBalanceToBridge, getMaxAmountToBridge } from '$libs/bridge';
import { InsufficientAllowanceError, InsufficientBalanceError, RevertedWithFailedError } from '$libs/error';
import { getBalance as getTokenBalance } from '$libs/token';
import { ETHToken, getBalance as getTokenBalance } from '$libs/token';
import { debounce } from '$libs/util/debounce';
import { getLogger } from '$libs/util/logger';
import { truncateString } from '$libs/util/truncateString';
import { renderBalance } from '$libs/util/renderBalance';
import { uid } from '$libs/util/uid';
import { account } from '$stores/account';
import { ethBalance } from '$stores/balance';
import { network } from '$stores/network';
import {
Expand Down Expand Up @@ -108,6 +108,16 @@
destChainId,
userAddress,
});
if (token.name === ETHToken.name) {
$ethBalance = $tokenBalance;
} else {
$ethBalance = await getTokenBalance({
token: ETHToken,
srcChainId,
destChainId,
userAddress,
});
}
} catch (err) {
log('Error updating balance: ', err);
//most likely we have a custom token that is not bridged yet
Expand All @@ -122,13 +132,6 @@
// Could happen as the user enters an amount
const debouncedValidateAmount = debounce(validateAmount, 300);
function renderBalance(balance: Maybe<FetchBalanceResult>) {
if (!balance) return '0.00';
let maxlength = Number(balance.formatted) < 0.000001 ? balance.decimals : 6;
return `${truncateString(balance.formatted, maxlength, '')} ${balance.symbol}`;
}
// Will trigger on input events. We update the entered amount
// and check it's validity
function inputAmount(event: Event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import { Button } from '$components/Button';
import { DesktopOrLarger } from '$components/DesktopOrLarger';
import { Icon } from '$components/Icon';
import { EthIcon, Icon } from '$components/Icon';
import { web3modal } from '$libs/connect';
import { noop } from '$libs/util/noop';
import { renderBalance } from '$libs/util/renderBalance';
import { ethBalance } from '$stores/balance';
export let connected = false;
Expand All @@ -17,8 +19,6 @@
let web3modalOpen = false;
let unsubscribeWeb3Modal = noop;
$: web3ButtonBalance = connected && isDesktopOrLarger ? 'show' : 'hide';
function connectWallet() {
if (web3modalOpen) return;
web3modal.openModal();
Expand All @@ -43,7 +43,13 @@
https://docs.walletconnect.com/2.0/web/web3modal/html/wagmi/components
-->
{#if connected}
<w3m-core-button balance={web3ButtonBalance} />
<Button class="px-[20px] py-2 mr-[8px] rounded-full" type="neutral" on:click={connectWallet}>
<span class="body-regular f-items-center space-x-2">
<svelte:component this={EthIcon} size={20} />
<span>{renderBalance($ethBalance)}</span>
</span>
</Button>
<w3m-core-button balance="hide" />
{:else}
<!-- TODO: fixing the width for English. i18n? -->
<Button class="px-[20px] py-2 rounded-full w-[215px]" type="neutral" loading={web3modalOpen} on:click={connectWallet}>
Expand Down
5 changes: 3 additions & 2 deletions packages/bridge-ui-v2/src/components/Icon/BLL.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
export let width = 32;
export let height = 32;
export let size = 32;
export let width = size;
export let height = size;
</script>

<svg {width} {height} viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down
5 changes: 3 additions & 2 deletions packages/bridge-ui-v2/src/components/Icon/ERC20.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
export let height = 30;
export let width = 30;
export let size = 30;
export let height = size;
export let width = size;
</script>

<svg
Expand Down
5 changes: 3 additions & 2 deletions packages/bridge-ui-v2/src/components/Icon/HORSE.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
export let width = 32;
export let height = 32;
export let size = 32;
export let width = size;
export let height = size;
</script>

<svg {width} {height} viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@
<div class="flex space-x-2 items-center">
{#if symbolToIconMap[value.symbol]}
<i role="img" aria-label={value.name}>
<svelte:component this={symbolToIconMap[value.symbol]} />
<svelte:component this={symbolToIconMap[value.symbol]} size={28}/>
</i>
{:else}
<i role="img" aria-label={value.symbol}>
<Erc20 />
<svelte:component this={Erc20} size={28}/>
</i>
{/if}
<span class="title-subsection-bold">{value.symbol}</span>
Expand Down
10 changes: 10 additions & 0 deletions packages/bridge-ui-v2/src/libs/util/renderBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { FetchBalanceResult } from '@wagmi/core';

import { truncateString } from '$libs/util/truncateString';

export function renderBalance(balance: Maybe<FetchBalanceResult>) {
if (!balance) return '0.00';

const maxlength = Number(balance.formatted) < 0.000001 ? balance.decimals : 6;
return `${truncateString(balance.formatted, maxlength, '')} ${balance.symbol}`;
}
4 changes: 4 additions & 0 deletions packages/bridge-ui-v2/src/stores/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { FetchBalanceResult } from '@wagmi/core';
import { writable } from 'svelte/store';

export const ethBalance = writable<Maybe<FetchBalanceResult>>(null);

0 comments on commit 6f6ee6d

Please sign in to comment.