Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better frontend handling when API is down #647

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions frontend/src/components/AccountReceiveTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@
<div :key="props.row.id" class="col-12">
<q-card class="card-border cursor-pointer q-pt-md col justify-center items-center">
<q-card-section class="row justify-center items-center">
<img class="q-mr-md" :src="getTokenLogoUri(props.row.token, tokens)" style="width: 1.2rem" />
<img
class="q-mr-md"
:src="getTokenLogoUri(props.row.token, tokens)"
style="width: 1.2rem"
v-if="getTokenInfo(props.row.token)"
/>
<div class="text-primary text-h6 header-black q-pb-none">
{{ formatAmount(props.row.amount, props.row.token, tokens) }}
{{ getTokenSymbol(props.row.token, tokens) }}
Expand Down Expand Up @@ -231,7 +236,12 @@
<!-- Amount column -->
<div v-else-if="col.name === 'amount'">
<div class="row justify-start items-center no-wrap">
<img class="col-auto q-mr-md" :src="getTokenLogoUri(props.row.token, tokens)" style="width: 1.2rem" />
<img
class="col-auto q-mr-md"
:src="getTokenLogoUri(props.row.token, tokens)"
style="width: 1.2rem"
v-if="getTokenInfo(props.row.token)"
/>
<div class="col-auto">
{{ formatAmount(col.value, props.row.token, tokens) }}
{{ getTokenSymbol(props.row.token, tokens) }}
Expand Down Expand Up @@ -729,6 +739,7 @@ function useReceivedFundsTable(userAnnouncements: Ref<UserAnnouncement[]>, spend
formatUnits,
getFeeEstimate,
getSenderOrReceiverEtherscanUrl,
getTokenInfo,
getTokenLogoUri,
getTokenSymbol,
initializeWithdraw,
Expand Down
24 changes: 21 additions & 3 deletions frontend/src/store/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ERC20_ABI, MAINNET_PROVIDER, MULTICALL_ABI, MULTICALL_ADDRESS } from 's
import { BigNumber, Contract, ExternalProvider, Web3Provider, parseUnits } from 'src/utils/ethers';
garyghayrat marked this conversation as resolved.
Show resolved Hide resolved
import { UmbraApi } from 'src/utils/umbra-api';
import { getChainById } from 'src/utils/utils';
import { notifyUser } from 'src/utils/alerts';
import useSettingsStore from 'src/store/settings';
import enLocal from 'src/i18n/locales/en-US.json';
import chLocal from 'src/i18n/locales/zh-CN.json';
Expand Down Expand Up @@ -155,7 +156,16 @@ export default function useWalletStore() {
async function getTokenBalances() {
// Setup
if (!provider.value) throw new Error('Provider not connected');
if (!relayer.value) throw new Error('Relayer instance not found');

// No longer throwing here if we don't have a relayer, just notifying the user
// because we don't need the relayer for balances as that is done via contract calls
if (!relayer.value) {
const nativeToken = currentChain.value?.nativeCurrency.symbol
? currentChain.value?.nativeCurrency.symbol
: 'native token';
notifyUser('error', `Cannot connect to token relayer/API.. only ${nativeToken} transfers available.`);
}

const multicall = new Contract(MULTICALL_ADDRESS, MULTICALL_ABI, provider.value);

// Generate balance calls using Multicall contract
Expand Down Expand Up @@ -253,13 +263,21 @@ export default function useWalletStore() {
signer.value = markRaw(provider.value.getSigner());

// Get user and network information
const [_userAddress, _network, _relayer] = await Promise.all([
const [_userAddress, _network] = await Promise.all([
signer.value.getAddress(), // get user's address
provider.value.getNetwork(), // get information on the connected network
UmbraApi.create(provider.value), // Configure the relayer (even if not withdrawing, this gets the list of tokens we allow to send)
]);
await utils.assertSupportedAddress(_userAddress);

// Configure the relayer (soft error handling, as we still want to allow the user to transfer native tokens if the relayer is down)
let _relayer: UmbraApi | undefined;
try {
_relayer = await UmbraApi.create(provider.value);
} catch (e) {
console.log("couldn't create relayer", e);
_relayer = undefined;
}

// If nothing has changed, no need to continue configuring.
if (_userAddress === userAddress.value && _network.chainId === chainId.value) {
setLoading(false);
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export const getTokenInfo = (tokenAddress: string, tokens: TokenInfoExtended[])
export const formatDate = (timestamp: number) => date.formatDate(timestamp, 'YYYY-MM-DD');

export const formatAmount = (amount: BigNumber, tokenAddress: string, tokens: TokenInfoExtended[]) => {
const decimals = getTokenInfo(tokenAddress, tokens).decimals;
const matchedToken = getTokenInfo(tokenAddress, tokens);
if (!matchedToken) return 'Relayer/API Connection Issue';
const decimals = matchedToken.decimals;
return Number(formatUnits(amount, decimals)).toLocaleString(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 10,
Expand All @@ -177,8 +179,11 @@ export const getTokenLogoUri = (tokenAddress: string, tokens: TokenInfoExtended[

export const formatTime = (timestamp: number) => date.formatDate(timestamp, 'h:mm A');

export const getTokenSymbol = (tokenAddress: string, tokens: TokenInfoExtended[]) =>
getTokenInfo(tokenAddress, tokens).symbol;
export const getTokenSymbol = (tokenAddress: string, tokens: TokenInfoExtended[]) => {
const matchedToken = getTokenInfo(tokenAddress, tokens);
const tokenSymbol = matchedToken ? matchedToken.symbol : '';
return tokenSymbol;
};

/**
* @notice Copies the address of type to the clipboard
Expand Down
Loading