Skip to content

Commit 3c8a093

Browse files
committed
feat: mint/repay new flow
1 parent 96691f3 commit 3c8a093

13 files changed

Lines changed: 217 additions & 285 deletions

File tree

features/adjustment/mint/form/submit-button/submit-button.tsx

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 { useVaultInfo, type VAULT_OWNER_ROLES } from 'modules/vaults';
32
import { MultiplePermissionedSubmitButton } from 'modules/vaults/components';
43
import { useMemo } from 'react';
@@ -39,14 +38,12 @@ export const SubmitButton = () => {
3938
const disabled = isSubmitting && !isValid;
4039

4140
return (
42-
<OracleReportButton action="minting">
43-
<MultiplePermissionedSubmitButton
44-
dashboardRoles={roles}
45-
type="submit"
46-
disabled={disabled}
47-
>
48-
Mint
49-
</MultiplePermissionedSubmitButton>
50-
</OracleReportButton>
41+
<MultiplePermissionedSubmitButton
42+
dashboardRoles={roles}
43+
type="submit"
44+
disabled={disabled}
45+
>
46+
Mint
47+
</MultiplePermissionedSubmitButton>
5148
);
5249
};

features/adjustment/mint/hooks/use-mint.ts

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,59 @@
11
import { useCallback } from 'react';
2-
import {
3-
useConfig,
4-
useWriteContract,
5-
usePublicClient,
6-
useEstimateGas,
7-
useAccount,
8-
} from 'wagmi';
2+
import { useEstimateGas, useAccount } from 'wagmi';
93
import { Address, encodeFunctionData } from 'viem';
104

115
import { dashboardAbi } from 'abi/dashboard-abi';
12-
import { useDappStatus } from 'modules/web3/hooks/use-dapp-status';
136
import { useVaultInfo } from 'modules/vaults';
14-
import {
15-
SubmitPayload,
16-
SubmitStepEnum,
17-
} from 'shared/components/submit-modal/types';
187
import invariant from 'tiny-invariant';
198
import { useVaultPermission } from 'modules/vaults/hooks/use-vault-permissions';
209
import { fallbackedAddress } from 'utils/fallbacked-address';
10+
import { useSendTransaction, withSuccess } from 'modules/web3';
11+
import { useReportStatus } from 'features/report/use-report';
2112

22-
export const useMint = (onMutate = () => {}) => {
23-
const { chainId } = useDappStatus();
24-
const wagmiConfig = useConfig();
13+
export const useMint = () => {
2514
const { activeVault } = useVaultInfo();
26-
const publicClient = usePublicClient();
27-
const { data: mintTx, writeContractAsync } = useWriteContract({
28-
config: wagmiConfig,
29-
mutation: {
30-
onMutate,
31-
},
32-
});
15+
const { shouldApplyReport, prepareReportCall } = useReportStatus();
16+
const { sendTX, ...rest } = useSendTransaction();
17+
18+
return {
19+
mint: useCallback(
20+
async (recipient: Address, amount: bigint, token: string) => {
21+
invariant(activeVault?.owner, '[useMint] owner is undefined');
3322

34-
const callMint = useCallback(
35-
async (
36-
recipient: Address,
37-
amount: bigint,
38-
token: string,
39-
setModalState: (submitStep: SubmitPayload) => void,
40-
) => {
41-
invariant(publicClient, '[useMintDashboard] publicClient is undefined');
23+
const loadingActionText = `Minting ${token} backed by vault`;
24+
const mainActionCompleteText = `Minted ${token} backed by vault`;
4225

43-
setModalState({ step: SubmitStepEnum.confirming });
44-
const tx = await writeContractAsync({
45-
abi: dashboardAbi,
46-
address: activeVault?.owner as Address,
47-
functionName: token === 'stETH' ? 'mintStETH' : 'mintWstETH',
48-
args: [recipient, amount],
49-
chainId,
50-
});
26+
const mintCall = {
27+
to: activeVault.owner,
28+
data: encodeFunctionData({
29+
abi: dashboardAbi,
30+
functionName: token === 'stETH' ? 'mintStETH' : 'mintWstETH',
31+
args: [recipient, amount],
32+
}),
33+
value: amount,
34+
loadingActionText,
35+
};
5136

52-
setModalState({ step: SubmitStepEnum.submitting, tx });
53-
await publicClient.waitForTransactionReceipt({
54-
hash: tx,
55-
});
37+
// if we have to post report, there will be extra modal due to async fetch
38+
const transactions = shouldApplyReport
39+
? async () => {
40+
return [await prepareReportCall(), mintCall];
41+
}
42+
: [mintCall];
5643

57-
return tx;
58-
},
59-
[chainId, writeContractAsync, activeVault?.owner, publicClient],
60-
);
44+
const { success } = await withSuccess(
45+
sendTX({
46+
transactions,
47+
mainActionLoadingText: loadingActionText,
48+
mainActionCompleteText,
49+
}),
50+
);
6151

62-
return {
63-
callMint,
64-
mintTx,
52+
return success;
53+
},
54+
[activeVault?.owner, prepareReportCall, sendTX, shouldApplyReport],
55+
),
56+
...rest,
6557
};
6658
};
6759

features/adjustment/mint/mint-form-context/mint-form-provider.tsx

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import {
55
useCallback,
66
createContext,
77
useContext,
8-
useState,
98
} from 'react';
109
import { FormProvider, useForm } from 'react-hook-form';
1110
import invariant from 'tiny-invariant';
1211

13-
import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate';
1412
import { useVaultInfo } from 'modules/vaults';
1513
import { useMint } from 'features/adjustment/mint/hooks';
1614

@@ -20,12 +18,6 @@ import {
2018
FormControllerContextValueType,
2119
} from 'shared/hook-form/form-controller';
2220
import { MintFormSchema } from 'features/adjustment/mint/types';
23-
import { SubmitModal } from 'shared/components';
24-
import {
25-
SubmitPayload,
26-
SubmitStep,
27-
SubmitStepEnum,
28-
} from 'shared/components/submit-modal/types';
2921
import { Address } from 'viem';
3022

3123
type MintDataContextValue = {
@@ -48,20 +40,17 @@ export const useMintFormData = () => {
4840
};
4941

5042
export const MintFormProvider: FC<{ children: ReactNode }> = ({ children }) => {
51-
const [submitStep, setSubmitStep] = useState<{
52-
step: SubmitStep;
53-
tx?: Address;
54-
}>(() => ({ step: SubmitStepEnum.edit }));
5543
const formObject = useForm<MintFormSchema>({
5644
defaultValues: {
5745
amount: undefined,
5846
token: 'stETH',
5947
recipient: '' as Address,
6048
},
6149
mode: 'all',
50+
// TODO: validation
6251
reValidateMode: 'onChange',
6352
});
64-
const { callMint } = useMint();
53+
const { mint, retryEvent } = useMint();
6554
const { activeVault } = useVaultInfo();
6655

6756
const mintData = useMemo(() => {
@@ -76,54 +65,35 @@ export const MintFormProvider: FC<{ children: ReactNode }> = ({ children }) => {
7665
};
7766
}, [activeVault]);
7867

79-
const { retryEvent, retryFire } = useFormControllerRetry();
80-
const setModalState = useCallback((submitStep: SubmitPayload) => {
81-
setSubmitStep(submitStep);
82-
}, []);
83-
8468
const onSubmit = useCallback(
8569
async ({ recipient, amount, token }: MintFormSchema) => {
86-
if (amount && recipient) {
87-
try {
88-
setModalState({ step: SubmitStepEnum.initiate });
89-
const tx = await callMint(recipient, amount, token, setModalState);
90-
setModalState({ step: SubmitStepEnum.overview, tx });
91-
return true;
92-
} catch (err) {
93-
if (
94-
err instanceof Error &&
95-
err.message.includes('User rejected the request')
96-
) {
97-
setModalState({ step: SubmitStepEnum.reject });
98-
} else {
99-
setModalState({ step: SubmitStepEnum.error });
100-
}
101-
}
102-
}
70+
// TODO: add validation, remove stub
71+
if (!recipient || !amount) return false;
72+
invariant(recipient, '[MintFormProvider] recipient is undefined');
73+
invariant(amount, '[MintFormProvider] amount is undefined');
74+
invariant(token, '[MintFormProvider] token is undefined');
10375

104-
return false;
76+
return await mint(recipient, amount, token);
10577
},
10678
// eslint-disable-next-line react-hooks/exhaustive-deps
107-
[callMint],
79+
[mint],
10880
);
10981

11082
const formControllerValue: FormControllerContextValueType<MintFormSchema> =
11183
useMemo(
11284
() => ({
11385
onSubmit,
11486
retryEvent,
115-
retryFire,
11687
onReset: formObject.reset,
11788
}),
118-
[retryFire, retryEvent, onSubmit, formObject.reset],
89+
[retryEvent, onSubmit, formObject.reset],
11990
);
12091

12192
return (
12293
<MintDataContext.Provider value={mintData}>
12394
<FormProvider {...formObject}>
12495
<FormControllerContext.Provider value={formControllerValue}>
12596
<FormController>{children}</FormController>
126-
<SubmitModal submitStep={submitStep} setModalState={setModalState} />
12797
</FormControllerContext.Provider>
12898
</FormProvider>
12999
</MintDataContext.Provider>

0 commit comments

Comments
 (0)