UNPKG

@dynamic-labs/sdk-react-core

Version:

A React SDK for implementing wallet web3 authentication and authorization to your website.

80 lines (75 loc) 3.12 kB
'use client' 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var utils = require('@dynamic-labs/utils'); require('@dynamic-labs/sdk-api-core'); var logger = require('../../../shared/logger.cjs'); require('@dynamic-labs/iconic'); require('@dynamic-labs/wallet-connector-core'); require('react'); require('react/jsx-runtime'); require('../../../context/ViewContext/ViewContext.cjs'); require('@dynamic-labs/wallet-book'); require('../../../utils/constants/colors.cjs'); require('../../../utils/constants/values.cjs'); require('../../../shared/consts/index.cjs'); const magicRpcError = -32603; const errorCodesMapping = { '-32002': 'Insufficient funds for this transaction.', 0: 'The operation either timed out or was not allowed.', 16: 'Invalid Passkey signature. Select the passkey for this account and device.', 3: 'You reached the limit of passkeys for this account.', 5: 'Invalid Passkey signature.', INSUFFICIENT_FUNDS: 'Insufficient funds for this transaction.', [magicRpcError.toString()]: 'A network error has occurred. Please try again later', }; const errorReasonsMapping = { 'provided ENS name resolves to null': 'Invalid address. Please check that the entered address is correct.', }; const getCode = (error) => { var _a; const err = error; return (err === null || err === void 0 ? void 0 : err.code) || ((_a = err === null || err === void 0 ? void 0 : err.cause) === null || _a === void 0 ? void 0 : _a.code); }; const getMessage = (error) => { const err = error; return err === null || err === void 0 ? void 0 : err.message; }; const hasReason = (error) => 'reason' in error && error.reason !== undefined; const isTransactionExecutionError = (error) => error !== undefined && error !== null && typeof error === 'object' && 'walk' in error && typeof error['walk'] === 'function'; const transactionErrorMessage = (err) => { let error = err; if (isTransactionExecutionError(err)) { error = err.walk(); } logger.logger.debug('transaction error:', error); if (utils.TransactionGasCannotBeSponsoredError.isInstance(error)) { return; } if (utils.InsufficientFundsError.isInstance(error)) { return errorCodesMapping.INSUFFICIENT_FUNDS; } if (isMagicInsufficientFundsError(error)) { return errorCodesMapping.INSUFFICIENT_FUNDS; } const code = getCode(error); if (code !== undefined && errorCodesMapping[code]) { return errorCodesMapping[code]; } if (hasReason(error) && errorReasonsMapping[error.reason]) { return errorReasonsMapping[error.reason]; } return 'Something went wrong.'; }; const isMagicInsufficientFundsError = (error) => { const code = getCode(error); const message = getMessage(error); return (code === magicRpcError && Boolean(message === null || message === void 0 ? void 0 : message.includes('insufficient funds for gas * price + value'))); }; exports.isTransactionExecutionError = isTransactionExecutionError; exports.transactionErrorMessage = transactionErrorMessage;