Skip to content

Commit

Permalink
Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Aug 21, 2023
1 parent 6b6b8eb commit 88ff4e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/core/src/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { BaseContract, ContractTransaction } from "ethers"

export type Falsy = false | 0 | '' | null | undefined

export type ContractFunctionNames<T extends BaseContract> = keyof { [K in Parameters<T['interface']['getFunction']>[0] as ReturnType<K extends keyof T ? T[K] extends (...args: any) => any ? T[K] : never : never> extends Promise<ContractTransaction> ? K : never]: void }
type ReplaceNever<T, R = string> = [T] extends [never] ? R : T;

export type ContractMethodNames<T extends BaseContract> = keyof { [K in Parameters<T['interface']['getFunction']>[0] as ReturnType<K extends keyof T ? T[K] extends (...args: any) => any ? T[K] : never : never> extends Promise<ContractTransaction> ? never : K]: void }
export type ContractFunctionNames<T extends BaseContract> = ReplaceNever<keyof { [K in Parameters<T['interface']['getFunction']>[0] as ReturnType<K extends keyof T ? T[K] extends (...args: any) => any ? T[K] : never : never> extends Promise<ContractTransaction> ? K : never]: void }>

export type ContractEventNames<T extends BaseContract> = keyof { [K in keyof T['filters']]: void }
export type ContractMethodNames<T extends BaseContract> = ReplaceNever<keyof { [K in Parameters<T['interface']['getFunction']>[0] as ReturnType<K extends keyof T ? T[K] extends (...args: any) => any ? T[K] : never : never> extends Promise<ContractTransaction> ? never : K]: void }>

export type Params<T extends BaseContract, FN extends ContractFunctionNames<T> | ContractMethodNames<T>> = Parameters<FN extends keyof T ? T[FN] extends (...args: any) => any ? T[FN] : never : never>
export type ContractEventNames<T extends BaseContract> = ReplaceNever<keyof { [K in keyof T['filters']]: void }>

export type Results<T extends BaseContract, FN extends ContractMethodNames<T>> = Awaited<ReturnType<FN extends keyof T ? T[FN] extends (...args: any) => any ? T[FN] : never : never>>
export type Params<T extends BaseContract, FN extends ContractFunctionNames<T> | ContractMethodNames<T>> = ReplaceNever<Parameters<FN extends keyof T ? T[FN] extends (...args: any) => any ? T[FN] : never : never>, any[]>

export type EventParams<T extends BaseContract, EN extends ContractEventNames<T>> = Parameters<T['filters'][EN]>
export type Results<T extends BaseContract, FN extends ContractMethodNames<T>> = ReplaceNever<Awaited<ReturnType<FN extends keyof T ? T[FN] extends (...args: any) => any ? T[FN] : never : never>>, any[]>

export type EventParams<T extends BaseContract, EN extends ContractEventNames<T>> = ReplaceNever<Parameters<T['filters'][EN]>, any[]>

export type EventRecord<T extends BaseContract, EN extends ContractEventNames<T>> = { [P in keyof EventParams<T, EN> as string]: any }

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/testing/utils/getResultProperty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RenderResult } from '@testing-library/react-hooks'
import { Contract } from 'ethers'
import { BaseContract } from 'ethers'
import { CallResult } from '../../helpers'

export type HookResult = {
[key: string]: CallResult<Contract, string>
[key: string]: CallResult<BaseContract, string>
}

export const getResultProperty = <T extends HookResult>(result: RenderResult<T>, property: keyof T) => {
Expand Down

0 comments on commit 88ff4e0

Please sign in to comment.