Skip to content

Commit

Permalink
feat: add config option for number type (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 12, 2022
1 parent 8f00ea8 commit a8cb964
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-falcons-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'abitype': patch
---

Added `NumberType` configuration option. `NumberType` sets the TypeScript type to use for `int` and `uint` values.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,12 @@ import { TypedData } from 'abitype'

ABIType tries to strike a balance between type exhaustiveness and speed with sensible defaults. In some cases, you might want to tune your configuration (e.g. fixed array length). To do this, the following configuration options are available:

| Option | Type | Default | Description |
| ---------------------------- | ----------------- | ------- | -------------------------------------------------------------------------------------------------------- |
| `ArrayMaxDepth` | `number \| false` | `2` | Maximum depth for nested array types (e.g. `string[][]`). When `false`, there is no maximum array depth. |
| `FixedArrayLengthLowerBound` | `number` | `1` | Lower bound for fixed array length |
| `FixedArrayLengthUpperBound` | `number` | `5` | Upper bound for fixed array length |
| Option | Type | Default | Description |
| ---------------------------- | ----------------- | ------------------ | -------------------------------------------------------------------------------------------------------- |
| `ArrayMaxDepth` | `number \| false` | `2` | Maximum depth for nested array types (e.g. `string[][]`). When `false`, there is no maximum array depth. |
| `FixedArrayLengthLowerBound` | `number` | `1` | Lower bound for fixed array length |
| `FixedArrayLengthUpperBound` | `number` | `5` | Upper bound for fixed array length |
| `NumberType` | TypeScript type | `number \| bigint` | TypeScript type to use for `int` and `uint` values. |

Configuration options are customizable using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html). Just extend the `Config` interface either directly in your code or in a `d.ts` file (e.g. `abi.d.ts`):

Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface DefaultConfig {
FixedArrayLengthLowerBound: 1
/** Upper bound for fixed array length */
FixedArrayLengthUpperBound: 5
/** TypeScript type to use for `int` and `uint` values */
NumberType: number | bigint
}

/**
Expand All @@ -42,4 +44,7 @@ export interface ResolvedConfig {
FixedArrayLengthUpperBound: Config['FixedArrayLengthUpperBound'] extends number
? Config['FixedArrayLengthUpperBound']
: DefaultConfig['FixedArrayLengthUpperBound']
NumberType: Config['NumberType'] extends number | bigint
? Config['NumberType']
: DefaultConfig['NumberType']
}
4 changes: 2 additions & 2 deletions src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ export function readContracts<T extends unknown[]>(

export function signTypedData<
TTypedData extends TypedData,
Schema extends TypedDataToPrimitiveTypes<TTypedData>,
TSchema extends TypedDataToPrimitiveTypes<TTypedData>,
>(_config: {
/** Named list of all type definitions */
types: TTypedData
/** Data to sign */
value: Schema[keyof Schema]
value: TSchema[keyof TSchema]
}) {
return {} as Address
}
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
TypedDataParameter,
TypedDataType,
} from './abi'
import { ResolvedConfig } from './config'
import { Merge, Tuple } from './types'

/**
Expand All @@ -44,7 +45,7 @@ type PrimitiveTypeLookup = {
} & {
[_ in SolidityFunction]: `${Address}${string}`
} & {
[_ in SolidityInt]: number | bigint
[_ in SolidityInt]: ResolvedConfig['NumberType']
} & {
[_ in SolidityString]: string
} & {
Expand Down
12 changes: 9 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/**
* Assert parameter is of a specific type.
*
* @param _value - Value that should be identical to type `T`.
* @param value - Value that should be identical to type `T`.
*/
export function expectType<T>(_value: T): void {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function expectType<T>(value: T): void {
// eslint-disable-next-line @typescript-eslint/no-empty-function
}

export function test(_name: string, _callback: () => void) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function test(name: string, _callback: () => void) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
}

Expand Down

0 comments on commit a8cb964

Please sign in to comment.