Skip to content

Commit

Permalink
Add typechain types
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Aug 18, 2023
1 parent 32b0edb commit 6b6b8eb
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 280 deletions.
14 changes: 6 additions & 8 deletions packages/core/src/helpers/calls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Interface } from 'ethers'
import { BaseContract, Interface } from 'ethers'
import { Call } from '../hooks/useCall'
import { Awaited, ContractMethodNames, Falsy, TypedContract } from '../model/types'
import { Awaited, ContractFunctionNames, ContractMethodNames, Falsy, Results } from '../model/types'
import { RawCall, RawCallResult } from '../providers'
import { QueryParams } from '../constants/type/QueryParams'
import { ChainId } from '../constants/chainId'
Expand Down Expand Up @@ -123,14 +123,14 @@ export function getCallsForUpdate(requests: RawCall[], options?: RefreshOptions)
*
* @public
*/
export type CallResult<T extends TypedContract, MN extends ContractMethodNames<T>> =
| { value: Awaited<ReturnType<T['functions'][MN]> | undefined>; error: Error | undefined }
export type CallResult<T extends BaseContract, MN extends ContractMethodNames<T>> =
| { value: Results<T, MN> | undefined; error: Error | undefined }
| undefined

/**
* @internal Intended for internal use - use it on your own risk
*/
export function decodeCallResult<T extends TypedContract, MN extends ContractMethodNames<T>>(
export function decodeCallResult<T extends BaseContract, MN extends ContractMethodNames<T>>(
call: Call | Falsy,
result: RawCallResult
): CallResult<T, MN> {
Expand All @@ -141,9 +141,7 @@ export function decodeCallResult<T extends TypedContract, MN extends ContractMet
try {
if (success) {
return {
value: call.contract.interface.decodeFunctionResult(call.method, value) as Awaited<
ReturnType<T['functions'][MN]>
>,
value: call.contract.interface.decodeFunctionResult(call.method, value) as Results<T, MN>,
error: undefined,
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/helpers/logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypedFilter } from '../hooks/useLogs'
import { Awaited, ContractEventNames, DetailedEventRecord, EventRecord, Falsy, TypedContract } from '../model/types'
import { BlockTag, EventFragment, Filter, FilterByBlockHash, LogParams } from 'ethers'
import { Awaited, ContractEventNames, DetailedEventRecord, EventRecord, Falsy } from '../model/types'
import { BaseContract, BlockTag, EventFragment, Filter, FilterByBlockHash, LogParams } from 'ethers'

/**
* @internal Intended for internal use - use it on your own risk
Expand Down Expand Up @@ -82,15 +82,15 @@ export function encodeFilterData(
*
* @public
*/
export type LogsResult<T extends TypedContract, EN extends ContractEventNames<T>> =
export type LogsResult<T extends BaseContract, EN extends ContractEventNames<T>> =
| { value: Awaited<DetailedEventRecord<T, EN>>[]; error: undefined }
| { value: undefined; error: Error }
| undefined

/**
* @internal Intended for internal use - use it on your own risk
*/
export function decodeLogs<T extends TypedContract, EN extends ContractEventNames<T>>(
export function decodeLogs<T extends BaseContract, EN extends ContractEventNames<T>>(
filter: TypedFilter | Falsy,
result: LogParams[] | Falsy | Error
): LogsResult<T, EN> {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from './useDebouncePair'
export * from './useEthers'
export * from './useMulticallAddress'
export * from './useCall'
export * from './useContractCall'
export * from './useContractFunction'
export * from './useEtherBalance'
export * from './useToken'
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/hooks/threeChains.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ describe('useCall - three chains', () => {
const timestampsFirstChain = useTimestamps(FIRST_TEST_CHAIN_ID)
const timestampsSecondChain = useTimestamps(SECOND_TEST_CHAIN_ID)
const timestampsThirdChain = useTimestamps(THIRD_TEST_CHAIN_ID)
const dTimestampsFirstChain = useDoubler(FIRST_TEST_CHAIN_ID)(timestampsFirstChain?.value[0])
const dTimestampsSecondChain = useDoubler(SECOND_TEST_CHAIN_ID)(timestampsSecondChain?.value[0])
const dTimestampsThirdChain = useDoubler(THIRD_TEST_CHAIN_ID)(timestampsThirdChain?.value[0])
const dTimestampsFirstChain = useDoubler(FIRST_TEST_CHAIN_ID)(timestampsFirstChain?.value?.[0])
const dTimestampsSecondChain = useDoubler(SECOND_TEST_CHAIN_ID)(timestampsSecondChain?.value?.[0])
const dTimestampsThirdChain = useDoubler(THIRD_TEST_CHAIN_ID)(timestampsThirdChain?.value?.[0])
return {
timestamps: {
[FIRST_TEST_CHAIN_ID]: timestampsFirstChain,
[SECOND_TEST_CHAIN_ID]: timestampsSecondChain,
[THIRD_TEST_CHAIN_ID]: timestampsThirdChain,
[FIRST_TEST_CHAIN_ID]: timestampsFirstChain as any,
[SECOND_TEST_CHAIN_ID]: timestampsSecondChain as any,
[THIRD_TEST_CHAIN_ID]: timestampsThirdChain as any,
},
doubled: {
[FIRST_TEST_CHAIN_ID]: dTimestampsFirstChain,
[SECOND_TEST_CHAIN_ID]: dTimestampsSecondChain,
[THIRD_TEST_CHAIN_ID]: dTimestampsThirdChain,
[FIRST_TEST_CHAIN_ID]: dTimestampsFirstChain as any,
[SECOND_TEST_CHAIN_ID]: dTimestampsSecondChain as any,
[THIRD_TEST_CHAIN_ID]: dTimestampsThirdChain as any,
},
}
},
Expand All @@ -146,7 +146,7 @@ describe('useCall - three chains', () => {
return false
}

return timestamps.every((timestamp) => timestamp !== undefined)
return timestamps.every((timestamp: any) => timestamp !== undefined)
})

expect(allDefined).to.be.true
Expand All @@ -155,7 +155,7 @@ describe('useCall - three chains', () => {
for (const chainId of chainIds) {
const timestamps = result.current.timestamps[chainId]
const doubled = result.current.doubled[chainId]
for (let i = 0; i < timestamps?.value[0]?.length; i++) {
for (let i = 0; i < timestamps?.value?.[0]?.length; i++) {
expect(timestamps?.value[0]?.[i] * BigInt(2)).to.eq(doubled[i]?.value[0])
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/hooks/useCall.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('useCall', () => {
)
await waitForCurrent((val) => val !== undefined)
expect(result.error).to.be.undefined
expect(result.current?.value[0]).to.eq(MOCK_TOKEN_INITIAL_BALANCE)
expect(result.current?.value?.[0]).to.eq(MOCK_TOKEN_INITIAL_BALANCE)
})

it('returns error on revert', async () => {
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('useCall', () => {
)
await waitForCurrent((val) => val !== undefined)
expect(result.error).to.be.undefined
expect(result.current?.value[0]).to.eq(endValue)
expect(result.current?.value?.[0]).to.eq(endValue)
}

it('Properly handles two calls', async () => {
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('useCall', () => {

await network1.provider.mine()

await waitForCurrent(({ block1 }) => block1 !== undefined && Number(block1.value[0]) === blockNumber + 1)
await waitForCurrent(({ block1 }) => block1 !== undefined && Number(block1.value?.[0]) === blockNumber + 1)
expect(getResultProperty(result, 'block1')).to.eq(blockNumber + 1)
expect(getResultProperty(result, 'block2')).to.eq(blockNumber)

Expand Down Expand Up @@ -409,16 +409,16 @@ describe('useCall', () => {
)

await waitForCurrent((val) => val?.doubled?.value?.[0] === BigInt(2))
const blockNumberBefore = result.current.blockNumber?.value[0]
const blockNumberBefore = result.current.blockNumber?.value?.[0]

await network1.provider.mine()

expect(result.current.doubled?.value[0]).to.eq(2)
expect(result.current.blockNumber?.value[0]).to.eq(blockNumberBefore)
expect(result.current.doubled?.value?.[0]).to.eq(2)
expect(result.current.blockNumber?.value?.[0]).to.eq(blockNumberBefore)
rerender({ num: 2 })
await waitForCurrent((val) => val?.doubled?.value?.[0] === BigInt(4))

expect(result.current.blockNumber?.value[0]).to.eq(blockNumberBefore)
expect(result.current.blockNumber?.value?.[0]).to.eq(blockNumberBefore)
})

it('should not throw error when call is Falsy', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/hooks/useCall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react'
import { Contract } from 'ethers'
import { ContractMethodNames, Falsy, TypedContract } from '../model/types'
import { BaseContract, Contract } from 'ethers'
import { ContractMethodNames, Falsy } from '../model/types'
import { useRawCalls } from './useRawCalls'
import { CallResult, decodeCallResult, encodeCallData } from '../helpers'
import { QueryParams } from '../constants/type/QueryParams'
Expand Down Expand Up @@ -68,7 +68,7 @@ export interface Call {
* return value?.[0]
* }
*/
export function useCall<T extends TypedContract, MN extends ContractMethodNames<T>>(
export function useCall<T extends BaseContract, MN extends ContractMethodNames<T>>(
call: Call | Falsy,
queryParams: QueryParams = {}
): CallResult<T, MN> {
Expand Down
107 changes: 0 additions & 107 deletions packages/core/src/hooks/useContractCall.test.tsx

This file was deleted.

Loading

0 comments on commit 6b6b8eb

Please sign in to comment.