@metamask-previews/network-controller
Advanced tools
Comparing version 12.1.2-preview.c338244 to 12.1.2-preview.c5136a4
@@ -196,3 +196,7 @@ import type { RestrictedControllerMessenger } from '@metamask/base-controller'; | ||
}; | ||
export declare type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction; | ||
export declare type NetworkControllerGetNetworkClientByIdAction = { | ||
type: `NetworkController:getNetworkClientById`; | ||
handler: NetworkController['getNetworkClientById']; | ||
}; | ||
export declare type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction | NetworkControllerGetNetworkClientByIdAction; | ||
export declare type NetworkControllerMessenger = RestrictedControllerMessenger<typeof name, NetworkControllerActions, NetworkControllerEvents, string, string>; | ||
@@ -261,2 +265,9 @@ export declare type NetworkControllerOptions = { | ||
/** | ||
* Refreshes the network meta with EIP-1559 support and the network status | ||
* based on the given network client ID. | ||
* | ||
* @param networkClientId - The ID of the network client to update. | ||
*/ | ||
lookupNetworkByClientId(networkClientId: NetworkClientId): Promise<void>; | ||
/** | ||
* Performs side effects after switching to a network. If the network is | ||
@@ -267,2 +278,4 @@ * available, updates the network state with the network ID of the network and | ||
* | ||
* @param networkClientId - (Optional) The ID of the network client to update. | ||
* If no ID is provided, uses the currently selected network. | ||
* @fires infuraIsBlocked if the network is Infura-supported and is blocking | ||
@@ -273,3 +286,3 @@ * requests. | ||
*/ | ||
lookupNetwork(): Promise<void>; | ||
lookupNetwork(networkClientId?: NetworkClientId): Promise<void>; | ||
/** | ||
@@ -292,6 +305,8 @@ * Convenience method to update provider network type settings. | ||
* | ||
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility. | ||
* @returns A promise that resolves to true if the network supports EIP-1559 | ||
* , false otherwise, or `undefined` if unable to determine the compatibility. | ||
*/ | ||
getEIP1559Compatibility(): Promise<boolean | undefined>; | ||
getEIP1559Compatibility(networkClientId?: NetworkClientId): Promise<boolean | undefined>; | ||
get1555CompatibilityWithNetworkClientId(networkClientId: NetworkClientId): Promise<boolean>; | ||
/** | ||
@@ -298,0 +313,0 @@ * Re-initializes the provider and block tracker for the current network. |
@@ -274,2 +274,3 @@ "use strict"; | ||
}); | ||
this.messagingSystem.registerActionHandler(`${this.name}:getNetworkClientById`, this.getNetworkClientById.bind(this)); | ||
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f"); | ||
@@ -330,2 +331,70 @@ } | ||
/** | ||
* Refreshes the network meta with EIP-1559 support and the network status | ||
* based on the given network client ID. | ||
* | ||
* @param networkClientId - The ID of the network client to update. | ||
*/ | ||
lookupNetworkByClientId(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const isInfura = isInfuraProviderType(networkClientId); | ||
let updatedNetworkStatus; | ||
let updatedIsEIP1559Compatible; | ||
try { | ||
updatedIsEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId); | ||
updatedNetworkStatus = constants_1.NetworkStatus.Available; | ||
} | ||
catch (error) { | ||
if (isErrorWithCode(error)) { | ||
let responseBody; | ||
if (isInfura && | ||
(0, utils_1.hasProperty)(error, 'message') && | ||
typeof error.message === 'string') { | ||
try { | ||
responseBody = JSON.parse(error.message); | ||
} | ||
catch (_a) { | ||
// error.message must not be JSON | ||
} | ||
} | ||
if ((0, utils_1.isPlainObject)(responseBody) && | ||
responseBody.error === constants_1.INFURA_BLOCKED_KEY) { | ||
updatedNetworkStatus = constants_1.NetworkStatus.Blocked; | ||
} | ||
else if (error.code === eth_rpc_errors_1.errorCodes.rpc.internal) { | ||
updatedNetworkStatus = constants_1.NetworkStatus.Unknown; | ||
} | ||
else { | ||
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable; | ||
} | ||
} | ||
else if (typeof Error !== 'undefined' && | ||
(0, utils_1.hasProperty)(error, 'message') && | ||
typeof error.message === 'string' && | ||
error.message.includes('No custom network client was found with the ID')) { | ||
throw error; | ||
} | ||
else { | ||
log('NetworkController - could not determine network status', error); | ||
updatedNetworkStatus = constants_1.NetworkStatus.Unknown; | ||
} | ||
} | ||
this.update((state) => { | ||
if (state.networksMetadata[networkClientId] === undefined) { | ||
state.networksMetadata[networkClientId] = { | ||
status: constants_1.NetworkStatus.Unknown, | ||
EIPS: {}, | ||
}; | ||
} | ||
const meta = state.networksMetadata[networkClientId]; | ||
meta.status = updatedNetworkStatus; | ||
if (updatedIsEIP1559Compatible === undefined) { | ||
delete meta.EIPS[1559]; | ||
} | ||
else { | ||
meta.EIPS[1559] = updatedIsEIP1559Compatible; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Performs side effects after switching to a network. If the network is | ||
@@ -336,2 +405,4 @@ * available, updates the network state with the network ID of the network and | ||
* | ||
* @param networkClientId - (Optional) The ID of the network client to update. | ||
* If no ID is provided, uses the currently selected network. | ||
* @fires infuraIsBlocked if the network is Infura-supported and is blocking | ||
@@ -342,4 +413,8 @@ * requests. | ||
*/ | ||
lookupNetwork() { | ||
lookupNetwork(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (networkClientId) { | ||
yield this.lookupNetworkByClientId(networkClientId); | ||
return; | ||
} | ||
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) { | ||
@@ -360,4 +435,4 @@ return; | ||
const [networkId, isEIP1559Compatible] = yield Promise.all([ | ||
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getNetworkId).call(this), | ||
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this), | ||
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getNetworkId).call(this, this.state.selectedNetworkClientId), | ||
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId), | ||
]); | ||
@@ -487,7 +562,11 @@ updatedNetworkStatus = constants_1.NetworkStatus.Available; | ||
* | ||
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility. | ||
* @returns A promise that resolves to true if the network supports EIP-1559 | ||
* , false otherwise, or `undefined` if unable to determine the compatibility. | ||
*/ | ||
getEIP1559Compatibility() { | ||
getEIP1559Compatibility(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (networkClientId) { | ||
return this.get1555CompatibilityWithNetworkClientId(networkClientId); | ||
} | ||
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) { | ||
@@ -500,3 +579,3 @@ return false; | ||
} | ||
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this); | ||
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId); | ||
this.update((state) => { | ||
@@ -511,2 +590,14 @@ if (isEIP1559Compatible !== undefined) { | ||
} | ||
get1555CompatibilityWithNetworkClientId(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let metadata = this.state.networksMetadata[networkClientId]; | ||
if (metadata === undefined) { | ||
yield this.lookupNetwork(networkClientId); | ||
metadata = this.state.networksMetadata[networkClientId]; | ||
} | ||
const { EIPS } = metadata; | ||
// may want to include some 'freshness' value - something to make sure we refetch this from time to time | ||
return EIPS[1559]; | ||
}); | ||
} | ||
/** | ||
@@ -701,9 +792,14 @@ * Re-initializes the provider and block tracker for the current network. | ||
}); | ||
}, _NetworkController_getNetworkId = function _NetworkController_getNetworkId() { | ||
}, _NetworkController_getNetworkId = function _NetworkController_getNetworkId(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const possibleNetworkId = yield new Promise((resolve, reject) => { | ||
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) { | ||
let ethQuery = __classPrivateFieldGet(this, _NetworkController_ethQuery, "f"); | ||
if (networkClientId) { | ||
const networkClient = this.getNetworkClientById(networkClientId); | ||
ethQuery = new eth_query_1.default(networkClient.provider); | ||
} | ||
if (!ethQuery) { | ||
throw new Error('Provider has not been initialized'); | ||
} | ||
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'net_version' }, (error, result) => { | ||
ethQuery.sendAsync({ method: 'net_version' }, (error, result) => { | ||
if (error) { | ||
@@ -720,8 +816,10 @@ reject(error); | ||
}); | ||
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock() { | ||
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock(networkClientId) { | ||
if (networkClientId === undefined) { | ||
networkClientId = this.state.selectedNetworkClientId; | ||
} | ||
const networkClient = this.getNetworkClientById(networkClientId); | ||
const ethQuery = new eth_query_1.default(networkClient.provider); | ||
return new Promise((resolve, reject) => { | ||
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) { | ||
throw new Error('Provider has not been initialized'); | ||
} | ||
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => { | ||
ethQuery.sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => { | ||
if (error) { | ||
@@ -736,5 +834,5 @@ reject(error); | ||
}); | ||
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility() { | ||
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility(networkClientId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this); | ||
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this, networkClientId); | ||
if (!latestBlock) { | ||
@@ -741,0 +839,0 @@ return undefined; |
{ | ||
"name": "@metamask-previews/network-controller", | ||
"version": "12.1.2-preview.c338244", | ||
"version": "12.1.2-preview.c5136a4", | ||
"description": "Provides an interface to the currently selected network via a MetaMask-compatible provider object", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
"@metamask/eth-json-rpc-infura": "^8.1.1", | ||
"@metamask/eth-json-rpc-middleware": "^11.0.0", | ||
"@metamask/eth-json-rpc-middleware": "^11.0.2", | ||
"@metamask/eth-json-rpc-provider": "^1.0.0", | ||
@@ -37,0 +37,0 @@ "@metamask/eth-query": "^3.0.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
222742
1819
32
419
8