From bb283bf3df2e4e9d3fa618a5fbfc09a13d0c22e4 Mon Sep 17 00:00:00 2001 From: robertlincecum Date: Mon, 21 Oct 2024 17:38:28 -0500 Subject: [PATCH] balance and block accesses subs working --- .../integration/sendquai.integration.test.ts | 4 +-- src/providers/abstract-provider.ts | 25 ++++++++-------- src/providers/provider-socket.ts | 13 ++++---- src/providers/provider-websocket.ts | 30 ++++++++++++++----- src/providers/provider.ts | 3 ++ 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/_tests/integration/sendquai.integration.test.ts b/src/_tests/integration/sendquai.integration.test.ts index c788d897..93658c16 100644 --- a/src/_tests/integration/sendquai.integration.test.ts +++ b/src/_tests/integration/sendquai.integration.test.ts @@ -64,8 +64,8 @@ describe('Test sending Quai', function () { from: wallet.address, }; console.log(`Sending quai to: ${receiverAddress}`); - provider.on({ address: receiverAddress }, () => { - console.log('Received quai'); + provider.on({ type: 'balance', address: receiverAddress }, (balance) => { + console.log(`Received quai on address ${receiverAddress}. New Balance is ${balance}`); }); const tx = (await wallet.sendTransaction(txObj)) as QuaiTransactionResponse; //wait 2 seconds diff --git a/src/providers/abstract-provider.ts b/src/providers/abstract-provider.ts index fbc7aa61..259e91d4 100644 --- a/src/providers/abstract-provider.ts +++ b/src/providers/abstract-provider.ts @@ -362,7 +362,19 @@ async function getSubscription(_event: ProviderEvent, zone?: Zone): Promise_event).topics || Array.isArray((_event).address)) { + if ((_event).type && (_event).address) { + const address = formatMixedCaseChecksumAddress( + isHexString((_event).address) ? (_event).address : await resolveAddress((_event).address), + ); + const filter = { + type: (_event).type, + address: address, + }; + if (!zone) { + zone = toZone(address.slice(0, 4)); + } + return { filter, tag: getTag('accesses', filter), type: 'accesses', zone }; + } else if ((_event).topics || (_event).address) { const event = _event; const filter: EventFilter = { @@ -418,17 +430,6 @@ async function getSubscription(_event: ProviderEvent, zone?: Zone): Promise_event).address) { - const address = formatMixedCaseChecksumAddress( - isHexString((_event).address) ? (_event).address : await resolveAddress((_event).address), - ); - const filter = { - address: address, - }; - if (!zone) { - zone = toZone(address.slice(0, 4)); - } - return { filter, tag: getTag('accesses', filter), type: 'accesses', zone }; } assertArgument(false, 'unknown ProviderEvent', 'event', _event); diff --git a/src/providers/provider-socket.ts b/src/providers/provider-socket.ts index b59ddffc..177a4b32 100644 --- a/src/providers/provider-socket.ts +++ b/src/providers/provider-socket.ts @@ -188,10 +188,10 @@ export class SocketBlockSubscriber extends SocketSubscriber { * @category Providers */ export class SocketAccessesSubscriber extends SocketSubscriber { - #logFilter: string; + #accessesFilter: string; - get logFilter(): EventFilter { - return JSON.parse(this.#logFilter); + get accessesFilter(): AccessesFilter { + return JSON.parse(this.#accessesFilter); } /** * Creates a new **SocketBlockSubscriber**. @@ -203,7 +203,7 @@ export class SocketAccessesSubscriber extends SocketSubscriber { */ constructor(provider: SocketProvider, filter: AccessesFilter, zone: Zone) { super(provider, ['accesses', filter.address], zone); - this.#logFilter = JSON.stringify(filter); + this.#accessesFilter = JSON.stringify(filter); } /** @@ -215,7 +215,10 @@ export class SocketAccessesSubscriber extends SocketSubscriber { * @returns {Promise} */ async _emit(provider: SocketProvider, message: any): Promise { - provider.emit(this.logFilter, this.zone, message); + if (this.accessesFilter.type === 'balance') { + message = await provider.getBalance(this.accessesFilter.address); + } + provider.emit(this.accessesFilter, this.zone, message); } } diff --git a/src/providers/provider-websocket.ts b/src/providers/provider-websocket.ts index 4bf5e1aa..557e1e5c 100644 --- a/src/providers/provider-websocket.ts +++ b/src/providers/provider-websocket.ts @@ -16,6 +16,7 @@ export interface WebSocketLike { onopen: null | ((...args: Array) => any); onmessage: null | ((...args: Array) => any); onerror: null | ((...args: Array) => any); + onclose: null | ((...args: Array) => any); readyState: number; @@ -116,6 +117,20 @@ export class WebSocketProvider extends SocketProvider { // @TODO: now what? Attempt reconnect? } }; + + // @TODO: implement onclose + // websocket.onclose = () => { + // console.log('WebSocket closed. Attempting to reconnect...'); + // setTimeout(() => { + // const baseUrl = websocket.url.split(':').slice(0, 2).join(':'); + // const shardSuffix = this._getOption('usePathing') ? `/${fromShard(shard, 'nickname')}` : `:${port}`; + // const newWebSocket = this.createWebSocket(baseUrl, shardSuffix); + // this.initWebSocket(newWebSocket, shard, port); + // this.#websockets.push(newWebSocket); + // this._urlMap.set(shard, newWebSocket); + // }, 500); // Reconnect after 5 seconds + // }; + websocket.onmessage = (message: { data: string }) => { this._processMessage(message.data); }; @@ -139,6 +154,12 @@ export class WebSocketProvider extends SocketProvider { } } + createWebSocket = (baseUrl: string, suffix: string): WebSocketLike => { + const tempWs = new _WebSocket(`${baseUrl}${suffix}`); + return tempWs as WebSocketLike; + // wait 2 minutes + }; + /** * Initialize the URL map with WebSocket connections. * @@ -152,11 +173,6 @@ export class WebSocketProvider extends SocketProvider { this._urlMap.clear(); try { const primeSuffix = this._getOption('usePathing') ? `/${fromShard(Shard.Prime, 'nickname')}` : ':8001'; - const createWebSocket = (baseUrl: string, suffix: string): WebSocketLike => { - const tempWs = new _WebSocket(`${baseUrl}${suffix}`); - return tempWs as WebSocketLike; - // wait 2 minutes - }; const initShardWebSockets = async (baseUrl: string) => { const shards = await this._getRunningLocations(Shard.Prime, true); @@ -168,7 +184,7 @@ export class WebSocketProvider extends SocketProvider { ? `/${fromShard(shardEnum, 'nickname')}` : `:${port}`; const shardUrl = baseUrl.split(':').slice(0, 2).join(':'); - const websocket = createWebSocket(shardUrl, shardSuffix); + const websocket = this.createWebSocket(shardUrl, shardSuffix); this.initWebSocket(websocket, shardEnum); this.#websockets.push(websocket); this._urlMap.set(shardEnum, websocket); @@ -185,7 +201,7 @@ export class WebSocketProvider extends SocketProvider { if (Array.isArray(urls)) { for (const url of urls) { const baseUrl = `${url.split(':')[0]}:${url.split(':')[1]}`; - const primeWebsocket = createWebSocket(baseUrl, primeSuffix); + const primeWebsocket = this.createWebSocket(baseUrl, primeSuffix); this.initWebSocket(primeWebsocket, Shard.Prime); this.#websockets.push(primeWebsocket); this._urlMap.set(Shard.Prime, primeWebsocket); diff --git a/src/providers/provider.ts b/src/providers/provider.ts index 9589fda6..9f2293cb 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -2627,12 +2627,15 @@ export interface EventFilter { nodeLocation?: NodeLocation; } +export type AccessesType = 'block' | 'balance'; + /** * An **AccessesFilter** allows efficiently filtering accesses (state uses) using address. * * @category Providers */ export interface AccessesFilter { + type: AccessesType; address: AddressLike; }