Skip to content

Commit ad7a559

Browse files
authored
feat(v4-sdk): add URVersion parameter to addAction for V2.1 encoding (#502)
1 parent 8734c7f commit ad7a559

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

sdks/v4-sdk/src/utils/v4Planner.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,72 @@ describe('RouterPlanner', () => {
8787
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000006f05b59d3b2000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000'
8888
)
8989
})
90+
91+
it('encodes a v4 exactIn swap with V2.1 (includes maxHopSlippage)', async () => {
92+
const route = new Route([DAI_USDC, USDC_WETH], DAI, WETH9[1])
93+
const maxHopSlippage = [BigNumber.from('10000'), BigNumber.from('20000')]
94+
95+
planner.addAction(
96+
Actions.SWAP_EXACT_IN,
97+
[
98+
{
99+
currencyIn: DAI.address,
100+
path: encodeRouteToPath(route),
101+
maxHopSlippage: maxHopSlippage,
102+
amountIn: ONE_ETHER_BN.toString(),
103+
amountOutMinimum: 0,
104+
},
105+
],
106+
URVersion.V2_1
107+
)
108+
109+
expect(planner.actions).toEqual('0x07')
110+
111+
// Decode with V2.1 ABI to verify maxHopSlippage values
112+
const decoded = defaultAbiCoder.decode(
113+
V4_SWAP_ACTIONS_V2_1[Actions.SWAP_EXACT_IN].map((v) => v.type),
114+
planner.params[0]
115+
)
116+
117+
expect(decoded[0].currencyIn).toEqual(DAI.address)
118+
expect(decoded[0].maxHopSlippage).toHaveLength(2)
119+
expect(decoded[0].maxHopSlippage[0].toString()).toEqual('10000')
120+
expect(decoded[0].maxHopSlippage[1].toString()).toEqual('20000')
121+
expect(decoded[0].amountIn.toString()).toEqual(ONE_ETHER_BN.toString())
122+
})
123+
124+
it('encodes a v4 exactOut swap with V2.1 (includes maxHopSlippage)', async () => {
125+
const route = new Route([DAI_USDC, USDC_WETH], DAI, WETH9[1])
126+
const maxHopSlippage = [BigNumber.from('15000'), BigNumber.from('25000')]
127+
128+
planner.addAction(
129+
Actions.SWAP_EXACT_OUT,
130+
[
131+
{
132+
currencyOut: WETH9[1].address,
133+
path: encodeRouteToPath(route, true),
134+
maxHopSlippage: maxHopSlippage,
135+
amountOut: ONE_ETHER_BN.toString(),
136+
amountInMaximum: ONE_ETHER_BN.mul(2).toString(),
137+
},
138+
],
139+
URVersion.V2_1
140+
)
141+
142+
expect(planner.actions).toEqual('0x09')
143+
144+
// Decode with V2.1 ABI to verify maxHopSlippage values
145+
const decoded = defaultAbiCoder.decode(
146+
V4_SWAP_ACTIONS_V2_1[Actions.SWAP_EXACT_OUT].map((v) => v.type),
147+
planner.params[0]
148+
)
149+
150+
expect(decoded[0].currencyOut).toEqual(WETH9[1].address)
151+
expect(decoded[0].maxHopSlippage).toHaveLength(2)
152+
expect(decoded[0].maxHopSlippage[0].toString()).toEqual('15000')
153+
expect(decoded[0].maxHopSlippage[1].toString()).toEqual('25000')
154+
expect(decoded[0].amountOut.toString()).toEqual(ONE_ETHER_BN.toString())
155+
})
90156
})
91157

92158
describe('addTrade', () => {

sdks/v4-sdk/src/utils/v4Planner.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ export class V4Planner {
207207
this.params = []
208208
}
209209

210-
addAction(type: Actions, parameters: any[]): V4Planner {
211-
let command = createAction(type, parameters)
210+
addAction(type: Actions, parameters: any[], urVersion: URVersion = URVersion.V2_0): V4Planner {
211+
let command = createAction(type, parameters, urVersion)
212212
this.params.push(command.encodedInput)
213213
this.actions = this.actions.concat(command.action.toString(16).padStart(2, '0'))
214214
return this
@@ -324,9 +324,13 @@ type RouterAction = {
324324
encodedInput: string
325325
}
326326

327-
function createAction(action: Actions, parameters: any[]): RouterAction {
327+
function createAction(action: Actions, parameters: any[], urVersion: URVersion = URVersion.V2_0): RouterAction {
328+
// Use V2.1 ABI for swap actions if V2.1, otherwise use base ABI (V2.0)
329+
const isSwapAction = action === Actions.SWAP_EXACT_IN || action === Actions.SWAP_EXACT_OUT
330+
const abiDef =
331+
urVersion === URVersion.V2_1 && isSwapAction ? V4_SWAP_ACTIONS_V2_1[action] : V4_BASE_ACTIONS_ABI_DEFINITION[action]
328332
const encodedInput = defaultAbiCoder.encode(
329-
V4_BASE_ACTIONS_ABI_DEFINITION[action].map((v) => v.type),
333+
abiDef.map((v) => v.type),
330334
parameters
331335
)
332336
return { action, encodedInput }

0 commit comments

Comments
 (0)