Skip to content

Commit

Permalink
Refacto handleBridge Fn
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Nov 21, 2023
1 parent 7228d87 commit 68f5ade
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
28 changes: 28 additions & 0 deletions src/custom/bridge/handlers/handleBridge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { parseUnits } from 'viem';

import { handleErrorMessage } from './handleErrorMessage';
import { ILoadingState } from '@/const';

export async function handleBridge(
setLoading: (state: ILoadingState) => void,
setRedeemSteps: (state: string) => void,
setAmount: (state: string) => void,
amount: string | undefined,
_handleLockEVM: any,

Check warning on line 11 in src/custom/bridge/handlers/handleBridge.tsx

View workflow job for this annotation

GitHub Actions / lint-frontend

Unexpected any. Specify a different type
decimals: number,
) {
setLoading({ lock: 'loading' });

try {
if (!amount) {
throw new Error('Missing param');
}
await _handleLockEVM(parseUnits(amount, decimals));
} catch (error) {
setLoading({ box: 'error', lock: 'error', mint: 'error' });

handleErrorMessage(error as Error, setLoading, setRedeemSteps, setAmount);
return false;
}
return true;
}
45 changes: 17 additions & 28 deletions src/pages/Index/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { BRIDGE_OFF, REDEEM_OFF } from '@/const/env/maintenance';
import { forwardBurn } from '@/custom/bridge/bridge';
import { handleApproveEVM } from '@/custom/bridge/handlers/handleApproveEvm';
import { handleApproveMASSA } from '@/custom/bridge/handlers/handleApproveMassa';
import { handleBridge } from '@/custom/bridge/handlers/handleBridge';
import {
ICustomError,
handleClosePopUp,
Expand Down Expand Up @@ -177,7 +178,14 @@ export function Index() {
useEffect(() => {
if (approveIsSuccess) {
setLoading({ approve: 'success' });
handleBridge();
handleBridge(
setLoading,
setRedeemSteps,
setAmount,
amount,
_handleLockEVM,
decimals,
);
}
if (approveIsError) {
setLoading({
Expand Down Expand Up @@ -287,32 +295,6 @@ export function Index() {
return true;
}

async function handleBridge() {
setLoading({ lock: 'loading' });

try {
if (!amount) {
throw new Error('Missing param');
}

await _handleLockEVM(parseUnits(amount, decimals));
} catch (error) {
setLoading({ box: 'error', lock: 'error', mint: 'error' });

if (error)
handleErrorMessage(
error as Error,
setLoading,
setRedeemSteps,
setAmount,
);

return false;
}

return true;
}

async function handleRedeem(client: Client) {
try {
if (!token || !evmAddress || !amount) {
Expand Down Expand Up @@ -400,7 +382,14 @@ export function Index() {
);

if (approved) {
await handleBridge();
await handleBridge(
setLoading,
setRedeemSteps,
setAmount,
amount,
_handleLockEVM,
decimals,
);
}
}
}
Expand Down

0 comments on commit 68f5ade

Please sign in to comment.