Skip to content

Commit

Permalink
feat(ur-sdk): allow commands override (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
marktoda authored Oct 8, 2024
1 parent 3a49888 commit 1ec1644
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
18 changes: 16 additions & 2 deletions sdks/universal-router-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
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,
ROUTER_AS_RECIPIENT,
WETH_ADDRESS,
UniversalRouterVersion,
} from './utils/constants'
export { CommandParser, UniversalRouterCommand, UniversalRouterCall, Param } from './utils/commandParser'
export {
CommandParser,
UniversalRouterCommand,
UniversalRouterCall,
Param,
CommandsDefinition,
} from './utils/commandParser'
23 changes: 18 additions & 5 deletions sdks/universal-router-sdk/src/utils/commandParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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}`)
Expand Down

0 comments on commit 1ec1644

Please sign in to comment.