Skip to content

Commit

Permalink
Merge branch 'main' into deploy-blobs-api
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerLamTd committed Mar 26, 2024
2 parents 2eec534 + d826e88 commit d36c3f4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@
import { switchChain } from '@wagmi/core';
import { log } from 'debug';
import { createEventDispatcher } from 'svelte';
import { t } from 'svelte-i18n';
import { ContractFunctionExecutionError, UserRejectedRequestError } from 'viem';
import { errorToast, warningToast } from '$components/NotificationToast/NotificationToast.svelte';
import { bridges, type BridgeTransaction } from '$libs/bridge';
import {
InsufficientBalanceError,
InvalidProofError,
NotConnectedError,
ProcessMessageError,
RetryError,
} from '$libs/error';
import { NotConnectedError } from '$libs/error';
import { getConnectedWallet } from '$libs/util/getConnectedWallet';
import { config } from '$libs/wagmi';
import { account } from '$stores/account';
Expand Down Expand Up @@ -62,41 +53,7 @@
dispatch('claimingTxSent', { txHash, type: 'claim' });
} catch (err) {
handleClaimError(err);
dispatch('error', { error: err, action: 'claim' });
}
};
const handleClaimError = (err: unknown) => {
switch (true) {
case err instanceof NotConnectedError:
warningToast({ title: $t('messages.account.required') });
break;
case err instanceof UserRejectedRequestError:
warningToast({ title: $t('transactions.actions.claim.rejected.title') });
break;
case err instanceof InsufficientBalanceError:
dispatch('insufficientFunds', { tx: bridgeTx });
break;
case err instanceof InvalidProofError:
errorToast({ title: $t('common.error'), message: $t('bridge.errors.invalid_proof_provided') });
break;
case err instanceof ProcessMessageError:
errorToast({ title: $t('bridge.errors.process_message_error') });
break;
case err instanceof RetryError:
errorToast({ title: $t('bridge.errors.retry_error') });
break;
case err instanceof ContractFunctionExecutionError:
console.error('!========= ContractFunctionExecutionError', err);
break;
default:
errorToast({
title: $t('bridge.errors.unknown_error.title'),
message: $t('bridge.errors.unknown_error.message'),
});
break;
}
};
</script>
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { t } from 'svelte-i18n';
import { ContractFunctionExecutionError, type Hash, zeroAddress } from 'viem';
import { ContractFunctionExecutionError, type Hash, UserRejectedRequestError, zeroAddress } from 'viem';
import { chainConfig } from '$chainConfig';
import { CloseButton } from '$components/Button';
import { errorToast, infoToast, successToast } from '$components/NotificationToast/NotificationToast.svelte';
import {
errorToast,
infoToast,
successToast,
warningToast,
} from '$components/NotificationToast/NotificationToast.svelte';
import OnAccount from '$components/OnAccount/OnAccount.svelte';
import Claim from '$components/Transactions/Dialogs/Claim.svelte';
import { getInvocationDelaysForDestBridge } from '$libs/bridge/getInvocationDelaysForDestBridge';
import { getProofReceiptForMsgHash } from '$libs/bridge/getProofReceiptForMsgHash';
import type { BridgeTransaction, GetProofReceiptResponse } from '$libs/bridge/types';
import {
InsufficientBalanceError,
InvalidProofError,
NotConnectedError,
ProcessMessageError,
RetryError,
} from '$libs/error';
import type { startPolling } from '$libs/polling/messageStatusPoller';
import type { NFT } from '$libs/token';
import { getLogger } from '$libs/util/logger';
Expand All @@ -23,15 +35,13 @@
import ClaimConfirmStep from './ClaimSteps/ClaimConfirmStep.svelte';
import ClaimPreCheck from './ClaimSteps/ClaimPreCheck.svelte';
import ClaimReviewStep from './ClaimSteps/ClaimReviewStep.svelte';
import { ClaimSteps, ClaimTypes, TWO_STEP_STATE } from './types';
import { ClaimSteps, ClaimTypes, INITIAL_STEP, TWO_STEP_STATE } from './types';
const log = getLogger('ClaimDialog');
const dialogId = `dialog-${uid()}`;
const dispatch = createEventDispatcher();
const INITIAL_STEP = ClaimSteps.CHECK;
export let dialogOpen = false;
export let polling: ReturnType<typeof startPolling>;
Expand Down Expand Up @@ -133,21 +143,52 @@
const handleClaimError = (event: CustomEvent<{ error: unknown; type: ClaimTypes }>) => {
//TODO: update this to display info alongside toasts
const error = event.detail.error;
if (error instanceof ContractFunctionExecutionError) {
if (error.message.includes('B_INVOCATION_TOO_EARLY')) {
errorToast({
title: $t('bridge.errors.claim.too_early.title'),
message: $t('bridge.errors.claim.too_early.message'),
});
} else {
const err = event.detail.error;
switch (true) {
case err instanceof NotConnectedError:
warningToast({ title: $t('messages.account.required') });
break;
case err instanceof UserRejectedRequestError:
warningToast({ title: $t('transactions.actions.claim.rejected.title') });
break;
case err instanceof InsufficientBalanceError:
dispatch('insufficientFunds', { tx: bridgeTx });
break;
case err instanceof InvalidProofError:
errorToast({ title: $t('common.error'), message: $t('bridge.errors.invalid_proof_provided') });
break;
case err instanceof ProcessMessageError:
errorToast({ title: $t('bridge.errors.process_message_error') });
break;
case err instanceof RetryError:
errorToast({ title: $t('bridge.errors.retry_error') });
break;
case err instanceof ContractFunctionExecutionError:
console.error(err);
if (err.message.includes('B_INVOCATION_TOO_EARLY')) {
errorToast({
title: $t('bridge.errors.claim.too_early.title'),
message: $t('bridge.errors.claim.too_early.message'),
});
} else if (err.message.includes('B_NOT_RECEIVED')) {
errorToast({
title: $t('bridge.errors.claim.not_received.title'),
message: $t('bridge.errors.claim.not_received.message'),
});
} else {
errorToast({
title: $t('bridge.errors.unknown_error.title'),
message: $t('bridge.errors.unknown_error.message'),
});
}
break;
default:
console.error(err);
errorToast({
title: $t('bridge.errors.unknown_error.title'),
message: $t('bridge.errors.unknown_error.message'),
});
}
} else {
console.error(event.detail.error);
break;
}
claiming = false;
};
Expand Down
8 changes: 6 additions & 2 deletions packages/bridge-ui/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
},
"cannot_fetch_balance": "Error fetching balance. Wrong chain?",
"claim": {
"not_received": {
"message": "The message is not proven and cannot be claimed right now.",
"title": "Not received"
},
"too_early": {
"message": "You need to wait for the delay to pass before claiming",
"title": "Too early"
Expand Down Expand Up @@ -402,8 +406,8 @@
"description": "You can add your own token by entering the contract address below. Make sure you are on the chain where the token is deployed.",
"title": "Add your own token"
},
"label": "Select token",
"imported_tokens": "Your imported tokens:"
"imported_tokens": "Your imported tokens:",
"label": "Select token"
},
"transactions": {
"actions": {
Expand Down
13 changes: 12 additions & 1 deletion packages/bridge-ui/src/libs/token/getCanonicalInfoForToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ const _getStatus = async ({ address, srcChainId, destChainId, type }: CheckCanon
if (srcCanonicalCheck === zeroAddress && destCanonicalCheck === zeroAddress) {
// if both are zero we are dealing with a canonical address
canonicalTokenAddress = srcChainTokenAddress;
canonicalChain = srcChainId;
// But either chain passed could be the canonical chain, so we need to check which one
const checkSrcChainForCanonicalChain = (await srcTokenVaultContract.read.canonicalToBridged([
srcChainId,
srcChainTokenAddress,
])) as Address;

if (checkSrcChainForCanonicalChain === zeroAddress) {
canonicalChain = destChainId;
} else {
canonicalChain = srcChainId;
}
} else if (destCanonicalCheck !== zeroAddress) {
// if the destination is not zero, we found a canonical address there
canonicalTokenAddress = destCanonicalCheck;
Expand All @@ -192,5 +202,6 @@ const _getStatus = async ({ address, srcChainId, destChainId, type }: CheckCanon
canonicalTokenAddress = srcCanonicalCheck;
canonicalChain = destChainId;
}
log('canonical info', canonicalTokenAddress, canonicalChain);
return { canonicalTokenAddress, canonicalChain };
};
2 changes: 2 additions & 0 deletions packages/bridge-ui/src/libs/token/getTokenAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const _getBridgedAddress = async ({
? 'erc1155VaultAddress'
: 'erc20VaultAddress';

log('getting bridged address', canonicalAddress, canonicalChainId, bridgedChainId, type, vaultAddressKey);

const client = await getPublicClient(config, { chainId: bridgedChainId });
if (!client) throw new Error('Could not get public client');

Expand Down
10 changes: 5 additions & 5 deletions packages/bridge-ui/src/libs/wagmi/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ export async function startWatching() {
log('Account changed', data);

refreshUserBalance();
const { chain } = data;
const { chainId } = data;

// We need to check if the chain is supported, and if not
// we present the user with a modal to switch networks.
if (chain?.id && !isSupportedChain(Number(chain.id))) {
log('Unsupported chain', chain);
if (chainId && !isSupportedChain(Number(chainId))) {
log('Unsupported chain', chainId);
switchChainModal.set(true);
return;
} else if (chain?.id) {
} else if (chainId) {
// When we switch networks, we are actually selecting
// the source chain.
const srcChain = chains.find((c) => c.id === Number(chain?.id));
const srcChain = chains.find((c) => c.id === Number(chainId));
if (srcChain) connectedSourceChain.set(srcChain);

refreshUserBalance();
Expand Down

0 comments on commit d36c3f4

Please sign in to comment.