-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Abi } from 'ox' | ||
import { describe, expectTypeOf, test } from 'vitest' | ||
|
||
describe('format', () => { | ||
test('infers abi', () => { | ||
const formatted = Abi.format(value) | ||
expectTypeOf(formatted).toEqualTypeOf([ | ||
'function approve(address spender, uint256 amount) returns (bool)', | ||
] as const) | ||
}) | ||
|
||
test('not narrowable', () => { | ||
const abi = {} as Abi.Abi | ||
const formatted = Abi.format(abi) | ||
expectTypeOf(formatted).toEqualTypeOf<readonly string[]>() | ||
}) | ||
|
||
const value = [ | ||
{ | ||
type: 'function', | ||
name: 'approve', | ||
stateMutability: 'nonpayable', | ||
inputs: [ | ||
{ | ||
name: 'spender', | ||
type: 'address', | ||
}, | ||
{ | ||
name: 'amount', | ||
type: 'uint256', | ||
}, | ||
], | ||
outputs: [{ type: 'bool' }], | ||
}, | ||
] as const | ||
}) | ||
|
||
describe('from', () => { | ||
test('infers abi', () => { | ||
const abi = Abi.from(value) | ||
expectTypeOf(abi).toEqualTypeOf(value) | ||
}) | ||
|
||
test('from signatures', () => { | ||
const abi = Abi.from([ | ||
'function approve(address spender, uint256 amount) returns (bool)', | ||
]) | ||
expectTypeOf(abi).toEqualTypeOf(value) | ||
}) | ||
|
||
test('not narrowable', () => { | ||
const abi = Abi.from({} as Abi.Abi) | ||
expectTypeOf(abi).toEqualTypeOf<Abi.Abi>() | ||
}) | ||
|
||
const value = [ | ||
{ | ||
type: 'function', | ||
name: 'approve', | ||
stateMutability: 'nonpayable', | ||
inputs: [ | ||
{ | ||
name: 'spender', | ||
type: 'address', | ||
}, | ||
{ | ||
name: 'amount', | ||
type: 'uint256', | ||
}, | ||
], | ||
outputs: [{ type: 'bool' }], | ||
}, | ||
] as const | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type * as Abi from '../Abi.js' | ||
|
||
/** @internal */ | ||
export function isSignatures( | ||
value: Abi.Abi | readonly string[], | ||
): value is readonly string[] { | ||
for (const item of value) { | ||
if (typeof item !== 'string') return false | ||
} | ||
return true | ||
} |