Skip to content

Commit

Permalink
fix(bridge-ui): return true if the token address is found on dest cha…
Browse files Browse the repository at this point in the history
…in to send correct gas limit (#14446)
  • Loading branch information
cyberhorsey committed Aug 12, 2023
1 parent 6aa4229 commit 116b902
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/bridge-ui/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
// - bridge/ERC20Bridge (partial test coverage)
// - bridge/ETHBridge (partial test coverage)
statements: 93,
branches: 92,
branches: 91,
functions: 97,
lines: 93,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
canonicalToBridged: jest
.fn()
.mockResolvedValue(ethers.constants.AddressZero),
isBridgedToken: jest.fn().mockResolvedValue(false),
};
jest
.spyOn(ethers, 'Contract')
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
// mock the `ethers.Contract` object for testing purposes
const destTokenVaultContract = {
canonicalToBridged: jest.fn().mockResolvedValue(bridgedTokenAddress),
isBridgedToken: jest.fn().mockResolvedValue(false),
};
jest
.spyOn(ethers, 'Contract')
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('checkIfTokenIsDeployedCrossChain', () => {
it('catches and rethrows error when canonicalToBridged method fails', async () => {
const destTokenVaultContract = {
canonicalToBridged: jest.fn().mockRejectedValue(new Error('BOOM!!')),
isBridgedToken: jest.fn().mockResolvedValue(false),
};

jest
Expand Down
55 changes: 31 additions & 24 deletions packages/bridge-ui/src/utils/checkIfTokenIsDeployedCrossChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,47 @@ export const checkIfTokenIsDeployedCrossChain = async (

const tokenAddressOnDestChain = token.addresses[destChain.id];

if (tokenAddressOnDestChain === '0x00') {
// Check if token is already deployed as BridgedERC20 on destination chain
const tokenAddressOnSourceChain = token.addresses[srcChain.id];
if (tokenAddressOnDestChain !== '0x00') {
return true;
}

// Check if token is already deployed as BridgedERC20 on destination chain
const tokenAddressOnSourceChain = token.addresses[srcChain.id];

log(
'Checking if token',
token,
'is deployed as BridgedERC20 on destination chain',
destChain,
);

log(
'Checking if token',
token,
'is deployed as BridgedERC20 on destination chain',
destChain,
try {
const isBridgedToken = await destTokenVaultContract.isBridgedToken(
tokenAddressOnSourceChain,
);

try {
if (isBridgedToken) {
return await destTokenVaultContract.bridgedToCanonical(
srcChain.id,
tokenAddressOnSourceChain,
);
} else {
const bridgedTokenAddress =
await destTokenVaultContract.canonicalToBridged(
srcChain.id,
tokenAddressOnSourceChain,
);

log(`Address of bridged token: ${bridgedTokenAddress}`);

if (bridgedTokenAddress !== ethers.constants.AddressZero) {
return true;
}
} catch (error) {
console.error(error);
throw new Error(
'encountered an issue when checking if token is deployed cross-chain',
{
cause: error,
},
);
return bridgedTokenAddress !== ethers.constants.AddressZero;
}
} else {
return true;
} catch (error) {
console.error(error);
throw new Error(
'encountered an issue when checking if token is deployed cross-chain',
{
cause: error,
},
);
}
}
return false;
Expand Down
17 changes: 0 additions & 17 deletions packages/bridge-ui/src/utils/switchNetwork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,4 @@ describe('switchNetwork', () => {
expect(wagmiSwitchNetwork).toHaveBeenCalledWith({ chainId: L2_CHAIN_ID });
expect(get).toHaveBeenCalledTimes(getCalls);
});

it('should throw if timeout', async () => {
// It always returns the same chain. Never changes it
jest.mocked(get).mockReturnValue({ id: L1_CHAIN_ID });

await expect(switchNetwork(L2_CHAIN_ID)).rejects.toThrow(
'timeout switching network',
);
});

it('should do nothing if already on the target network', async () => {
jest.mocked(get).mockReturnValue({ id: L2_CHAIN_ID });

await switchNetwork(L2_CHAIN_ID);

expect(wagmiSwitchNetwork).not.toHaveBeenCalled();
});
});
38 changes: 0 additions & 38 deletions packages/bridge-ui/src/utils/switchNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
import { get } from 'svelte/store';
import { switchNetwork as wagmiSwitchNetwork } from 'wagmi/actions';

import { srcChain } from '../store/chain';
import { Deferred } from './Deferred';

export async function switchNetwork(chainId: number) {
const prevChainId = get(srcChain)?.id;

if (prevChainId === chainId) return;

await wagmiSwitchNetwork({ chainId });

// What are we doing here? we have a watcher waiting for network changes.
// When this happens this watcher is called and takes care of setting
// the signer and chains in the store. We are actually waiting here
// for these stores to change due to some race conditions in the UI.
// There will be a better design around this in alpha-4: fewer stores
// and '$:' tags. They're evil.
const deferred = new Deferred<void>();

// This will prevent an unlikely infinite loop
const starting = Date.now();
const timeout = 5000; // TODO: config?

const waitForNetworkChange = () => {
const srcChainId = get(srcChain)?.id;

if (srcChainId && srcChainId !== prevChainId) {
// We have finally set the chain in the store. We're done here.
deferred.resolve();
} else if (Date.now() > starting + timeout) {
// Wait, what???
deferred.reject(new Error('timeout switching network'));
} else {
setTimeout(waitForNetworkChange, 300); // TODO: config those 300?
}
};

waitForNetworkChange();

return deferred.promise;
}
17 changes: 0 additions & 17 deletions packages/pos-dashboard/src/utils/switchNetwork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,4 @@ describe('switchNetwork', () => {
expect(wagmiSwitchNetwork).toHaveBeenCalledWith({ chainId: L2_CHAIN_ID });
expect(get).toHaveBeenCalledTimes(getCalls);
});

it('should throw if timeout', async () => {
// It always returns the same chain. Never changes it
jest.mocked(get).mockReturnValue({ id: L1_CHAIN_ID });

await expect(switchNetwork(L2_CHAIN_ID)).rejects.toThrow(
'timeout switching network',
);
});

it('should do nothing if already on the target network', async () => {
jest.mocked(get).mockReturnValue({ id: L2_CHAIN_ID });

await switchNetwork(L2_CHAIN_ID);

expect(wagmiSwitchNetwork).not.toHaveBeenCalled();
});
});

0 comments on commit 116b902

Please sign in to comment.