Skip to content

Commit

Permalink
♻️ (chore): extract chain-related code to ./utils/chains
Browse files Browse the repository at this point in the history
  • Loading branch information
nickadamson committed Dec 28, 2023
1 parent b4e3466 commit dfca83d
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 42 deletions.
15 changes: 0 additions & 15 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
8 changes: 6 additions & 2 deletions src/entities/assets/index.ts
Original file line number Diff line number Diff line change
@@ -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';
5 changes: 3 additions & 2 deletions src/entities/assets/supported-asset.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/entities/assets/supported-asset.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
OptionAssetPair,
SupportedAsset,
SUPPORTED_ASSETS,
type SupportedAssetSymbol,
} from './assets';
export {
type ContractConstructorArgs,
Expand Down
7 changes: 2 additions & 5 deletions src/entities/options/option-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/entities/options/subgraph-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -23,6 +22,7 @@ export {
OptionAssetPair,
SupportedAsset,
SUPPORTED_ASSETS,
type SupportedAssetSymbol,
type ContractConstructorArgs,
type IClearinghouse,
type IERC20,
Expand Down Expand Up @@ -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,
Expand All @@ -298,5 +292,9 @@ export {
type Market,
type OptionData,
type Underlying,
SUPPORTED_CHAINS,
type SupportedChain,
type SupportedChainId,
isSupportedChainId,
} from './utils';
export { ValoremSDK } from './sdk';
7 changes: 0 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
Chain,
WriteContractParameters,
} from 'viem';
import type { SUPPORTED_CHAINS } from './constants';

export type SimulatedTxRequest = WriteContractParameters<
Abi,
Expand All @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions src/utils/chains.ts
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit dfca83d

Please sign in to comment.