Skip to content

Commit

Permalink
fix: format abi item fallback formatting (#259)
Browse files Browse the repository at this point in the history
* fix: format abi item fallback formatting

* chore: tweaks

* fix: type

---------

Co-authored-by: Tom Meagher <tom@meagher.co>
  • Loading branch information
KedziaPawel and tmm authored Dec 3, 2024
1 parent ee60e67 commit f417a23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-turtles-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"abitype": patch
---

Fixed `formatAbiItem` fallback item formatting.
7 changes: 7 additions & 0 deletions packages/abitype/src/human-readable/formatAbiItem.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ test('formatAbiItem', () => {
stateMutability: 'nonpayable',
}
expectTypeOf(formatAbiItem(abiItem)).toEqualTypeOf<string>()

expectTypeOf(
formatAbiItem({ type: 'fallback', stateMutability: 'nonpayable' }),
).toEqualTypeOf<'fallback() external'>()
expectTypeOf(
formatAbiItem({ type: 'fallback', stateMutability: 'payable' }),
).toEqualTypeOf<'fallback() external payable'>()
})
9 changes: 8 additions & 1 deletion packages/abitype/src/human-readable/formatAbiItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ test.each([
type: 'fallback',
stateMutability: 'nonpayable',
} as const,
expected: 'fallback()',
expected: 'fallback() external',
},
{
abiItem: {
type: 'fallback',
stateMutability: 'payable',
} as const,
expected: 'fallback() external payable',
},
{
abiItem: {
Expand Down
9 changes: 7 additions & 2 deletions packages/abitype/src/human-readable/formatAbiItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export type FormatAbiItem<abiItem extends Abi[number]> =
| (abiItem extends AbiFallback
? AbiFallback extends abiItem
? string
: 'fallback()'
: `fallback() external${abiItem['stateMutability'] extends 'payable'
? ' payable'
: ''}`
: never)
| (abiItem extends AbiReceive
? AbiReceive extends abiItem
Expand Down Expand Up @@ -126,6 +128,9 @@ export function formatAbiItem<const abiItem extends Abi[number]>(
return `constructor(${formatAbiParameters(abiItem.inputs as Params)})${
abiItem.stateMutability === 'payable' ? ' payable' : ''
}`
if (abiItem.type === 'fallback') return 'fallback()' as Result
if (abiItem.type === 'fallback')
return `fallback() external${
abiItem.stateMutability === 'payable' ? ' payable' : ''
}` as Result
return 'receive() external payable' as Result
}

0 comments on commit f417a23

Please sign in to comment.