From b80a0787907f70a5214cc45e84c166bdb2a61f2b Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 29 Nov 2024 02:37:00 -0800 Subject: [PATCH 1/3] PKG -- [transport-http] Add Event DataProvider --- .../src/subscribe/handlers/events.ts | 104 ++++++++++++++++++ .../transport-http/src/subscribe/subscribe.ts | 9 +- .../src/sdk-transport/subscriptions.ts | 22 +++- 3 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 packages/transport-http/src/subscribe/handlers/events.ts diff --git a/packages/transport-http/src/subscribe/handlers/events.ts b/packages/transport-http/src/subscribe/handlers/events.ts new file mode 100644 index 000000000..fd0c96f90 --- /dev/null +++ b/packages/transport-http/src/subscribe/handlers/events.ts @@ -0,0 +1,104 @@ +import {SdkTransport} from "@onflow/typedefs" +import {createSubscriptionHandler} from "./types" + +type EventsArgs = + SdkTransport.SubscriptionArguments + +type EventsData = + SdkTransport.SubscriptionData + +type EventsArgsModel = ( + | { + start_block_id: string + } + | { + start_block_height: number + } + | {} +) & { + event_types?: string[] + addresses?: string[] + contracts?: string[] +} + +type EventsDataModel = { + block_id: string + block_height: number + block_timestamp: string + type: string + transaction_id: string + transaction_index: number + event_index: number + payload: string +} + +export const eventsHandler = createSubscriptionHandler<{ + Topic: SdkTransport.SubscriptionTopic.EVENTS + Args: EventsArgs + Data: EventsData + ArgsModel: EventsArgsModel + DataModel: EventsDataModel +}>({ + topic: SdkTransport.SubscriptionTopic.EVENTS, + createSubscriber: (initialArgs, onData, onError) => { + let resumeArgs: EventsArgs = { + ...initialArgs, + } + + return { + sendData(rawData: EventsDataModel) { + // Parse the raw data + const result: EventsData = { + event: { + blockId: rawData.block_id, + blockHeight: rawData.block_height, + blockTimestamp: rawData.block_timestamp, + type: rawData.type, + transactionId: rawData.transaction_id, + transactionIndex: rawData.transaction_index, + eventIndex: rawData.event_index, + payload: rawData.payload, + }, + } + + // Update the resume args + resumeArgs = { + ...resumeArgs, + startBlockHeight: result.event.blockHeight + 1, + startBlockId: undefined, + } + + onData(result) + }, + sendError(error: Error) { + onError(error) + }, + encodeArgs(args: EventsArgs) { + let encodedArgs: EventsArgsModel = { + event_types: args.filter?.eventTypes, + addresses: args.filter?.addresses, + contracts: args.filter?.contracts, + } + + if ("startBlockHeight" in args) { + return { + ...encodedArgs, + start_block_height: args.startBlockHeight, + } + } + + if ("startBlockId" in args) { + return { + ...encodedArgs, + start_block_id: args.startBlockId, + } + } + + return encodedArgs + }, + get connectionArgs() { + return resumeArgs + }, + } + }, +}) diff --git a/packages/transport-http/src/subscribe/subscribe.ts b/packages/transport-http/src/subscribe/subscribe.ts index 49d0a71c5..d88573eeb 100644 --- a/packages/transport-http/src/subscribe/subscribe.ts +++ b/packages/transport-http/src/subscribe/subscribe.ts @@ -1,9 +1,14 @@ import {SdkTransport} from "@onflow/typedefs" import {SubscriptionManager} from "./subscription-manager" import {blocksHandler} from "./handlers/blocks" -import {blockDigestsHandler} from "./handlers/block_digests" +import {blockDigestsHandler} from "./handlers/block-digests" +import {eventsHandler} from "./handlers/events" -const SUBSCRIPTION_HANDLERS = [blocksHandler, blockDigestsHandler] +const SUBSCRIPTION_HANDLERS = [ + blocksHandler, + blockDigestsHandler, + eventsHandler, +] // Map of SubscriptionManager instances by access node URL let subscriptionManagerMap: Map< diff --git a/packages/typedefs/src/sdk-transport/subscriptions.ts b/packages/typedefs/src/sdk-transport/subscriptions.ts index b0ecf2f15..2530c30ba 100644 --- a/packages/typedefs/src/sdk-transport/subscriptions.ts +++ b/packages/typedefs/src/sdk-transport/subscriptions.ts @@ -1,4 +1,4 @@ -import {Block, BlockDigest} from ".." +import {Block, BlockDigest, Event, EventFilter} from ".." export type SubscriptionSchema = { [SubscriptionTopic.BLOCKS]: SchemaItem< @@ -13,11 +13,31 @@ export type SubscriptionSchema = { blockDigest: BlockDigest } > + [SubscriptionTopic.EVENTS]: SchemaItem< + // TODO: We do not know the data model types yet + ( + | { + startBlockId: string + } + | { + startBlockHeight: number + } + | {} + ) & { + filter: EventFilter + }, + { + event: Omit & { + payload: string + } + } + > } export enum SubscriptionTopic { BLOCKS = "blocks", BLOCK_DIGESTS = "block_digests", + EVENTS = "events", } type BlockArgs = From 3bcf43d96520d6e6139ee1abe0fcc4bd2451d50e Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 29 Nov 2024 06:47:35 -0800 Subject: [PATCH 2/3] update model --- packages/transport-http/src/subscribe/handlers/events.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transport-http/src/subscribe/handlers/events.ts b/packages/transport-http/src/subscribe/handlers/events.ts index fd0c96f90..b3e25a2a9 100644 --- a/packages/transport-http/src/subscribe/handlers/events.ts +++ b/packages/transport-http/src/subscribe/handlers/events.ts @@ -7,7 +7,7 @@ type EventsArgs = type EventsData = SdkTransport.SubscriptionData -type EventsArgsModel = ( +export type EventsArgsModel = ( | { start_block_id: string } From 29723038d380a60a1064abc5823508abe0f4b82a Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Mon, 2 Dec 2024 15:37:58 -0800 Subject: [PATCH 3/3] fix build --- .../src/subscribe/handlers/events.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/transport-http/src/subscribe/handlers/events.ts b/packages/transport-http/src/subscribe/handlers/events.ts index b3e25a2a9..ca91be04f 100644 --- a/packages/transport-http/src/subscribe/handlers/events.ts +++ b/packages/transport-http/src/subscribe/handlers/events.ts @@ -7,7 +7,7 @@ type EventsArgs = type EventsData = SdkTransport.SubscriptionData -export type EventsArgsModel = ( +export type EventsArgsDto = ( | { start_block_id: string } @@ -21,7 +21,7 @@ export type EventsArgsModel = ( contracts?: string[] } -type EventsDataModel = { +type EventsDataDto = { block_id: string block_height: number block_timestamp: string @@ -36,8 +36,8 @@ export const eventsHandler = createSubscriptionHandler<{ Topic: SdkTransport.SubscriptionTopic.EVENTS Args: EventsArgs Data: EventsData - ArgsModel: EventsArgsModel - DataModel: EventsDataModel + ArgsDto: EventsArgsDto + DataDto: EventsDataDto }>({ topic: SdkTransport.SubscriptionTopic.EVENTS, createSubscriber: (initialArgs, onData, onError) => { @@ -46,7 +46,7 @@ export const eventsHandler = createSubscriptionHandler<{ } return { - sendData(rawData: EventsDataModel) { + onData(rawData: EventsDataDto) { // Parse the raw data const result: EventsData = { event: { @@ -70,11 +70,11 @@ export const eventsHandler = createSubscriptionHandler<{ onData(result) }, - sendError(error: Error) { + onError(error: Error) { onError(error) }, - encodeArgs(args: EventsArgs) { - let encodedArgs: EventsArgsModel = { + argsToDto(args: EventsArgs) { + let encodedArgs: EventsArgsDto = { event_types: args.filter?.eventTypes, addresses: args.filter?.addresses, contracts: args.filter?.contracts,