Skip to content

Commit

Permalink
refactor: rename type ChainId to ChainIdType and update reference…
Browse files Browse the repository at this point in the history
…s throughout the codebase
  • Loading branch information
novaliu86 committed Nov 16, 2024
1 parent 4ccbd0b commit cb00659
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ISwapResponse, ITokenPrice, ITokenStaticInfo } from "./common/interfaces.js";
import type { ChainId } from "./common/unames.js";
import type { ChainIdType } from "./common/unames.js";

const resolveErrorAsync = async (response: Response): Promise<{ succeeded: boolean; error: string; }> => {

Expand Down Expand Up @@ -41,7 +41,7 @@ export class SwapnetClient {
) {}

public async getSupportedTokensAsync(
chainId: ChainId,
chainId: ChainIdType,
): Promise<{
succeeded: true,
tokens: ITokenStaticInfo[],
Expand Down Expand Up @@ -72,7 +72,7 @@ export class SwapnetClient {
}

public async swapAsync(
chainId: ChainId,
chainId: ChainIdType,
sellTokenAddress: string,
buyTokenAddress: string,
sellAmount: bigint | undefined,
Expand Down Expand Up @@ -122,7 +122,7 @@ export class SwapnetClient {
}

public async getTokenPricesAsync(
chainId: ChainId,
chainId: ChainIdType,
tokens: string [],
): Promise<{
succeeded: true,
Expand Down
4 changes: 2 additions & 2 deletions src/common/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChainId, LiquiditySourceUname } from "./unames.js";
import type { ChainIdType, LiquiditySourceUname } from "./unames.js";

export interface ITokenAmountInfo {
referenceId: number;
Expand Down Expand Up @@ -72,7 +72,7 @@ export interface ITokenStaticInfo {
}

export interface ITokenPrice {
chainId: ChainId;
chainId: ChainIdType;
address: string;
usdPrice: number;
}
15 changes: 9 additions & 6 deletions src/common/unames.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

export type ChainIdType = 1 | 42161 | 8453 | 81457 | 101;

export enum ChainId {
EthereumMainnet = "1",
ArbitrumOne = "42161",
BaseMainnet = "8453",
BlastMainnet = "81457",
SuiMainnet = "101",
export const ChainId: { [key: string]: ChainIdType } = {
EthereumMainnet: 1,
ArbitrumOne: 42161,
BaseMainnet: 8453,
BlastMainnet: 81457,
SuiMainnet: 101,
};

export const ChainIds = Object.values(ChainId);

export enum LiquiditySourceUname {
UniswapV2 = "UniswapV2",
UniswapV3 = "UniswapV3",
Expand Down
14 changes: 7 additions & 7 deletions src/common/universalRouterDeployments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { ChainId, LiquiditySourceUname } from "./unames.js";
import { type ChainIdType, LiquiditySourceUname } from "./unames.js";

const notDeployed: LiquiditySourceUname [] = [];
const revision1: LiquiditySourceUname [] = [
Expand All @@ -11,10 +11,10 @@ const revision1: LiquiditySourceUname [] = [
LiquiditySourceUname.RingswapV2,
];

export const universalRouterDeploymentByChainId: Record<ChainId, LiquiditySourceUname []> = {
[ChainId.EthereumMainnet]: revision1,
[ChainId.ArbitrumOne]: revision1,
[ChainId.BaseMainnet]: revision1,
[ChainId.BlastMainnet]: revision1,
[ChainId.SuiMainnet]: notDeployed,
export const universalRouterDeploymentByChainId: Record<ChainIdType, LiquiditySourceUname []> = {
1: revision1,
42161: revision1,
8453: revision1,
81457: revision1,
101: notDeployed,
};
4 changes: 2 additions & 2 deletions src/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Wallet, Contract } from "ethers";
import erc20Abi from './abi/erc20.json' assert { type: "json" };
// @ts-ignore
import permit2Abi from './abi/permit2.json' assert { type: "json" };
import { parse, SwapnetClient, type IEncodeOptions, universalRouterByChainId, PERMIT2_ADDRESS, ChainId } from "./index.js";
import { parse, SwapnetClient, type IEncodeOptions, universalRouterByChainId, PERMIT2_ADDRESS, type ChainIdType } from "./index.js";

const approveAsync = async (
sellTokenAddress: string,
Expand All @@ -28,7 +28,7 @@ const approveAsync = async (
};

export const tradeE2eAsync = async (
chainId: ChainId,
chainId: ChainIdType,
sellTokenAddress: string,
buyTokenAddress: string,
sellAmount: bigint,
Expand Down
4 changes: 2 additions & 2 deletions src/routers/routerBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IRoutingPlan } from "../common/routingPlan.js";
import type { ChainId } from "../common/unames.js";
import type { ChainIdType } from "../common/unames.js";
import { toAmountOutMinimum } from "../utils.js";
import { type IEncodeOptions, type IResolvedEncodeOptions, type IRouter } from "./types.js";

Expand All @@ -8,7 +8,7 @@ export abstract class RouterBase implements IRouter {

public constructor(
public readonly name: string,
public readonly chainId: ChainId,
public readonly chainId: ChainIdType,
public readonly routerAddress: string,
public readonly tokenProxyAddress: string | undefined = undefined) {
}
Expand Down
4 changes: 2 additions & 2 deletions src/routers/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IRoutingPlan } from "../common/routingPlan.js";
import type { ChainId } from "../common/unames.js";
import type { ChainIdType } from "../common/unames.js";


export interface IEncodeOptions {
Expand All @@ -20,7 +20,7 @@ export interface IResolvedEncodeOptions {

export interface IRouterInfo {
name: string;
chainId: ChainId;
chainId: ChainIdType;
routerAddress: string;
tokenProxyAddress: string | undefined;
};
Expand Down
12 changes: 6 additions & 6 deletions src/routers/universalRouter/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PartialRecord } from "../../common/typeUtils.js";
import { ChainId } from "../../common/unames.js";
import type { ChainIdType } from "../../common/unames.js";

export const deployedAddressesByChainId: PartialRecord<ChainId, string> = {
[ChainId.EthereumMainnet]: "0x907F8f9BD32549F874e50608cF96829f1D851909",
[ChainId.ArbitrumOne]: "0x59BF0A0823D79bd3a3082Fb8Bd953828ed36EA12",
[ChainId.BaseMainnet]: "0x1f352ecdc178ef919849EeaA6ad3301337fb9CFB",
[ChainId.BlastMainnet]: "0x2C8754B44865002415dD5CEBa6Cd67258D1eCe2e",
export const deployedAddressesByChainId: PartialRecord<ChainIdType, string> = {
1: "0x907F8f9BD32549F874e50608cF96829f1D851909",
42161: "0x59BF0A0823D79bd3a3082Fb8Bd953828ed36EA12",
8453: "0x1f352ecdc178ef919849EeaA6ad3301337fb9CFB",
81457: "0x2C8754B44865002415dD5CEBa6Cd67258D1eCe2e",
};
10 changes: 5 additions & 5 deletions src/routers/universalRouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Interface } from 'ethers';
// @ts-ignore
import universalRouterData from '../../abi/universalRouter.json' assert { type: "json" };
import { type IRoutingPlan } from '../../common/routingPlan.js';
import { ChainId, universalRouterUname } from '../../common/unames.js';
import { universalRouterUname, type ChainIdType } from '../../common/unames.js';
import { PERMIT2_ADDRESS } from '../../ethers-override/permit2AsIf.js';
import { RouterBase, resolveEncodeOptions } from '../routerBase.js';
import type { IEncodeOptions } from '../types.js';
Expand All @@ -17,7 +17,7 @@ import { universalRouterPluginByLiquiditySourceUname } from '../../liquiditySour
const universalRouterInterface: Interface = new Interface(universalRouterData.abi);

export class UniversalRouter extends RouterBase {
public constructor(_chainId: ChainId, _routerAddress: string, _tokenProxyAddress: string = PERMIT2_ADDRESS) {
public constructor(_chainId: ChainIdType, _routerAddress: string, _tokenProxyAddress: string = PERMIT2_ADDRESS) {
super(universalRouterUname, _chainId, _routerAddress, _tokenProxyAddress);
}

Expand Down Expand Up @@ -116,9 +116,9 @@ export class UniversalRouter extends RouterBase {
}
}

export const universalRouterByChainId: Map<ChainId, UniversalRouter> = new Map();
export const universalRouterByChainId: Map<ChainIdType, UniversalRouter> = new Map();

Object.entries(deployedAddressesByChainId).map(([chainIdString, routerAddress]) => {
const chainId = chainIdString as ChainId;
Object.entries(deployedAddressesByChainId).map(([chainIdStr, routerAddress]) => {
const chainId = parseInt(chainIdStr) as ChainIdType;
universalRouterByChainId.set(chainId, new UniversalRouter(chainId, routerAddress));
});

0 comments on commit cb00659

Please sign in to comment.