-
Notifications
You must be signed in to change notification settings - Fork 13
/
contracts.ts
72 lines (63 loc) · 2.1 KB
/
contracts.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
import { BaseContract } from '@ethersproject/contracts';
import {
TOKENS,
CHAINS,
getTokenAddress,
getWithdrawalQueueAddress,
} from '@lido-sdk/constants';
import {
WstethAbiFactory,
StethAbiFactory,
LdoAbiFactory,
Factory,
createContractGetter,
WithdrawalQueueAbiFactory,
} from '@lido-sdk/contracts';
import { useMemo } from 'react';
import { useSDK } from '../hooks';
export const contractHooksFactory = <C extends BaseContract>(
factory: Factory<C>,
getTokenAddress: (chainId: CHAINS) => string,
): {
useContractRPC: () => C;
useContractWeb3: () => C | null;
} => {
const getContract = createContractGetter(factory);
return {
useContractRPC: () => {
const { chainId, providerRpc } = useSDK();
const tokenAddress = getTokenAddress(chainId);
return getContract(tokenAddress, providerRpc);
},
useContractWeb3: () => {
const { chainId, providerWeb3 } = useSDK();
const tokenAddress = getTokenAddress(chainId);
const signer = useMemo(() => {
return providerWeb3?.getSigner();
}, [providerWeb3]);
if (!signer) return null;
return getContract(tokenAddress, signer);
},
};
};
const wsteth = contractHooksFactory(WstethAbiFactory, (chainId) =>
getTokenAddress(chainId, TOKENS.WSTETH),
);
export const useWSTETHContractRPC = wsteth.useContractRPC;
export const useWSTETHContractWeb3 = wsteth.useContractWeb3;
const steth = contractHooksFactory(StethAbiFactory, (chainId) =>
getTokenAddress(chainId, TOKENS.STETH),
);
export const useSTETHContractRPC = steth.useContractRPC;
export const useSTETHContractWeb3 = steth.useContractWeb3;
const ldo = contractHooksFactory(LdoAbiFactory, (chainId) =>
getTokenAddress(chainId, TOKENS.LDO),
);
export const useLDOContractRPC = ldo.useContractRPC;
export const useLDOContractWeb3 = ldo.useContractWeb3;
const withdrawalQueue = contractHooksFactory(
WithdrawalQueueAbiFactory,
(chainId) => getWithdrawalQueueAddress(chainId),
);
export const useWithdrawalQueueContractRPC = withdrawalQueue.useContractRPC;
export const useWithdrawalQueueContractWeb3 = withdrawalQueue.useContractWeb3;