Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ur-sdk): Add V4 Parser to UR parser #147

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[] {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open to feedback on this remapping of names. The alternative was to keep command.params as a V4RouterAction object like

{
   actionName: SWAP_EXACT_IN,
   ActionType: V4Action.SWAP_EXACT_IN,
   params: [...[
}

opted for this to be more standardized with the other non-v4 command datas

return actions.map((action) => {
return {
name: action.actionName,
value: action.params.map((param) => {
return {
name: param.name,
value: param.value,
}
}),
}
})
}
Loading
Loading