Skip to content

Commit 96691f3

Browse files
committed
chore: structure
1 parent 672dc60 commit 96691f3

16 files changed

Lines changed: 275 additions & 395 deletions

File tree

features/create-vault/create-vault-form/submit-modal/submit-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const SubmitModal: FC<ModalProps> = () => {
104104
SubmitStepEnum.submitting === step) &&
105105
tx && <TxLinkEtherscan txHash={tx} />}
106106

107-
{step === SubmitStepEnum.reject && (
107+
{step === SubmitStepEnum.reject && retryFire && (
108108
<ButtonLink onClick={retryFire}>Retry</ButtonLink>
109109
)}
110110

features/supply/fund/fund-form-context/fund-form-provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const FundFormProvider: FC<{ children: ReactNode }> = ({ children }) => {
1818
reValidateMode: 'onChange',
1919
});
2020

21-
const { fund, retryFire } = useFund();
21+
const { fund, retryEvent } = useFund();
2222

2323
const onSubmit = useCallback(
2424
async ({ amount }: FundFormSchema) => {
@@ -32,10 +32,10 @@ export const FundFormProvider: FC<{ children: ReactNode }> = ({ children }) => {
3232
useMemo(
3333
() => ({
3434
onSubmit,
35-
retryFire,
35+
retryEvent,
3636
onReset: formObject.reset,
3737
}),
38-
[retryFire, onSubmit, formObject.reset],
38+
[onSubmit, retryEvent, formObject.reset],
3939
);
4040

4141
return (

features/supply/fund/hooks/use-fund.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
TransactionEntry,
1111
useSendTransaction,
1212
withSuccess,
13-
} from 'modules/web3/hooks/use-send-tx';
13+
} from 'modules/web3';
1414

1515
export const useFund = () => {
1616
const { activeVault } = useVaultInfo();
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { OracleReportButton } from 'features/report';
21
import { PermissionedSubmitButton } from 'modules/vaults/components';
32

43
import { useFormContext } from 'react-hook-form';
@@ -10,14 +9,12 @@ export const SubmitButton = () => {
109
const disabled = isSubmitting || !isValid || !isDirty;
1110

1211
return (
13-
<OracleReportButton action="withdrawal">
14-
<PermissionedSubmitButton
15-
type="submit"
16-
dashboardRole="withdrawer"
17-
disabled={disabled}
18-
>
19-
Withdraw
20-
</PermissionedSubmitButton>
21-
</OracleReportButton>
12+
<PermissionedSubmitButton
13+
type="submit"
14+
dashboardRole="withdrawer"
15+
disabled={disabled}
16+
>
17+
Withdraw
18+
</PermissionedSubmitButton>
2219
);
2320
};

features/supply/withdraw/hooks/use-withdraw.ts

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,93 @@
11
import { useCallback } from 'react';
2-
import { useEstimateGas, usePublicClient, useWriteContract } from 'wagmi';
2+
import { useEstimateGas, usePublicClient } from 'wagmi';
33
import { Address, encodeFunctionData } from 'viem';
44

55
import { dashboardAbi } from 'abi/dashboard-abi';
66
import { useDappStatus } from 'modules/web3/hooks/use-dapp-status';
77
import { useVaultInfo } from 'modules/vaults';
88
import invariant from 'tiny-invariant';
99
import { useVaultPermission } from 'modules/vaults/hooks/use-vault-permissions';
10-
import {
11-
SubmitPayload,
12-
SubmitStepEnum,
13-
} from 'shared/components/submit-modal/types';
10+
1411
import { fallbackedAddress } from 'utils/fallbacked-address';
12+
import {
13+
TransactionEntry,
14+
useSendTransaction,
15+
withSuccess,
16+
} from 'modules/web3';
17+
import { useReportStatus } from 'features/report/use-report';
18+
import { fetchReportMerkle } from 'features/report/ipfs';
19+
import { getVaultHubContract } from 'modules/vaults/contracts/vault-hub';
1520

1621
type WithdrawArgs = {
1722
recipient: Address;
1823
amount: bigint;
19-
setModalState: (submitStep: SubmitPayload) => void;
2024
};
2125

22-
export const useWithdraw = (onMutate = () => {}) => {
26+
export const useWithdraw = () => {
2327
const { activeVault } = useVaultInfo();
2428
const publicClient = usePublicClient();
29+
const { sendTX, ...rest } = useSendTransaction();
30+
const { shouldApplyReport } = useReportStatus();
2531

26-
const { data: withdrawTx, writeContractAsync } = useWriteContract({
27-
mutation: {
28-
onMutate,
29-
},
30-
});
31-
32-
const callWithdraw = useCallback(
33-
async ({ amount, recipient, setModalState }: WithdrawArgs) => {
32+
const withdraw = useCallback(
33+
async ({ amount, recipient }: WithdrawArgs) => {
3434
invariant(activeVault, '[useWithdraw] activeVault is undefined');
3535
invariant(publicClient, '[useWithdraw] publicClient is undefined');
3636

37-
setModalState({ step: SubmitStepEnum.confirming });
38-
const tx = await writeContractAsync({
39-
abi: dashboardAbi,
40-
address: activeVault.owner,
41-
functionName: 'withdraw',
42-
args: [recipient, amount],
43-
});
37+
const transactions: TransactionEntry[] = [];
38+
39+
if (shouldApplyReport) {
40+
const hub = getVaultHubContract(publicClient);
41+
const reportCid = (await hub.read.latestReportData())[2];
42+
43+
const report = await fetchReportMerkle(
44+
publicClient.chain.id,
45+
reportCid,
46+
activeVault.address,
47+
);
48+
49+
transactions.push({
50+
loadingActionText: 'Applying oracle report',
51+
to: hub.address,
52+
data: encodeFunctionData({
53+
abi: hub.abi,
54+
functionName: 'updateVaultData',
55+
args: [
56+
activeVault.address,
57+
report.totalValueWei,
58+
report.inOutDelta,
59+
report.fee,
60+
report.liabilityShares,
61+
report.proof,
62+
],
63+
}),
64+
});
65+
}
4466

45-
setModalState({ step: SubmitStepEnum.submitting, tx });
46-
await publicClient.waitForTransactionReceipt({
47-
hash: tx,
67+
transactions.push({
68+
loadingActionText: 'Withdrawing ETH from vault',
69+
to: activeVault.owner,
70+
data: encodeFunctionData({
71+
abi: dashboardAbi,
72+
functionName: 'withdraw',
73+
args: [recipient, amount],
74+
}),
4875
});
4976

50-
return tx;
77+
return withSuccess(
78+
sendTX({
79+
transactions,
80+
mainActionLoadingText: 'Withdrawing ETH from vault',
81+
mainActionCompleteText: 'ETH withdrawn from vault',
82+
}),
83+
);
5184
},
52-
[activeVault, writeContractAsync, publicClient],
85+
[activeVault, publicClient, shouldApplyReport, sendTX],
5386
);
5487

5588
return {
56-
callWithdraw,
57-
withdrawTx,
89+
withdraw,
90+
...rest,
5891
};
5992
};
6093

features/supply/withdraw/withdraw-form-context/withdraw-form-provider.tsx

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@ import {
55
useCallback,
66
createContext,
77
useContext,
8-
useState,
98
} from 'react';
109
import { FormProvider, useForm } from 'react-hook-form';
11-
import { Address, isAddress } from 'viem';
10+
import type { Address } from 'viem';
1211
import invariant from 'tiny-invariant';
1312

14-
import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate';
1513
import { useWithdrawable, useWithdraw } from 'features/supply/withdraw/hooks';
1614

1715
import {
1816
FormController,
1917
FormControllerContext,
2018
FormControllerContextValueType,
2119
} from 'shared/hook-form/form-controller';
22-
import { SubmitModal } from 'shared/components';
2320

2421
import { WithdrawFormSchema } from 'features/supply/withdraw/types';
25-
import {
26-
SubmitPayload,
27-
SubmitStep,
28-
SubmitStepEnum,
29-
} from 'shared/components/submit-modal/types';
3022

3123
type WithdrawDataContextValue = {
3224
withdrawableAmount: bigint | undefined;
@@ -53,10 +45,6 @@ export const useWithdrawFormData = () => {
5345
export const WithdrawFormProvider: FC<{ children: ReactNode }> = ({
5446
children,
5547
}) => {
56-
const [submitStep, setSubmitStep] = useState<{
57-
step: SubmitStep;
58-
tx?: Address;
59-
}>(() => ({ step: SubmitStepEnum.edit }));
6048
const formObject = useForm<WithdrawFormSchema>({
6149
defaultValues: {
6250
amount: undefined,
@@ -65,11 +53,7 @@ export const WithdrawFormProvider: FC<{ children: ReactNode }> = ({
6553
mode: 'all',
6654
reValidateMode: 'onChange',
6755
});
68-
const { callWithdraw } = useWithdraw();
69-
const { retryEvent, retryFire } = useFormControllerRetry();
70-
const setModalState = useCallback((submitStep: SubmitPayload) => {
71-
setSubmitStep(submitStep);
72-
}, []);
56+
const { withdraw, retryEvent } = useWithdraw();
7357

7458
const {
7559
data: withdrawableAmount,
@@ -95,49 +79,34 @@ export const WithdrawFormProvider: FC<{ children: ReactNode }> = ({
9579

9680
const onSubmit = useCallback(
9781
async ({ amount, recipient }: WithdrawFormSchema) => {
98-
try {
99-
if (amount && recipient && isAddress(recipient)) {
100-
setModalState({ step: SubmitStepEnum.initiate });
101-
const tx = await callWithdraw({ amount, recipient, setModalState });
102-
setModalState({ step: SubmitStepEnum.overview, tx });
103-
return true;
104-
}
105-
} catch (err) {
106-
if (
107-
err instanceof Error &&
108-
err.message.includes('User rejected the request')
109-
) {
110-
setModalState({ step: SubmitStepEnum.reject });
111-
} else {
112-
setModalState({ step: SubmitStepEnum.error });
113-
}
82+
invariant(
83+
amount,
84+
'[WithdrawFormProvider] withdrawableAmount is undefined',
85+
);
86+
invariant(recipient, '[WithdrawFormProvider] recipient is undefined');
11487

115-
return false;
116-
}
88+
const { success } = await withdraw({ amount, recipient });
11789

118-
return false;
90+
return success;
11991
},
120-
// eslint-disable-next-line react-hooks/exhaustive-deps
121-
[callWithdraw],
92+
[withdraw],
12293
);
12394

12495
const formControllerValue: FormControllerContextValueType<WithdrawFormSchema> =
12596
useMemo(
12697
() => ({
12798
onSubmit,
12899
retryEvent,
129-
retryFire,
130100
onReset: formObject.reset,
131101
}),
132-
[retryFire, retryEvent, onSubmit, formObject.reset],
102+
[retryEvent, onSubmit, formObject.reset],
133103
);
134104

135105
return (
136106
<WithdrawDataContext.Provider value={withdrawData}>
137107
<FormProvider {...formObject}>
138108
<FormControllerContext.Provider value={formControllerValue}>
139109
<FormController>{children}</FormController>
140-
<SubmitModal submitStep={submitStep} setModalState={setModalState} />
141110
</FormControllerContext.Provider>
142111
</FormProvider>
143112
</WithdrawDataContext.Provider>

modules/web3/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './use-wstETH-by-stETH';
88
export * from './use-stETH-by-wstETH';
99
export * from './use-is-metamask';
1010
export * from './use-aa';
11+
export * from './use-send-tx';

0 commit comments

Comments
 (0)