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

fix(bridge-ui): handle wrong bridge address #13880

Merged
merged 21 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use record to store chainId => token address
  • Loading branch information
jscriptcoder committed Jun 2, 2023
commit 6cb7b0297bffa36dc1943ce6af60536f59051c38
16 changes: 5 additions & 11 deletions packages/bridge-ui/src/components/BridgeForm/SelectToken.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { HTMLBridgeForm } from '../../domain/dom';
import type { Token } from '../../domain/token';
import { tokenService } from '../../storage/services';
import { destChain,srcChain } from '../../store/chain';
import { destChain, srcChain } from '../../store/chain';
import { signer } from '../../store/signer';
import { token } from '../../store/token';
import { userTokens } from '../../store/userToken';
Expand Down Expand Up @@ -63,16 +63,10 @@

const token = {
name: tokenName,
addresses: [
{
chainId: $srcChain.id,
address: tokenAddress,
},
{
chainId: $destChain.id,
address: '0x00',
},
],
addresses: {
[$srcChain.id]: tokenAddress,
[$destChain.id]: '0x00',
},
decimals: decimals,
symbol: symbol,
logoComponent: null,
Expand Down
7 changes: 2 additions & 5 deletions packages/bridge-ui/src/domain/token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Address } from '@wagmi/core';
import type { ComponentType } from 'svelte';

type TokenAddress = {
chainId: number;
address: Address;
};
import type { ChainID } from './chain';

export type Token = {
name: string;
addresses: TokenAddress[];
addresses: Record<ChainID, Address>;
symbol: string;
decimals: number;
logoUrl?: string;
Expand Down
42 changes: 12 additions & 30 deletions packages/bridge-ui/src/token/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,21 @@ import type { Token } from '../domain/token';

export const ETHToken: Token = {
name: 'Ethereum',
addresses: [
{
chainId: L1_CHAIN_ID,
address: '0x00',
},
{
chainId: L2_CHAIN_ID,
address: '0x00',
},
],
addresses: {
[L1_CHAIN_ID]: '0x00',
[L2_CHAIN_ID]: '0x00',
},
decimals: 18,
symbol: 'ETH',
logoComponent: Eth,
};

export const TKOToken: Token = {
name: 'Taiko',
addresses: [
{
chainId: L1_CHAIN_ID,
address: '0x00',
},
{
chainId: L2_CHAIN_ID,
address: '0x00',
},
],
addresses: {
[L1_CHAIN_ID]: '0x00',
[L2_CHAIN_ID]: '0x00',
},
decimals: 18,
symbol: 'TKO',
logoComponent: Tko,
Expand All @@ -51,16 +39,10 @@ export const testERC20Tokens: Token[] = TEST_ERC20.map(
name,
symbol,

addresses: [
{
chainId: L1_CHAIN_ID,
address,
},
{
chainId: L2_CHAIN_ID,
address: '0x00',
},
],
addresses: {
[L1_CHAIN_ID]: address,
[L2_CHAIN_ID]: '0x00',
},
decimals: 18,
logoComponent: symbolToLogoComponent[symbol] || Unknown,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
symbol: 'TEST',
decimals: 18,
logoComponent: null,
addresses: [
{ chainId: 1, address: '0x0000000000000000000000000000000000000001' },
{ chainId: 4, address: '0x0000000000000000000000000000000000000002' },
{ chainId: 5, address: '0x00' },
],
addresses: {
1: '0x0000000000000000000000000000000000000001',
4: '0x0000000000000000000000000000000000000002',
5: '0x00',
},
};
const destTokenVaultAddress = '0x0000000000000000000000000000000000000004';

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
const ethToken: Token = {
...token,
symbol: ETHToken.symbol,
addresses: [],
addresses: {},
};
const result = await checkIfTokenIsDeployedCrossChain(
ethToken,
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
);
expect(destTokenVaultContract.canonicalToBridged).toHaveBeenCalledWith(
srcChain.id,
token.addresses.find((a) => a.chainId === srcChain.id)?.address,
token.addresses[srcChain.id],
);
});

Expand Down Expand Up @@ -118,7 +118,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
);
expect(destTokenVaultContract.canonicalToBridged).toHaveBeenCalledWith(
srcChain.id,
token.addresses.find((a) => a.chainId === srcChain.id)?.address,
token.addresses[srcChain.id],
);
});

Expand Down
12 changes: 4 additions & 8 deletions packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ export const checkIfTokenIsDeployedCrossChain = async (
provider,
);

const tokenAddressOnDestChain = token.addresses.find(
(a) => a.chainId === destChain.id,
);
const tokenAddressOnDestChain = token.addresses[destChain.id];

if (tokenAddressOnDestChain && tokenAddressOnDestChain.address === '0x00') {
if (tokenAddressOnDestChain === '0x00') {
// Check if token is already deployed as BridgedERC20 on destination chain
const tokenAddressOnSourceChain = token.addresses.find(
(a) => a.chainId === srcChain.id,
);
const tokenAddressOnSourceChain = token.addresses[srcChain.id];

log(
'Checking if token',
Expand All @@ -43,7 +39,7 @@ export const checkIfTokenIsDeployedCrossChain = async (
const bridgedTokenAddress =
await destTokenVaultContract.canonicalToBridged(
srcChain.id,
tokenAddressOnSourceChain.address,
tokenAddressOnSourceChain,
);

log(`Address of bridged token: ${bridgedTokenAddress}`);
Expand Down
6 changes: 2 additions & 4 deletions packages/bridge-ui/src/utils/getAddressForToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ export async function getAddressForToken(
signer: Signer,
): Promise<Address> {
// Get the address for the token on the source chain
let address = token.addresses.find((t) => t.chainId === srcChain.id).address;
let address = token.addresses[srcChain.id];

// If the token isn't ETH or has no address...
if (!isETH(token) && (!address || address === '0x00')) {
// Find the address on the destination chain instead
const destChainAddress = token.addresses.find(
(t) => t.chainId === destChain.id,
).address;
const destChainAddress = token.addresses[destChain.id];

// Get the token vault contract on the source chain. The idea is to find
// the bridged address for the token on the destination chain if it's been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ jest.mock('ethers', () => {

const mockToken = {
name: 'MockToken',
addresses: [
{
chainId: L1_CHAIN_ID,
address: '0x00',
},
],
addresses: {
[L1_CHAIN_ID]: '0x00',
},
decimals: 18,
symbol: 'MKT',
} as unknown as Token;
Expand Down
3 changes: 2 additions & 1 deletion packages/bridge-ui/src/utils/getIsMintedWithEstimation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber, Contract, type Signer } from 'ethers';

import { freeMintErc20ABI } from '../constants/abi';
import { L1_CHAIN_ID } from '../constants/envVars';
import type { Token } from '../domain/token';
import { getLogger } from './logger';

Expand All @@ -17,7 +18,7 @@ export async function getIsMintedWithEstimation(
const address = signer.getAddress();

const l1TokenContract = new Contract(
token.addresses[0].address, // L1 address
token.addresses[L1_CHAIN_ID],
freeMintErc20ABI,
signer,
);
Expand Down
9 changes: 3 additions & 6 deletions packages/bridge-ui/src/utils/mintERC20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ jest.mock('ethers', () => {

const mockToken = {
name: 'MockToken',
addresses: [
{
chainId: L1_CHAIN_ID,
address: '0x00',
},
],
addresses: {
[L1_CHAIN_ID]: '0x00',
},
decimals: 18,
symbol: 'MKT',
} as unknown as Token;
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge-ui/src/utils/mintERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export async function mintERC20(
signer: Signer,
): Promise<Transaction> {
// If we're not already, switch to L1
if (srcChainId !== token.addresses[0].chainId) {
if (srcChainId !== L1_CHAIN_ID) {
await selectChain(chains[L1_CHAIN_ID]);
}

const l1TokenContract = new Contract(
token.addresses[0].address,
token.addresses[L1_CHAIN_ID],
freeMintErc20ABI,
signer,
);
Expand Down
16 changes: 5 additions & 11 deletions packages/bridge-ui/src/utils/recommendProcessingFee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,10 @@ const mockSigner = {} as Signer;

const mockToken = {
name: 'MockToken',
addresses: [
{
chainId: L1_CHAIN_ID,
address: '0x00',
},
{
chainId: L2_CHAIN_ID,
address: '0x123', // token is deployed on L2
},
],
addresses: {
[L1_CHAIN_ID]: '0x00',
[L2_CHAIN_ID]: '0x123', // token is deployed on L2
},
decimals: 18,
symbol: 'MKT',
logoComponent: null,
Expand Down Expand Up @@ -176,7 +170,7 @@ describe('recommendProcessingFee()', () => {

expect(mockContract.canonicalToBridged).toHaveBeenCalledWith(
taikoChain.id,
mockToken.addresses[1].address,
mockToken.addresses[L2_CHAIN_ID],
);
});

Expand Down
8 changes: 2 additions & 6 deletions packages/bridge-ui/src/utils/recommendProcessingFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ export async function recommendProcessingFee(
let gasLimit = ethGasLimit;

if (!isETH(token)) {
let srcChainAddr = token.addresses.find(
(t) => t.chainId === srcChain.id,
).address;
let srcChainAddr = token.addresses[srcChain.id];

if (!srcChainAddr || srcChainAddr === '0x00') {
srcChainAddr = token.addresses.find(
(t) => t.chainId === destChain.id,
).address;
srcChainAddr = token.addresses[destChain.id];
}

const srcTokenVault = new Contract(
Expand Down