@web3-react/store
Advanced tools
Comparing version 8.0.22-beta.0 to 8.0.23-beta.0
@@ -9,6 +9,2 @@ import type { Actions, Web3ReactStore } from '@web3-react/types'; | ||
export declare const MAX_SAFE_CHAIN_ID = 4503599627370476; | ||
export declare class ChainIdNotAllowedError extends Error { | ||
readonly chainId: number; | ||
constructor(chainId: number, allowedChainIds: number[]); | ||
} | ||
export declare function createWeb3ReactStoreAndActions(allowedChainIds?: number[]): [Web3ReactStore, Actions]; | ||
export declare function createWeb3ReactStoreAndActions(): [Web3ReactStore, Actions]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createWeb3ReactStoreAndActions = exports.ChainIdNotAllowedError = exports.MAX_SAFE_CHAIN_ID = void 0; | ||
exports.createWeb3ReactStoreAndActions = exports.MAX_SAFE_CHAIN_ID = void 0; | ||
const address_1 = require("@ethersproject/address"); | ||
@@ -18,16 +18,2 @@ const zustand_1 = require("zustand"); | ||
} | ||
class ChainIdNotAllowedError extends Error { | ||
constructor(chainId, allowedChainIds) { | ||
super(`chainId ${chainId} not included in ${allowedChainIds.toString()}`); | ||
this.chainId = chainId; | ||
this.name = ChainIdNotAllowedError.name; | ||
Object.setPrototypeOf(this, ChainIdNotAllowedError.prototype); | ||
} | ||
} | ||
exports.ChainIdNotAllowedError = ChainIdNotAllowedError; | ||
function ensureChainIdIsAllowed(chainId, allowedChainIds) { | ||
return allowedChainIds.some((allowedChainId) => chainId === allowedChainId) | ||
? undefined | ||
: new ChainIdNotAllowedError(chainId, allowedChainIds); | ||
} | ||
function validateAccount(account) { | ||
@@ -40,8 +26,4 @@ return (0, address_1.getAddress)(account); | ||
activating: false, | ||
error: undefined, | ||
}; | ||
function createWeb3ReactStoreAndActions(allowedChainIds) { | ||
if ((allowedChainIds === null || allowedChainIds === void 0 ? void 0 : allowedChainIds.length) === 0) { | ||
throw new Error(`allowedChainIds is length 0`); | ||
} | ||
function createWeb3ReactStoreAndActions() { | ||
const store = (0, zustand_1.createStore)()(() => DEFAULT_STATE); | ||
@@ -67,4 +49,3 @@ // flag for tracking updates so we don't clobber data when cancelling activation | ||
* Used to report a `stateUpdate` which is merged with existing state. The first `stateUpdate` that results in chainId | ||
* and accounts being set will also set activating to false, indicating a successful connection. Similarly, if an | ||
* error is set, the first `stateUpdate` that results in chainId and accounts being set will clear this error. | ||
* and accounts being set will also set activating to false, indicating a successful connection. | ||
* | ||
@@ -90,38 +71,19 @@ * @param stateUpdate - The state update to report. | ||
const accounts = (_b = stateUpdate.accounts) !== null && _b !== void 0 ? _b : existingState.accounts; | ||
// determine the next error | ||
let error = existingState.error; | ||
if (chainId && allowedChainIds) { | ||
// if we have a chainId allowlist and a chainId, we need to ensure it's allowed | ||
const chainIdError = ensureChainIdIsAllowed(chainId, allowedChainIds); | ||
// warn if we're going to clobber existing error | ||
if (chainIdError && error) { | ||
if (!(error instanceof ChainIdNotAllowedError) || error.chainId !== chainIdError.chainId) { | ||
console.debug(`${error.name} is being clobbered by ${chainIdError.name}`); | ||
} | ||
} | ||
error = chainIdError; | ||
} | ||
// ensure that the error is cleared when appropriate | ||
if (error && !(error instanceof ChainIdNotAllowedError) && chainId && accounts) { | ||
error = undefined; | ||
} | ||
// ensure that the activating flag is cleared when appropriate | ||
let activating = existingState.activating; | ||
if (activating && (error || (chainId && accounts))) { | ||
if (activating && chainId && accounts) { | ||
activating = false; | ||
} | ||
return { chainId, accounts, activating, error }; | ||
return { chainId, accounts, activating }; | ||
}); | ||
} | ||
/** | ||
* Used to report an `error`, which clears all existing state. | ||
* | ||
* @param error - The error to report. If undefined, the state will be reset to its default value. | ||
* Resets connector state back to the default state. | ||
*/ | ||
function reportError(error) { | ||
function resetState() { | ||
nullifier++; | ||
store.setState(() => (Object.assign(Object.assign({}, DEFAULT_STATE), { error }))); | ||
store.setState(DEFAULT_STATE); | ||
} | ||
return [store, { startActivation, update, reportError }]; | ||
return [store, { startActivation, update, resetState }]; | ||
} | ||
exports.createWeb3ReactStoreAndActions = createWeb3ReactStoreAndActions; |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "8.0.22-beta.0", | ||
"version": "8.0.23-beta.0", | ||
"files": [ | ||
@@ -28,6 +28,6 @@ "dist/*" | ||
"@ethersproject/address": "^5", | ||
"@web3-react/types": "^8.0.17-beta.0", | ||
"@web3-react/types": "^8.0.18-beta.0", | ||
"zustand": "^4.0.0-rc.0" | ||
}, | ||
"gitHead": "f8e5531f8facb53d7cb35165d1878db284c24736" | ||
"gitHead": "b497295f7f4aeecf86845551e8fd0ee53d27d202" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40064
94