Skip to content

Commit

Permalink
[contract-addresses] Add ChainId type predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinodavid committed Oct 27, 2023
1 parent b19e29e commit ac5e271
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
37 changes: 22 additions & 15 deletions packages/contract-addresses/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
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;
fillQuoteTransformer: string;
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);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/contract-addresses/test/addresses_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
});
});

0 comments on commit ac5e271

Please sign in to comment.