Skip to content

Commit

Permalink
add support for arrays to LiteralToBroad, fix static and dynamic abi …
Browse files Browse the repository at this point in the history
…to primitive
  • Loading branch information
alvrs committed Jun 23, 2023
1 parent 942eff9 commit ce9f69e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
6 changes: 4 additions & 2 deletions packages/schema-type/src/typescript/dynamicAbiTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hex } from "viem";
import { StaticAbiType, DynamicAbiType } from "./schemaAbiTypes";
import { LiteralToBroad } from "./utils";

// Variable-length ABI types, where their lengths are encoded by a PackedCounter within the record

Expand Down Expand Up @@ -112,8 +113,9 @@ export const dynamicAbiTypeToDefaultValue = {
string: "",
} as const satisfies Record<DynamicAbiType, DynamicPrimitiveType>;

export type DynamicAbiTypeToPrimitiveType<TDynamicAbiType extends DynamicAbiType = DynamicAbiType> =
(typeof dynamicAbiTypeToDefaultValue)[TDynamicAbiType];
export type DynamicAbiTypeToPrimitiveType<TDynamicAbiType extends DynamicAbiType = DynamicAbiType> = LiteralToBroad<
(typeof dynamicAbiTypeToDefaultValue)[TDynamicAbiType]
>;

export type ArrayAbiTypeToStaticAbiType<T extends string> = T extends `${infer StaticAbiType}[]`
? StaticAbiType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { Hex } from "viem";
import { schemaAbiTypeToDefaultValue } from "./schemaAbiTypeToDefaultValue";
import { SchemaAbiType } from "./schemaAbiTypes";

type LiteralToBroad<T> = T extends number
? number
: T extends bigint
? bigint
: T extends Hex
? Hex
: T extends boolean
? boolean
: T extends string
? string
: never;
import { LiteralToBroad } from "./utils";

export type SchemaAbiTypeToPrimitiveType<T extends SchemaAbiType> = LiteralToBroad<
(typeof schemaAbiTypeToDefaultValue)[T]
Expand Down
6 changes: 4 additions & 2 deletions packages/schema-type/src/typescript/staticAbiTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hex } from "viem";
import { StaticAbiType } from "./schemaAbiTypes";
import { LiteralToBroad } from "./utils";

// Fixed-length ABI types

Expand Down Expand Up @@ -109,8 +110,9 @@ export const staticAbiTypeToDefaultValue = {
address: "0x0000000000000000000000000000000000000000",
} as const satisfies Record<StaticAbiType, StaticPrimitiveType>;

export type StaticAbiTypeToPrimitiveType<TStaticAbiType extends StaticAbiType = StaticAbiType> =
(typeof staticAbiTypeToDefaultValue)[TStaticAbiType];
export type StaticAbiTypeToPrimitiveType<TStaticAbiType extends StaticAbiType = StaticAbiType> = LiteralToBroad<
(typeof staticAbiTypeToDefaultValue)[TStaticAbiType]
>;

export const staticAbiTypeToByteLength = {
uint8: 1,
Expand Down
16 changes: 16 additions & 0 deletions packages/schema-type/src/typescript/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { Hex } from "viem";

export type TupleSplit<T, N extends number, O extends readonly any[] = readonly []> = O["length"] extends N
? [O, T]
: T extends readonly [infer F, ...infer R]
? TupleSplit<readonly [...R], N, readonly [...O, F]>
: [O, T];

export type LiteralToBroad<T> = T extends Array<infer Element>
? LiteralToBroad<Element>[]
: T extends number
? number
: T extends bigint
? bigint
: T extends Hex
? Hex
: T extends boolean
? boolean
: T extends string
? string
: never;

0 comments on commit ce9f69e

Please sign in to comment.