From 45ed414f8a980eac8f166c7fcab41ca74e83577b Mon Sep 17 00:00:00 2001 From: Alice <34962750+hensha256@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:51:49 -0400 Subject: [PATCH] feat(v3-sdk): add decode to multicall (#164) --- sdks/v3-sdk/src/multicall.test.ts | 21 +++++++++++++++++++++ sdks/v3-sdk/src/multicall.ts | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/sdks/v3-sdk/src/multicall.test.ts b/sdks/v3-sdk/src/multicall.test.ts index 246247132..0508ada4e 100644 --- a/sdks/v3-sdk/src/multicall.test.ts +++ b/sdks/v3-sdk/src/multicall.test.ts @@ -22,4 +22,25 @@ describe('Multicall', () => { ) }) }) + + describe('#decodeMulticall', () => { + it('works for string array with length >1', async () => { + const calldatas: string[] = [ + '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + ] + + // first encode it + const multicall = Multicall.encodeMulticall(calldatas) + expect(multicall).toBe( + '0xac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000020bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' + ) + + // then decode it + const decodedCalldata = Multicall.decodeMulticall(multicall) + expect(decodedCalldata.length).toBe(calldatas.length) + expect(decodedCalldata[0]).toBe(calldatas[0]) + expect(decodedCalldata[1]).toBe(calldatas[1]) + }) + }) }) diff --git a/sdks/v3-sdk/src/multicall.ts b/sdks/v3-sdk/src/multicall.ts index 1b6af88e1..8aee4c982 100644 --- a/sdks/v3-sdk/src/multicall.ts +++ b/sdks/v3-sdk/src/multicall.ts @@ -16,4 +16,8 @@ export abstract class Multicall { return calldatas.length === 1 ? calldatas[0] : Multicall.INTERFACE.encodeFunctionData('multicall', [calldatas]) } + + public static decodeMulticall(multicall: string): string[] { + return Multicall.INTERFACE.decodeFunctionData('multicall', multicall).data + } }