Skip to content

Commit

Permalink
enforce adoption to enum LiquiditySourceUname
Browse files Browse the repository at this point in the history
  • Loading branch information
novaliu86 committed Nov 13, 2024
1 parent f8cb448 commit 8de8d27
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
3 changes: 2 additions & 1 deletion src/common/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LiquiditySourceUname } from "./unames.js";

export interface ITokenAmountInfo {
referenceId: number;
Expand Down Expand Up @@ -42,7 +43,7 @@ export type IEncodingDetails = IUniswapV3Details | ILimitOrderDetailsBase;

export interface IRouteInfoInResponse {
address: string;
name: string;
name: LiquiditySourceUname;
details?: IEncodingDetails;
fromTokens: Array<ITokenAmountInfo>;
toTokens: Array<ITokenAmountInfo>;
Expand Down
3 changes: 2 additions & 1 deletion src/common/routingPlan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { LiquiditySourceUname } from "./unames.js";

export interface LiquidityInfoBase {
source: string;
source: LiquiditySourceUname;
address: string;
};

Expand Down
95 changes: 48 additions & 47 deletions src/routers/universalRouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { IEncodeOptions } from '../types.js';
import { deployedAddressesByChainId } from './addresses.js';
import { getFewWrappedTokenAddress } from './fewTokenHelper.js';
import { CommandType, RoutePlanner, UniswapV2ForkNames, UniswapV3ForkNames, type IPermitWithSignature } from './routerCommands.js';
import { LiquiditySourceUname } from '../../common/unames.js';


const CONTRACT_BALANCE = 2n ** 255n;
Expand All @@ -26,35 +27,35 @@ const encodeV3RouteToPath = (inputTokenAddress: string, outputTokenAddress: stri
return solidityPacked(types, path)
}

const toV2ForkName = (protocol: string): UniswapV2ForkNames => {
if (protocol === "UniswapV2") {
const toV2ForkName = (source: LiquiditySourceUname): UniswapV2ForkNames => {
if (source === LiquiditySourceUname.UniswapV2) {
return UniswapV2ForkNames.Uniswap;
}
else if (protocol === "ThrusterV2-3k") {
else if (source === LiquiditySourceUname.ThrusterV2_3k) {
return UniswapV2ForkNames.Thruster3k;
}
else if (protocol === "ThrusterV2-10k") {
else if (source === LiquiditySourceUname.ThrusterV2_10k) {
return UniswapV2ForkNames.Thruster10k;
}
else if (protocol === "RingswapV2") {
else if (source === LiquiditySourceUname.RingswapV2) {
return UniswapV2ForkNames.Ringswap;
}

throw new Error(`Invalid V2 protocol ${protocol}!`);
throw new Error(`Invalid V2 liquidity source ${source}!`);
}

const toV3ForkName = (protocol: string): UniswapV3ForkNames => {
if (protocol === "UniswapV3") {
const toV3ForkName = (source: LiquiditySourceUname): UniswapV3ForkNames => {
if (source === LiquiditySourceUname.UniswapV3) {
return UniswapV3ForkNames.Uniswap;
}
else if (protocol === "ThrusterV3") {
else if (source === LiquiditySourceUname.ThrusterV3) {
return UniswapV3ForkNames.Thruster;
}
else if (protocol === "RingswapV3") {
return UniswapV3ForkNames.Ringswap;
}
// else if (source === LiquiditySourceUname.RingswapV3) {
// return UniswapV3ForkNames.Ringswap;
// }

throw new Error(`Invalid V3 protocol ${protocol}!`);
throw new Error(`Invalid V3 protocol ${source}!`);
}

export class UniversalRouter extends RouterBase {
Expand Down Expand Up @@ -121,13 +122,13 @@ export class UniversalRouter extends RouterBase {
}

if (
liquidityInfo.source === "UniswapV2" ||
liquidityInfo.source === "ThrusterV2-3k" ||
liquidityInfo.source === "ThrusterV2-10k" ||
liquidityInfo.source === "RingswapV2"
liquidityInfo.source === LiquiditySourceUname.UniswapV2 ||
liquidityInfo.source === LiquiditySourceUname.ThrusterV2_3k ||
liquidityInfo.source === LiquiditySourceUname.ThrusterV2_10k ||
liquidityInfo.source === LiquiditySourceUname.RingswapV2
) {
let path = [ fromToken, toToken ];
if (liquidityInfo.source === "RingswapV2") {
if (liquidityInfo.source === LiquiditySourceUname.RingswapV2) {
const fewWrappedFromToken = getFewWrappedTokenAddress(fromToken);
const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
path = [ fewWrappedFromToken, fewWrappedToToken ];
Expand All @@ -149,7 +150,7 @@ export class UniversalRouter extends RouterBase {
toV2ForkName(liquidityInfo.source),
]);

if (liquidityInfo.source === "RingswapV2") {
if (liquidityInfo.source === LiquiditySourceUname.RingswapV2) {
const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
planner.addCommand(CommandType.WRAP_UNWRAP_FEW_TOKEN, [
fewWrappedToToken,
Expand All @@ -160,26 +161,26 @@ export class UniversalRouter extends RouterBase {
}
}
else if (
liquidityInfo.source === "UniswapV3" ||
liquidityInfo.source === "ThrusterV3" ||
liquidityInfo.source === "RingswapV3"
liquidityInfo.source === LiquiditySourceUname.UniswapV3 ||
// liquidityInfo.source === LiquiditySourceUname.RingswapV3
liquidityInfo.source === LiquiditySourceUname.ThrusterV3
) {
let path: string;
if (liquidityInfo.source === "RingswapV3") {
const fewWrappedFromToken = getFewWrappedTokenAddress(fromToken);
const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
path = encodeV3RouteToPath(fewWrappedFromToken, fewWrappedToToken, Number((liquidityInfo as UniswapV3Info).fee));

planner.addCommand(CommandType.WRAP_UNWRAP_FEW_TOKEN, [
fromToken,
ROUTER_AS_RECIPIENT,
amountIn,
true,
]);
}
else {
path = encodeV3RouteToPath(fromToken, toToken, Number((liquidityInfo as UniswapV3Info).fee));
}
// if (liquidityInfo.source === LiquiditySourceUname.RingswapV3) {
// const fewWrappedFromToken = getFewWrappedTokenAddress(fromToken);
// const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
// path = encodeV3RouteToPath(fewWrappedFromToken, fewWrappedToToken, Number((liquidityInfo as UniswapV3Info).fee));

// planner.addCommand(CommandType.WRAP_UNWRAP_FEW_TOKEN, [
// fromToken,
// ROUTER_AS_RECIPIENT,
// amountIn,
// true,
// ]);
// }
// else {
path = encodeV3RouteToPath(fromToken, toToken, Number((liquidityInfo as UniswapV3Info).fee));
// }

planner.addCommand(CommandType.V3_SWAP_EXACT_IN, [
ROUTER_AS_RECIPIENT, // recipientIsUser
Expand All @@ -190,17 +191,17 @@ export class UniversalRouter extends RouterBase {
toV3ForkName(liquidityInfo.source),
]);

if (liquidityInfo.source === "RingswapV3") {
const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
planner.addCommand(CommandType.WRAP_UNWRAP_FEW_TOKEN, [
fewWrappedToToken,
ROUTER_AS_RECIPIENT,
0n, // minAmountOut
false,
]);
}
// if (liquidityInfo.source === LiquiditySourceUname.RingswapV3) {
// const fewWrappedToToken = getFewWrappedTokenAddress(toToken);
// planner.addCommand(CommandType.WRAP_UNWRAP_FEW_TOKEN, [
// fewWrappedToToken,
// ROUTER_AS_RECIPIENT,
// 0n, // minAmountOut
// false,
// ]);
// }
}
else if (liquidityInfo.source === "CurveV1") {
else if (liquidityInfo.source === LiquiditySourceUname.CurveV1) {
planner.addCommand(CommandType.CURVE_V1, [
liquidityInfo.address,
fromToken,
Expand Down

0 comments on commit 8de8d27

Please sign in to comment.