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

test: add tests for isDepositMode #2031

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
129 changes: 129 additions & 0 deletions packages/arb-token-bridge-ui/src/util/__tests__/isDepositMode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but it would be nice to have enum for chain ids somewhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we can generate enums from the orbit chains data json, but i think the original idea was to only keep core chain's ids as enum

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this test at least, we could move the definition of the chain ids to the outer scope so we don't need to add comments every time.

})
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 true for L1 source chain and L3 destination chain', () => {
const result1 = isDepositMode({
sourceChainId: ChainId.Ethereum,
destinationChainId: 1380012617 // RARI mainnet
})
expect(result1).toBe(true)

const result2 = isDepositMode({
sourceChainId: ChainId.Ethereum,
destinationChainId: 660279 // Xai
})

expect(result2).toBe(true)
})
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 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.'))
})
})
24 changes: 21 additions & 3 deletions packages/arb-token-bridge-ui/src/util/isDepositMode.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,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 ||
Expand Down
Loading