Skip to content

Commit

Permalink
feat(examples): overloads (#149)
Browse files Browse the repository at this point in the history
* wip: overloads

* feat: overloads

* feat: support optional properties

* refactor: remove barrel files

* chore: changeset

Co-authored-by: 0xRaiden <[email protected]>

* chore: cleanup

* test(examples): update

---------

Co-authored-by: 0xRaiden <[email protected]>
  • Loading branch information
tmm and Raiden1411 authored Jun 9, 2023
1 parent 43d3a9d commit ba21aae
Show file tree
Hide file tree
Showing 56 changed files with 1,501 additions and 1,453 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-shrimps-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"abitype": patch
---

Removed internal barrel exports.
108 changes: 67 additions & 41 deletions examples/readContract.test-d.ts → examples/read.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@ import {
wagmiMintExampleAbi,
wagmiMintExampleHumanReadableAbi,
writingEditionsFactoryAbi,
zeroAddress,
} from 'abitype/test'
import { assertType, test } from 'vitest'
import { assertType, expectTypeOf, test } from 'vitest'

import { readContract, readWagmiMintExample } from './readContract.js'
import { read, readWagmiMintExample, useRead } from './read.js'

test('readContract', () => {
test('read', () => {
test('args', () => {
test('zero', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
functionName: 'name',
})
assertType<string>(result)
})

test('one', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
functionName: 'tokenURI',
args: [123n],
Expand All @@ -32,29 +29,26 @@ test('readContract', () => {
})

test('two or more', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: writingEditionsFactoryAbi,
functionName: 'predictDeterministicAddress',
args: [zeroAddress, '0xfoo'],
args: ['0x', '0xfoo'],
})
assertType<Address>(result)
})
})

test('return types', () => {
test('string', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
functionName: 'name',
})
assertType<string>(result)
})

test('Address', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
functionName: 'ownerOf',
args: [123n],
Expand All @@ -63,20 +57,18 @@ test('readContract', () => {
})

test('number', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
functionName: 'balanceOf',
args: [zeroAddress],
args: ['0x'],
})
assertType<ResolvedConfig['BigIntType']>(result)
})
})

test('behavior', () => {
test('write function not allowed', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: wagmiMintExampleAbi,
// @ts-expect-error Trying to use non-read function
functionName: 'approve',
Expand All @@ -101,16 +93,14 @@ test('readContract', () => {
outputs: [{ type: 'address', name: '' }],
},
]
const result1 = readContract({
address: zeroAddress,
const result1 = read({
abi: abi,
functionName: 'foo',
})
const result2 = readContract({
address: zeroAddress,
const result2 = read({
abi: abi,
functionName: 'bar',
args: [zeroAddress],
args: ['0x'],
})
type Result1 = typeof result1
type Result2 = typeof result2
Expand All @@ -135,16 +125,14 @@ test('readContract', () => {
outputs: [{ type: 'address', name: '' }],
},
]
const result1 = readContract({
address: zeroAddress,
const result1 = read({
abi: abi,
functionName: 'foo',
})
const result2 = readContract({
address: zeroAddress,
const result2 = read({
abi: abi,
functionName: 'bar',
args: [zeroAddress],
args: ['0x'],
})
type Result1 = typeof result1
type Result2 = typeof result2
Expand All @@ -153,8 +141,7 @@ test('readContract', () => {
})

test('defined inline', () => {
const result1 = readContract({
address: zeroAddress,
const result1 = read({
abi: [
{
name: 'foo',
Expand All @@ -173,8 +160,7 @@ test('readContract', () => {
],
functionName: 'foo',
})
const result2 = readContract({
address: zeroAddress,
const result2 = read({
abi: [
{
name: 'foo',
Expand All @@ -192,7 +178,7 @@ test('readContract', () => {
},
],
functionName: 'bar',
args: [zeroAddress],
args: ['0x'],
})
type Result1 = typeof result1
type Result2 = typeof result2
Expand All @@ -201,11 +187,10 @@ test('readContract', () => {
})

test('human readable', () => {
const result = readContract({
address: zeroAddress,
const result = read({
abi: parseAbi(wagmiMintExampleHumanReadableAbi),
functionName: 'balanceOf',
args: [zeroAddress],
args: ['0x'],
})
assertType<bigint>(result)
})
Expand All @@ -216,19 +201,60 @@ test('readWagmiMintExample', () => {
test('args', () => {
test('zero', () => {
const result = readWagmiMintExample({
address: '0x…',
functionName: 'name',
})
assertType<string>(result)
})

test('one', () => {
const result = readWagmiMintExample({
address: '0x…',
functionName: 'balanceOf',
args: ['0x'],
args: ['0x'],
})
assertType<bigint>(result)
})
})
})

const abi = parseAbi([
'function foo() returns (bool)',
'function foo() returns (uint8)',
'function foo(uint) view returns (address)',
'function foo(address) view returns (uint)',
'function foo(uint256, address) view returns (address, uint8)',
'function bar() pure returns (address)',
'function baz(uint) pure returns (string)',
'function boo(bytes32) pure returns (bytes32)',
])
expectTypeOf(
read({
abi,
functionName: 'foo',
args: [123n, '0x'],
}),
).toEqualTypeOf<readonly [Address, number]>()
expectTypeOf(
read({
abi,
functionName: 'foo',
}),
).toEqualTypeOf<boolean | number>()
expectTypeOf(
useRead({
abi,
functionName: 'foo',
args: [123n, '0x'],
}),
).toEqualTypeOf<readonly [Address, number]>()

const abi2 = parseAbi([
'function foo(address) view returns (uint)',
// 'function foo(address) view returns (string)',
])
const res = read({
abi: abi2,
functionName: 'foo',
args: ['0x'],
})
res
// ^?
48 changes: 48 additions & 0 deletions examples/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { type Abi } from 'abitype'
import type { wagmiMintExampleAbi } from 'abitype/test'

import type {
ContractParameters,
ContractReturnType,
DeepPartial,
} from './types.js'

export declare function read<
const abi extends Abi | readonly unknown[], // `readonly unknown[]` allows for non-const asserted types
functionName extends string,
const args extends readonly unknown[] | undefined,
>(
parameters: ReadParameters<abi, functionName, args>,
): ReadReturnType<abi, functionName, args>

export declare function readWagmiMintExample<
functionName extends string,
const args extends readonly unknown[] | undefined,
>(
parameters: Omit<
ReadParameters<typeof wagmiMintExampleAbi, functionName, args>,
'abi'
>,
): ReadReturnType<typeof wagmiMintExampleAbi, functionName, args>

export declare function useRead<
const abi extends Abi | readonly unknown[], // `readonly unknown[]` allows for non-const asserted types
const functionName extends string,
const args extends readonly unknown[] | undefined,
>(
parameters: DeepPartial<ReadParameters<abi, functionName, args>, 1>,
): ReadReturnType<abi, functionName, args>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

export type ReadParameters<
abi extends Abi | readonly unknown[],
functionName extends string,
args extends readonly unknown[] | undefined = readonly unknown[] | undefined,
> = { abi: abi } & ContractParameters<abi, functionName, 'pure' | 'view', args>

export type ReadReturnType<
abi extends Abi | readonly unknown[],
functionName extends string,
args extends readonly unknown[] | undefined,
> = ContractReturnType<abi, functionName, args>
23 changes: 0 additions & 23 deletions examples/readContract.ts

This file was deleted.

Loading

1 comment on commit ba21aae

@vercel
Copy link

@vercel vercel bot commented on ba21aae Jun 9, 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:

abitype – ./

abitype.vercel.app
abitype-wagmi-dev.vercel.app
abitype-git-main-wagmi-dev.vercel.app
abitype.dev

Please sign in to comment.