Skip to content

Commit

Permalink
block wrong chain ids
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Mar 24, 2023
1 parent 83e6d36 commit 9e565b2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
13 changes: 13 additions & 0 deletions packages/bridge-ui/src/components/Transaction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import { providers } from '../provider/providers';
import { bridges } from '../bridge/bridges';
import { tokenVaults } from '../vault/tokenVaults';
import { isOnCorrectChain } from '../utils/isOnCorrectChain';
export let transaction: BridgeTransaction;
export let fromChain: Chain;
Expand Down Expand Up @@ -77,6 +78,12 @@
await switchChainAndSetSigner(chain);
}
// confirm after switch chain that it worked.
if (!isOnCorrectChain($signer, bridgeTx.toChainId)) {
errorToast('You are connected to the wrong chain in your wallet');
return;
}
// For now just handling this case for when the user has near 0 balance during their first bridge transaction to L2
// TODO: estimate Claim transaction
const userBalance = await $signer.getBalance('latest');
Expand Down Expand Up @@ -120,6 +127,12 @@
await switchChainAndSetSigner(chain);
}
// confirm after switch chain that it worked.
if (!isOnCorrectChain($signer, bridgeTx.fromChainId)) {
errorToast('You are connected to the wrong chain in your wallet');
return;
}
const tx = await bridges[
bridgeTx.message?.data === '0x' || !bridgeTx.message?.data
? BridgeType.ETH
Expand Down
21 changes: 5 additions & 16 deletions packages/bridge-ui/src/components/form/BridgeForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import { chains } from '../../chain/chains';
import { providers } from '../../provider/providers';
import { tokenVaults } from '../../vault/tokenVaults';
import { isOnCorrectChain } from '../../utils/isOnCorrectChain';
let amount: string;
let amountInput: HTMLInputElement;
Expand Down Expand Up @@ -217,22 +218,7 @@
throw Error('Invalid custom recipient address');
}
const signerChain = await $signer.getChainId();
if (signerChain !== $fromChain.id) {
errorToast('You are connected to the wrong chain in your wallet');
return;
}
const bridgeAddress = chains[$fromChain.id].bridgeAddress;
const tokenVaultAddress = tokenVaults[$fromChain.id];
const codeNotDeployed = [bridgeAddress, tokenVaultAddress].find(
async (a) => {
return (await $signer.provider.getCode(a)) === '0x';
},
);
if (codeNotDeployed) {
if (!isOnCorrectChain($signer, $fromChain.id)) {
errorToast('You are connected to the wrong chain in your wallet');
return;
}
Expand All @@ -250,6 +236,9 @@
$fromChain,
);
const bridgeAddress = chains[$fromChain.id].bridgeAddress;
const tokenVaultAddress = tokenVaults[$fromChain.id];
const bridgeOpts: BridgeOpts = {
amountInWei: amountInWei,
signer: $signer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<Modal
title={$_('switchChainModal.title')}
showXButton={false}
isOpen={$isSwitchEthereumChainModalOpen}>
<div class="w-100 text-center px-4">
<span class="font-light text-sm">{$_('switchChainModal.subtitle')}</span>
Expand Down
28 changes: 28 additions & 0 deletions packages/bridge-ui/src/utils/isOnCorrectChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ethers } from 'ethers';
import { chains } from '../chain/chains';
import { tokenVaults } from '../vault/tokenVaults';

export async function isOnCorrectChain(
signer: ethers.Signer,
wantChain: number,
) {
const signerChain = await signer.getChainId();
if (signerChain !== wantChain) {
return false;
}

const bridgeAddress = chains[wantChain].bridgeAddress;
const tokenVaultAddress = tokenVaults[wantChain];

const bridgeAddressCode = await signer.provider.getCode(bridgeAddress);

const tokenVaultAddressCode = await signer.provider.getCode(
tokenVaultAddress,
);

if (bridgeAddressCode === '0x' || tokenVaultAddressCode === '0x') {
return false;
}

return true;
}

0 comments on commit 9e565b2

Please sign in to comment.