Skip to content

Commit

Permalink
feat: sort chain IDs with core chains on top (#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkx authored Oct 3, 2024
1 parent 7c9c055 commit 508ee40
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
45 changes: 45 additions & 0 deletions packages/arb-token-bridge-ui/src/util/__tests__/networks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { registerCustomArbitrumNetwork } from '@arbitrum/sdk'
import {
ChainId,
getBaseChainIdByChainId,
getDestinationChainIds,
getSupportedChainIds
} from '../networks'
import { orbitTestnets } from '../orbitChainsList'
Expand Down Expand Up @@ -210,3 +211,47 @@ describe('getSupportedChainIds', () => {
})
})
})

describe('getDestinationChainIds', () => {
function isAscending(arr: number[]) {
return arr.every(
(value, index) => index === 0 || value >= Number(arr[index - 1])
)
}

it('should return a sorted list for Ethereum Mainnet', () => {
const destinationChainIds = getDestinationChainIds(ChainId.Ethereum)
const defaultChainId = destinationChainIds[0]
const nonDefaultChainIds = destinationChainIds.slice(1)

expect(defaultChainId).toBe(ChainId.ArbitrumOne)
expect(isAscending(nonDefaultChainIds)).toBe(true)
})

it('should return a sorted list for Arbitrum One', () => {
const destinationChainIds = getDestinationChainIds(ChainId.ArbitrumOne)
const defaultChainId = destinationChainIds[0]
const nonDefaultChainIds = destinationChainIds.slice(1)

expect(defaultChainId).toBe(ChainId.Ethereum)
expect(isAscending(nonDefaultChainIds)).toBe(true)
})

it('should return a sorted list for Sepolia', () => {
const destinationChainIds = getDestinationChainIds(ChainId.Sepolia)
const defaultChainId = destinationChainIds[0]
const nonDefaultChainIds = destinationChainIds.slice(1)

expect(defaultChainId).toBe(ChainId.ArbitrumSepolia)
expect(isAscending(nonDefaultChainIds)).toBe(true)
})

it('should return a sorted list for Arbitrum Sepolia', () => {
const destinationChainIds = getDestinationChainIds(ChainId.ArbitrumSepolia)
const defaultChainId = destinationChainIds[0]
const nonDefaultChainIds = destinationChainIds.slice(1)

expect(defaultChainId).toBe(ChainId.Sepolia)
expect(isAscending(nonDefaultChainIds)).toBe(true)
})
})
30 changes: 27 additions & 3 deletions packages/arb-token-bridge-ui/src/util/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,31 @@ export function getChildChainIds(chain: ArbitrumNetwork | L1Network) {
return Array.from(new Set(childChainIds))
}

/**
* Sorts an array of chain IDs in ascending order (default) but keep core chains on top.
* This is helpful e.g. when we grab the default chain which is the first chain on top.
*/
export function sortChainIds(chainIds: number[]) {
return chainIds.sort((a, b) => {
const { isCoreChain: isCoreChainA } = isNetwork(a)
const { isCoreChain: isCoreChainB } = isNetwork(b)

if (isCoreChainA && isCoreChainB) {
// Both are core chains, sort in ascending order
return a - b
} else if (isCoreChainA) {
// Only A is core chain, it should come first
return -1
} else if (isCoreChainB) {
// Only B is core chain, it should come first
return 1
} else {
// Neither are core chains, sort in ascending order
return a - b
}
})
}

export function getDestinationChainIds(chainId: ChainId): ChainId[] {
const chain = getChainByChainId(chainId)

Expand All @@ -499,9 +524,8 @@ export function getDestinationChainIds(chainId: ChainId): ChainId[] {
const validDestinationChainIds = getChildChainIds(chain)

if (parentChainId) {
// always make parent chain the first element
return [parentChainId, ...validDestinationChainIds]
return sortChainIds([parentChainId, ...validDestinationChainIds])
}

return validDestinationChainIds
return sortChainIds(validDestinationChainIds)
}

0 comments on commit 508ee40

Please sign in to comment.