@metamask-previews/network-controller 23.2.0-preview-66f06159 → 23.2.0-preview-ddbcf048
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -12
- package/dist/NetworkController.cjs +3 -51
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +5 -29
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +5 -29
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +3 -51
- package/dist/NetworkController.mjs.map +1 -1
- package/dist/create-auto-managed-network-client.cjs +31 -46
- package/dist/create-auto-managed-network-client.cjs.map +1 -1
- package/dist/create-auto-managed-network-client.d.cts +1 -11
- package/dist/create-auto-managed-network-client.d.cts.map +1 -1
- package/dist/create-auto-managed-network-client.d.mts +1 -11
- package/dist/create-auto-managed-network-client.d.mts.map +1 -1
- package/dist/create-auto-managed-network-client.mjs +31 -46
- package/dist/create-auto-managed-network-client.mjs.map +1 -1
- package/dist/create-network-client.cjs +17 -43
- package/dist/create-network-client.cjs.map +1 -1
- package/dist/create-network-client.d.cts +2 -11
- package/dist/create-network-client.d.cts.map +1 -1
- package/dist/create-network-client.d.mts +2 -11
- package/dist/create-network-client.d.mts.map +1 -1
- package/dist/create-network-client.mjs +17 -43
- package/dist/create-network-client.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -31,32 +31,11 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
|
|
|
31
31
|
* used to instantiate the network client when it is needed.
|
|
32
32
|
* @param args.getRpcServiceOptions - Factory for constructing RPC service
|
|
33
33
|
* options. See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
34
|
-
* @param args.getBlockTrackerOptions - Factory for constructing block tracker
|
|
35
|
-
* options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.
|
|
36
34
|
* @param args.messenger - The network controller messenger.
|
|
37
|
-
* @param args.isRpcFailoverEnabled - Whether or not requests sent to the
|
|
38
|
-
* primary RPC endpoint for this network should be automatically diverted to
|
|
39
|
-
* provided failover endpoints if the primary is unavailable.
|
|
40
35
|
* @returns The auto-managed network client.
|
|
41
36
|
*/
|
|
42
|
-
function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions,
|
|
37
|
+
function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions, messenger, }) {
|
|
43
38
|
let networkClient;
|
|
44
|
-
const ensureNetworkClientCreated = () => {
|
|
45
|
-
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
46
|
-
configuration: networkClientConfiguration,
|
|
47
|
-
getBlockTrackerOptions,
|
|
48
|
-
getRpcServiceOptions,
|
|
49
|
-
messenger,
|
|
50
|
-
isRpcFailoverEnabled,
|
|
51
|
-
}));
|
|
52
|
-
// We don't need to test this; this itself is to catch an edge case where
|
|
53
|
-
// `createNetworkClient` is mocked improperly in a test
|
|
54
|
-
/* istanbul ignore next */
|
|
55
|
-
if (networkClient === undefined) {
|
|
56
|
-
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
57
|
-
}
|
|
58
|
-
return networkClient;
|
|
59
|
-
};
|
|
60
39
|
const providerProxy = new Proxy(UNINITIALIZED_TARGET, {
|
|
61
40
|
// TODO: Replace `any` with type
|
|
62
41
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -64,7 +43,15 @@ function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServ
|
|
|
64
43
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
65
44
|
return networkClient?.provider;
|
|
66
45
|
}
|
|
67
|
-
|
|
46
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
47
|
+
configuration: networkClientConfiguration,
|
|
48
|
+
getRpcServiceOptions,
|
|
49
|
+
messenger,
|
|
50
|
+
}));
|
|
51
|
+
if (networkClient === undefined) {
|
|
52
|
+
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
53
|
+
}
|
|
54
|
+
const { provider } = networkClient;
|
|
68
55
|
if (propertyName in provider) {
|
|
69
56
|
// Typecast: We know that `[propertyName]` is a propertyName on
|
|
70
57
|
// `provider`.
|
|
@@ -92,7 +79,12 @@ function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServ
|
|
|
92
79
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
93
80
|
return true;
|
|
94
81
|
}
|
|
95
|
-
|
|
82
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
83
|
+
configuration: networkClientConfiguration,
|
|
84
|
+
getRpcServiceOptions,
|
|
85
|
+
messenger,
|
|
86
|
+
}));
|
|
87
|
+
const { provider } = networkClient;
|
|
96
88
|
return propertyName in provider;
|
|
97
89
|
},
|
|
98
90
|
});
|
|
@@ -103,7 +95,15 @@ function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServ
|
|
|
103
95
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
104
96
|
return networkClient?.blockTracker;
|
|
105
97
|
}
|
|
106
|
-
|
|
98
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
99
|
+
configuration: networkClientConfiguration,
|
|
100
|
+
getRpcServiceOptions,
|
|
101
|
+
messenger,
|
|
102
|
+
}));
|
|
103
|
+
if (networkClient === undefined) {
|
|
104
|
+
throw new Error("It looks like createNetworkClient returned undefined. Perhaps it's mocked?");
|
|
105
|
+
}
|
|
106
|
+
const { blockTracker } = networkClient;
|
|
107
107
|
if (propertyName in blockTracker) {
|
|
108
108
|
// Typecast: We know that `[propertyName]` is a propertyName on
|
|
109
109
|
// `provider`.
|
|
@@ -131,38 +131,23 @@ function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServ
|
|
|
131
131
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
132
132
|
return true;
|
|
133
133
|
}
|
|
134
|
-
|
|
134
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
135
|
+
configuration: networkClientConfiguration,
|
|
136
|
+
getRpcServiceOptions,
|
|
137
|
+
messenger,
|
|
138
|
+
}));
|
|
139
|
+
const { blockTracker } = networkClient;
|
|
135
140
|
return propertyName in blockTracker;
|
|
136
141
|
},
|
|
137
142
|
});
|
|
138
143
|
const destroy = () => {
|
|
139
144
|
networkClient?.destroy();
|
|
140
145
|
};
|
|
141
|
-
const withRpcFailoverEnabled = () => {
|
|
142
|
-
return createAutoManagedNetworkClient({
|
|
143
|
-
networkClientConfiguration,
|
|
144
|
-
getRpcServiceOptions,
|
|
145
|
-
getBlockTrackerOptions,
|
|
146
|
-
messenger,
|
|
147
|
-
isRpcFailoverEnabled: true,
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
const withRpcFailoverDisabled = () => {
|
|
151
|
-
return createAutoManagedNetworkClient({
|
|
152
|
-
networkClientConfiguration,
|
|
153
|
-
getRpcServiceOptions,
|
|
154
|
-
getBlockTrackerOptions,
|
|
155
|
-
messenger,
|
|
156
|
-
isRpcFailoverEnabled: false,
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
146
|
return {
|
|
160
147
|
configuration: networkClientConfiguration,
|
|
161
148
|
provider: providerProxy,
|
|
162
149
|
blockTracker: blockTrackerProxy,
|
|
163
150
|
destroy,
|
|
164
|
-
withRpcFailoverEnabled,
|
|
165
|
-
withRpcFailoverDisabled,
|
|
166
151
|
};
|
|
167
152
|
}
|
|
168
153
|
exports.createAutoManagedNetworkClient = createAutoManagedNetworkClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AAGA,uEAA8D;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAA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AO,KAAK,CAAC;aACd;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB;YACzC,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,IAAI,CAAC;aACb;YACD,MAAM,EAAE,YAAY,EAAE,GAAG,0BAA0B,EAAE,CAAC;YACtD,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,MAAM,sBAAsB,GAAG,GAAG,EAAE;QAClC,OAAO,8BAA8B,CAAC;YACpC,0BAA0B;YAC1B,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB,EAAE,IAAI;SAC3B,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,uBAAuB,GAAG,GAAG,EAAE;QACnC,OAAO,8BAA8B,CAAC;YACpC,0BAA0B;YAC1B,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB,EAAE,KAAK;SAC5B,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;QACP,sBAAsB;QACtB,uBAAuB;KACxB,CAAC;AACJ,CAAC;AArKD,wEAqKC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\n\nimport type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type { NetworkControllerMessenger } from './NetworkController';\nimport type { RpcServiceOptions } from './rpc-service/rpc-service';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n withRpcFailoverEnabled: () => AutoManagedNetworkClient<Configuration>;\n withRpcFailoverDisabled: () => AutoManagedNetworkClient<Configuration>;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.getRpcServiceOptions - Factory for constructing RPC service\n * options. See {@link NetworkControllerOptions.getRpcServiceOptions}.\n * @param args.getBlockTrackerOptions - Factory for constructing block tracker\n * options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.\n * @param args.messenger - The network controller messenger.\n * @param args.isRpcFailoverEnabled - Whether or not requests sent to the\n * primary RPC endpoint for this network should be automatically diverted to\n * provided failover endpoints if the primary is unavailable.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n}: {\n networkClientConfiguration: Configuration;\n getRpcServiceOptions: (\n rpcEndpointUrl: string,\n ) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;\n getBlockTrackerOptions: (\n rpcEndpointUrl: string,\n ) => Omit<PollingBlockTrackerOptions, 'provider'>;\n messenger: NetworkControllerMessenger;\n isRpcFailoverEnabled: boolean;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getBlockTrackerOptions,\n getRpcServiceOptions,\n messenger,\n isRpcFailoverEnabled,\n });\n\n // We don't need to test this; this itself is to catch an edge case where\n // `createNetworkClient` is mocked improperly in a test\n /* istanbul ignore next */\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n\n return networkClient;\n };\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n const { provider } = ensureNetworkClientCreated();\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n const { provider } = ensureNetworkClientCreated();\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n const { blockTracker } = ensureNetworkClientCreated();\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n const { blockTracker } = ensureNetworkClientCreated();\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n const withRpcFailoverEnabled = () => {\n return createAutoManagedNetworkClient({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled: true,\n });\n };\n\n const withRpcFailoverDisabled = () => {\n return createAutoManagedNetworkClient({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled: false,\n });\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n withRpcFailoverEnabled,\n withRpcFailoverDisabled,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AACA,uEAA8D;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,SAAS,GAOV;IACC,IAAI,aAAwC,CAAC;IAE7C,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,oBAAoB,EAAE;QACpD,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB,EAAE,QAAiB;YAC5D,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,aAAa,EAAE,QAAQ,CAAC;aAChC;YAED,aAAa,KAAb,aAAa,GAAK,IAAA,2CAAmB,EAAC;gBACpC,aAAa,EAAE,0BAA0B;gBACzC,oBAAoB;gBACpB,SAAS;aACV,CAAC,EAAC;YACH,IAAI,aAAa,KAAK,SAAS,EAAE;gBAC/B,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;aACH;YACD,MAAM,EAAE,QAAQ,EAAE,GAAG,aAAa,CAAC;YAEnC,IAAI,Y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AAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC;AAlJD,wEAkJC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type { NetworkControllerMessenger } from './NetworkController';\nimport type { RpcServiceOptions } from './rpc-service/rpc-service';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.getRpcServiceOptions - Factory for constructing RPC service\n * options. See {@link NetworkControllerOptions.getRpcServiceOptions}.\n * @param args.messenger - The network controller messenger.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n}: {\n networkClientConfiguration: Configuration;\n getRpcServiceOptions: (\n rpcEndpointUrl: string,\n ) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;\n messenger: NetworkControllerMessenger;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { PollingBlockTrackerOptions } from "@metamask/eth-block-tracker";
|
|
2
1
|
import type { NetworkControllerMessenger } from "./NetworkController.cjs";
|
|
3
2
|
import type { RpcServiceOptions } from "./rpc-service/rpc-service.cjs";
|
|
4
3
|
import type { BlockTracker, NetworkClientConfiguration, Provider } from "./types.cjs";
|
|
@@ -30,8 +29,6 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
30
29
|
provider: ProxyWithAccessibleTarget<Provider>;
|
|
31
30
|
blockTracker: ProxyWithAccessibleTarget<BlockTracker>;
|
|
32
31
|
destroy: () => void;
|
|
33
|
-
withRpcFailoverEnabled: () => AutoManagedNetworkClient<Configuration>;
|
|
34
|
-
withRpcFailoverDisabled: () => AutoManagedNetworkClient<Configuration>;
|
|
35
32
|
};
|
|
36
33
|
/**
|
|
37
34
|
* This function creates two proxies, one that wraps a provider and another that
|
|
@@ -48,20 +45,13 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
48
45
|
* used to instantiate the network client when it is needed.
|
|
49
46
|
* @param args.getRpcServiceOptions - Factory for constructing RPC service
|
|
50
47
|
* options. See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
51
|
-
* @param args.getBlockTrackerOptions - Factory for constructing block tracker
|
|
52
|
-
* options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.
|
|
53
48
|
* @param args.messenger - The network controller messenger.
|
|
54
|
-
* @param args.isRpcFailoverEnabled - Whether or not requests sent to the
|
|
55
|
-
* primary RPC endpoint for this network should be automatically diverted to
|
|
56
|
-
* provided failover endpoints if the primary is unavailable.
|
|
57
49
|
* @returns The auto-managed network client.
|
|
58
50
|
*/
|
|
59
|
-
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions,
|
|
51
|
+
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions, messenger, }: {
|
|
60
52
|
networkClientConfiguration: Configuration;
|
|
61
53
|
getRpcServiceOptions: (rpcEndpointUrl: string) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;
|
|
62
|
-
getBlockTrackerOptions: (rpcEndpointUrl: string) => Omit<PollingBlockTrackerOptions, 'provider'>;
|
|
63
54
|
messenger: NetworkControllerMessenger;
|
|
64
|
-
isRpcFailoverEnabled: boolean;
|
|
65
55
|
}): AutoManagedNetworkClient<Configuration>;
|
|
66
56
|
export {};
|
|
67
57
|
//# sourceMappingURL=create-auto-managed-network-client.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gCAA4B;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,sCAAkC;AACnE,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,SAAS,GACV,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,SAAS,EAAE,0BAA0B,CAAC;CACvC,GAAG,wBAAwB,CAAC,aAAa,CAAC,CAsI1C"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { PollingBlockTrackerOptions } from "@metamask/eth-block-tracker";
|
|
2
1
|
import type { NetworkControllerMessenger } from "./NetworkController.mjs";
|
|
3
2
|
import type { RpcServiceOptions } from "./rpc-service/rpc-service.mjs";
|
|
4
3
|
import type { BlockTracker, NetworkClientConfiguration, Provider } from "./types.mjs";
|
|
@@ -30,8 +29,6 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
30
29
|
provider: ProxyWithAccessibleTarget<Provider>;
|
|
31
30
|
blockTracker: ProxyWithAccessibleTarget<BlockTracker>;
|
|
32
31
|
destroy: () => void;
|
|
33
|
-
withRpcFailoverEnabled: () => AutoManagedNetworkClient<Configuration>;
|
|
34
|
-
withRpcFailoverDisabled: () => AutoManagedNetworkClient<Configuration>;
|
|
35
32
|
};
|
|
36
33
|
/**
|
|
37
34
|
* This function creates two proxies, one that wraps a provider and another that
|
|
@@ -48,20 +45,13 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
48
45
|
* used to instantiate the network client when it is needed.
|
|
49
46
|
* @param args.getRpcServiceOptions - Factory for constructing RPC service
|
|
50
47
|
* options. See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
51
|
-
* @param args.getBlockTrackerOptions - Factory for constructing block tracker
|
|
52
|
-
* options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.
|
|
53
48
|
* @param args.messenger - The network controller messenger.
|
|
54
|
-
* @param args.isRpcFailoverEnabled - Whether or not requests sent to the
|
|
55
|
-
* primary RPC endpoint for this network should be automatically diverted to
|
|
56
|
-
* provided failover endpoints if the primary is unavailable.
|
|
57
49
|
* @returns The auto-managed network client.
|
|
58
50
|
*/
|
|
59
|
-
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions,
|
|
51
|
+
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions, messenger, }: {
|
|
60
52
|
networkClientConfiguration: Configuration;
|
|
61
53
|
getRpcServiceOptions: (rpcEndpointUrl: string) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;
|
|
62
|
-
getBlockTrackerOptions: (rpcEndpointUrl: string) => Omit<PollingBlockTrackerOptions, 'provider'>;
|
|
63
54
|
messenger: NetworkControllerMessenger;
|
|
64
|
-
isRpcFailoverEnabled: boolean;
|
|
65
55
|
}): AutoManagedNetworkClient<Configuration>;
|
|
66
56
|
export {};
|
|
67
57
|
//# sourceMappingURL=create-auto-managed-network-client.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gCAA4B;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,sCAAkC;AACnE,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,SAAS,GACV,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,SAAS,EAAE,0BAA0B,CAAC;CACvC,GAAG,wBAAwB,CAAC,aAAa,CAAC,CAsI1C"}
|
|
@@ -28,32 +28,11 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
|
|
|
28
28
|
* used to instantiate the network client when it is needed.
|
|
29
29
|
* @param args.getRpcServiceOptions - Factory for constructing RPC service
|
|
30
30
|
* options. See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
31
|
-
* @param args.getBlockTrackerOptions - Factory for constructing block tracker
|
|
32
|
-
* options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.
|
|
33
31
|
* @param args.messenger - The network controller messenger.
|
|
34
|
-
* @param args.isRpcFailoverEnabled - Whether or not requests sent to the
|
|
35
|
-
* primary RPC endpoint for this network should be automatically diverted to
|
|
36
|
-
* provided failover endpoints if the primary is unavailable.
|
|
37
32
|
* @returns The auto-managed network client.
|
|
38
33
|
*/
|
|
39
|
-
export function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions,
|
|
34
|
+
export function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions, messenger, }) {
|
|
40
35
|
let networkClient;
|
|
41
|
-
const ensureNetworkClientCreated = () => {
|
|
42
|
-
networkClient ?? (networkClient = createNetworkClient({
|
|
43
|
-
configuration: networkClientConfiguration,
|
|
44
|
-
getBlockTrackerOptions,
|
|
45
|
-
getRpcServiceOptions,
|
|
46
|
-
messenger,
|
|
47
|
-
isRpcFailoverEnabled,
|
|
48
|
-
}));
|
|
49
|
-
// We don't need to test this; this itself is to catch an edge case where
|
|
50
|
-
// `createNetworkClient` is mocked improperly in a test
|
|
51
|
-
/* istanbul ignore next */
|
|
52
|
-
if (networkClient === undefined) {
|
|
53
|
-
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
54
|
-
}
|
|
55
|
-
return networkClient;
|
|
56
|
-
};
|
|
57
36
|
const providerProxy = new Proxy(UNINITIALIZED_TARGET, {
|
|
58
37
|
// TODO: Replace `any` with type
|
|
59
38
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -61,7 +40,15 @@ export function createAutoManagedNetworkClient({ networkClientConfiguration, get
|
|
|
61
40
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
62
41
|
return networkClient?.provider;
|
|
63
42
|
}
|
|
64
|
-
|
|
43
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
44
|
+
configuration: networkClientConfiguration,
|
|
45
|
+
getRpcServiceOptions,
|
|
46
|
+
messenger,
|
|
47
|
+
}));
|
|
48
|
+
if (networkClient === undefined) {
|
|
49
|
+
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
50
|
+
}
|
|
51
|
+
const { provider } = networkClient;
|
|
65
52
|
if (propertyName in provider) {
|
|
66
53
|
// Typecast: We know that `[propertyName]` is a propertyName on
|
|
67
54
|
// `provider`.
|
|
@@ -89,7 +76,12 @@ export function createAutoManagedNetworkClient({ networkClientConfiguration, get
|
|
|
89
76
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
90
77
|
return true;
|
|
91
78
|
}
|
|
92
|
-
|
|
79
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
80
|
+
configuration: networkClientConfiguration,
|
|
81
|
+
getRpcServiceOptions,
|
|
82
|
+
messenger,
|
|
83
|
+
}));
|
|
84
|
+
const { provider } = networkClient;
|
|
93
85
|
return propertyName in provider;
|
|
94
86
|
},
|
|
95
87
|
});
|
|
@@ -100,7 +92,15 @@ export function createAutoManagedNetworkClient({ networkClientConfiguration, get
|
|
|
100
92
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
101
93
|
return networkClient?.blockTracker;
|
|
102
94
|
}
|
|
103
|
-
|
|
95
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
96
|
+
configuration: networkClientConfiguration,
|
|
97
|
+
getRpcServiceOptions,
|
|
98
|
+
messenger,
|
|
99
|
+
}));
|
|
100
|
+
if (networkClient === undefined) {
|
|
101
|
+
throw new Error("It looks like createNetworkClient returned undefined. Perhaps it's mocked?");
|
|
102
|
+
}
|
|
103
|
+
const { blockTracker } = networkClient;
|
|
104
104
|
if (propertyName in blockTracker) {
|
|
105
105
|
// Typecast: We know that `[propertyName]` is a propertyName on
|
|
106
106
|
// `provider`.
|
|
@@ -128,38 +128,23 @@ export function createAutoManagedNetworkClient({ networkClientConfiguration, get
|
|
|
128
128
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
129
129
|
return true;
|
|
130
130
|
}
|
|
131
|
-
|
|
131
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
132
|
+
configuration: networkClientConfiguration,
|
|
133
|
+
getRpcServiceOptions,
|
|
134
|
+
messenger,
|
|
135
|
+
}));
|
|
136
|
+
const { blockTracker } = networkClient;
|
|
132
137
|
return propertyName in blockTracker;
|
|
133
138
|
},
|
|
134
139
|
});
|
|
135
140
|
const destroy = () => {
|
|
136
141
|
networkClient?.destroy();
|
|
137
142
|
};
|
|
138
|
-
const withRpcFailoverEnabled = () => {
|
|
139
|
-
return createAutoManagedNetworkClient({
|
|
140
|
-
networkClientConfiguration,
|
|
141
|
-
getRpcServiceOptions,
|
|
142
|
-
getBlockTrackerOptions,
|
|
143
|
-
messenger,
|
|
144
|
-
isRpcFailoverEnabled: true,
|
|
145
|
-
});
|
|
146
|
-
};
|
|
147
|
-
const withRpcFailoverDisabled = () => {
|
|
148
|
-
return createAutoManagedNetworkClient({
|
|
149
|
-
networkClientConfiguration,
|
|
150
|
-
getRpcServiceOptions,
|
|
151
|
-
getBlockTrackerOptions,
|
|
152
|
-
messenger,
|
|
153
|
-
isRpcFailoverEnabled: false,
|
|
154
|
-
});
|
|
155
|
-
};
|
|
156
143
|
return {
|
|
157
144
|
configuration: networkClientConfiguration,
|
|
158
145
|
provider: providerProxy,
|
|
159
146
|
blockTracker: blockTrackerProxy,
|
|
160
147
|
destroy,
|
|
161
|
-
withRpcFailoverEnabled,
|
|
162
|
-
withRpcFailoverDisabled,
|
|
163
148
|
};
|
|
164
149
|
}
|
|
165
150
|
//# sourceMappingURL=create-auto-managed-network-client.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAsB,EACtB,SAAS,EACT,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EAAE,YAAY,EAAE,GAAG,0BAA0B,EAAE,CAAC;YACtD,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,MAAM,sBAAsB,GAAG,GAAG,EAAE;QAClC,OAAO,8BAA8B,CAAC;YACpC,0BAA0B;YAC1B,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB,EAAE,IAAI;SAC3B,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,uBAAuB,GAAG,GAAG,EAAE;QACnC,OAAO,8BAA8B,CAAC;YACpC,0BAA0B;YAC1B,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB,EAAE,KAAK;SAC5B,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;QACP,sBAAsB;QACtB,uBAAuB;KACxB,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\n\nimport type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type { NetworkControllerMessenger } from './NetworkController';\nimport type { RpcServiceOptions } from './rpc-service/rpc-service';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n withRpcFailoverEnabled: () => AutoManagedNetworkClient<Configuration>;\n withRpcFailoverDisabled: () => AutoManagedNetworkClient<Configuration>;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.getRpcServiceOptions - Factory for constructing RPC service\n * options. See {@link NetworkControllerOptions.getRpcServiceOptions}.\n * @param args.getBlockTrackerOptions - Factory for constructing block tracker\n * options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.\n * @param args.messenger - The network controller messenger.\n * @param args.isRpcFailoverEnabled - Whether or not requests sent to the\n * primary RPC endpoint for this network should be automatically diverted to\n * provided failover endpoints if the primary is unavailable.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n}: {\n networkClientConfiguration: Configuration;\n getRpcServiceOptions: (\n rpcEndpointUrl: string,\n ) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;\n getBlockTrackerOptions: (\n rpcEndpointUrl: string,\n ) => Omit<PollingBlockTrackerOptions, 'provider'>;\n messenger: NetworkControllerMessenger;\n isRpcFailoverEnabled: boolean;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getBlockTrackerOptions,\n getRpcServiceOptions,\n messenger,\n isRpcFailoverEnabled,\n });\n\n // We don't need to test this; this itself is to catch an edge case where\n // `createNetworkClient` is mocked improperly in a test\n /* istanbul ignore next */\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n\n return networkClient;\n };\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n const { provider } = ensureNetworkClientCreated();\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n const { provider } = ensureNetworkClientCreated();\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n const { blockTracker } = ensureNetworkClientCreated();\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n const { blockTracker } = ensureNetworkClientCreated();\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n const withRpcFailoverEnabled = () => {\n return createAutoManagedNetworkClient({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled: true,\n });\n };\n\n const withRpcFailoverDisabled = () => {\n return createAutoManagedNetworkClient({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled: false,\n });\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n withRpcFailoverEnabled,\n withRpcFailoverDisabled,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,SAAS,GAOV;IACC,IAAI,aAAwC,CAAC;IAE7C,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,oBAAoB,EAAE;QACpD,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB,EAAE,QAAiB;YAC5D,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,aAAa,EAAE,QAAQ,CAAC;aAChC;YAED,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC;gBACpC,aAAa,EAAE,0BAA0B;gBACzC,oBAAoB;gBACpB,SAAS;aACV,CAAC,EAAC;YACH,IAAI,aAAa,KAAK,SAAS,EAAE;gBAC/B,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;aACH;YACD,MAAM,EAAE,QAAQ,EAAE,GAAG,aAAa,CAAC;YAEnC,IAAI,YAAY,IAAI,QAAQ,EAAE;gBAC5B,+DAA+D;gBAC/D,cAAc;gBACd,MAAM,KAAK,GAAG,QAAQ,CAAC,YAAqC,CAAC,CAAC;gBAC9D,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;oBAC/B,kEAAkE;oBAClE,2DAA2D;oBAC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AChC,+DAA+D;gBAC/D,cAAc;gBACd,MAAM,KAAK,GAAG,YAAY,CAAC,YAAyC,CAAC,CAAC;gBACtE,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;oBAC/B,kEAAkE;oBAClE,2DAA2D;oBAC3D,gDAAgD;oBAChD,gCAAgC;oBAChC,8DAA8D;oBAC9D,OAAO,UAAyB,GAAG,IAAW;wBAC5C,wDAAwD;wBACxD,+DAA+D;wBAC/D,kCAAkC;wBAClC,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACpE,CAAC,CAAC;iBACH;gBACD,OAAO,KAAK,CAAC;aACd;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB;YACzC,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,IAAI,CAAC;aACb;YACD,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC;gBACpC,aAAa,EAAE,0BAA0B;gBACzC,oBAAoB;gBACpB,SAAS;aACV,CAAC,EAAC;YACH,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;YACvC,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type { NetworkControllerMessenger } from './NetworkController';\nimport type { RpcServiceOptions } from './rpc-service/rpc-service';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.getRpcServiceOptions - Factory for constructing RPC service\n * options. See {@link NetworkControllerOptions.getRpcServiceOptions}.\n * @param args.messenger - The network controller messenger.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n}: {\n networkClientConfiguration: Configuration;\n getRpcServiceOptions: (\n rpcEndpointUrl: string,\n ) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;\n messenger: NetworkControllerMessenger;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n messenger,\n });\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
@@ -17,28 +17,23 @@ const SECOND = 1000;
|
|
|
17
17
|
* @param args.configuration - The network configuration.
|
|
18
18
|
* @param args.getRpcServiceOptions - Factory for constructing RPC service
|
|
19
19
|
* options. See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
20
|
-
* @param args.getBlockTrackerOptions - Factory for constructing block tracker
|
|
21
|
-
* options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.
|
|
22
20
|
* @param args.messenger - The network controller messenger.
|
|
23
|
-
* @
|
|
24
|
-
* primary RPC endpoint for this network should be automatically diverted to
|
|
25
|
-
* provided failover endpoints if the primary is unavailable. This effectively
|
|
26
|
-
* causes the `failoverRpcUrls` property of the network client configuration
|
|
27
|
-
* to be honored or ignored.
|
|
21
|
+
* See {@link NetworkControllerOptions.getRpcServiceOptions}.
|
|
28
22
|
* @returns The network client.
|
|
29
23
|
*/
|
|
30
|
-
function createNetworkClient({ configuration, getRpcServiceOptions,
|
|
24
|
+
function createNetworkClient({ configuration, getRpcServiceOptions, messenger, }) {
|
|
31
25
|
const primaryEndpointUrl = configuration.type === types_1.NetworkClientType.Infura
|
|
32
26
|
? `https://${configuration.network}.infura.io/v3/${configuration.infuraProjectId}`
|
|
33
27
|
: configuration.rpcUrl;
|
|
34
|
-
const availableEndpointUrls =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
const availableEndpointUrls = [
|
|
29
|
+
primaryEndpointUrl,
|
|
30
|
+
...(configuration.failoverRpcUrls ?? []),
|
|
31
|
+
];
|
|
32
|
+
const rpcService = new rpc_service_chain_1.RpcServiceChain(availableEndpointUrls.map((endpointUrl) => ({
|
|
38
33
|
...getRpcServiceOptions(endpointUrl),
|
|
39
34
|
endpointUrl,
|
|
40
35
|
})));
|
|
41
|
-
|
|
36
|
+
rpcService.onBreak(({ endpointUrl, failoverEndpointUrl, ...rest }) => {
|
|
42
37
|
let error;
|
|
43
38
|
if ('error' in rest) {
|
|
44
39
|
error = rest.error;
|
|
@@ -53,13 +48,13 @@ function createNetworkClient({ configuration, getRpcServiceOptions, getBlockTrac
|
|
|
53
48
|
error,
|
|
54
49
|
});
|
|
55
50
|
});
|
|
56
|
-
|
|
51
|
+
rpcService.onDegraded(({ endpointUrl }) => {
|
|
57
52
|
messenger.publish('NetworkController:rpcEndpointDegraded', {
|
|
58
53
|
chainId: configuration.chainId,
|
|
59
54
|
endpointUrl,
|
|
60
55
|
});
|
|
61
56
|
});
|
|
62
|
-
|
|
57
|
+
rpcService.onRetry(({ endpointUrl, attempt }) => {
|
|
63
58
|
messenger.publish('NetworkController:rpcEndpointRequestRetried', {
|
|
64
59
|
endpointUrl,
|
|
65
60
|
attempt,
|
|
@@ -67,17 +62,18 @@ function createNetworkClient({ configuration, getRpcServiceOptions, getBlockTrac
|
|
|
67
62
|
});
|
|
68
63
|
const rpcApiMiddleware = configuration.type === types_1.NetworkClientType.Infura
|
|
69
64
|
? (0, eth_json_rpc_infura_1.createInfuraMiddleware)({
|
|
70
|
-
rpcService
|
|
65
|
+
rpcService,
|
|
71
66
|
options: {
|
|
72
67
|
source: 'metamask',
|
|
73
68
|
},
|
|
74
69
|
})
|
|
75
|
-
: (0, eth_json_rpc_middleware_1.createFetchMiddleware)({ rpcService
|
|
70
|
+
: (0, eth_json_rpc_middleware_1.createFetchMiddleware)({ rpcService });
|
|
76
71
|
const rpcProvider = (0, eth_json_rpc_provider_1.providerFromMiddleware)(rpcApiMiddleware);
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
const blockTrackerOpts = process.env.IN_TEST && configuration.type === types_1.NetworkClientType.Custom
|
|
73
|
+
? { pollingInterval: SECOND }
|
|
74
|
+
: {};
|
|
75
|
+
const blockTracker = new eth_block_tracker_1.PollingBlockTracker({
|
|
76
|
+
...blockTrackerOpts,
|
|
81
77
|
provider: rpcProvider,
|
|
82
78
|
});
|
|
83
79
|
const networkMiddleware = configuration.type === types_1.NetworkClientType.Infura
|
|
@@ -103,28 +99,6 @@ function createNetworkClient({ configuration, getRpcServiceOptions, getBlockTrac
|
|
|
103
99
|
return { configuration, provider, blockTracker, destroy };
|
|
104
100
|
}
|
|
105
101
|
exports.createNetworkClient = createNetworkClient;
|
|
106
|
-
/**
|
|
107
|
-
* Create the block tracker for the network.
|
|
108
|
-
*
|
|
109
|
-
* @param args - The arguments.
|
|
110
|
-
* @param args.networkClientType - The type of the network client ("infura" or
|
|
111
|
-
* "custom").
|
|
112
|
-
* @param args.endpointUrl - The URL of the endpoint.
|
|
113
|
-
* @param args.getOptions - Factory for the block tracker options.
|
|
114
|
-
* @param args.provider - The EIP-1193 provider for the network's JSON-RPC
|
|
115
|
-
* middleware stack.
|
|
116
|
-
* @returns The created block tracker.
|
|
117
|
-
*/
|
|
118
|
-
function createBlockTracker({ networkClientType, endpointUrl, getOptions, provider, }) {
|
|
119
|
-
const testOptions = process.env.IN_TEST && networkClientType === types_1.NetworkClientType.Custom
|
|
120
|
-
? { pollingInterval: SECOND }
|
|
121
|
-
: {};
|
|
122
|
-
return new eth_block_tracker_1.PollingBlockTracker({
|
|
123
|
-
...testOptions,
|
|
124
|
-
...getOptions(endpointUrl),
|
|
125
|
-
provider,
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
102
|
/**
|
|
129
103
|
* Create middleware for infura.
|
|
130
104
|
*
|