-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathuseBorrowAndUnwrap.ts
60 lines (55 loc) · 2.04 KB
/
useBorrowAndUnwrap.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
import borrowAndUnwrap, { type BorrowAndUnwrapInput } from 'clients/api/mutations/borrowAndUnwrap';
import queryClient from 'clients/api/queryClient';
import FunctionKey from 'constants/functionKey';
import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction';
import { useGetNativeTokenGatewayContract } from 'libs/contracts';
import { useChainId } from 'libs/wallet';
import type { VToken } from 'types';
import { callOrThrow } from 'utilities';
type TrimmedBorrowAndUnwrapInput = Omit<BorrowAndUnwrapInput, 'nativeTokenGatewayContract'>;
type Options = UseSendTransactionOptions<TrimmedBorrowAndUnwrapInput>;
const useBorrowAndUnwrap = (
{ vToken, poolComptrollerAddress }: { vToken: VToken; poolComptrollerAddress: string },
options?: Options,
) => {
const { chainId } = useChainId();
const nativeToken = vToken.underlyingToken.tokenWrapped;
const nativeTokenGatewayContract = useGetNativeTokenGatewayContract({
passSigner: true,
comptrollerContractAddress: poolComptrollerAddress,
});
return useSendTransaction({
fnKey: FunctionKey.BORROW_AND_UNWRAP,
fn: (input: TrimmedBorrowAndUnwrapInput) =>
callOrThrow({ nativeTokenGatewayContract }, params =>
borrowAndUnwrap({
...input,
...params,
}),
),
onConfirmed: async () => {
const accountAddress = await nativeTokenGatewayContract?.signer.getAddress();
queryClient.invalidateQueries(FunctionKey.GET_V_TOKEN_BALANCES_ALL);
queryClient.invalidateQueries([
FunctionKey.GET_BALANCE_OF,
{
chainId,
accountAddress,
vTokenAddress: vToken.address,
},
]);
queryClient.invalidateQueries([
FunctionKey.GET_BALANCE_OF,
{
chainId,
accountAddress,
tokenAddress: nativeToken?.address,
},
]);
queryClient.invalidateQueries(FunctionKey.GET_MAIN_MARKETS);
queryClient.invalidateQueries(FunctionKey.GET_ISOLATED_POOLS);
},
options,
});
};
export default useBorrowAndUnwrap;