From 1ec1644ac8791447f6421b3036a7d32f8da9ff5d Mon Sep 17 00:00:00 2001 From: marktoda Date: Tue, 8 Oct 2024 11:22:16 -0400 Subject: [PATCH] feat(ur-sdk): allow commands override (#152) --- sdks/universal-router-sdk/src/index.ts | 18 +++++++++++++-- .../src/utils/commandParser.ts | 23 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/sdks/universal-router-sdk/src/index.ts b/sdks/universal-router-sdk/src/index.ts index aad96d1a0..26eba8bc1 100644 --- a/sdks/universal-router-sdk/src/index.ts +++ b/sdks/universal-router-sdk/src/index.ts @@ -1,7 +1,15 @@ export { SwapRouter, MigrateV3ToV4Options } from './swapRouter' export * from './entities' export * from './utils/routerTradeAdapter' -export { RoutePlanner, CommandType } from './utils/routerCommands' +export { + RoutePlanner, + CommandType, + COMMAND_DEFINITION, + CommandDefinition, + Parser, + Subparser, + ParamType, +} from './utils/routerCommands' export { UNIVERSAL_ROUTER_CREATION_BLOCK, UNIVERSAL_ROUTER_ADDRESS, @@ -9,4 +17,10 @@ export { WETH_ADDRESS, UniversalRouterVersion, } from './utils/constants' -export { CommandParser, UniversalRouterCommand, UniversalRouterCall, Param } from './utils/commandParser' +export { + CommandParser, + UniversalRouterCommand, + UniversalRouterCall, + Param, + CommandsDefinition, +} from './utils/commandParser' diff --git a/sdks/universal-router-sdk/src/utils/commandParser.ts b/sdks/universal-router-sdk/src/utils/commandParser.ts index df0f2e77a..75ae81aaf 100644 --- a/sdks/universal-router-sdk/src/utils/commandParser.ts +++ b/sdks/universal-router-sdk/src/utils/commandParser.ts @@ -2,7 +2,7 @@ import { ethers } from 'ethers' import { abi } from '@uniswap/universal-router/artifacts/contracts/UniversalRouter.sol/UniversalRouter.json' import { Interface } from '@ethersproject/abi' import { V4BaseActionsParser, V4RouterAction } from '@uniswap/v4-sdk' -import { CommandType, COMMAND_DEFINITION, Subparser, Parser } from '../utils/routerCommands' +import { CommandType, CommandDefinition, COMMAND_DEFINITION, Subparser, Parser } from '../utils/routerCommands' export type Param = { readonly name: string @@ -25,19 +25,32 @@ export type V3PathItem = { readonly fee: number } -// Parses UniversalRouter commands +export interface CommandsDefinition { + [key: number]: CommandDefinition +} + +// Parses UniversalRouter V2 commands export abstract class CommandParser { public static INTERFACE: Interface = new Interface(abi) public static parseCalldata(calldata: string): UniversalRouterCall { + const genericParser = new GenericCommandParser(COMMAND_DEFINITION) const txDescription = CommandParser.INTERFACE.parseTransaction({ data: calldata }) const { commands, inputs } = txDescription.args + return genericParser.parse(commands, inputs) + } +} + +// Parses commands based on given command definition +export class GenericCommandParser { + constructor(private readonly commandDefinition: CommandsDefinition) {} - const commandTypes = CommandParser.getCommands(commands) + public parse(commands: string, inputs: string[]): UniversalRouterCall { + const commandTypes = GenericCommandParser.getCommands(commands) return { commands: commandTypes.map((commandType: CommandType, i: number) => { - const commandDef = COMMAND_DEFINITION[commandType] + const commandDef = this.commandDefinition[commandType] if (commandDef.parser === Parser.V4Actions) { const { actions } = V4BaseActionsParser.parseCalldata(inputs[i]) @@ -82,7 +95,7 @@ export abstract class CommandParser { return { commandName: CommandType[commandType], commandType, - params: inputs, + params: inputs.map((input) => ({ name: 'command', value: input })), } } else { throw new Error(`Unsupported parser: ${commandDef}`)