@sentio/runtime 1.40.5 → 2.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/provider.ts DELETED
@@ -1,75 +0,0 @@
1
- import { getNetwork, Provider, StaticJsonRpcProvider } from '@ethersproject/providers'
2
- import { Networkish } from '@ethersproject/networks'
3
- import PQueue from 'p-queue'
4
- import { ConnectionInfo } from '@ethersproject/web'
5
- import { ChainConfig } from './chain-config'
6
- import { Endpoints } from './endpoints'
7
-
8
- export const DummyProvider = new StaticJsonRpcProvider(undefined, 1)
9
-
10
- export function getProvider(networkish?: Networkish): Provider {
11
- if (!networkish) {
12
- networkish = 1
13
- }
14
- const network = getNetwork(networkish)
15
-
16
- if (!Endpoints.INSTANCE.providers) {
17
- throw Error('Provider not set')
18
- }
19
- const value = Endpoints.INSTANCE.providers.get(network.chainId)
20
- if (value === undefined) {
21
- throw Error(
22
- 'Provider not found for chain ' +
23
- network.chainId +
24
- ', configured chains: ' +
25
- [...Endpoints.INSTANCE.providers.keys()].join(' ')
26
- )
27
- }
28
- return value
29
- }
30
-
31
- export function setProvider(config: Record<string, ChainConfig>, concurrency = 4, useChainServer = false) {
32
- Endpoints.INSTANCE.providers = new Map<number, Provider>()
33
-
34
- for (const chainIdStr in config) {
35
- if (isNaN(Number.parseInt(chainIdStr))) {
36
- continue
37
- }
38
-
39
- const chainConfig = config[chainIdStr]
40
- const chainId = Number(chainIdStr)
41
-
42
- // let providers: StaticJsonRpcProvider[] = []
43
- // for (const http of chainConfig.Https) {
44
- // providers.push(new StaticJsonRpcProvider(http, chainId))
45
- // }
46
- // random shuffle
47
- // providers = providers.sort(() => Math.random() - 0.5)
48
-
49
- // const provider = new FallbackProvider(providers)
50
-
51
- let rpcAddress = ''
52
- if (useChainServer && chainConfig.ChainServer) {
53
- rpcAddress = chainConfig.ChainServer
54
- } else {
55
- const idx = Math.floor(Math.random() * chainConfig.Https.length)
56
- rpcAddress = chainConfig.Https[idx]
57
- }
58
-
59
- const provider = new QueuedStaticJsonRpcProvider(rpcAddress, chainId, concurrency)
60
- Endpoints.INSTANCE.providers.set(chainId, provider)
61
- }
62
- }
63
-
64
- class QueuedStaticJsonRpcProvider extends StaticJsonRpcProvider {
65
- executor: PQueue
66
-
67
- constructor(url: ConnectionInfo | string, network: Networkish, concurrency: number) {
68
- super(url, network)
69
- this.executor = new PQueue({ concurrency: concurrency })
70
- }
71
-
72
- send(method: string, params: Array<any>): Promise<any> {
73
- return this.executor.add(() => super.send(method, params))
74
- }
75
- }