From dfca83d3cee63a6e31bb126ecdcd4d3838005bc4 Mon Sep 17 00:00:00 2001 From: Nick Adamson Date: Thu, 28 Dec 2023 10:19:36 -0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(chore):=20extract=20chain?= =?UTF-8?q?-related=20code=20to=20`./utils/chains`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants.ts | 15 ---------- src/entities/assets/index.ts | 8 ++++-- src/entities/assets/supported-asset.test.ts | 5 ++-- src/entities/assets/supported-asset.ts | 4 ++- src/entities/index.ts | 1 + src/entities/options/option-type.ts | 7 ++--- src/entities/options/subgraph-position.ts | 4 +-- src/index.ts | 14 ++++------ src/types.ts | 7 ----- src/utils/chains.ts | 31 +++++++++++++++++++++ src/utils/index.ts | 6 ++++ 11 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 src/utils/chains.ts diff --git a/src/constants.ts b/src/constants.ts index 70b1fc40..e08b6092 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,18 +1,3 @@ -import { - arbitrum, - arbitrumSepolia, - arbitrumGoerli, - foundry, -} from 'viem/chains'; - -/** Chains */ -export const SUPPORTED_CHAINS = { - arbitrum, - arbitrumSepolia, - arbitrumGoerli, - foundry, -}; - /** Contracts */ // Valorem Clearinghouse on Arbitrum One (mainnet) & Arbitrum Goerli (testnet) export const CLEAR_ADDRESS = '0x402A401B1944EBb5A3030F36Aa70d6b5794190c9'; diff --git a/src/entities/assets/index.ts b/src/entities/assets/index.ts index d62ca61d..20d67e4d 100644 --- a/src/entities/assets/index.ts +++ b/src/entities/assets/index.ts @@ -1,3 +1,7 @@ -export { Asset, type ERC20Token } from './asset'; +export { type ERC20Token, Asset } from './asset'; export { OptionAssetPair } from './asset-pair'; -export { SupportedAsset, SUPPORTED_ASSETS } from './supported-asset'; +export { + type SupportedAssetSymbol, + SupportedAsset, + SUPPORTED_ASSETS, +} from './supported-asset'; diff --git a/src/entities/assets/supported-asset.test.ts b/src/entities/assets/supported-asset.test.ts index 9bb5570b..a5629cff 100644 --- a/src/entities/assets/supported-asset.test.ts +++ b/src/entities/assets/supported-asset.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; -import type { SupportedAssetSymbol, SupportedChainId } from '../../types'; -import { SUPPORTED_CHAINS } from '../../constants'; +import type { SupportedChainId } from '../../utils/chains'; +import { SUPPORTED_CHAINS } from '../../utils/chains'; +import type { SupportedAssetSymbol } from './supported-asset'; import { SupportedAsset, SUPPORTED_ASSETS } from './supported-asset'; const arbitrumWETH = SUPPORTED_ASSETS.filter( diff --git a/src/entities/assets/supported-asset.ts b/src/entities/assets/supported-asset.ts index 15e96fd0..6deb8808 100644 --- a/src/entities/assets/supported-asset.ts +++ b/src/entities/assets/supported-asset.ts @@ -1,8 +1,10 @@ import type { Address } from 'viem'; -import type { SupportedAssetSymbol, SupportedChainId } from '../../types'; +import type { SupportedChainId } from '../../utils/chains'; import type { ERC20Token } from './asset'; import { Asset } from './asset'; +export type SupportedAssetSymbol = 'USDC' | 'WETH'; + interface SupportedERC20Token extends ERC20Token { chainId: SupportedChainId; symbol: SupportedAssetSymbol; diff --git a/src/entities/index.ts b/src/entities/index.ts index 7af493c4..556ef000 100644 --- a/src/entities/index.ts +++ b/src/entities/index.ts @@ -4,6 +4,7 @@ export { OptionAssetPair, SupportedAsset, SUPPORTED_ASSETS, + type SupportedAssetSymbol, } from './assets'; export { type ContractConstructorArgs, diff --git a/src/entities/options/option-type.ts b/src/entities/options/option-type.ts index bf91ec53..3c62ddfa 100644 --- a/src/entities/options/option-type.ts +++ b/src/entities/options/option-type.ts @@ -6,11 +6,8 @@ import { sliceHex, toBytes, } from 'viem'; -import type { - OptionTypeInfo, - SimulatedTxRequest, - SupportedChainId, -} from '../../types'; +import type { OptionTypeInfo, SimulatedTxRequest } from '../../types'; +import type { SupportedChainId } from '../../utils/chains'; import type { Trader } from '../trader/base-trader'; import type { ClearinghouseContract } from '../contracts/clearinghouse'; import { SupportedAsset } from '../assets/supported-asset'; diff --git a/src/entities/options/subgraph-position.ts b/src/entities/options/subgraph-position.ts index f3293cdf..c37af291 100644 --- a/src/entities/options/subgraph-position.ts +++ b/src/entities/options/subgraph-position.ts @@ -6,6 +6,7 @@ import type { SubgraphOptionType, } from '../../lib/subgraph/types'; import { BASE_SCALAR_BN } from '../../constants'; +import { isSupportedChainId } from '../../utils/chains'; import { OptionType } from './option-type'; export class SubgraphPosition extends OptionType { @@ -194,8 +195,7 @@ export class SubgraphPosition extends OptionType { subgraphPosition: SubgraphOptionPosition; chainId: number; }) { - if (chainId !== 42161 && chainId !== 421613) - throw new Error('Unsupported chainId'); + if (!isSupportedChainId(chainId)) throw new Error('Unsupported chainId'); // determine if ERC-1155 is exercisable option or redeemable claim const isClaim = subgraphPosition.token.type === 2; diff --git a/src/index.ts b/src/index.ts index c0bf360d..153fbd4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ /* eslint-disable camelcase */ export { CLEAR_ABI, SEAPORT_V1_5_ABI, SEAPORT_VALIDATOR_ABI } from './abi'; export { - SUPPORTED_CHAINS, CLEAR_ADDRESS, CLEAR_ADDRESS_FOUNDRY, SEAPORT_ADDRESS, @@ -23,6 +22,7 @@ export { OptionAssetPair, SupportedAsset, SUPPORTED_ASSETS, + type SupportedAssetSymbol, type ContractConstructorArgs, type IClearinghouse, type IERC20, @@ -280,13 +280,7 @@ export { type SubgraphClaimERC1155, type SubgraphOptionType, } from './lib'; -export type { - SimulatedTxRequest, - SupportedChain, - SupportedChainId, - SupportedAssetSymbol, - OptionTypeInfo, -} from './types'; +export type { SimulatedTxRequest, OptionTypeInfo } from './types'; export { createSIWEMessage, toUnix, @@ -298,5 +292,9 @@ export { type Market, type OptionData, type Underlying, + SUPPORTED_CHAINS, + type SupportedChain, + type SupportedChainId, + isSupportedChainId, } from './utils'; export { ValoremSDK } from './sdk'; diff --git a/src/types.ts b/src/types.ts index 37e1a688..780664c7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,6 @@ import type { Chain, WriteContractParameters, } from 'viem'; -import type { SUPPORTED_CHAINS } from './constants'; export type SimulatedTxRequest = WriteContractParameters< Abi, @@ -15,12 +14,6 @@ export type SimulatedTxRequest = WriteContractParameters< Chain >; -export type SupportedChain = - (typeof SUPPORTED_CHAINS)[keyof typeof SUPPORTED_CHAINS]; -export type SupportedChainId = SupportedChain['id']; - -export type SupportedAssetSymbol = 'USDC' | 'WETH'; - // TODO(These types need to be extended and enhanced to support the full range of types used) // And to favor enums and true data keys over token symbols and strings/numbers interpolated // throughout the codebase. diff --git a/src/utils/chains.ts b/src/utils/chains.ts new file mode 100644 index 00000000..dc1fb494 --- /dev/null +++ b/src/utils/chains.ts @@ -0,0 +1,31 @@ +import { + arbitrum, + arbitrumSepolia, + arbitrumGoerli, + foundry, +} from 'viem/chains'; + +export type SupportedChain = + (typeof SUPPORTED_CHAINS)[keyof typeof SUPPORTED_CHAINS]; +export type SupportedChainId = SupportedChain['id']; + +export const SUPPORTED_CHAINS = { + arbitrum, + arbitrumSepolia, + arbitrumGoerli, + foundry, +}; + +/** + * Determines whether a given chain ID corresponds to a supported chain. + * + * @param chainId - The chain ID to check. + * @returns True if the chainId is supported, otherwise false. + */ +export function isSupportedChainId( + chainId: number, +): chainId is SupportedChainId { + return Object.values(SUPPORTED_CHAINS) + .map((chain) => chain.id) + .includes(chainId as SupportedChainId); +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 10bb9992..26c1ddd2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,9 @@ +export { + type SupportedChain, + type SupportedChainId, + SUPPORTED_CHAINS, + isSupportedChainId, +} from './chains'; export { createSIWEMessage } from './siwe'; export { toUnix, get8AMUTCDate, get24HrTimestamps } from './timestamps'; export { Brent, OptionsGreeks, TypeOfOption } from './vol';