dequanto 0.1.39 → 0.1.45

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 (204) hide show
  1. package/lib/cjs/clients/ClientPool.js +23 -2
  2. package/lib/cjs/clients/ClientPool.js.map +1 -1
  3. package/lib/cjs/clients/WalletClient.js +98 -0
  4. package/lib/cjs/clients/WalletClient.js.map +1 -0
  5. package/lib/cjs/clients/Web3Client.js +39 -10
  6. package/lib/cjs/clients/Web3Client.js.map +1 -1
  7. package/lib/cjs/clients/Web3ClientFactory.js +7 -37
  8. package/lib/cjs/clients/Web3ClientFactory.js.map +1 -1
  9. package/lib/cjs/config/Config.js +4 -1
  10. package/lib/cjs/config/Config.js.map +1 -1
  11. package/lib/cjs/config/ConfigDefaults.js +3 -6
  12. package/lib/cjs/config/ConfigDefaults.js.map +1 -1
  13. package/lib/cjs/config/ConfigProvider.js +5 -1
  14. package/lib/cjs/config/ConfigProvider.js.map +1 -1
  15. package/lib/cjs/contracts/ContractWriter.js.map +1 -1
  16. package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js +10 -1
  17. package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
  18. package/lib/cjs/explorer/BlockchainExplorer.js +65 -43
  19. package/lib/cjs/explorer/BlockchainExplorer.js.map +1 -1
  20. package/lib/cjs/explorer/BlockchainExplorerFactory.js +6 -4
  21. package/lib/cjs/explorer/BlockchainExplorerFactory.js.map +1 -1
  22. package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js +5 -5
  23. package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
  24. package/lib/cjs/json/JsonArrayMultiStore.js.map +1 -1
  25. package/lib/cjs/json/JsonArrayStore.js +1 -1
  26. package/lib/cjs/json/JsonArrayStore.js.map +1 -1
  27. package/lib/cjs/json/JsonObjectStore.js +2 -1
  28. package/lib/cjs/json/JsonObjectStore.js.map +1 -1
  29. package/lib/cjs/json/JsonStoreFs.js +84 -18
  30. package/lib/cjs/json/JsonStoreFs.js.map +1 -1
  31. package/lib/cjs/models/TChain.js +4 -0
  32. package/lib/cjs/models/TChain.js.map +1 -0
  33. package/lib/cjs/models/TExplorer.js +4 -0
  34. package/lib/cjs/models/TExplorer.js.map +1 -0
  35. package/lib/cjs/models/TPlatform.js +1 -0
  36. package/lib/cjs/models/TPlatform.js.map +1 -1
  37. package/lib/cjs/rpc/transports/ITransport.js.map +1 -1
  38. package/lib/cjs/rpc/transports/RpcTransport.js +4 -0
  39. package/lib/cjs/rpc/transports/RpcTransport.js.map +1 -1
  40. package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js +14 -2
  41. package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
  42. package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js +4 -0
  43. package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
  44. package/lib/cjs/safe/GnosisSafeHandler.js.map +1 -1
  45. package/lib/cjs/tokens/TokenProviders/TPChain.js +5 -1
  46. package/lib/cjs/tokens/TokenProviders/TPChain.js.map +1 -1
  47. package/lib/cjs/txs/TxWriter.js +8 -1
  48. package/lib/cjs/txs/TxWriter.js.map +1 -1
  49. package/lib/cjs/utils/$buffer.js.map +1 -1
  50. package/lib/cjs/utils/$config.js +67 -2
  51. package/lib/cjs/utils/$config.js.map +1 -1
  52. package/lib/cjs/utils/$dependency.js +4 -0
  53. package/lib/cjs/utils/$dependency.js.map +1 -1
  54. package/lib/cjs/utils/$path.js +9 -0
  55. package/lib/cjs/utils/$path.js.map +1 -1
  56. package/lib/cjs/utils/$ref.js +30 -0
  57. package/lib/cjs/utils/$ref.js.map +1 -0
  58. package/lib/cjs/utils/$secret.js +15 -2
  59. package/lib/cjs/utils/$secret.js.map +1 -1
  60. package/lib/cjs/utils/$sig.js +18 -9
  61. package/lib/cjs/utils/$sig.js.map +1 -1
  62. package/lib/cjs/wallets/EIP6963ProviderFactory.js +192 -0
  63. package/lib/cjs/wallets/EIP6963ProviderFactory.js.map +1 -0
  64. package/lib/cjs/wallets/Signer.js +30 -0
  65. package/lib/cjs/wallets/Signer.js.map +1 -0
  66. package/lib/esm/clients/ClientPool.js.map +1 -1
  67. package/lib/esm/clients/ClientPool.mjs +23 -2
  68. package/lib/esm/clients/WalletClient.js.map +1 -0
  69. package/lib/esm/clients/WalletClient.mjs +91 -0
  70. package/lib/esm/clients/Web3Client.js.map +1 -1
  71. package/lib/esm/clients/Web3Client.mjs +39 -10
  72. package/lib/esm/clients/Web3ClientFactory.js.map +1 -1
  73. package/lib/esm/clients/Web3ClientFactory.mjs +7 -37
  74. package/lib/esm/config/Config.js.map +1 -1
  75. package/lib/esm/config/Config.mjs +4 -1
  76. package/lib/esm/config/ConfigDefaults.js.map +1 -1
  77. package/lib/esm/config/ConfigDefaults.mjs +3 -6
  78. package/lib/esm/config/ConfigProvider.js.map +1 -1
  79. package/lib/esm/config/ConfigProvider.mjs +5 -1
  80. package/lib/esm/contracts/ContractWriter.js.map +1 -1
  81. package/lib/esm/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
  82. package/lib/esm/contracts/deploy/storage/DeploymentsStorage.mjs +10 -1
  83. package/lib/esm/explorer/BlockchainExplorer.js.map +1 -1
  84. package/lib/esm/explorer/BlockchainExplorer.mjs +65 -43
  85. package/lib/esm/explorer/BlockchainExplorerFactory.js.map +1 -1
  86. package/lib/esm/explorer/BlockchainExplorerFactory.mjs +6 -4
  87. package/lib/esm/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
  88. package/lib/esm/explorer/verifiers/FsHtmlVerifier.mjs +5 -5
  89. package/lib/esm/json/JsonArrayMultiStore.js.map +1 -1
  90. package/lib/esm/json/JsonArrayStore.js.map +1 -1
  91. package/lib/esm/json/JsonArrayStore.mjs +1 -1
  92. package/lib/esm/json/JsonObjectStore.js.map +1 -1
  93. package/lib/esm/json/JsonObjectStore.mjs +2 -1
  94. package/lib/esm/json/JsonStoreFs.js.map +1 -1
  95. package/lib/esm/json/JsonStoreFs.mjs +84 -18
  96. package/lib/esm/models/TChain.js.map +1 -0
  97. package/lib/esm/models/TChain.mjs +3 -0
  98. package/lib/esm/models/TExplorer.js.map +1 -0
  99. package/lib/esm/models/TExplorer.mjs +3 -0
  100. package/lib/esm/models/TPlatform.js.map +1 -1
  101. package/lib/esm/models/TPlatform.mjs +1 -0
  102. package/lib/esm/rpc/transports/ITransport.js.map +1 -1
  103. package/lib/esm/rpc/transports/RpcTransport.js.map +1 -1
  104. package/lib/esm/rpc/transports/RpcTransport.mjs +4 -0
  105. package/lib/esm/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
  106. package/lib/esm/rpc/transports/compatibility/EIP1193Transport.mjs +14 -2
  107. package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
  108. package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.mjs +3 -0
  109. package/lib/esm/safe/GnosisSafeHandler.js.map +1 -1
  110. package/lib/esm/tokens/TokenProviders/TPChain.js.map +1 -1
  111. package/lib/esm/tokens/TokenProviders/TPChain.mjs +5 -1
  112. package/lib/esm/txs/TxWriter.js.map +1 -1
  113. package/lib/esm/txs/TxWriter.mjs +8 -1
  114. package/lib/esm/utils/$buffer.js.map +1 -1
  115. package/lib/esm/utils/$config.js.map +1 -1
  116. package/lib/esm/utils/$config.mjs +64 -2
  117. package/lib/esm/utils/$dependency.js.map +1 -1
  118. package/lib/esm/utils/$dependency.mjs +4 -0
  119. package/lib/esm/utils/$path.js.map +1 -1
  120. package/lib/esm/utils/$path.mjs +9 -0
  121. package/lib/esm/utils/$ref.js.map +1 -0
  122. package/lib/esm/utils/$ref.mjs +27 -0
  123. package/lib/esm/utils/$secret.js.map +1 -1
  124. package/lib/esm/utils/$secret.mjs +15 -2
  125. package/lib/esm/utils/$sig.js.map +1 -1
  126. package/lib/esm/utils/$sig.mjs +18 -9
  127. package/lib/esm/wallets/EIP6963ProviderFactory.js.map +1 -0
  128. package/lib/esm/wallets/EIP6963ProviderFactory.mjs +185 -0
  129. package/lib/esm/wallets/Signer.js.map +1 -0
  130. package/lib/esm/wallets/Signer.mjs +26 -0
  131. package/lib/types/clients/ClientPool.d.ts +13 -5
  132. package/lib/types/clients/WalletClient.d.ts +28 -0
  133. package/lib/types/clients/Web3Client.d.ts +7 -4
  134. package/lib/types/clients/Web3ClientFactory.d.ts +1 -1
  135. package/lib/types/clients/interfaces/IWeb3Client.d.ts +2 -5
  136. package/lib/types/config/AConfigBase.d.ts +0 -7
  137. package/lib/types/config/interface/IConfigData.d.ts +5 -2
  138. package/lib/types/explorer/BlockchainExplorer.d.ts +7 -4
  139. package/lib/types/explorer/BlockchainExplorerFactory.d.ts +7 -3
  140. package/lib/types/explorer/verifiers/FsHtmlVerifier.d.ts +3 -3
  141. package/lib/types/json/JsonArrayMultiStore.d.ts +2 -2
  142. package/lib/types/json/JsonArrayStore.d.ts +4 -3
  143. package/lib/types/json/JsonObjectStore.d.ts +5 -3
  144. package/lib/types/json/JsonStoreFs.d.ts +3 -2
  145. package/lib/types/models/TAccount.d.ts +9 -0
  146. package/lib/types/models/TChain.d.ts +45 -0
  147. package/lib/types/models/TEth.d.ts +8 -0
  148. package/lib/types/models/TExplorer.d.ts +31 -0
  149. package/lib/types/rpc/transports/ITransport.d.ts +2 -1
  150. package/lib/types/rpc/transports/compatibility/EIP1193Transport.d.ts +5 -1
  151. package/lib/types/rpc/transports/compatibility/IEIP6963Provider.d.ts +21 -0
  152. package/lib/types/utils/$config.d.ts +5 -0
  153. package/lib/types/utils/$ref.d.ts +11 -0
  154. package/lib/types/utils/$secret.d.ts +1 -0
  155. package/lib/types/utils/$sig.d.ts +1 -0
  156. package/lib/types/wallets/EIP6963ProviderFactory.d.ts +47 -0
  157. package/lib/types/wallets/Signer.d.ts +15 -0
  158. package/package.json +1 -1
  159. package/src/clients/ClientPool.ts +49 -15
  160. package/src/clients/WalletClient.ts +115 -0
  161. package/src/clients/Web3Client.ts +47 -16
  162. package/src/clients/Web3ClientFactory.ts +8 -39
  163. package/src/clients/interfaces/IWeb3Client.ts +2 -7
  164. package/src/config/AConfigBase.ts +3 -11
  165. package/src/config/Config.ts +6 -1
  166. package/src/config/ConfigDefaults.ts +3 -6
  167. package/src/config/ConfigProvider.ts +5 -1
  168. package/src/config/interface/IConfigData.ts +9 -4
  169. package/src/contracts/ContractWriter.ts +1 -0
  170. package/src/contracts/deploy/storage/DeploymentsStorage.ts +13 -3
  171. package/src/explorer/BlockchainExplorer.ts +82 -51
  172. package/src/explorer/BlockchainExplorerFactory.ts +19 -6
  173. package/src/explorer/verifiers/FsHtmlVerifier.ts +8 -8
  174. package/src/json/JsonArrayMultiStore.ts +2 -2
  175. package/src/json/JsonArrayStore.ts +5 -2
  176. package/src/json/JsonObjectStore.ts +7 -4
  177. package/src/json/JsonStoreFs.ts +103 -18
  178. package/src/models/TAccount.ts +10 -2
  179. package/src/models/TChain.ts +56 -0
  180. package/src/models/TEth.ts +9 -3
  181. package/src/models/TExplorer.ts +35 -0
  182. package/src/models/TPlatform.ts +1 -0
  183. package/src/rpc/transports/ITransport.ts +2 -1
  184. package/src/rpc/transports/RpcTransport.ts +4 -0
  185. package/src/rpc/transports/compatibility/EIP1193Transport.ts +19 -6
  186. package/src/rpc/transports/compatibility/IEIP6963Provider.ts +22 -0
  187. package/src/safe/GnosisSafeHandler.ts +1 -1
  188. package/src/tokens/TokenProviders/TPChain.ts +5 -1
  189. package/src/txs/TxWriter.ts +8 -2
  190. package/src/utils/$buffer.ts +1 -1
  191. package/src/utils/$config.ts +73 -2
  192. package/src/utils/$dependency.ts +5 -0
  193. package/src/utils/$path.ts +9 -0
  194. package/src/utils/$ref.ts +40 -0
  195. package/src/utils/$secret.ts +16 -3
  196. package/src/utils/$sig.ts +19 -10
  197. package/src/wallets/EIP6963ProviderFactory.ts +252 -0
  198. package/src/wallets/Signer.ts +32 -0
  199. package/lib/cjs/wallets/WalletBsc.js +0 -19
  200. package/lib/cjs/wallets/WalletBsc.js.map +0 -1
  201. package/lib/esm/wallets/WalletBsc.js.map +0 -1
  202. package/lib/esm/wallets/WalletBsc.mjs +0 -12
  203. package/lib/types/wallets/WalletBsc.d.ts +0 -10
  204. package/src/wallets/WalletBsc.ts +0 -13
@@ -0,0 +1,47 @@
1
+ import { TAddress } from '../models/TAddress';
2
+ import { TEth } from '../models/TEth';
3
+ import { TGlobal } from '../utils/$ref';
4
+ import { class_EventEmitter } from 'atma-utils';
5
+ import { IEIP6963Provider } from '../rpc/transports/compatibility/IEIP6963Provider';
6
+ interface EIP6963ProviderInfo {
7
+ rdns?: string;
8
+ uuid: string;
9
+ name: string;
10
+ icon?: string;
11
+ chainId?: number;
12
+ }
13
+ export interface EIP6963ProviderDetail {
14
+ info: EIP6963ProviderInfo;
15
+ provider: IEIP6963Provider;
16
+ accounts?: TEth.Address[];
17
+ }
18
+ interface IProviderEvents {
19
+ onProviderRegistered: (detail: EIP6963ProviderDetail) => void;
20
+ onProviderConnected: (detail: EIP6963ProviderDetail, chainId: number) => void;
21
+ onAccountsConnected: (detail: EIP6963ProviderDetail, accounts: TAddress[]) => void;
22
+ onAccountsDisconnected: (detail: EIP6963ProviderDetail) => void;
23
+ onChainChanged: (detail: EIP6963ProviderDetail, chainId: number) => void;
24
+ onAccountsChanged: (detail: EIP6963ProviderDetail, accounts: TEth.Address[]) => void;
25
+ }
26
+ export declare class EIP6963ProviderFactory extends class_EventEmitter<IProviderEvents> {
27
+ private listeners;
28
+ providers: EIP6963ProviderDetail[];
29
+ selected: EIP6963ProviderDetail;
30
+ global: TGlobal;
31
+ constructor();
32
+ isConnected(address?: TEth.Address): boolean;
33
+ connect(id?: string): Promise<`0x${string}`[]>;
34
+ disconnect(): void;
35
+ useProvider(id: string): void;
36
+ getProviders(): EIP6963ProviderDetail[];
37
+ requestAccounts(id?: string): Promise<TEth.Address[]>;
38
+ private requestProvider;
39
+ private listenToNewProvider;
40
+ private onAnnounceProvider;
41
+ getProviderOrFirst(id?: string): EIP6963ProviderDetail;
42
+ getProvider(id?: string, optional?: boolean): EIP6963ProviderDetail;
43
+ private getId;
44
+ private addEventListeners;
45
+ private removeEventListeners;
46
+ }
47
+ export {};
@@ -0,0 +1,15 @@
1
+ import { TEth } from '../models/TEth';
2
+ import { Rpc, RpcTypes } from '../rpc/Rpc';
3
+ export declare class Signer implements TEth.IRpcSigner {
4
+ opts: {
5
+ rpc?: Rpc;
6
+ } & Partial<TEth.IRpcSigner>;
7
+ constructor(opts: {
8
+ rpc?: Rpc;
9
+ } & Partial<TEth.IRpcSigner>);
10
+ eth_sign(account: TEth.Address, data: TEth.Hex): Promise<TEth.Hex>;
11
+ eth_signTransaction(tx: TEth.TxLike): Promise<TEth.Hex>;
12
+ eth_signTypedData_v4(address: TEth.Address, typedData: Partial<RpcTypes.TypedData>): Promise<TEth.Hex>;
13
+ personal_sign(challenge: string, address: TEth.Address): Promise<TEth.Hex>;
14
+ protected call(method: keyof TEth.IRpcSigner, params: any[]): Promise<any>;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dequanto",
3
- "version": "0.1.39",
3
+ "version": "0.1.45",
4
4
  "author": {
5
5
  "name": "Alex Kit",
6
6
  "email": "alex.kit@atmajs.com"
@@ -7,38 +7,35 @@ import { $contract } from '@dequanto/utils/$contract';
7
7
  import { $logger, l } from '@dequanto/utils/$logger';
8
8
  import { $promise } from '@dequanto/utils/$promise';
9
9
  import { $array } from '@dequanto/utils/$array';
10
- import { $bigint } from '@dequanto/utils/$bigint';
11
10
 
12
11
  import { PromiseEventWrap } from './model/PromiEventWrap';
13
12
  import { IWeb3ClientStatus } from './interfaces/IWeb3ClientStatus';
14
13
  import { ClientStatus } from './model/ClientStatus';
15
14
  import { ClientPoolTrace, ClientPoolTraceError, ErrorCode } from './ClientPoolStats';
16
- import { class_Dfr, obj_extendDefaults } from 'atma-utils';
15
+ import { class_Dfr } from 'atma-utils';
17
16
  import { ClientErrorUtil } from './utils/ClientErrorUtil';
18
17
  import { IWeb3ClientOptions } from './interfaces/IWeb3Client';
19
18
  import { RateLimitGuard } from './handlers/RateLimitGuard';
20
19
  import { Web3BatchRequests } from './Web3BatchRequests';
21
20
 
22
- import { HttpTransport } from '@dequanto/rpc/transports/HttpTransport';
23
- import { WsTransport } from '@dequanto/rpc/transports/WsTransport';
24
- import { Rpc } from '@dequanto/rpc/Rpc';
21
+ import { Rpc, RpcTypes } from '@dequanto/rpc/Rpc';
25
22
 
26
- import { Web3Transport } from '@dequanto/rpc/transports/compatibility/Web3Transport';
27
23
  import { PromiseEvent } from '@dequanto/class/PromiseEvent';
28
24
  import { TTransport } from '@dequanto/rpc/transports/ITransport';
29
25
 
30
26
  import { TRpc } from '@dequanto/rpc/RpcBase';
31
27
  import { TEth } from '@dequanto/models/TEth';
32
28
  import { $rpc } from '@dequanto/rpc/$rpc';
33
- import { Web3 } from './compatibility/Web3';
29
+ import { DataLike } from '@dequanto/utils/types';
34
30
 
35
- export interface IPoolClientConfig {
31
+ export interface IRpcConfig {
36
32
  url?: string
37
33
  options?: TTransport.Options.Http | TTransport.Options.Ws
38
34
 
39
- /** Will be a pefered node for submitting transactions */
35
+ /** Will be a preferred node for submitting transactions */
40
36
  safe?: boolean
41
37
  distinct?: boolean
38
+ wallet?: boolean
42
39
 
43
40
  web3?: TTransport.Transport | Promise<TTransport.Transport>
44
41
 
@@ -52,7 +49,7 @@ export interface IPoolClientConfig {
52
49
  /** True if the node supports traceTransaction calls */
53
50
  traceable?: boolean
54
51
 
55
- // requestCout/timeSpan: e.g. 100/1min
52
+ // requestCount/timeSpan: e.g. 100/1min
56
53
  rateLimit?: string
57
54
 
58
55
  // the max block range to fetch per single request when getting Logs
@@ -70,6 +67,9 @@ export interface IPoolClientConfig {
70
67
  export interface IPoolWeb3Request {
71
68
  ws?: boolean
72
69
  preferSafe?: boolean
70
+
71
+ /** Request wallet node to submit unsigned transactions */
72
+ wallet?: boolean
73
73
  distinct?: boolean
74
74
  name?: string
75
75
 
@@ -248,9 +248,29 @@ export class ClientPool {
248
248
  return max;
249
249
  }
250
250
 
251
+ wrappedPromiEvent <T> (asyncFactory: () => Promise<T>): PromiseEvent<T> {
252
+ let promiEvent = new PromiseEventWrap();
253
+
254
+ asyncFactory().then(
255
+ (result) => {
256
+ promiEvent.resolve(result);
257
+ },
258
+ (error) => {
259
+ promiEvent.reject(error);
260
+ }
261
+ );
262
+ return promiEvent as any as PromiseEvent<T>;
263
+ }
264
+
251
265
  callPromiEvent<TResult extends PromiseEvent<any>>(
252
266
  fn: (web3: WClient) => TResult
253
- , opts?: { preferSafe?: boolean, parallel?: number, silent?: boolean, distinct?: boolean }
267
+ , opts?: {
268
+ preferSafe?: boolean,
269
+ parallel?: number,
270
+ silent?: boolean,
271
+ distinct?: boolean,
272
+ wallet?: boolean
273
+ }
254
274
  , used: Map<WClient, number> = new Map<WClient, number>()
255
275
  , errors = []
256
276
  , root?: PromiseEventWrap
@@ -381,7 +401,7 @@ export class ClientPool {
381
401
  return null;
382
402
  }
383
403
  let promise = method(wClient);
384
- return options?.timeout == null ? promise : $promise.timeout(promise, options.timeout);
404
+ return options?.timeout == null ? promise : $promise.timeout(promise, options.timeout, `${methodName} in ${ wClient.config?.url }`);
385
405
  }
386
406
 
387
407
  let nodes = await alot(this.clients).mapAsync(async (wClient, idx) => {
@@ -397,7 +417,8 @@ export class ClientPool {
397
417
  ]);
398
418
  let blockNumber = Number(blockNumberUint);
399
419
 
400
- let ping = Math.round((Date.now() - start) / 4);
420
+ let requestsCount = options?.calls?.length ?? 4
421
+ let ping = Math.round((Date.now() - start) / requestsCount);
401
422
  let blockNumberBehind = 0;
402
423
  if (syncing?.currentBlock && syncing.currentBlock < blockNumber) {
403
424
  blockNumberBehind = blockNumber - Number(syncing.currentBlock);
@@ -528,6 +549,9 @@ export class ClientPool {
528
549
  healthy = safe;
529
550
  }
530
551
  }
552
+ if (opts?.wallet === true) {
553
+ healthy = healthy.filter(x => x.config.wallet === true);
554
+ }
531
555
 
532
556
  let arr = healthy.length > 0
533
557
  ? healthy
@@ -722,7 +746,7 @@ export class WClient {
722
746
 
723
747
  rpc: Rpc
724
748
 
725
- config: IPoolClientConfig
749
+ config: IRpcConfig
726
750
  rateLimitGuard: RateLimitGuard
727
751
 
728
752
  /** For getLogs method, as some providers limit the range request or page result value */
@@ -772,7 +796,7 @@ export class WClient {
772
796
  }
773
797
  }
774
798
 
775
- constructor(mix: IPoolClientConfig) {
799
+ constructor(mix: IRpcConfig) {
776
800
  const hasUrl = 'url' in mix && typeof mix.url === 'string';
777
801
  const hasWeb3 = 'web3' in mix && typeof mix.web3 != null;
778
802
  if (hasUrl || hasWeb3) {
@@ -877,6 +901,16 @@ export class WClient {
877
901
  return promise;
878
902
  }
879
903
 
904
+ sign (address: TEth.Address, message: string): Promise<string> {
905
+ return this.rpc.eth_sign(address, message);
906
+ }
907
+
908
+ signTypedData (address: TEth.Address, typedData: DataLike<RpcTypes.TypedData>): Promise<TEth.Hex> {
909
+ return this
910
+ .rpc
911
+ .eth_signTypedData_v4(address, typedData);
912
+ }
913
+
880
914
  async callBatched<TResult = any>(requests: TRpc.IRpcAction[]): Promise<TResult[]> {
881
915
  let total = requests.length;
882
916
  let spanLimit = this.getSpanLimit(requests.length);
@@ -0,0 +1,115 @@
1
+ import di from 'a-di';
2
+ import { TEth } from '@dequanto/models/TEth';
3
+ import { Rpc, RpcTypes } from '@dequanto/rpc/Rpc';
4
+ import { $require } from '@dequanto/utils/$require';
5
+ import { DataLike } from '@dequanto/utils/types';
6
+ import { EIP6963ProviderDetail, EIP6963ProviderFactory } from '@dequanto/wallets/EIP6963ProviderFactory';
7
+ import { WClient } from './ClientPool';
8
+ import { $hex } from '@dequanto/utils/$hex';
9
+
10
+ /** Wallet actions only. For all Node (Chain) related actions - Web3Client should be used */
11
+
12
+ export class WalletClient {
13
+ public factory: EIP6963ProviderFactory;
14
+
15
+ constructor (factory?: EIP6963ProviderFactory) {
16
+ this.factory = factory ?? di.resolve(EIP6963ProviderFactory);
17
+ }
18
+
19
+ // Client abstract methods
20
+
21
+ //Set the provider by UUID as the default one for eth_** methods
22
+ async useProvider (uuid: string) {
23
+ this.factory.useProvider(uuid);
24
+ }
25
+
26
+ async getProvider (uuid?: string): Promise<EIP6963ProviderDetail> {
27
+ return this.factory.getProvider(uuid, true);
28
+ }
29
+
30
+ async getProviders (): Promise<EIP6963ProviderDetail[]> {
31
+ let { providers } = this.factory;
32
+ return providers;
33
+ }
34
+
35
+ async getAccounts (uuid?: string): Promise<TEth.Address[]> {
36
+ let rpc = this.getRpc(uuid);
37
+ return rpc.eth_accounts();
38
+ }
39
+
40
+ /** Find the provider by UUID or use the first one to connect to and to requestAccounts */
41
+ async connect (uuid?: string): Promise<TEth.Address[]> {
42
+ return this.factory.connect(uuid);
43
+ }
44
+
45
+ async disconnect () {
46
+ this.factory.disconnect();
47
+ }
48
+
49
+ isConnected (address?: TEth.Address): boolean {
50
+ return this.factory.isConnected(address);
51
+ }
52
+
53
+ // RPC methods
54
+ eth_accounts () {
55
+ let rpc = this.getRpc();
56
+ return rpc.eth_accounts();
57
+ }
58
+
59
+ eth_requestAccounts () {
60
+ let rpc = this.getRpc();
61
+ return rpc.eth_requestAccounts();
62
+ }
63
+
64
+ eth_sign (address: TEth.Address, message: string): Promise<string> {
65
+ let rpc = this.getRpc();
66
+ return rpc.eth_sign(address, message);
67
+ }
68
+
69
+ eth_signTypedData_v4 (address: TEth.Address, typedData: DataLike<RpcTypes.TypedData>) {
70
+ let rpc = this.getRpc();
71
+ return rpc.eth_signTypedData_v4(address, typedData);
72
+ }
73
+
74
+ eth_sendTransaction (...args: Parameters<Rpc['eth_sendTransaction']>) {
75
+ let rpc = this.getRpc();
76
+ return rpc.eth_sendTransaction(...args);
77
+ }
78
+
79
+ eth_chainId (...args: Parameters<Rpc['eth_chainId']>) {
80
+ let rpc = this.getRpc();
81
+ return rpc.eth_chainId(...args);
82
+ }
83
+
84
+ wallet_addEthereumChain (...args: Parameters<Rpc['wallet_addEthereumChain']>) {
85
+ let rpc = this.getRpc();
86
+ return rpc.wallet_addEthereumChain(...args);
87
+ }
88
+
89
+ wallet_switchEthereumChain (...args: Parameters<Rpc['wallet_switchEthereumChain']>) {
90
+ let rpc = this.getRpc();
91
+ return rpc.wallet_switchEthereumChain(...args);
92
+ }
93
+
94
+ async getClientFor (chainId: number) {
95
+ let { selected } = this.factory;
96
+ $require.notNull(selected, 'No selected EIP6963 provider');
97
+ let client = new WClient({ web3: selected.provider as any });
98
+
99
+ let providerChainId = await client.rpc.eth_chainId();
100
+ if (providerChainId !== chainId) {
101
+ await client.rpc.wallet_switchEthereumChain({
102
+ chainId: $hex.ensure(chainId)
103
+ });
104
+ providerChainId = await client.rpc.eth_chainId();
105
+ $require.eq(providerChainId, chainId, 'Chain ID mismatch');
106
+ }
107
+
108
+ return client;
109
+ }
110
+
111
+ private getRpc (uuid?: string) {
112
+ let p = this.factory.getProvider(uuid, false);
113
+ return new Rpc(p.provider)
114
+ }
115
+ }
@@ -6,7 +6,7 @@ import type { TPlatform } from '@dequanto/models/TPlatform';
6
6
 
7
7
  import type { IWeb3Client, IWeb3ClientOptions } from './interfaces/IWeb3Client';
8
8
 
9
- import { ClientPool, IPoolClientConfig, IPoolWeb3Request, WClient } from './ClientPool';
9
+ import { ClientPool, IRpcConfig, IPoolWeb3Request, WClient } from './ClientPool';
10
10
  import { BlockDateResolver } from '@dequanto/blocks/BlockDateResolver';
11
11
  import { $number } from '@dequanto/utils/$number';
12
12
 
@@ -31,6 +31,7 @@ import { $sig } from '@dequanto/utils/$sig';
31
31
  import { DataLike } from '@dequanto/utils/types';
32
32
  import { ErrorCode } from './ClientPoolStats';
33
33
  import { $date } from '@dequanto/utils/$date';
34
+ import { WalletClient } from './WalletClient';
34
35
 
35
36
  export abstract class Web3Client implements IWeb3Client {
36
37
 
@@ -58,20 +59,14 @@ export abstract class Web3Client implements IWeb3Client {
58
59
  return this.forked?.platform ?? this.platform;
59
60
  }
60
61
 
61
- async sign(txData: TEth.TxLike, privateKey: TEth.Hex): Promise<string> {
62
- let rpc = await this.getRpc();
63
- let sig = await $sig.signTx( txData, { key: privateKey }, rpc);
64
- let tx = $sig.TxSerializer.serialize(txData, sig);
65
- return tx;
66
- }
67
-
68
62
  public options: IWeb3ClientOptions;
69
63
  public pool: ClientPool;
70
64
  public debug: ClientDebugMethods;
65
+ public wallet = di.resolve(WalletClient);
71
66
 
72
67
  constructor(options: IWeb3ClientOptions)
73
- constructor(endpoints: IPoolClientConfig[])
74
- constructor(mix: IWeb3ClientOptions | IPoolClientConfig[]) {
68
+ constructor(endpoints: IRpcConfig[])
69
+ constructor(mix: IWeb3ClientOptions | IRpcConfig[]) {
75
70
  if (Array.isArray(mix)) {
76
71
  this.options = { endpoints: mix }
77
72
  } else if (mix != null) {
@@ -159,10 +154,7 @@ export abstract class Web3Client implements IWeb3Client {
159
154
  ): Promise<RpcSubscription<TEth.Hex>>;
160
155
  async subscribe(type, ...params): Promise<RpcSubscription<any>> {
161
156
  let wClient = await this.pool.getWrappedWeb3({ ws: true });
162
- console.log(`Check ensure connected`);
163
157
  await wClient.ensureConnected();
164
- console.log(`Check ensure connectd OK`);
165
-
166
158
  switch (type) {
167
159
  case 'newBlockHeaders':
168
160
  type = 'newHeads';
@@ -398,10 +390,49 @@ export abstract class Web3Client implements IWeb3Client {
398
390
  }, { preferSafe: true, distinct: true });
399
391
  }
400
392
 
393
+ // async signTx(txData: TEth.TxLike, privateKey: TEth.Hex): Promise<string> {
394
+ // let rpc = await this.getRpc();
395
+ // let sig = await $sig.signTx( txData, { key: privateKey }, rpc);
396
+ // let tx = $sig.TxSerializer.serialize(txData, sig);
397
+ // return tx;
398
+ // }
399
+
400
+ async sign(address: TEth.Address, message: string): Promise<string> {
401
+ if (this.wallet.isConnected(address)) {
402
+ return this.wallet.eth_sign(address, message);
403
+ }
404
+ return this.pool.call(wClient => {
405
+ return wClient.sign(address, message);
406
+ }, {
407
+ wallet: true
408
+ });
409
+ }
410
+
411
+ signTypedData(address: TEth.Address, typedData: DataLike<RpcTypes.TypedData>): Promise<TEth.Hex> {
412
+ if (this.wallet.isConnected(address)) {
413
+ return this.wallet.eth_signTypedData_v4(address, typedData);
414
+ }
415
+ return this.pool.call(wClient => {
416
+ return wClient.signTypedData(address, typedData);
417
+ }, {
418
+ wallet: true
419
+ });
420
+ }
421
+
401
422
  sendTransaction(data: TEth.TxLike): PromiseEvent<TEth.TxReceipt> {
423
+ if (this.wallet.isConnected(data.from)) {
424
+ return this.pool.wrappedPromiEvent <TEth.TxReceipt> (async () => {
425
+ let client = await this.wallet.getClientFor(this.chainId);
426
+ return client.sendTransaction(data);
427
+ });
428
+ }
402
429
  return this.pool.callPromiEvent(wClient => {
403
430
  return wClient.sendTransaction(data);
404
- }, { preferSafe: true, distinct: true });
431
+ }, {
432
+ preferSafe: true,
433
+ distinct: true,
434
+ wallet: this.platform === 'hardhat' ? void 0 : true
435
+ });
405
436
  }
406
437
 
407
438
  getBlockNumber() {
@@ -514,9 +545,9 @@ export abstract class Web3Client implements IWeb3Client {
514
545
  }
515
546
 
516
547
  static url<T extends Web3Client>(options: IWeb3ClientOptions)
517
- static url<T extends Web3Client>(endpoints: IPoolClientConfig[], opts?: Partial<IWeb3ClientOptions>)
548
+ static url<T extends Web3Client>(endpoints: IRpcConfig[], opts?: Partial<IWeb3ClientOptions>)
518
549
  static url<T extends Web3Client>(url: string, opts?: Partial<IWeb3ClientOptions>): T
519
- static url<T extends Web3Client>(mix: IWeb3ClientOptions | IPoolClientConfig[] | string, opts?: Partial<IWeb3ClientOptions>): T {
550
+ static url<T extends Web3Client>(mix: IWeb3ClientOptions | IRpcConfig[] | string, opts?: Partial<IWeb3ClientOptions>): T {
520
551
  const Ctor: any = this;
521
552
  let options: IWeb3ClientOptions;
522
553
  if (typeof mix === 'string') {
@@ -1,5 +1,4 @@
1
1
  import di from 'a-di';
2
- import alot from 'alot';
3
2
  import { TPlatform } from '@dequanto/models/TPlatform';
4
3
  import { IWeb3EndpointOptions } from './interfaces/IWeb3EndpointOptions';
5
4
  import { HardhatProvider } from '@dequanto/hardhat/HardhatProvider';
@@ -7,54 +6,24 @@ import { Config } from '@dequanto/config/Config';
7
6
  import { EvmWeb3Client } from './EvmWeb3Client';
8
7
  import { $require } from '@dequanto/utils/$require';
9
8
  import { $config } from '@dequanto/utils/$config';
10
- import { IConfigData } from '@dequanto/config/interface/IConfigData';
11
9
 
12
10
  export namespace Web3ClientFactory {
13
11
 
14
- export function get (platform: TPlatform | string | number, opts?: IWeb3EndpointOptions) {
15
- let web3 = $config.get<IConfigData['web3']>('web3');
16
- if (typeof platform ==='number') {
17
- let chain = alot.fromObject(web3).find(x => x.value.chainId === platform);
18
- if (chain == null) {
19
- throw new Error(`Unsupported platform ${platform} for web3 client`);
20
- }
21
- platform = chain.key
22
- }
23
- if (platform === 'hardhat' || platform.startsWith('hh:')) {
12
+ export function get (platform: TPlatform | number, opts?: IWeb3EndpointOptions) {
13
+ if (typeof platform === 'string' && (platform === 'hardhat' || platform.startsWith('hh:'))) {
24
14
  let client = di.resolve(HardhatProvider).client('localhost', opts);
25
15
  if (platform.startsWith('hh:')) {
26
16
  client.configureFork(platform.slice(3));
27
17
  }
28
18
  return client;
29
19
  }
30
- let cfg = web3[platform];
31
- $require.notNull(cfg, `Unsupported platform ${platform} for web3 client`)
32
- return new EvmWeb3Client({ platform, ...cfg });
33
20
 
34
- // switch (platform) {
35
- // case 'bsc':
36
- // return di.resolve(BscWeb3Client, opts);
37
- // case 'eth':
38
- // return di.resolve(EthWeb3Client, opts);
39
- // case 'eth:goerli':
40
- // return di.resolve(EthWeb3Client, {
41
- // platform: platform,
42
- // chainId: 5,
43
- // ...(opts ?? {})
44
- // });
45
- // case 'polygon':
46
- // return di.resolve(PolyWeb3Client, opts);
47
- // case 'arbitrum':
48
- // return di.resolve(ArbWeb3Client, opts);
49
- // case 'xdai':
50
- // return di.resolve(XDaiWeb3Client, opts);
51
- // default:
52
- // let cfg = config.web3[platform];
53
- // if (cfg != null) {
54
- // return new EvmWeb3Client({ platform, ...cfg });
55
- // }
56
- // throw new Error(`Unsupported platform ${platform} for web3 client`);
57
- // }
21
+ let options = $config.getWeb3Options(platform);
22
+ $require.notNull(options, `Unsupported platform ${platform} for web3 client`)
23
+ return new EvmWeb3Client({
24
+ ...options,
25
+ ...(opts ?? {})
26
+ });
58
27
  }
59
28
 
60
29
  /** Same as sync variation, but ensures the config is being fetched */
@@ -1,21 +1,16 @@
1
- import { TBufferLike } from '@dequanto/models/TBufferLike';
2
1
  import { TPlatform } from '@dequanto/models/TPlatform';
3
-
4
- import { IPoolClientConfig } from '../ClientPool';
5
- import { TEth } from '@dequanto/models/TEth';
2
+ import { IRpcConfig } from '../ClientPool';
6
3
  import { TTransport } from '@dequanto/rpc/transports/ITransport';
7
4
 
8
5
  export interface IWeb3Client {
9
6
  platform: string
10
7
  chainId: number
11
8
  defaultGasLimit: number
12
-
13
- sign (txData: TEth.Tx, privateKey: string): Promise<TBufferLike>
14
9
  }
15
10
 
16
11
 
17
12
  export interface IWeb3ClientOptions {
18
- endpoints: IPoolClientConfig[]
13
+ endpoints: IRpcConfig[]
19
14
  platform?: TPlatform
20
15
  chainId?: number
21
16
  // Token symbol: e.g. ETH
@@ -1,5 +1,7 @@
1
+ import { IRpcConfig } from '@dequanto/clients/ClientPool'
1
2
  import { IConfigData } from './interface/IConfigData'
2
-
3
+ import { TPlatform } from '@dequanto/models/TPlatform'
4
+ import { TEth } from '@dequanto/models/TEth'
3
5
 
4
6
  type TConfigParamsBase = {
5
7
  config?: Partial<IConfigData>
@@ -18,16 +20,6 @@ export type TConfigParamsBrowser = TConfigParamsBase & {
18
20
  }
19
21
  export type TConfigParams = TConfigParamsNode | TConfigParamsBrowser;
20
22
 
21
- export interface IProviderEndpoint {
22
- url: string
23
- private?: boolean
24
- safe?: boolean
25
-
26
- // Is not used in general ClientPool, only retrievable via getWeb3 method.
27
- manual?: boolean
28
-
29
- traceable?: boolean
30
- }
31
23
 
32
24
  export interface IConfigProvider {
33
25
 
@@ -15,7 +15,7 @@ export class Config {
15
15
 
16
16
  obj_extend(config, cfg);
17
17
 
18
- if (config.web3 == null) {
18
+ if (config.web3 == null && config.chains == null) {
19
19
  let message = `web3 is not defined in the config file`;
20
20
  $logger.log(message);
21
21
  singleton.reject(new Error(message));
@@ -34,6 +34,11 @@ export class Config {
34
34
  return Config.fetch();
35
35
  }
36
36
 
37
+ // static async getExplorerConfig (platform: TPlatform | number): Promise<TExplorer> {
38
+ // let config = await Config.get();
39
+
40
+ // }
41
+
37
42
  static async extend (json) {
38
43
  await provider.extend(json);
39
44
  }
@@ -7,6 +7,7 @@ export const ConfigDefaults = <IConfigData> {
7
7
  }
8
8
  },
9
9
  "tokens": null,
10
+ "chains": null,
10
11
  "blockchainExplorer": {
11
12
  "bsc": {
12
13
  "key": "MB1EM53BDJFKDIHUZ5JJT946BJJUQIHFP2",
@@ -116,14 +117,10 @@ export const ConfigDefaults = <IConfigData> {
116
117
  "chainToken": "ETH",
117
118
  "endpoints": [
118
119
  {
119
- "url": "https://eth.public-rpc.com"
120
+ "url": "https://ethereum-rpc.publicnode.com"
120
121
  },
121
122
  {
122
- "url": "https://main-light.eth.linkpool.io",
123
- "rateLimit": "2000/5m"
124
- },
125
- {
126
- "url": "wss://main-light.eth.linkpool.io/ws",
123
+ "url": "https://eth.drpc.org",
127
124
  "rateLimit": "2000/5m"
128
125
  }
129
126
  ]
@@ -101,14 +101,18 @@ export class ConfigProvider implements IConfigProvider {
101
101
  }
102
102
 
103
103
  private async getSourcesBrowser (parameters: TConfigParamsNode) {
104
- return [
104
+ let sources = [
105
105
  {
106
106
  config: {
107
107
  ...ConfigDefaults,
108
108
  ...(parameters?.config ?? {}),
109
109
  }
110
110
  },
111
+ {
112
+ localStorage: '0xweb/config'
113
+ }
111
114
  ];
115
+ return sources;
112
116
  }
113
117
  }
114
118
 
@@ -2,7 +2,8 @@ import { IContractDetails } from '@dequanto/models/IContractDetails';
2
2
  import { ITokenGlob } from '@dequanto/models/ITokenGlob';
3
3
  import { TAddress } from '@dequanto/models/TAddress';
4
4
  import { TPlatform } from '@dequanto/models/TPlatform';
5
- import { IProviderEndpoint } from '../AConfigBase';
5
+ import { IRpcConfig } from '@dequanto/clients/ClientPool';
6
+ import { TChain } from '@dequanto/models/TChain';
6
7
 
7
8
 
8
9
  export interface IConfigData {
@@ -31,11 +32,15 @@ export interface IConfigData {
31
32
  host: string;
32
33
  };
33
34
  };
35
+
36
+ chains: TChain[]
37
+
34
38
  web3: {
35
39
  [platform in TPlatform]: {
36
- chainId?: number;
37
- chainToken?: string;
38
- endpoints: IProviderEndpoint[];
40
+ chainId?: number
41
+ chainToken?: string
42
+ aliases?: string[]
43
+ endpoints: IRpcConfig[]
39
44
  };
40
45
  };
41
46
  contracts?: {