-
Notifications
You must be signed in to change notification settings - Fork 5k
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
1 parent
c919c9b
commit 8877b33
Showing
14 changed files
with
554 additions
and
109 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
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,7 @@ | ||
// TODO: Convert it to static file | ||
type _SolidityIndexRange = 1 | 2 | 3 | 4 | 5; | ||
|
||
export type ConvertToNumber< | ||
T extends string, | ||
Range extends number = _SolidityIndexRange, | ||
> = Range extends unknown ? (`${Range}` extends T ? Range : never) : never; |
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 |
---|---|---|
@@ -1,57 +1,187 @@ | ||
// TODO: Adding reference of source definition/doc for these types | ||
export interface JsonAbiParameter { | ||
readonly name: string; | ||
readonly type: string; | ||
readonly baseType?: string; | ||
readonly indexed?: boolean; | ||
readonly components?: Array<JsonAbiParameter>; | ||
readonly arrayLength?: number; | ||
readonly arrayChildren?: Array<JsonAbiParameter>; | ||
} | ||
import { | ||
Address, | ||
Bytes, | ||
Numbers, | ||
ObjectValueToTuple, | ||
ArrayToIndexObject, | ||
FixedSizeArray, | ||
} from 'web3-utils'; | ||
import { ConvertToNumber } from './number_map_type'; | ||
|
||
export interface JsonAbiStruct { | ||
export interface AbiStruct { | ||
[key: string]: unknown; | ||
name?: string; | ||
type: string; | ||
} | ||
|
||
export interface JsonAbiCoderStruct extends JsonAbiStruct { | ||
export interface AbiCoderStruct extends AbiStruct { | ||
[key: string]: unknown; | ||
components?: Array<JsonAbiStruct>; | ||
components?: Array<AbiStruct>; | ||
} | ||
|
||
// https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
export type AbiParameter = { | ||
readonly name: string; | ||
readonly type: string; | ||
readonly baseType?: string; | ||
readonly indexed?: boolean; | ||
readonly components?: ReadonlyArray<AbiParameter>; | ||
readonly arrayLength?: number; | ||
readonly arrayChildren?: ReadonlyArray<AbiParameter>; | ||
}; | ||
|
||
type FragmentTypes = 'constructor' | 'event' | 'function'; | ||
|
||
export interface JsonAbiBaseFragment { | ||
name?: string; | ||
type: FragmentTypes; | ||
inputs?: Array<JsonAbiParameter>; | ||
} | ||
export type AbiBaseFragment = { | ||
readonly type: FragmentTypes; | ||
}; | ||
|
||
export interface JsonAbiConstructorFragment extends JsonAbiBaseFragment { | ||
type: 'constructor'; | ||
stateMutability: 'nonpayable' | 'payable'; | ||
} | ||
// https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
export type AbiConstructorFragment = AbiBaseFragment & { | ||
readonly type: 'constructor'; | ||
readonly stateMutability: 'nonpayable' | 'payable'; | ||
readonly inputs: ReadonlyArray<AbiParameter>; | ||
}; | ||
|
||
export interface JsonAbiFunctionFragment extends JsonAbiBaseFragment { | ||
type: 'function'; | ||
stateMutability: 'nonpayable' | 'payable' | 'pure' | 'view'; | ||
outputs?: Array<JsonAbiParameter>; | ||
// https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
export type AbiFunctionFragment = AbiBaseFragment & { | ||
readonly name: string; | ||
readonly type: 'function'; | ||
readonly stateMutability: 'nonpayable' | 'payable' | 'pure' | 'view'; | ||
readonly inputs: ReadonlyArray<AbiParameter>; | ||
readonly outputs: ReadonlyArray<AbiParameter>; | ||
|
||
// legacy properties | ||
constant?: boolean; // stateMutability == 'pure' or stateMutability == 'view' | ||
payable?: boolean; // stateMutability == 'payable' | ||
} | ||
readonly constant?: boolean; // stateMutability == 'pure' or stateMutability == 'view' | ||
readonly payable?: boolean; // stateMutability == 'payable' | ||
}; | ||
|
||
export interface JsonAbiEventFragment extends JsonAbiBaseFragment { | ||
type: 'event'; | ||
anonymous?: boolean; | ||
} | ||
// https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
export type AbiEventFragment = AbiBaseFragment & { | ||
readonly name: string; | ||
readonly type: 'event'; | ||
readonly inputs: ReadonlyArray<AbiParameter>; | ||
readonly anonymous?: boolean; | ||
}; | ||
|
||
// https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
export type JsonAbiFragment = | ||
| JsonAbiConstructorFragment | ||
| JsonAbiFunctionFragment | ||
| JsonAbiEventFragment; | ||
export type AbiFragment = AbiConstructorFragment | AbiFunctionFragment | AbiEventFragment; | ||
|
||
export type ContractAbi = ReadonlyArray<AbiFragment>; | ||
|
||
export type AbiInput = string | AbiParameter | { readonly [key: string]: unknown }; | ||
|
||
export type FilterAbis<Abis extends ContractAbi, Filter, Abi = Abis[number]> = Abi extends Filter | ||
? Abi | ||
: never; | ||
|
||
type _TypedArray<Type, Size extends string> = Size extends '' | ||
? Type[] | ||
: FixedSizeArray<Type, ConvertToNumber<Size>>; | ||
|
||
export type PrimitiveAddressType<Type extends string> = Type extends `address[${infer Size}]` | ||
? _TypedArray<Address, Size> | ||
: Type extends 'address' | ||
? Address | ||
: never; | ||
|
||
export type PrimitiveStringType<Type extends string> = Type extends `string${string}[${infer Size}]` | ||
? _TypedArray<string, Size> | ||
: Type extends 'string' | `string${string}` | ||
? string | ||
: never; | ||
|
||
export type PrimitiveBooleanType<Type extends string> = Type extends `bool[${infer Size}]` | ||
? _TypedArray<boolean, Size> | ||
: Type extends 'bool' | ||
? boolean | ||
: never; | ||
|
||
export type PrimitiveIntegerType<Type extends string> = Type extends `uint${string}[${infer Size}]` | ||
? _TypedArray<Numbers, Size> | ||
: Type extends 'uint' | 'int' | `int${string}` | `uint${string}` | ||
? Numbers | ||
: never; | ||
|
||
export type PrimitiveBytesType<Type extends string> = Type extends `bytes${string}[${infer Size}]` | ||
? _TypedArray<Bytes, Size> | ||
: Type extends 'bytes' | `bytes${string}` | ||
? Bytes | ||
: never; | ||
|
||
export type PrimitiveTupleType< | ||
Type extends string, | ||
Components extends ReadonlyArray<AbiParameter> | undefined = [], | ||
> = Components extends ReadonlyArray<AbiParameter> | ||
? Type extends 'tuple' | ||
? { | ||
// eslint-disable-next-line no-use-before-define | ||
[Param in Components[number] as Param['name']]: MatchPrimitiveType< | ||
Param['type'], | ||
Param['components'] | ||
>; | ||
} | ||
: Type extends `tuple[${infer Size}]` | ||
? _TypedArray< | ||
{ | ||
// eslint-disable-next-line no-use-before-define | ||
[Param in Components[number] as Param['name']]: MatchPrimitiveType< | ||
Param['type'], | ||
Param['components'] | ||
>; | ||
}, | ||
Size | ||
> | ||
: never | ||
: never; | ||
|
||
export type MatchPrimitiveType< | ||
Type extends string, | ||
Components extends ReadonlyArray<AbiParameter> | undefined, | ||
> = | ||
| PrimitiveAddressType<Type> | ||
| PrimitiveStringType<Type> | ||
| PrimitiveBooleanType<Type> | ||
| PrimitiveIntegerType<Type> | ||
| PrimitiveBytesType<Type> | ||
| PrimitiveTupleType<Type, Components> | ||
| Type; | ||
|
||
// Only intended to use locally so why not exported | ||
// TODO: Inspect Record<string, AbiParameter> not working constraint | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type _ExtractParameterType<T extends Record<string, any>> = { | ||
[K in keyof T]: MatchPrimitiveType<T[K]['type'], T[K]['components']>; | ||
}; | ||
|
||
export type ContractMethodOutputParameters<Params extends ReadonlyArray<AbiParameter>> = | ||
ObjectValueToTuple<_ExtractParameterType<ArrayToIndexObject<Params>>>; | ||
|
||
export type ContractMethodInputParameters<Params extends ReadonlyArray<AbiParameter>> = { | ||
[Param in Params[number] as Param['name']]: MatchPrimitiveType< | ||
Param['type'], | ||
Param['components'] | ||
>; | ||
}; | ||
|
||
export type ContractConstructor<Abis extends ContractAbi> = { | ||
[Abi in FilterAbis<Abis, AbiConstructorFragment> as 'constructor']: { | ||
readonly Abi: Abi; | ||
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>; | ||
}; | ||
}['constructor']; | ||
|
||
export type ContractMethods<Abis extends ContractAbi> = { | ||
[Abi in FilterAbis<Abis, AbiFunctionFragment> as Abi['name']]: { | ||
readonly Abi: Abi; | ||
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>; | ||
readonly Outputs: ContractMethodOutputParameters<Abi['outputs']>; | ||
}; | ||
}; | ||
|
||
export type AbiInput = string | JsonAbiParameter | { readonly [key: string]: unknown }; | ||
export type ContractEvents<Abis extends ContractAbi> = { | ||
[Abi in FilterAbis<Abis, AbiEventFragment> as Abi['name']]: { | ||
readonly Abi: Abi; | ||
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>; | ||
}; | ||
}; |
Oops, something went wrong.