@metamask-previews/network-controller 26.0.0-preview-e3eb8eca → 26.0.0-preview-58cfb208

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +0 -19
  2. package/dist/NetworkController.cjs +10 -37
  3. package/dist/NetworkController.cjs.map +1 -1
  4. package/dist/NetworkController.d.cts +11 -146
  5. package/dist/NetworkController.d.cts.map +1 -1
  6. package/dist/NetworkController.d.mts +11 -146
  7. package/dist/NetworkController.d.mts.map +1 -1
  8. package/dist/NetworkController.mjs +10 -37
  9. package/dist/NetworkController.mjs.map +1 -1
  10. package/dist/constants.cjs +10 -18
  11. package/dist/constants.cjs.map +1 -1
  12. package/dist/constants.d.cts +10 -18
  13. package/dist/constants.d.cts.map +1 -1
  14. package/dist/constants.d.mts +10 -18
  15. package/dist/constants.d.mts.map +1 -1
  16. package/dist/constants.mjs +10 -18
  17. package/dist/constants.mjs.map +1 -1
  18. package/dist/create-auto-managed-network-client.cjs +1 -4
  19. package/dist/create-auto-managed-network-client.cjs.map +1 -1
  20. package/dist/create-auto-managed-network-client.d.cts +2 -5
  21. package/dist/create-auto-managed-network-client.d.cts.map +1 -1
  22. package/dist/create-auto-managed-network-client.d.mts +2 -5
  23. package/dist/create-auto-managed-network-client.d.mts.map +1 -1
  24. package/dist/create-auto-managed-network-client.mjs +1 -4
  25. package/dist/create-auto-managed-network-client.mjs.map +1 -1
  26. package/dist/create-network-client.cjs +42 -115
  27. package/dist/create-network-client.cjs.map +1 -1
  28. package/dist/create-network-client.d.cts +2 -5
  29. package/dist/create-network-client.d.cts.map +1 -1
  30. package/dist/create-network-client.d.mts +2 -5
  31. package/dist/create-network-client.d.mts.map +1 -1
  32. package/dist/create-network-client.mjs +42 -115
  33. package/dist/create-network-client.mjs.map +1 -1
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.cts +1 -1
  36. package/dist/index.d.cts.map +1 -1
  37. package/dist/index.d.mts +1 -1
  38. package/dist/index.d.mts.map +1 -1
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/rpc-service/rpc-service-chain.cjs +39 -255
  41. package/dist/rpc-service/rpc-service-chain.cjs.map +1 -1
  42. package/dist/rpc-service/rpc-service-chain.d.cts +25 -117
  43. package/dist/rpc-service/rpc-service-chain.d.cts.map +1 -1
  44. package/dist/rpc-service/rpc-service-chain.d.mts +25 -117
  45. package/dist/rpc-service/rpc-service-chain.d.mts.map +1 -1
  46. package/dist/rpc-service/rpc-service-chain.mjs +39 -255
  47. package/dist/rpc-service/rpc-service-chain.mjs.map +1 -1
  48. package/dist/rpc-service/rpc-service-requestable.cjs.map +1 -1
  49. package/dist/rpc-service/rpc-service-requestable.d.cts +5 -15
  50. package/dist/rpc-service/rpc-service-requestable.d.cts.map +1 -1
  51. package/dist/rpc-service/rpc-service-requestable.d.mts +5 -15
  52. package/dist/rpc-service/rpc-service-requestable.d.mts.map +1 -1
  53. package/dist/rpc-service/rpc-service-requestable.mjs.map +1 -1
  54. package/dist/rpc-service/rpc-service.cjs +33 -63
  55. package/dist/rpc-service/rpc-service.cjs.map +1 -1
  56. package/dist/rpc-service/rpc-service.d.cts +24 -35
  57. package/dist/rpc-service/rpc-service.d.cts.map +1 -1
  58. package/dist/rpc-service/rpc-service.d.mts +24 -35
  59. package/dist/rpc-service/rpc-service.d.mts.map +1 -1
  60. package/dist/rpc-service/rpc-service.mjs +34 -64
  61. package/dist/rpc-service/rpc-service.mjs.map +1 -1
  62. package/dist/rpc-service/shared.cjs.map +1 -1
  63. package/dist/rpc-service/shared.d.cts +3 -30
  64. package/dist/rpc-service/shared.d.cts.map +1 -1
  65. package/dist/rpc-service/shared.d.mts +3 -30
  66. package/dist/rpc-service/shared.d.mts.map +1 -1
  67. package/dist/rpc-service/shared.mjs.map +1 -1
  68. package/package.json +1 -2
@@ -2,37 +2,29 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEPRECATED_NETWORKS = exports.INFURA_BLOCKED_KEY = exports.NetworkStatus = void 0;
4
4
  /**
5
- * Represents the availability status of an RPC endpoint. (Regrettably, the
6
- * name of this type is a misnomer.)
7
- *
8
- * The availability status is set both automatically (as requests are made) and
9
- * manually (when `lookupNetwork` is called).
5
+ * Represents the availability state of the currently selected network.
10
6
  */
11
7
  var NetworkStatus;
12
8
  (function (NetworkStatus) {
13
9
  /**
14
- * Either the availability status of the RPC endpoint has not been determined,
15
- * or request that `lookupNetwork` performed returned an unknown error.
10
+ * The network may or may not be able to receive requests, but either no
11
+ * attempt has been made to determine this, or an attempt was made but was
12
+ * unsuccessful.
16
13
  */
17
14
  NetworkStatus["Unknown"] = "unknown";
18
15
  /**
19
- * The RPC endpoint is consistently returning successful (2xx) responses.
16
+ * The network is able to receive and respond to requests.
20
17
  */
21
18
  NetworkStatus["Available"] = "available";
22
19
  /**
23
- * Either the last request to the RPC endpoint was either too slow, or the
24
- * endpoint is consistently returning errors and the number of retries has
25
- * been reached.
26
- */
27
- NetworkStatus["Degraded"] = "degraded";
28
- /**
29
- * The RPC endpoint is consistently returning enough 5xx errors that requests
30
- * have been paused.
20
+ * The network was unable to receive and respond to requests for unknown
21
+ * reasons.
31
22
  */
32
23
  NetworkStatus["Unavailable"] = "unavailable";
33
24
  /**
34
- * The RPC endpoint is inaccessible for the user based on their location. This
35
- * status only applies to Infura networks.
25
+ * The network is not only unavailable, but is also inaccessible for the user
26
+ * specifically based on their location. This state only applies to Infura
27
+ * networks.
36
28
  */
37
29
  NetworkStatus["Blocked"] = "blocked";
38
30
  })(NetworkStatus || (exports.NetworkStatus = NetworkStatus = {}));
@@ -1 +1 @@
1
- {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,IAAY,aA0BX;AA1BD,WAAY,aAAa;IACvB;;;OAGG;IACH,oCAAmB,CAAA;IACnB;;OAEG;IACH,wCAAuB,CAAA;IACvB;;;;OAIG;IACH,sCAAqB,CAAA;IACrB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;OAGG;IACH,oCAAmB,CAAA;AACrB,CAAC,EA1BW,aAAa,6BAAb,aAAa,QA0BxB;AAEY,QAAA,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;;;GAIG;AACU,QAAA,mBAAmB,GAAG,IAAI,GAAG,CAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC","sourcesContent":["/**\n * Represents the availability status of an RPC endpoint. (Regrettably, the\n * name of this type is a misnomer.)\n *\n * The availability status is set both automatically (as requests are made) and\n * manually (when `lookupNetwork` is called).\n */\nexport enum NetworkStatus {\n /**\n * Either the availability status of the RPC endpoint has not been determined,\n * or request that `lookupNetwork` performed returned an unknown error.\n */\n Unknown = 'unknown',\n /**\n * The RPC endpoint is consistently returning successful (2xx) responses.\n */\n Available = 'available',\n /**\n * Either the last request to the RPC endpoint was either too slow, or the\n * endpoint is consistently returning errors and the number of retries has\n * been reached.\n */\n Degraded = 'degraded',\n /**\n * The RPC endpoint is consistently returning enough 5xx errors that requests\n * have been paused.\n */\n Unavailable = 'unavailable',\n /**\n * The RPC endpoint is inaccessible for the user based on their location. This\n * status only applies to Infura networks.\n */\n Blocked = 'blocked',\n}\n\nexport const INFURA_BLOCKED_KEY = 'countryBlocked';\n\n/**\n * A set of deprecated network ChainId.\n * The network controller will exclude those the networks begin as default network,\n * without the need to remove the network from constant list of controller-utils.\n */\nexport const DEPRECATED_NETWORKS = new Set<string>(['0xe704', '0x5']);\n"]}
1
+ {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,aAsBX;AAtBD,WAAY,aAAa;IACvB;;;;OAIG;IACH,oCAAmB,CAAA;IACnB;;OAEG;IACH,wCAAuB,CAAA;IACvB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;;OAIG;IACH,oCAAmB,CAAA;AACrB,CAAC,EAtBW,aAAa,6BAAb,aAAa,QAsBxB;AAEY,QAAA,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;;;GAIG;AACU,QAAA,mBAAmB,GAAG,IAAI,GAAG,CAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC","sourcesContent":["/**\n * Represents the availability state of the currently selected network.\n */\nexport enum NetworkStatus {\n /**\n * The network may or may not be able to receive requests, but either no\n * attempt has been made to determine this, or an attempt was made but was\n * unsuccessful.\n */\n Unknown = 'unknown',\n /**\n * The network is able to receive and respond to requests.\n */\n Available = 'available',\n /**\n * The network was unable to receive and respond to requests for unknown\n * reasons.\n */\n Unavailable = 'unavailable',\n /**\n * The network is not only unavailable, but is also inaccessible for the user\n * specifically based on their location. This state only applies to Infura\n * networks.\n */\n Blocked = 'blocked',\n}\n\nexport const INFURA_BLOCKED_KEY = 'countryBlocked';\n\n/**\n * A set of deprecated network ChainId.\n * The network controller will exclude those the networks begin as default network,\n * without the need to remove the network from constant list of controller-utils.\n */\nexport const DEPRECATED_NETWORKS = new Set<string>(['0xe704', '0x5']);\n"]}
@@ -1,34 +1,26 @@
1
1
  /**
2
- * Represents the availability status of an RPC endpoint. (Regrettably, the
3
- * name of this type is a misnomer.)
4
- *
5
- * The availability status is set both automatically (as requests are made) and
6
- * manually (when `lookupNetwork` is called).
2
+ * Represents the availability state of the currently selected network.
7
3
  */
8
4
  export declare enum NetworkStatus {
9
5
  /**
10
- * Either the availability status of the RPC endpoint has not been determined,
11
- * or request that `lookupNetwork` performed returned an unknown error.
6
+ * The network may or may not be able to receive requests, but either no
7
+ * attempt has been made to determine this, or an attempt was made but was
8
+ * unsuccessful.
12
9
  */
13
10
  Unknown = "unknown",
14
11
  /**
15
- * The RPC endpoint is consistently returning successful (2xx) responses.
12
+ * The network is able to receive and respond to requests.
16
13
  */
17
14
  Available = "available",
18
15
  /**
19
- * Either the last request to the RPC endpoint was either too slow, or the
20
- * endpoint is consistently returning errors and the number of retries has
21
- * been reached.
22
- */
23
- Degraded = "degraded",
24
- /**
25
- * The RPC endpoint is consistently returning enough 5xx errors that requests
26
- * have been paused.
16
+ * The network was unable to receive and respond to requests for unknown
17
+ * reasons.
27
18
  */
28
19
  Unavailable = "unavailable",
29
20
  /**
30
- * The RPC endpoint is inaccessible for the user based on their location. This
31
- * status only applies to Infura networks.
21
+ * The network is not only unavailable, but is also inaccessible for the user
22
+ * specifically based on their location. This state only applies to Infura
23
+ * networks.
32
24
  */
33
25
  Blocked = "blocked"
34
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,oBAAY,aAAa;IACvB;;;OAGG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB;;;;OAIG;IACH,QAAQ,aAAa;IACrB;;;OAGG;IACH,WAAW,gBAAgB;IAC3B;;;OAGG;IACH,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,kBAAkB,mBAAmB,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAqC,CAAC"}
1
+ {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,aAAa;IACvB;;;;OAIG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB;;;OAGG;IACH,WAAW,gBAAgB;IAC3B;;;;OAIG;IACH,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,kBAAkB,mBAAmB,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAqC,CAAC"}
@@ -1,34 +1,26 @@
1
1
  /**
2
- * Represents the availability status of an RPC endpoint. (Regrettably, the
3
- * name of this type is a misnomer.)
4
- *
5
- * The availability status is set both automatically (as requests are made) and
6
- * manually (when `lookupNetwork` is called).
2
+ * Represents the availability state of the currently selected network.
7
3
  */
8
4
  export declare enum NetworkStatus {
9
5
  /**
10
- * Either the availability status of the RPC endpoint has not been determined,
11
- * or request that `lookupNetwork` performed returned an unknown error.
6
+ * The network may or may not be able to receive requests, but either no
7
+ * attempt has been made to determine this, or an attempt was made but was
8
+ * unsuccessful.
12
9
  */
13
10
  Unknown = "unknown",
14
11
  /**
15
- * The RPC endpoint is consistently returning successful (2xx) responses.
12
+ * The network is able to receive and respond to requests.
16
13
  */
17
14
  Available = "available",
18
15
  /**
19
- * Either the last request to the RPC endpoint was either too slow, or the
20
- * endpoint is consistently returning errors and the number of retries has
21
- * been reached.
22
- */
23
- Degraded = "degraded",
24
- /**
25
- * The RPC endpoint is consistently returning enough 5xx errors that requests
26
- * have been paused.
16
+ * The network was unable to receive and respond to requests for unknown
17
+ * reasons.
27
18
  */
28
19
  Unavailable = "unavailable",
29
20
  /**
30
- * The RPC endpoint is inaccessible for the user based on their location. This
31
- * status only applies to Infura networks.
21
+ * The network is not only unavailable, but is also inaccessible for the user
22
+ * specifically based on their location. This state only applies to Infura
23
+ * networks.
32
24
  */
33
25
  Blocked = "blocked"
34
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,oBAAY,aAAa;IACvB;;;OAGG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB;;;;OAIG;IACH,QAAQ,aAAa;IACrB;;;OAGG;IACH,WAAW,gBAAgB;IAC3B;;;OAGG;IACH,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,kBAAkB,mBAAmB,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAqC,CAAC"}
1
+ {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,aAAa;IACvB;;;;OAIG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB;;;OAGG;IACH,WAAW,gBAAgB;IAC3B;;;;OAIG;IACH,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,kBAAkB,mBAAmB,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAqC,CAAC"}
@@ -1,35 +1,27 @@
1
1
  /**
2
- * Represents the availability status of an RPC endpoint. (Regrettably, the
3
- * name of this type is a misnomer.)
4
- *
5
- * The availability status is set both automatically (as requests are made) and
6
- * manually (when `lookupNetwork` is called).
2
+ * Represents the availability state of the currently selected network.
7
3
  */
8
4
  export var NetworkStatus;
9
5
  (function (NetworkStatus) {
10
6
  /**
11
- * Either the availability status of the RPC endpoint has not been determined,
12
- * or request that `lookupNetwork` performed returned an unknown error.
7
+ * The network may or may not be able to receive requests, but either no
8
+ * attempt has been made to determine this, or an attempt was made but was
9
+ * unsuccessful.
13
10
  */
14
11
  NetworkStatus["Unknown"] = "unknown";
15
12
  /**
16
- * The RPC endpoint is consistently returning successful (2xx) responses.
13
+ * The network is able to receive and respond to requests.
17
14
  */
18
15
  NetworkStatus["Available"] = "available";
19
16
  /**
20
- * Either the last request to the RPC endpoint was either too slow, or the
21
- * endpoint is consistently returning errors and the number of retries has
22
- * been reached.
23
- */
24
- NetworkStatus["Degraded"] = "degraded";
25
- /**
26
- * The RPC endpoint is consistently returning enough 5xx errors that requests
27
- * have been paused.
17
+ * The network was unable to receive and respond to requests for unknown
18
+ * reasons.
28
19
  */
29
20
  NetworkStatus["Unavailable"] = "unavailable";
30
21
  /**
31
- * The RPC endpoint is inaccessible for the user based on their location. This
32
- * status only applies to Infura networks.
22
+ * The network is not only unavailable, but is also inaccessible for the user
23
+ * specifically based on their location. This state only applies to Infura
24
+ * networks.
33
25
  */
34
26
  NetworkStatus["Blocked"] = "blocked";
35
27
  })(NetworkStatus || (NetworkStatus = {}));
@@ -1 +1 @@
1
- {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAN,IAAY,aA0BX;AA1BD,WAAY,aAAa;IACvB;;;OAGG;IACH,oCAAmB,CAAA;IACnB;;OAEG;IACH,wCAAuB,CAAA;IACvB;;;;OAIG;IACH,sCAAqB,CAAA;IACrB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;OAGG;IACH,oCAAmB,CAAA;AACrB,CAAC,EA1BW,aAAa,KAAb,aAAa,QA0BxB;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC","sourcesContent":["/**\n * Represents the availability status of an RPC endpoint. (Regrettably, the\n * name of this type is a misnomer.)\n *\n * The availability status is set both automatically (as requests are made) and\n * manually (when `lookupNetwork` is called).\n */\nexport enum NetworkStatus {\n /**\n * Either the availability status of the RPC endpoint has not been determined,\n * or request that `lookupNetwork` performed returned an unknown error.\n */\n Unknown = 'unknown',\n /**\n * The RPC endpoint is consistently returning successful (2xx) responses.\n */\n Available = 'available',\n /**\n * Either the last request to the RPC endpoint was either too slow, or the\n * endpoint is consistently returning errors and the number of retries has\n * been reached.\n */\n Degraded = 'degraded',\n /**\n * The RPC endpoint is consistently returning enough 5xx errors that requests\n * have been paused.\n */\n Unavailable = 'unavailable',\n /**\n * The RPC endpoint is inaccessible for the user based on their location. This\n * status only applies to Infura networks.\n */\n Blocked = 'blocked',\n}\n\nexport const INFURA_BLOCKED_KEY = 'countryBlocked';\n\n/**\n * A set of deprecated network ChainId.\n * The network controller will exclude those the networks begin as default network,\n * without the need to remove the network from constant list of controller-utils.\n */\nexport const DEPRECATED_NETWORKS = new Set<string>(['0xe704', '0x5']);\n"]}
1
+ {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,IAAY,aAsBX;AAtBD,WAAY,aAAa;IACvB;;;;OAIG;IACH,oCAAmB,CAAA;IACnB;;OAEG;IACH,wCAAuB,CAAA;IACvB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;;OAIG;IACH,oCAAmB,CAAA;AACrB,CAAC,EAtBW,aAAa,KAAb,aAAa,QAsBxB;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC","sourcesContent":["/**\n * Represents the availability state of the currently selected network.\n */\nexport enum NetworkStatus {\n /**\n * The network may or may not be able to receive requests, but either no\n * attempt has been made to determine this, or an attempt was made but was\n * unsuccessful.\n */\n Unknown = 'unknown',\n /**\n * The network is able to receive and respond to requests.\n */\n Available = 'available',\n /**\n * The network was unable to receive and respond to requests for unknown\n * reasons.\n */\n Unavailable = 'unavailable',\n /**\n * The network is not only unavailable, but is also inaccessible for the user\n * specifically based on their location. This state only applies to Infura\n * networks.\n */\n Blocked = 'blocked',\n}\n\nexport const INFURA_BLOCKED_KEY = 'countryBlocked';\n\n/**\n * A set of deprecated network ChainId.\n * The network controller will exclude those the networks begin as default network,\n * without the need to remove the network from constant list of controller-utils.\n */\nexport const DEPRECATED_NETWORKS = new Set<string>(['0xe704', '0x5']);\n"]}
@@ -25,8 +25,6 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
25
25
  * then cached for subsequent usages.
26
26
  *
27
27
  * @param args - The arguments.
28
- * @param args.networkClientId - The ID that will be assigned to the new network
29
- * client in the registry.
30
28
  * @param args.networkClientConfiguration - The configuration object that will be
31
29
  * used to instantiate the network client when it is needed.
32
30
  * @param args.getRpcServiceOptions - Factory for constructing RPC service
@@ -40,12 +38,11 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
40
38
  * @param args.logger - A `loglevel` logger.
41
39
  * @returns The auto-managed network client.
42
40
  */
43
- function createAutoManagedNetworkClient({ networkClientId, networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions = () => ({}), messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }) {
41
+ function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions = () => ({}), messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }) {
44
42
  let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;
45
43
  let networkClient;
46
44
  const ensureNetworkClientCreated = () => {
47
45
  networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
48
- id: networkClientId,
49
46
  configuration: networkClientConfiguration,
50
47
  getRpcServiceOptions,
51
48
  getBlockTrackerOptions,
@@ -1 +1 @@
1
- {"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AAIA,uEAA8D;AAY9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,8BAA8B,CAE5C,EACA,eAAe,EACf,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAsB,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GAaP;IACC,IAAI,oBAAoB,GAAG,yBAAyB,CAAC;IACrD,IAAI,aAAwC,CAAC;IAE7C,MAAM,0BAA0B,GAAG,GAAkB,EAAE;QACrD,aAAa,KAAb,aAAa,GAAK,IAAA,2CAAmB,EAAC;YACpC,EAAE,EAAE,eAAe;YACnB,aAAa,EAAE,0BAA0B;YACzC,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB;YACpB,MAAM;SACP,CAAC,EAAC;QAEH,IAAI,aAAa,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C;IAC3B,CAAC,CAAC;IAEF,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,oBAAoB,GAAG,IAAI,CAAC;QAC5B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,MAAM,kBAAkB,GAAG,GAAG,EAAE;QAC9B,oBAAoB,GAAG,KAAK,CAAC;QAC7B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;QACP,iBAAiB;QACjB,kBAAkB;KACnB,CAAC;AACJ,CAAC;AAjKD,wEAiKC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\nimport type { Logger } from 'loglevel';\n\nimport type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n NetworkClientId,\n NetworkControllerMessenger,\n} 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 enableRpcFailover: () => void;\n disableRpcFailover: () => 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 */\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.networkClientId - The ID that will be assigned to the new network\n * client in the registry.\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 * @param args.logger - A `loglevel` logger.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientId,\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions = () => ({}),\n messenger,\n isRpcFailoverEnabled: givenIsRpcFailoverEnabled,\n logger,\n}: {\n networkClientId: NetworkClientId;\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 logger?: Logger;\n}): AutoManagedNetworkClient<Configuration> {\n let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n id: networkClientId,\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n logger,\n });\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\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 enableRpcFailover = () => {\n isRpcFailoverEnabled = true;\n destroy();\n networkClient = undefined;\n };\n\n const disableRpcFailover = () => {\n isRpcFailoverEnabled = false;\n destroy();\n networkClient = undefined;\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n enableRpcFailover,\n disableRpcFailover,\n };\n}\n"]}
1
+ {"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AAIA,uEAA8D;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,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K,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACpE,CAAC,CAAC;gBACJ,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB;YACzC,IAAI,YAAY,KAAK,wBAAwB,EAAE,CAAC;gBAC9C,OAAO,IAAI,CAAC;YACd,CAAC;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,iBAAiB,GAAG,GAAG,EAAE;QAC7B,oBAAoB,GAAG,IAAI,CAAC;QAC5B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,MAAM,kBAAkB,GAAG,GAAG,EAAE;QAC9B,oBAAoB,GAAG,KAAK,CAAC;QAC7B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;QACP,iBAAiB;QACjB,kBAAkB;KACnB,CAAC;AACJ,CAAC;AA9JD,wEA8JC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\nimport type { Logger } from 'loglevel';\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 enableRpcFailover: () => void;\n disableRpcFailover: () => 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 */\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 * @param args.logger - A `loglevel` logger.\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: givenIsRpcFailoverEnabled,\n logger,\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 logger?: Logger;\n}): AutoManagedNetworkClient<Configuration> {\n let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n logger,\n });\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\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 enableRpcFailover = () => {\n isRpcFailoverEnabled = true;\n destroy();\n networkClient = undefined;\n };\n\n const disableRpcFailover = () => {\n isRpcFailoverEnabled = false;\n destroy();\n networkClient = undefined;\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n enableRpcFailover,\n disableRpcFailover,\n };\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import type { PollingBlockTrackerOptions } from "@metamask/eth-block-tracker";
2
2
  import type { Logger } from "loglevel";
3
- import type { NetworkClientId, NetworkControllerMessenger } from "./NetworkController.cjs";
3
+ import type { NetworkControllerMessenger } from "./NetworkController.cjs";
4
4
  import type { RpcServiceOptions } from "./rpc-service/rpc-service.cjs";
5
5
  import type { BlockTracker, NetworkClientConfiguration, Provider } from "./types.cjs";
6
6
  /**
@@ -45,8 +45,6 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
45
45
  * then cached for subsequent usages.
46
46
  *
47
47
  * @param args - The arguments.
48
- * @param args.networkClientId - The ID that will be assigned to the new network
49
- * client in the registry.
50
48
  * @param args.networkClientConfiguration - The configuration object that will be
51
49
  * used to instantiate the network client when it is needed.
52
50
  * @param args.getRpcServiceOptions - Factory for constructing RPC service
@@ -60,8 +58,7 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
60
58
  * @param args.logger - A `loglevel` logger.
61
59
  * @returns The auto-managed network client.
62
60
  */
63
- export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientId, networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions, messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }: {
64
- networkClientId: NetworkClientId;
61
+ export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions, messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }: {
65
62
  networkClientConfiguration: Configuration;
66
63
  getRpcServiceOptions: (rpcEndpointUrl: string) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;
67
64
  getBlockTrackerOptions?: (rpcEndpointUrl: string) => Omit<PollingBlockTrackerOptions, 'provider'>;
@@ -1 +1 @@
1
- {"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,oCAAoC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB;AAIvC,OAAO,KAAK,EACV,eAAe,EACf,0BAA0B,EAC3B,gCAA4B;AAC7B,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;IACpB,iBAAiB,EAAE,MAAM,IAAI,CAAC;IAC9B,kBAAkB,EAAE,MAAM,IAAI,CAAC;CAChC,CAAC;AASF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,eAAe,EACf,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAmC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GACP,EAAE;IACD,eAAe,EAAE,eAAe,CAAC;IACjC,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,sBAAsB,CAAC,EAAE,CACvB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,0BAA0B,EAAE,UAAU,CAAC,CAAC;IAClD,SAAS,EAAE,0BAA0B,CAAC;IACtC,oBAAoB,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CA2I1C"}
1
+ {"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,oCAAoC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB;AAIvC,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;IACpB,iBAAiB,EAAE,MAAM,IAAI,CAAC;IAC9B,kBAAkB,EAAE,MAAM,IAAI,CAAC;CAChC,CAAC;AASF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAmC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GACP,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,sBAAsB,CAAC,EAAE,CACvB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,0BAA0B,EAAE,UAAU,CAAC,CAAC;IAClD,SAAS,EAAE,0BAA0B,CAAC;IACtC,oBAAoB,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CA0I1C"}
@@ -1,6 +1,6 @@
1
1
  import type { PollingBlockTrackerOptions } from "@metamask/eth-block-tracker";
2
2
  import type { Logger } from "loglevel";
3
- import type { NetworkClientId, NetworkControllerMessenger } from "./NetworkController.mjs";
3
+ import type { NetworkControllerMessenger } from "./NetworkController.mjs";
4
4
  import type { RpcServiceOptions } from "./rpc-service/rpc-service.mjs";
5
5
  import type { BlockTracker, NetworkClientConfiguration, Provider } from "./types.mjs";
6
6
  /**
@@ -45,8 +45,6 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
45
45
  * then cached for subsequent usages.
46
46
  *
47
47
  * @param args - The arguments.
48
- * @param args.networkClientId - The ID that will be assigned to the new network
49
- * client in the registry.
50
48
  * @param args.networkClientConfiguration - The configuration object that will be
51
49
  * used to instantiate the network client when it is needed.
52
50
  * @param args.getRpcServiceOptions - Factory for constructing RPC service
@@ -60,8 +58,7 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
60
58
  * @param args.logger - A `loglevel` logger.
61
59
  * @returns The auto-managed network client.
62
60
  */
63
- export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientId, networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions, messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }: {
64
- networkClientId: NetworkClientId;
61
+ export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions, messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }: {
65
62
  networkClientConfiguration: Configuration;
66
63
  getRpcServiceOptions: (rpcEndpointUrl: string) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;
67
64
  getBlockTrackerOptions?: (rpcEndpointUrl: string) => Omit<PollingBlockTrackerOptions, 'provider'>;
@@ -1 +1 @@
1
- {"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,oCAAoC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB;AAIvC,OAAO,KAAK,EACV,eAAe,EACf,0BAA0B,EAC3B,gCAA4B;AAC7B,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;IACpB,iBAAiB,EAAE,MAAM,IAAI,CAAC;IAC9B,kBAAkB,EAAE,MAAM,IAAI,CAAC;CAChC,CAAC;AASF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,eAAe,EACf,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAmC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GACP,EAAE;IACD,eAAe,EAAE,eAAe,CAAC;IACjC,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,sBAAsB,CAAC,EAAE,CACvB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,0BAA0B,EAAE,UAAU,CAAC,CAAC;IAClD,SAAS,EAAE,0BAA0B,CAAC;IACtC,oBAAoB,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CA2I1C"}
1
+ {"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,oCAAoC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB;AAIvC,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;IACpB,iBAAiB,EAAE,MAAM,IAAI,CAAC;IAC9B,kBAAkB,EAAE,MAAM,IAAI,CAAC;CAChC,CAAC;AASF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAmC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GACP,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,oBAAoB,EAAE,CACpB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,aAAa,CAAC,CAAC;IAChE,sBAAsB,CAAC,EAAE,CACvB,cAAc,EAAE,MAAM,KACnB,IAAI,CAAC,0BAA0B,EAAE,UAAU,CAAC,CAAC;IAClD,SAAS,EAAE,0BAA0B,CAAC;IACtC,oBAAoB,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CA0I1C"}
@@ -22,8 +22,6 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
22
22
  * then cached for subsequent usages.
23
23
  *
24
24
  * @param args - The arguments.
25
- * @param args.networkClientId - The ID that will be assigned to the new network
26
- * client in the registry.
27
25
  * @param args.networkClientConfiguration - The configuration object that will be
28
26
  * used to instantiate the network client when it is needed.
29
27
  * @param args.getRpcServiceOptions - Factory for constructing RPC service
@@ -37,12 +35,11 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
37
35
  * @param args.logger - A `loglevel` logger.
38
36
  * @returns The auto-managed network client.
39
37
  */
40
- export function createAutoManagedNetworkClient({ networkClientId, networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions = () => ({}), messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }) {
38
+ export function createAutoManagedNetworkClient({ networkClientConfiguration, getRpcServiceOptions, getBlockTrackerOptions = () => ({}), messenger, isRpcFailoverEnabled: givenIsRpcFailoverEnabled, logger, }) {
41
39
  let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;
42
40
  let networkClient;
43
41
  const ensureNetworkClientCreated = () => {
44
42
  networkClient ?? (networkClient = createNetworkClient({
45
- id: networkClientId,
46
43
  configuration: networkClientConfiguration,
47
44
  getRpcServiceOptions,
48
45
  getBlockTrackerOptions,
@@ -1 +1 @@
1
- {"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAY9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,eAAe,EACf,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAsB,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GAaP;IACC,IAAI,oBAAoB,GAAG,yBAAyB,CAAC;IACrD,IA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,KAAK,wBAAwB,EAAE,CAAC;gBAC9C,OAAO,IAAI,CAAC;YACd,CAAC;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,iBAAiB,GAAG,GAAG,EAAE;QAC7B,oBAAoB,GAAG,IAAI,CAAC;QAC5B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,MAAM,kBAAkB,GAAG,GAAG,EAAE;QAC9B,oBAAoB,GAAG,KAAK,CAAC;QAC7B,OAAO,EAAE,CAAC;QACV,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;QACP,iBAAiB;QACjB,kBAAkB;KACnB,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\nimport type { Logger } from 'loglevel';\n\nimport type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n NetworkClientId,\n NetworkControllerMessenger,\n} 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 enableRpcFailover: () => void;\n disableRpcFailover: () => 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 */\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.networkClientId - The ID that will be assigned to the new network\n * client in the registry.\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 * @param args.logger - A `loglevel` logger.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientId,\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions = () => ({}),\n messenger,\n isRpcFailoverEnabled: givenIsRpcFailoverEnabled,\n logger,\n}: {\n networkClientId: NetworkClientId;\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 logger?: Logger;\n}): AutoManagedNetworkClient<Configuration> {\n let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n id: networkClientId,\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n logger,\n });\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\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 enableRpcFailover = () => {\n isRpcFailoverEnabled = true;\n destroy();\n networkClient = undefined;\n };\n\n const disableRpcFailover = () => {\n isRpcFailoverEnabled = false;\n destroy();\n networkClient = undefined;\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n enableRpcFailover,\n disableRpcFailover,\n };\n}\n"]}
1
+ {"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAsB,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,EAC/C,MAAM,GAYP;IACC,IAAI,oBAAoB,GAAG,yBAAyB,CAAC;IACrD,IAAI,aAAwC,CAAC;IAE7C,MAAM,0BAA0B,GAAG,GAAkB,EAAE;QACrD,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC;YACpC,aAAa,EAAE,0BAA0B;YACzC,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB;YACpB,MAAM;SACP,CAAC,EAAC;QAEH,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;QACJ,CAAC;QAED,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC;IAEF,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,CAAC;gBAC9C,OAAO,aAAa,EAAE,QAAQ,CAAC;YACjC,CAAC;YAED,MAAM,EAAE,QAAQ,EAAE,GAAG,0BAA0B,EAAE,CAAC;YAElD,IAAI,YAAY,IAAI,QAAQ,E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AAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTrackerOptions } from '@metamask/eth-block-tracker';\nimport type { Logger } from 'loglevel';\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 enableRpcFailover: () => void;\n disableRpcFailover: () => 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 */\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 * @param args.logger - A `loglevel` logger.\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: givenIsRpcFailoverEnabled,\n logger,\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 logger?: Logger;\n}): AutoManagedNetworkClient<Configuration> {\n let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n logger,\n });\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\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 enableRpcFailover = () => {\n isRpcFailoverEnabled = true;\n destroy();\n networkClient = undefined;\n };\n\n const disableRpcFailover = () => {\n isRpcFailoverEnabled = false;\n destroy();\n networkClient = undefined;\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n enableRpcFailover,\n disableRpcFailover,\n };\n}\n"]}