This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
useContractReader.ts
91 lines (83 loc) 路 3.1 KB
/
useContractReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { BaseContract, EventFilter } from 'ethers';
import { useEffect } from 'react';
import { useQuery } from 'react-query';
import { useBlockNumberContext } from '~~/context';
import { contractFuncKey, mergeDefaultUpdateOptions, processQueryOptions } from '~~/functions';
import { useEthersUpdater } from '~~/hooks/useEthersUpdater';
import { THookResult, TUpdateOptions } from '~~/models';
import { keyNamespace } from '~~/models/constants';
const queryKey = { namespace: keyNamespace.contracts, key: 'useContractReader' } as const;
/**
* #### Summary
* Enables you to call a contract function with arguments and receive the output. You can use this to easily track of contract outputs in react states
*
* ##### 鉁忥笍 Notes
* - uses the ethers.Contract object's provider to access the network
* - formatter is a function that can change the format of the output
* @param contract Contract reading from
* @param contractFunc Contract variable or function to read
* @param args Typed tuple argument to contract function or variable
* @param funcEventFilter Optional if only want contract to update on event
* @param options Options for how often and when to update
* @returns
*/
export const useContractReader = <
GContract extends BaseContract,
GContractFunc extends (...args: any[]) => Promise<any>
>(
contract: GContract | undefined,
contractFunc: GContractFunc | undefined,
args?: Parameters<GContractFunc>,
funcEventFilter?: EventFilter | undefined,
options: TUpdateOptions = mergeDefaultUpdateOptions()
): THookResult<Awaited<ReturnType<GContractFunc>> | undefined> => {
const keys = [
{
...queryKey,
...contractFuncKey(contract, contractFunc),
},
{ args: args ?? [], funcEventFilter },
] as const;
const { data, refetch, status } = useQuery(
keys,
async (keys): Promise<Awaited<ReturnType<GContractFunc>> | undefined> => {
const { args } = keys.queryKey[1];
if (contractFunc != null && contract != null) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await contractFunc(...args);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result;
}
return undefined;
},
{
...processQueryOptions<Awaited<ReturnType<GContractFunc>> | undefined>(options),
}
);
/**
* event based updates:
* you can use an event and only call the contract when there is an event
*/
useEffect(() => {
if (funcEventFilter != null) {
const listener = (): void => {
void refetch();
};
try {
contract?.on(funcEventFilter, listener);
return (): void => {
contract?.off(funcEventFilter, listener);
};
} catch (e) {
console.log(e);
}
}
}, [contract, funcEventFilter, refetch]);
const blockNumber = useBlockNumberContext();
/**
* if event based updates is on, interval updates are disabled
*/
const allowBlockNumberIntervalUpdate = funcEventFilter == null;
useEthersUpdater(refetch, blockNumber, options, allowBlockNumberIntervalUpdate);
return [data, refetch, status];
};