diff --git a/utils/gear-hooks/package.json b/utils/gear-hooks/package.json index bc1ad3d331..345c5ff7fc 100644 --- a/utils/gear-hooks/package.json +++ b/utils/gear-hooks/package.json @@ -1,6 +1,6 @@ { "name": "@gear-js/react-hooks", - "version": "0.6.1", + "version": "0.6.2", "description": "React hooks used across Gear applications", "author": "Gear Technologies", "license": "GPL-3.0", @@ -21,14 +21,14 @@ "prepare": "npm run build" }, "peerDependencies": { - "@gear-js/api": "0.33.0", + "@gear-js/api": "0.33.1", "@polkadot/api": "10.9.1", "@polkadot/extension-dapp": "0.46.5", "react": "^18.2.0", "react-transition-group": "4.4.5" }, "devDependencies": { - "@gear-js/api": "0.33.0", + "@gear-js/api": "0.33.1", "@polkadot/api": "10.9.1", "@polkadot/extension-dapp": "0.46.5", "@polkadot/extension-inject": "0.46.5", diff --git a/utils/gear-hooks/src/hooks/api/useProgram/useHandlers/useHandleSignStatus.ts b/utils/gear-hooks/src/hooks/api/useProgram/useHandlers/useHandleSignStatus.ts index 16abd27e02..3a1f06c2f7 100644 --- a/utils/gear-hooks/src/hooks/api/useProgram/useHandlers/useHandleSignStatus.ts +++ b/utils/gear-hooks/src/hooks/api/useProgram/useHandlers/useHandleSignStatus.ts @@ -3,8 +3,8 @@ import { HexString } from '@polkadot/util/types'; import { useContext } from 'react'; import { DEFAULT_SUCCESS_OPTIONS, DEFAULT_ERROR_OPTIONS } from 'consts'; import { AlertContext, ApiContext } from 'context'; +import { getExtrinsicFailedMessage } from 'utils'; import { Callbacks, Method, HandleSignStatusParams, TransactionStatus, ProgramError } from '../types'; -import { getExtrinsicFailedMessage } from '../utils'; function useHandleSignStatus() { const { api } = useContext(ApiContext); // сircular dependency fix diff --git a/utils/gear-hooks/src/hooks/api/useProgram/utils.ts b/utils/gear-hooks/src/hooks/api/useProgram/utils.ts index 92e9654cac..4e6d952f5c 100644 --- a/utils/gear-hooks/src/hooks/api/useProgram/utils.ts +++ b/utils/gear-hooks/src/hooks/api/useProgram/utils.ts @@ -1,15 +1,7 @@ import { GearApi } from '@gear-js/api'; import { UnsubscribePromise } from '@polkadot/api/types'; -import { Event } from '@polkadot/types/interfaces'; import { ProgramStatus } from './types'; -const getExtrinsicFailedMessage = (api: GearApi, event: Event) => { - const { docs, method: errorMethod } = api.getExtrinsicFailedError(event); - const formattedDocs = docs.filter(Boolean).join('. '); - - return `${errorMethod}: ${formattedDocs}`; -}; - const waitForProgramInit = (api: GearApi, programId: string) => { let unsubPromise: UnsubscribePromise; @@ -28,4 +20,4 @@ const waitForProgramInit = (api: GearApi, programId: string) => { }).finally(unsubscribe); }; -export { getExtrinsicFailedMessage, waitForProgramInit }; +export { waitForProgramInit }; diff --git a/utils/gear-hooks/src/hooks/api/useSendMessage.ts b/utils/gear-hooks/src/hooks/api/useSendMessage.ts index 855df8358f..700e3d0990 100644 --- a/utils/gear-hooks/src/hooks/api/useSendMessage.ts +++ b/utils/gear-hooks/src/hooks/api/useSendMessage.ts @@ -3,10 +3,10 @@ import { web3FromSource } from '@polkadot/extension-dapp'; import { EventRecord } from '@polkadot/types/interfaces'; import { AnyJson, ISubmittableResult } from '@polkadot/types/types'; import { HexString } from '@polkadot/util/types'; -import { useContext, useRef } from 'react'; +import { useContext } from 'react'; import { AccountContext, AlertContext, ApiContext } from 'context'; import { DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from 'consts'; -import { getAutoGasLimit } from 'utils'; +import { getAutoGasLimit, getExtrinsicFailedMessage } from 'utils'; type UseSendMessageOptions = { isMaxGasLimit?: boolean; @@ -34,13 +34,18 @@ function useSendMessage( const title = 'gear.sendMessage'; const handleEventsStatus = (events: EventRecord[], onSuccess?: () => void, onError?: () => void) => { - events.forEach(({ event: { method, section } }) => { + events.forEach(({ event }) => { + const { method, section } = event; + if (method === 'MessageQueued') { if (!disableAlerts) alert.success(`${section}.MessageQueued`); onSuccess && onSuccess(); } else if (method === 'ExtrinsicFailed') { - alert.error('Extrinsic Failed', { title }); + const message = getExtrinsicFailedMessage(api, event); + + console.error(message); + alert.error(message, { title }); onError && onError(); } @@ -89,7 +94,11 @@ function useSendMessage( .then(({ signer }) => api.message.signAndSend(address, { signer }, (result) => handleStatus(result, alertId, onSuccess, onError)), ) - .catch(({ message }: Error) => { + .catch((error: Error) => { + const { message } = error; + + console.error(error); + if (alertId) { alert.update(alertId, message, DEFAULT_ERROR_OPTIONS); } else { diff --git a/utils/gear-hooks/src/utils/index.ts b/utils/gear-hooks/src/utils/index.ts index 046b16d874..36dba5dad5 100644 --- a/utils/gear-hooks/src/utils/index.ts +++ b/utils/gear-hooks/src/utils/index.ts @@ -1,5 +1,6 @@ -import { GasInfo } from '@gear-js/api'; +import { GasInfo, GearApi } from '@gear-js/api'; import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'; +import { Event } from '@polkadot/types/interfaces'; import { bnToBn } from '@polkadot/util'; const isLoggedIn = ({ address }: InjectedAccountWithMeta) => localStorage.account === address; @@ -9,4 +10,11 @@ const getAutoGasLimit = ({ waited, min_limit }: GasInfo) => const withoutCommas = (value: string) => value.replace(/,/g, ''); -export { isLoggedIn, getAutoGasLimit, withoutCommas }; +const getExtrinsicFailedMessage = (api: GearApi, event: Event) => { + const { docs, method: errorMethod } = api.getExtrinsicFailedError(event); + const formattedDocs = docs.filter(Boolean).join('. '); + + return `${errorMethod}: ${formattedDocs}`; +}; + +export { isLoggedIn, getAutoGasLimit, withoutCommas, getExtrinsicFailedMessage }; diff --git a/utils/gear-hooks/yarn.lock b/utils/gear-hooks/yarn.lock index d9df9a17e2..d0af02986f 100644 --- a/utils/gear-hooks/yarn.lock +++ b/utils/gear-hooks/yarn.lock @@ -48,14 +48,14 @@ __metadata: languageName: node linkType: hard -"@gear-js/api@npm:0.33.0": - version: 0.33.0 - resolution: "@gear-js/api@npm:0.33.0" +"@gear-js/api@npm:0.33.1": + version: 0.33.1 + resolution: "@gear-js/api@npm:0.33.1" peerDependencies: "@polkadot/api": 10.9.1 "@polkadot/wasm-crypto": 7.2.1 rxjs: 7.8.1 - checksum: 49cdd845dfc357baf2c206d628cfa879b67a091d0f2c14b212b7e9942f70bbd1b64ffe9b30a4749d492f156a61cca57936aa5a382e71465a2f7abf168ce382ca + checksum: 6de7f89e52c7a17ae0978dc9c30b9eac15a19df45193647ed3bbc2690b2f15daaf25ffcc9b3df9e0f1b57d000519461f44b2b60173b94251f5edcd2bfda104c7 languageName: node linkType: hard @@ -63,7 +63,7 @@ __metadata: version: 0.0.0-use.local resolution: "@gear-js/react-hooks@workspace:." dependencies: - "@gear-js/api": 0.33.0 + "@gear-js/api": 0.33.1 "@polkadot/api": 10.9.1 "@polkadot/extension-dapp": 0.46.5 "@polkadot/extension-inject": 0.46.5 @@ -84,7 +84,7 @@ __metadata: rollup-plugin-typescript2: 0.31.2 typescript: 4.9.5 peerDependencies: - "@gear-js/api": 0.33.0 + "@gear-js/api": 0.33.1 "@polkadot/api": 10.9.1 "@polkadot/extension-dapp": 0.46.5 react: ^18.2.0