Skip to content

Commit

Permalink
rewrite parser with plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
novaliu86 committed Nov 14, 2024
1 parent 25dd0f3 commit c82b6a3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 63 deletions.
88 changes: 88 additions & 0 deletions src/liquiditySourcePlugins/parserPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

import { LiquiditySourceUname, type IBebopLimitOrderDetails, type IRouteInfoInResponse, type ISwapnetLimitOrderDetails, type IUniswapV3Details, type LiquidityInfo, type PartialRecord } from "../../sdk/index.js";

Check failure on line 2 in src/liquiditySourcePlugins/parserPlugins.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20 and ubuntu-latest

Cannot find module '../../sdk/index.js' or its corresponding type declarations.


const converWithoutDetails = (route: IRouteInfoInResponse): LiquidityInfo => {
return {
source: route.name,
address: route.address,
};
};

const converWithFee = (route: IRouteInfoInResponse): LiquidityInfo => {
if (route.details === undefined || (route.details as IUniswapV3Details).fee === undefined) {
throw new Error(`Invalid Uniswap V3 route details!`);
}

const fee: bigint = BigInt((route.details as IUniswapV3Details).fee);

return {
source: route.name,
address: route.address,
fee,
};
};

export interface ILiquiditySourceParserPlugin {
converToLiquidityInfo: (route: IRouteInfoInResponse) => LiquidityInfo;
};


export const parserPluginByLiquiditySourceUname: PartialRecord<LiquiditySourceUname, ILiquiditySourceParserPlugin> = {
[LiquiditySourceUname.UniswapV2]: {
converToLiquidityInfo: converWithoutDetails,
},
[LiquiditySourceUname.ThrusterV2_3k]: {
converToLiquidityInfo: converWithoutDetails,
},
[LiquiditySourceUname.ThrusterV2_10k]: {
converToLiquidityInfo: converWithoutDetails,
},
[LiquiditySourceUname.RingswapV2]: {
converToLiquidityInfo: converWithoutDetails,
},
[LiquiditySourceUname.UniswapV3]: {
converToLiquidityInfo: converWithFee,
},
[LiquiditySourceUname.PancakeswapV3]: {
converToLiquidityInfo: converWithFee,
},
[LiquiditySourceUname.ThrusterV3]: {
converToLiquidityInfo: converWithFee,
},
// [LiquiditySourceUname.RingswapV3]: {
// converToLiquidityInfo: converWithFee,
// },
[LiquiditySourceUname.CurveV1]: {
converToLiquidityInfo: converWithoutDetails,
},
[LiquiditySourceUname.BebopLimitOrder]: {
converToLiquidityInfo: (route: IRouteInfoInResponse): LiquidityInfo => {
const details = route.details as IBebopLimitOrderDetails;
return {
source: route.name,
address: route.address,
isSingleOrder: details.isSingleOrder,
calldata: details.calldata,
partialFillOffset: details.partialFillOffset,
};
},
},
[LiquiditySourceUname.NativeLimitOrder]: {
converToLiquidityInfo: (route: IRouteInfoInResponse): LiquidityInfo => {
const details = route.details as ISwapnetLimitOrderDetails;
return {
source: route.name,
address: details.maker,
maker: details.maker,
makerToken: details.makerToken,
takerToken: details.takerToken,
makerAmount: BigInt(details.makerAmount),
takerAmount: BigInt(details.takerAmount),
nonce: BigInt(details.nonce),
deadline: BigInt(details.deadline),
signature: details.signature,
};
},
},
};
74 changes: 11 additions & 63 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,25 @@

import Graph from "graph-data-structure";
import { type IBebopLimitOrderDetails, type IRouteInfoInResponse, type ISwapnetLimitOrderDetails, type ISwapResponse, type IUniswapV3Details, } from "./common/interfaces.js";

import { type IRouteInfoInResponse, type ISwapResponse, } from "./common/interfaces.js";
import { type Swap, type TokenOperation, type IRoutingPlan, type LiquidityInfo, } from "./common/routingPlan.js";
import { LiquiditySourceUname } from "./common/unames.js";
import { parserPluginByLiquiditySourceUname } from "./liquiditySourcePlugins/parserPlugins.js";


const toSwap = (route: IRouteInfoInResponse, tokenOpsById: Map<number, TokenOperation>): Swap => {

let liquidityInfo: LiquidityInfo;
if (
route.name === LiquiditySourceUname.UniswapV2 ||
route.name === LiquiditySourceUname.ThrusterV2_3k ||
route.name === LiquiditySourceUname.ThrusterV2_10k ||
route.name === LiquiditySourceUname.RingswapV2
const plugin = parserPluginByLiquiditySourceUname[route.name];

) {
liquidityInfo = {
source: route.name,
address: route.address,
}
}
else if (
route.name === LiquiditySourceUname.UniswapV3 ||
route.name === LiquiditySourceUname.PancakeswapV3 ||
route.name === LiquiditySourceUname.ThrusterV3
) {
if (route.details === undefined || (route.details as IUniswapV3Details).fee === undefined) {
throw new Error(`Invalid Uniswap V3 route details!`);
}

let fee: bigint = BigInt((route.details as IUniswapV3Details).fee);

liquidityInfo = {
source: route.name,
address: route.address,
fee,
}
}
else if (route.name === LiquiditySourceUname.CurveV1) {
let liquidityInfo: LiquidityInfo;
if (plugin === undefined) {
liquidityInfo = {
source: route.name,
address: route.address,
}
}
else if (route.name === LiquiditySourceUname.BebopLimitOrder) {
const details = route.details as IBebopLimitOrderDetails;
liquidityInfo = {
source: route.name,
address: route.address,
isSingleOrder: details.isSingleOrder,
calldata: details.calldata,
partialFillOffset: details.partialFillOffset,
};
}
else if (route.name === LiquiditySourceUname.NativeLimitOrder) {
const details = route.details as ISwapnetLimitOrderDetails;
liquidityInfo = {
source: route.name,
address: details.maker,
maker: details.maker,
makerToken: details.makerToken,
takerToken: details.takerToken,
makerAmount: BigInt(details.makerAmount),
takerAmount: BigInt(details.takerAmount),
nonce: BigInt(details.nonce),
deadline: BigInt(details.deadline),
signature: details.signature,
};
// throw new Error(`Invalid route name ${route.name}!`);
}
else {
liquidityInfo = {
source: route.name,
address: route.address,
}
// throw new Error(`Invalid route name ${route.name}!`);
liquidityInfo = plugin.converToLiquidityInfo(route);
}

const fromTokenOp = tokenOpsById.get(route.fromTokens[0].referenceId);
Expand All @@ -89,7 +37,7 @@ const toSwap = (route: IRouteInfoInResponse, tokenOpsById: Map<number, TokenOper
amountIn: BigInt(route.fromTokens[0].amount),
amountOut: BigInt(route.toTokens[0].amount),
liquidityInfo,
}
};
}

export const parse = (swapResponse: ISwapResponse): IRoutingPlan => {
Expand Down

0 comments on commit c82b6a3

Please sign in to comment.