-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement useHandleOperation and refactor useWriteSmartContract…
… for improved operation handling
- Loading branch information
Showing
9 changed files
with
207 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { OperationStatus } from '@massalabs/massa-web3'; | ||
|
||
export const ERROR_STATUSES = [ | ||
OperationStatus.Error, | ||
OperationStatus.SpeculativeError, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type ToasterMessage = { | ||
pending: string; | ||
success: string; | ||
error: string; | ||
timeout?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useState } from 'react'; | ||
import { CHAIN_ID, Operation, OperationStatus } from '@massalabs/massa-web3'; | ||
import Intl from '../i18n'; | ||
import { toast } from '../../../components'; | ||
import { logSmartContractEvents, showToast } from '../utils'; | ||
import { ToasterMessage } from './types'; | ||
import { ERROR_STATUSES } from './const'; | ||
|
||
// TODO: Need to be refactored with the useWriteSmartContract.tsx | ||
export function useHandleOperation() { | ||
const [state, setState] = useState({ | ||
isOpPending: false, | ||
isPending: false, | ||
isSuccess: false, | ||
isError: false, | ||
opId: undefined as string | undefined, | ||
}); | ||
|
||
async function handleOperation( | ||
operation: Operation, | ||
messages: ToasterMessage, | ||
final = false, | ||
): Promise<Operation | undefined> { | ||
const networkInfo = await operation.provider.networkInfos(); | ||
const isMainnet = networkInfo.chainId === CHAIN_ID.Mainnet; | ||
|
||
if (state.isOpPending) { | ||
throw new Error('Operation is already pending'); | ||
} | ||
|
||
setState({ | ||
...state, | ||
isOpPending: true, | ||
isPending: true, | ||
isSuccess: false, | ||
isError: false, | ||
opId: undefined, | ||
}); | ||
|
||
try { | ||
setState((prev) => ({ ...prev, opId: operation.id })); | ||
|
||
const loadingToastId = showToast( | ||
'loading', | ||
messages.pending, | ||
operation.id, | ||
isMainnet, | ||
); | ||
|
||
const finalStatus = final | ||
? await operation.waitFinalExecution() | ||
: await operation.waitSpeculativeExecution(); | ||
|
||
dismissLoadingToast(loadingToastId); | ||
|
||
if (finalStatus === OperationStatus.NotFound) { | ||
handleOperationTimeout(messages.timeout, operation.id); | ||
throw new Error('Operation not found'); | ||
} else if (ERROR_STATUSES.includes(finalStatus)) { | ||
logSmartContractEvents(operation.provider, operation.id); | ||
throw new Error(`Operation failed with status: ${finalStatus}`); | ||
} else { | ||
handleOperationSuccess(messages.success, operation.id); | ||
return operation; | ||
} | ||
} catch (error) { | ||
handleOperationError(error, messages.error, state.opId); | ||
} finally { | ||
setState((prev) => ({ ...prev, isOpPending: false, isPending: false })); | ||
} | ||
} | ||
|
||
function dismissLoadingToast(toastId?: string): void { | ||
if (toastId) { | ||
toast.dismiss(toastId); | ||
} else { | ||
console.warn('Attempted to dismiss a toast with undefined ID.'); | ||
} | ||
} | ||
|
||
function handleOperationTimeout( | ||
timeoutMessage?: string, | ||
opId?: string, | ||
): void { | ||
setState((prev) => ({ ...prev, isError: true })); | ||
showToast('error', timeoutMessage || Intl.t('steps.failed-timeout'), opId); | ||
} | ||
|
||
function handleOperationSuccess(successMessage: string, opId?: string): void { | ||
setState((prev) => ({ ...prev, isSuccess: true })); | ||
showToast('success', successMessage, opId); | ||
} | ||
|
||
function handleOperationError( | ||
error: unknown, | ||
errorMessage: string, | ||
opId?: string, | ||
): void { | ||
console.error('Error during smart contract call:', error); | ||
setState((prev) => ({ ...prev, isError: true })); | ||
showToast('error', errorMessage, opId); | ||
} | ||
|
||
return { | ||
opId: state.opId, | ||
isOpPending: state.isOpPending, | ||
isPending: state.isPending, | ||
isSuccess: state.isSuccess, | ||
isError: state.isError, | ||
handleOperation, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './hooks/useWriteSmartContract'; | ||
export * from './hooks/useHandleOperation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters