From c5650da703acd71debac661188451e6f3e669ad6 Mon Sep 17 00:00:00 2001 From: David Walsh <5778036+rhinodavid@users.noreply.github.com> Date: Thu, 2 Nov 2023 16:30:05 -0600 Subject: [PATCH] [contract-addresses] Add ChainId type predicate (#756) --- packages/contract-addresses/src/index.ts | 37 +++++++++++-------- .../contract-addresses/test/addresses_test.ts | 11 +++++- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/packages/contract-addresses/src/index.ts b/packages/contract-addresses/src/index.ts index c29a383d82..4a7a5c32f3 100644 --- a/packages/contract-addresses/src/index.ts +++ b/packages/contract-addresses/src/index.ts @@ -1,20 +1,16 @@ import addresses from '../addresses.json'; export interface ContractAddresses { - zrxToken: string; - etherToken: string; - zeroExGovernor: string; - zrxVault: string; - staking: string; - stakingProxy: string; erc20BridgeProxy: string; erc20BridgeSampler: string; - exchangeProxyGovernor: string; + etherToken: string; exchangeProxy: string; - exchangeProxyTransformerDeployer: string; exchangeProxyFlashWallet: string; + exchangeProxyGovernor: string; exchangeProxyLiquidityProviderSandbox: string; - zrxTreasury: string; + exchangeProxyTransformerDeployer: string; + staking: string; + stakingProxy: string; transformers: { wethTransformer: string; payTakerTransformer: string; @@ -22,21 +18,32 @@ export interface ContractAddresses { affiliateFeeTransformer: string; positiveSlippageFeeTransformer: string; }; + zeroExGovernor: string; + zrxToken: string; + zrxTreasury: string; + zrxVault: string; } export enum ChainId { Mainnet = 1, Goerli = 5, - Ganache = 1337, + Optimism = 10, BSC = 56, Polygon = 137, - PolygonMumbai = 80001, - Avalanche = 43114, Fantom = 250, - Celo = 42220, - Optimism = 10, - Arbitrum = 42161, + Ganache = 1337, Base = 8453, + Arbitrum = 42161, + Avalanche = 43114, + Celo = 42220, + PolygonMumbai = 80001, +} + +/** + * Narrow a JavaScript number to a Chain ID. + */ +export function isChainId(chainId: number): chainId is ChainId { + return Object.values(ChainId).includes(chainId); } /** diff --git a/packages/contract-addresses/test/addresses_test.ts b/packages/contract-addresses/test/addresses_test.ts index 0d5829bd3d..33f398e792 100644 --- a/packages/contract-addresses/test/addresses_test.ts +++ b/packages/contract-addresses/test/addresses_test.ts @@ -2,7 +2,7 @@ import * as chai from 'chai'; import { bufferToHex, rlphash } from 'ethereumjs-util'; import 'mocha'; -import { ChainId, getContractAddressesForChainOrThrow } from '../src'; +import { ChainId, getContractAddressesForChainOrThrow, isChainId } from '../src'; const expect = chai.expect; @@ -68,3 +68,12 @@ describe('addresses.json sanity test', () => { }); }); }); + +describe("isChainId", () => { + it("should return true for existing chain ids", () => { + expect(isChainId(1)).to.be.true; + }); + it("should return false for non-existing chain ids", () => { + expect(isChainId(666)).to.be.false; + }); +});