|
1 | 1 | import fetch, { Response } from 'node-fetch'; |
2 | 2 | import { RequestArguments, Address, ChainListEntry, EIP1193Provider, RichListEntry, Supply, WalletListEntry } from './types.js'; |
| 3 | +import { BlockcoreDns, DnsListEntry, ServiceListEntry } from '@blockcore/dns'; |
| 4 | +import { WebRequest } from './Request.js'; |
3 | 5 |
|
4 | 6 | export class IndexerProvider { |
5 | | - private baseUrl: string; |
| 7 | + private dns: BlockcoreDns; |
| 8 | + private nameservers: DnsListEntry[] = []; |
| 9 | + private services: ServiceListEntry[] = []; |
| 10 | + private currentServices: ServiceListEntry[] = []; |
| 11 | + private network = 'STRAX'; // Should we default to BTC? |
6 | 12 |
|
7 | | - public constructor(baseUrlOrNetwork?: string) { |
8 | | - baseUrlOrNetwork = baseUrlOrNetwork || 'CITY'; |
9 | | - |
10 | | - if (baseUrlOrNetwork.indexOf('http') > -1) { |
11 | | - this.baseUrl = baseUrlOrNetwork; |
12 | | - } else { |
13 | | - this.baseUrl = this.getNetworkUrl(baseUrlOrNetwork); |
14 | | - } |
| 13 | + public constructor() { |
| 14 | + this.dns = new BlockcoreDns(''); |
15 | 15 | } |
16 | 16 |
|
17 | | - setProvider(provider: string) { |
18 | | - this.baseUrl = provider; |
| 17 | + setNetwork(network: string) { |
| 18 | + this.network = network; |
| 19 | + this.filterServices(); |
19 | 20 | } |
20 | 21 |
|
21 | | - on(event: string, callback: any) { |
22 | | - console.log(event, callback); |
23 | | - // "accountsChanged" |
24 | | - // "chainChanged" |
25 | | - // "networkChanged" |
| 22 | + filterServices() { |
| 23 | + this.currentServices = this.services.filter((s) => s.symbol === this.network && s.online === true); |
26 | 24 | } |
27 | 25 |
|
28 | | - private async fetchText(url: string): Promise<string> { |
29 | | - const response = await this.fetchUrl(url); |
30 | | - return response.text(); |
| 26 | + /** Attempts to load the latest status of all services from all known nameservers. */ |
| 27 | + async load() { |
| 28 | + this.nameservers = await BlockcoreDns.getDnsServers(); |
| 29 | + |
| 30 | + const servicesMap = new Map(); |
| 31 | + |
| 32 | + for (let i = 0; i < this.nameservers.length; i++) { |
| 33 | + const nameserver = this.nameservers[i]; |
| 34 | + |
| 35 | + if (!nameserver) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + this.dns.setActiveServer(nameserver.url); |
| 40 | + |
| 41 | + const services = await this.dns.getServicesByType('Indexer'); |
| 42 | + |
| 43 | + services.forEach((item) => servicesMap.set(item.domain, { ...servicesMap.get(item.domain), ...item })); |
| 44 | + } |
| 45 | + |
| 46 | + this.services = Array.from(servicesMap.values()); |
| 47 | + |
| 48 | + this.filterServices(); |
31 | 49 | } |
32 | 50 |
|
33 | | - private async fetchJson<T>(url: string): Promise<T> { |
34 | | - const response = await this.fetchUrl(url); |
35 | | - return response.json() as Promise<T>; |
| 51 | + getNameServers() { |
| 52 | + return this.nameservers; |
36 | 53 | } |
37 | 54 |
|
38 | | - private async fetchUrl(url: string): Promise<Response> { |
39 | | - return await fetch(url, { |
40 | | - method: 'GET', |
41 | | - // mode: 'cors', |
42 | | - // cache: 'no-cache', |
43 | | - // credentials: 'same-origin', |
44 | | - headers: { |
45 | | - 'Content-Type': 'application/json', |
46 | | - }, |
47 | | - redirect: 'follow', |
48 | | - referrerPolicy: 'no-referrer', |
49 | | - }); |
| 55 | + async getIndexersByNetwork(network: string) { |
| 56 | + return this.dns.getServicesByTypeAndNetwork('Indexer', network); |
50 | 57 | } |
51 | 58 |
|
52 | | - public setNetwork(network: string): void { |
53 | | - this.baseUrl = this.getNetworkUrl(network); |
| 59 | + on(event: string, callback: any) { |
| 60 | + console.log(event, callback); |
| 61 | + // "accountsChanged" |
| 62 | + // "chainChanged" |
| 63 | + // "networkChanged" |
54 | 64 | } |
55 | 65 |
|
56 | | - public getNetworkUrl(network: string): string { |
57 | | - return `https://${network.toLowerCase()}.indexer.blockcore.net`; |
| 66 | + private getRandomInt(max: number) { |
| 67 | + return Math.floor(Math.random() * max); |
58 | 68 | } |
59 | 69 |
|
60 | | - public getBaseUrl(): string { |
61 | | - return this.baseUrl; |
| 70 | + private getUrl(): string | undefined { |
| 71 | + // TODO: This can be simplified, I'm just too tired to refactor right now. |
| 72 | + if (this.currentServices.length > 1) { |
| 73 | + const serviceIndex = this.getRandomInt(this.currentServices.length); |
| 74 | + return `https://${this.currentServices[serviceIndex]?.domain}`; |
| 75 | + } else if (this.currentServices.length == 1) { |
| 76 | + return `https://${this.currentServices[0]?.domain}`; |
| 77 | + } else { |
| 78 | + return undefined; |
| 79 | + } |
62 | 80 | } |
63 | 81 |
|
64 | 82 | //** Returns the result from the officially hosted list of Blockcore supported chains. */ |
65 | 83 | public getNetworks() { |
66 | | - return this.fetchJson<ChainListEntry[]>('https://chains.blockcore.net/CHAINS.json'); |
| 84 | + return WebRequest.fetchJson<ChainListEntry[]>('https://chains.blockcore.net/CHAINS.json'); |
67 | 85 | } |
68 | 86 |
|
69 | 87 | public getSupply() { |
70 | | - return this.fetchJson<Supply>(this.baseUrl + '/api/insight/supply'); |
| 88 | + return WebRequest.fetchJson<Supply>(this.getUrl() + '/api/insight/supply'); |
71 | 89 | } |
72 | 90 |
|
73 | 91 | public async getCirculatingSupply() { |
74 | | - return this.fetchText(this.baseUrl + '/api/insight/supply/circulating'); |
| 92 | + return WebRequest.fetchText(this.getUrl() + '/api/insight/supply/circulating'); |
75 | 93 | } |
76 | 94 |
|
77 | 95 | public async getTotalSupply() { |
78 | | - return this.fetchText(this.baseUrl + '/api/insight/supply/total'); |
| 96 | + return WebRequest.fetchText(this.getUrl() + '/api/insight/supply/total'); |
79 | 97 | } |
80 | 98 |
|
81 | 99 | public async getEstimateRewards() { |
82 | | - return this.fetchText(this.baseUrl + '/api/insight/rewards'); |
| 100 | + return WebRequest.fetchText(this.getUrl() + '/api/insight/rewards'); |
83 | 101 | } |
84 | 102 | public async getWallets() { |
85 | | - return this.fetchJson<WalletListEntry[]>(this.baseUrl + '/api/insight/wallets'); |
| 103 | + return WebRequest.fetchJson<WalletListEntry[]>(this.getUrl() + '/api/insight/wallets'); |
86 | 104 | } |
87 | 105 |
|
88 | 106 | public async getRichList() { |
89 | | - return this.fetchJson<RichListEntry[]>(this.baseUrl + '/api/insight/richlist'); |
| 107 | + return WebRequest.fetchJson<RichListEntry[]>(this.getUrl() + '/api/insight/richlist'); |
90 | 108 | } |
91 | 109 |
|
92 | 110 | public async getAddress(address: string) { |
93 | | - return this.fetchJson<Address>(`${this.baseUrl}/api/query/address/${address}`); |
| 111 | + return WebRequest.fetchJson<Address>(`${this.getUrl()}/api/query/address/${address}`); |
94 | 112 | } |
95 | 113 |
|
96 | 114 | public async getAddressTransactions(address: string) { |
97 | | - return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions`); |
| 115 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/address/${address}/transactions`); |
98 | 116 | } |
99 | 117 |
|
100 | 118 | public async getAddressUnconfirmedTransactions(address: string) { |
101 | | - return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unconfirmed`); |
| 119 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/address/${address}/transactions/unconfirmed`); |
102 | 120 | } |
103 | 121 |
|
104 | 122 | public async getAddressSpentTransactions(address: string) { |
105 | | - return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/spent`); |
| 123 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/address/${address}/transactions/spent`); |
106 | 124 | } |
107 | 125 |
|
108 | 126 | public async getAddressUnspentTransactions(address: string) { |
109 | | - return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unspent`); |
| 127 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/address/${address}/transactions/unspent`); |
110 | 128 | } |
111 | 129 |
|
112 | 130 | public async getMempoolTransactions() { |
113 | | - return this.fetchJson(`${this.baseUrl}/api/query/mempool/transactions`); |
| 131 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/mempool/transactions`); |
114 | 132 | } |
115 | 133 |
|
116 | 134 | public async getMempoolTransactionsCount() { |
117 | | - return this.fetchText(`${this.baseUrl}/api/query/mempool/transactions/count`); |
| 135 | + return WebRequest.fetchText(`${this.getUrl()}/api/query/mempool/transactions/count`); |
118 | 136 | } |
119 | 137 |
|
120 | 138 | public async getTransactionById(id: string) { |
121 | | - return this.fetchJson(`${this.baseUrl}/api/query/transaction/${id}`); |
| 139 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/transaction/${id}`); |
122 | 140 | } |
123 | 141 |
|
124 | 142 | public async getBlock() { |
125 | | - return this.fetchJson(`${this.baseUrl}/api/query/block`); |
| 143 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block`); |
126 | 144 | } |
127 | 145 |
|
128 | 146 | public async getBlockTransactionsByHash(hash: string) { |
129 | | - return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}/transactions`); |
| 147 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block/${hash}/transactions`); |
130 | 148 | } |
131 | 149 |
|
132 | 150 | public async getBlockByHash(hash: string) { |
133 | | - return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}`); |
| 151 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block/${hash}`); |
134 | 152 | } |
135 | 153 |
|
136 | 154 | public async getBlockByIndex(index: string) { |
137 | | - return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}`); |
| 155 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block/index/${index}`); |
138 | 156 | } |
139 | 157 |
|
140 | 158 | public async getBlockTransactionsByIndex(index: string) { |
141 | | - return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}/transactions`); |
| 159 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block/index/${index}/transactions`); |
142 | 160 | } |
143 | 161 |
|
144 | 162 | public async getLatestBlock() { |
145 | | - return this.fetchJson(`${this.baseUrl}/api/query/block/latest`); |
| 163 | + return WebRequest.fetchJson(`${this.getUrl()}/api/query/block/latest`); |
146 | 164 | } |
147 | 165 | } |
0 commit comments