Skip to content

Commit

Permalink
feat(ur-sdk): Add V4 Parser to UR parser (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
marktoda authored Oct 4, 2024
1 parent e825eb6 commit d22530b
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 132 deletions.
2 changes: 1 addition & 1 deletion sdks/universal-router-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@uniswap/v2-sdk": "^4.6.0",
"@uniswap/v3-core": "1.0.0",
"@uniswap/v3-sdk": "^3.17.0",
"@uniswap/v4-sdk": "^1.6.3",
"@uniswap/v4-sdk": "^1.10.0",
"bignumber.js": "^9.0.2",
"ethers": "^5.7.0"
},
Expand Down
93 changes: 64 additions & 29 deletions sdks/universal-router-sdk/src/utils/commandParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ethers } from 'ethers'
import { abi } from '@uniswap/universal-router/artifacts/contracts/UniversalRouter.sol/UniversalRouter.json'
import { Interface } from '@ethersproject/abi'
import { CommandType, COMMAND_ABI_DEFINITION, Subparser } from '../utils/routerCommands'
import { V4BaseActionsParser, V4RouterAction } from '@uniswap/v4-sdk'
import { CommandType, COMMAND_DEFINITION, Subparser, Parser } from '../utils/routerCommands'

export type Param = {
readonly name: string
Expand Down Expand Up @@ -36,35 +37,55 @@ export abstract class CommandParser {

return {
commands: commandTypes.map((commandType: CommandType, i: number) => {
const abiDef = COMMAND_ABI_DEFINITION[commandType]
const rawParams = ethers.utils.defaultAbiCoder.decode(
abiDef.map((command) => command.type),
inputs[i]
)
const params = rawParams.map((param: any, j: number) => {
switch (abiDef[j].subparser) {
case Subparser.V3PathExactIn:
return {
name: abiDef[j].name,
value: parseV3PathExactIn(param),
}
case Subparser.V3PathExactOut:
return {
name: abiDef[j].name,
value: parseV3PathExactOut(param),
}
default:
return {
name: abiDef[j].name,
value: param,
}
const commandDef = COMMAND_DEFINITION[commandType]

if (commandDef.parser === Parser.V4Actions) {
const { actions } = V4BaseActionsParser.parseCalldata(inputs[i])
return {
commandName: CommandType[commandType],
commandType,
params: v4RouterCallToParams(actions),
}
})

return {
commandName: CommandType[commandType],
commandType,
params,
} else if (commandDef.parser === Parser.Abi) {
const abiDef = commandDef.params
const rawParams = ethers.utils.defaultAbiCoder.decode(
abiDef.map((command) => command.type),
inputs[i]
)

const params = rawParams.map((param: any, j: number) => {
switch (abiDef[j].subparser) {
case Subparser.V3PathExactIn:
return {
name: abiDef[j].name,
value: parseV3PathExactIn(param),
}
case Subparser.V3PathExactOut:
return {
name: abiDef[j].name,
value: parseV3PathExactOut(param),
}
default:
return {
name: abiDef[j].name,
value: param,
}
}
})
return {
commandName: CommandType[commandType],
commandType,
params,
}
} else if (commandDef.parser === Parser.V3Actions) {
// TODO: implement better parsing here
return {
commandName: CommandType[commandType],
commandType,
params: inputs,
}
} else {
throw new Error(`Unsupported parser: ${commandDef}`)
}
}),
}
Expand Down Expand Up @@ -127,3 +148,17 @@ export function parseV3PathExactOut(path: string): readonly V3PathItem[] {

return res
}

function v4RouterCallToParams(actions: readonly V4RouterAction[]): readonly Param[] {
return actions.map((action) => {
return {
name: action.actionName,
value: action.params.map((param) => {
return {
name: param.name,
value: param.value,
}
}),
}
})
}
Loading

0 comments on commit d22530b

Please sign in to comment.