Skip to content

Commit

Permalink
fix: #1960
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Mar 15, 2024
1 parent 3b3fb00 commit 4d52c74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-shirts-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed boolean conversion issue.
20 changes: 20 additions & 0 deletions src/utils/abi/encodeAbiParameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1776,3 +1776,23 @@ test('getArrayComponents', () => {
expect(getArrayComponents('uint256[][]')).toEqual([null, 'uint256[]'])
expect(getArrayComponents('uint256')).toBeUndefined()
})

test('https://github.com/wevm/viem/issues/1960', () => {
expect(() =>
encodeAbiParameters(
[
{
name: 'boolz',
type: 'bool[]',
internalType: 'bool[]',
},
] as const,
// @ts-expect-error
[['true']],
),
).toThrowErrorMatchingInlineSnapshot(`
[ViemError: Invalid boolean value: "true" (type: string). Expected: \`true\` or \`false\`.
Version: [email protected]]
`)
})
5 changes: 5 additions & 0 deletions src/utils/abi/encodeAbiParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
InvalidAddressError,
type InvalidAddressErrorType,
} from '../../errors/address.js'
import { BaseError } from '../../errors/base.js'
import type { ErrorType } from '../../errors/utils.js'
import type { Hex } from '../../types/misc.js'
import { type IsAddressErrorType, isAddress } from '../address/isAddress.js'
Expand Down Expand Up @@ -311,6 +312,10 @@ export type EncodeBoolErrorType =
| ErrorType

function encodeBool(value: boolean): PreparedParam {
if (typeof value !== 'boolean')
throw new BaseError(
`Invalid boolean value: "${value}" (type: ${typeof value}). Expected: \`true\` or \`false\`.`,
)
return { dynamic: false, encoded: padHex(boolToHex(value)) }
}

Expand Down

0 comments on commit 4d52c74

Please sign in to comment.