Skip to content

Commit

Permalink
fix: decoding dynamic bytes w/ zero data
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Mar 1, 2023
1 parent 51281ce commit 59a60cb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-moles-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed decoding zero data bytes
44 changes: 44 additions & 0 deletions src/utils/abi/decodeAbiParameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Address } from 'abitype'
import { AbiCoder } from 'ethers/lib/utils'
import { assertType, describe, expect, test } from 'vitest'
import { multicall3Abi } from '../../constants'
import { Hex } from '../../types'
import { address } from '../../_test'
import { seaportContractConfig } from '../../_test/abis'
Expand Down Expand Up @@ -1852,6 +1854,48 @@ describe('seaport', () => {
})
})

describe('multicall3', () => {
test('zero data', () => {
expect(
decodeAbiParameters(
multicall3Abi[0].outputs,
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000',
),
).toMatchInlineSnapshot(`
[
[
{
"returnData": "0x",
"success": true,
},
],
]
`)
})

test('zero data + non-zero data', () => {
expect(
decodeAbiParameters(
multicall3Abi[0].outputs,
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000d752bd588f61926e40000000000000000000000000000000000000000000000002e0e220d3d6cf5630000000000000000000000000000000000000000000000000000000063fa0b5f',
),
).toMatchInlineSnapshot(`
[
[
{
"returnData": "0x",
"success": true,
},
{
"returnData": "0x00000000000000000000000000000000000000000000000d752bd588f61926e40000000000000000000000000000000000000000000000002e0e220d3d6cf5630000000000000000000000000000000000000000000000000000000063fa0b5f",
"success": true,
},
],
]
`)
})
})

test('invalid size', () => {
expect(() =>
decodeAbiParameters(
Expand Down
4 changes: 3 additions & 1 deletion src/utils/abi/decodeAbiParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ function decodeBytes<TParam extends AbiParameter>(
// so we need to read the offset of the data part first.
const offset = hexToNumber(slice(data, position, position + 32))
const length = hexToNumber(slice(data, offset, offset + 32))
const value = slice(data, offset + 32, offset + 32 + length)
const value =
// If there is no length, we have zero data.
length === 0 ? '0x' : slice(data, offset + 32, offset + 32 + length)
return { consumed: 32, value }
}

Expand Down

2 comments on commit 59a60cb

@vercel
Copy link

@vercel vercel bot commented on 59a60cb Mar 1, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

viem-site – ./site

viem-site.vercel.app
www.viem.sh
viem-site-wagmi-dev.vercel.app
viem-site-git-main-wagmi-dev.vercel.app
viem.sh

@vercel
Copy link

@vercel vercel bot commented on 59a60cb Mar 1, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/browser

viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app

Please sign in to comment.