From 0e5ed94f9ff7c2bb5cd39d9c6b788b9d23377e0a Mon Sep 17 00:00:00 2001 From: Fionna <13184582+fionnachan@users.noreply.github.com> Date: Tue, 29 Oct 2024 00:28:10 +0800 Subject: [PATCH 1/2] test: add tests for isDepositMode --- .../src/util/__tests__/isDepositMode.test.ts | 121 ++++++++++++++++++ .../src/util/isDepositMode.ts | 6 + 2 files changed, 127 insertions(+) create mode 100644 packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts diff --git a/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts b/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts new file mode 100644 index 0000000000..538866ccc8 --- /dev/null +++ b/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts @@ -0,0 +1,121 @@ +import { registerCustomArbitrumNetwork } from '@arbitrum/sdk' + +import { isDepositMode } from '../isDepositMode' +import { ChainId } from '../networks' +import { orbitMainnets } from '../orbitChainsList' + +beforeAll(() => { + const xaiMainnetChainId = 660279 + + const xaiMainnet = orbitMainnets[xaiMainnetChainId] + + if (!xaiMainnet) { + throw new Error(`Could not find Xai Mainnet in the Orbit chains list.`) + } + + registerCustomArbitrumNetwork(xaiMainnet) + + const rariMainnetChainId = 1380012617 + + const rariMainnet = orbitMainnets[rariMainnetChainId] + + if (!rariMainnet) { + throw new Error(`Could not find Rari Mainnet in the Orbit chains list.`) + } + + registerCustomArbitrumNetwork(rariMainnet) +}) + +describe('isDepositMode', () => { + it('should return true for L1 source chain and L2 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: ChainId.Ethereum, + destinationChainId: ChainId.ArbitrumOne + }) + expect(result1).toBe(true) + + const result2 = isDepositMode({ + sourceChainId: ChainId.Ethereum, + destinationChainId: ChainId.ArbitrumNova + }) + + expect(result2).toBe(true) + }) + it('should return true for L2 source chain and L3 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: ChainId.ArbitrumOne, + destinationChainId: 660279 // Xai + }) + expect(result1).toBe(true) + + const result2 = isDepositMode({ + sourceChainId: ChainId.ArbitrumOne, + destinationChainId: 1380012617 // RARI mainnet + }) + + expect(result2).toBe(true) + }) + it('should return false for L2 source chain and L1 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: ChainId.ArbitrumOne, + destinationChainId: ChainId.Ethereum + }) + expect(result1).toBe(false) + + const result2 = isDepositMode({ + sourceChainId: ChainId.ArbitrumNova, + destinationChainId: ChainId.Ethereum + }) + + expect(result2).toBe(false) + }) + it('should return false for L3 source chain and L2 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: 1380012617, // RARI mainnet + destinationChainId: ChainId.ArbitrumOne + }) + expect(result1).toBe(false) + + const result2 = isDepositMode({ + sourceChainId: 660279, // Xai + destinationChainId: ChainId.ArbitrumOne + }) + + expect(result2).toBe(false) + }) + it('should return false for L1 source chain and L3 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: ChainId.Ethereum, + destinationChainId: 1380012617 // RARI mainnet + }) + expect(result1).toBe(false) + + const result2 = isDepositMode({ + sourceChainId: ChainId.Ethereum, + destinationChainId: 660279 // Xai + }) + + expect(result2).toBe(false) + }) + it('should return false for L3 source chain and L1 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: 1380012617, // RARI mainnet + destinationChainId: ChainId.Ethereum + }) + expect(result1).toBe(false) + + const result2 = isDepositMode({ + sourceChainId: 660279, // Xai + destinationChainId: ChainId.Ethereum + }) + + expect(result2).toBe(false) + }) + it('should return false for L2 source chain and L2 destination chain', () => { + const result1 = isDepositMode({ + sourceChainId: ChainId.ArbitrumOne, + destinationChainId: ChainId.ArbitrumNova + }) + expect(result1).toBe(false) + }) +}) diff --git a/packages/arb-token-bridge-ui/src/util/isDepositMode.ts b/packages/arb-token-bridge-ui/src/util/isDepositMode.ts index dd22cceb7c..5d979f1587 100644 --- a/packages/arb-token-bridge-ui/src/util/isDepositMode.ts +++ b/packages/arb-token-bridge-ui/src/util/isDepositMode.ts @@ -1,5 +1,11 @@ import { isNetwork } from '../util/networks' +/** + * determines if the UI is in deposit mode or withdrawal mode + * + * @note this function classifies L1 -> L3 as deposit mode + * @returns boolean + */ export function isDepositMode({ sourceChainId, destinationChainId From b81e6553321c6e02076f7b3a94da44727059ca75 Mon Sep 17 00:00:00 2001 From: Fionna <13184582+fionnachan@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:08:49 +0000 Subject: [PATCH 2/2] update --- .../src/util/__tests__/isDepositMode.test.ts | 26 ++++++++++++------- .../src/util/isDepositMode.ts | 18 ++++++++++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts b/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts index 538866ccc8..17f0a92ea6 100644 --- a/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts +++ b/packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts @@ -83,19 +83,19 @@ describe('isDepositMode', () => { expect(result2).toBe(false) }) - it('should return false for L1 source chain and L3 destination chain', () => { + it('should return true for L1 source chain and L3 destination chain', () => { const result1 = isDepositMode({ sourceChainId: ChainId.Ethereum, destinationChainId: 1380012617 // RARI mainnet }) - expect(result1).toBe(false) + expect(result1).toBe(true) const result2 = isDepositMode({ sourceChainId: ChainId.Ethereum, destinationChainId: 660279 // Xai }) - expect(result2).toBe(false) + expect(result2).toBe(true) }) it('should return false for L3 source chain and L1 destination chain', () => { const result1 = isDepositMode({ @@ -111,11 +111,19 @@ describe('isDepositMode', () => { expect(result2).toBe(false) }) - it('should return false for L2 source chain and L2 destination chain', () => { - const result1 = isDepositMode({ - sourceChainId: ChainId.ArbitrumOne, - destinationChainId: ChainId.ArbitrumNova - }) - expect(result1).toBe(false) + it('should throw error for L2 source chain and L2 destination chain', () => { + expect(() => + isDepositMode({ + sourceChainId: ChainId.ArbitrumOne, + destinationChainId: ChainId.ArbitrumNova + }) + ).toThrow(new Error('Arbitrum One to Arbitrum Nova is not supported.')) + + expect(() => + isDepositMode({ + sourceChainId: ChainId.ArbitrumNova, + destinationChainId: ChainId.ArbitrumOne + }) + ).toThrow(new Error('Arbitrum Nova to Arbitrum One is not supported.')) }) }) diff --git a/packages/arb-token-bridge-ui/src/util/isDepositMode.ts b/packages/arb-token-bridge-ui/src/util/isDepositMode.ts index 5d979f1587..ac9ed42078 100644 --- a/packages/arb-token-bridge-ui/src/util/isDepositMode.ts +++ b/packages/arb-token-bridge-ui/src/util/isDepositMode.ts @@ -16,10 +16,22 @@ export function isDepositMode({ const { isEthereumMainnetOrTestnet: isSourceChainEthereum, isArbitrum: isSourceChainArbitrum, - isBase: isSourceChainBase + isBase: isSourceChainBase, + isArbitrumOne: isSourceChainArbitrumOne, + isArbitrumNova: isSourceChainArbitrumNova } = isNetwork(sourceChainId) - const { isOrbitChain: isDestinationChainOrbit } = - isNetwork(destinationChainId) + const { + isOrbitChain: isDestinationChainOrbit, + isArbitrumOne: isDestinationChainArbitrumOne, + isArbitrumNova: isDestinationChainArbitrumNova + } = isNetwork(destinationChainId) + + if (isSourceChainArbitrumOne && isDestinationChainArbitrumNova) { + throw new Error('Arbitrum One to Arbitrum Nova is not supported.') + } + if (isSourceChainArbitrumNova && isDestinationChainArbitrumOne) { + throw new Error('Arbitrum Nova to Arbitrum One is not supported.') + } const isDepositMode = isSourceChainEthereum ||