Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKG -- [transport-http] Add Event DataProvider #2037

Draft
wants to merge 6 commits into
base: jribbink/block-digests
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions packages/transport-http/src/subscribe/handlers/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {SdkTransport} from "@onflow/typedefs"
import {createSubscriptionHandler} from "./types"

type EventsArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.EVENTS>

type EventsData =
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.EVENTS>

export type EventsArgsDto = (
| {
start_block_id: string
}
| {
start_block_height: number
}
| {}
) & {
event_types?: string[]
addresses?: string[]
contracts?: string[]
}

type EventsDataDto = {
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
ArgsDto: EventsArgsDto
DataDto: EventsDataDto
}>({
topic: SdkTransport.SubscriptionTopic.EVENTS,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: EventsArgs = {
...initialArgs,
}

return {
onData(rawData: EventsDataDto) {
// 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)
},
onError(error: Error) {
onError(error)
},
argsToDto(args: EventsArgs) {
let encodedArgs: EventsArgsDto = {
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
},
}
},
})
7 changes: 6 additions & 1 deletion packages/transport-http/src/subscribe/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import {SdkTransport} from "@onflow/typedefs"
import {SubscriptionManager} from "./subscription-manager"
import {blocksHandler} from "./handlers/blocks"
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<
Expand Down
22 changes: 21 additions & 1 deletion packages/typedefs/src/sdk-transport/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Block, BlockDigest} from ".."
import {Block, BlockDigest, Event, EventFilter} from ".."

export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
Expand All @@ -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<Event, "data"> & {
payload: string
}
}
>
}

export enum SubscriptionTopic {
BLOCKS = "blocks",
BLOCK_DIGESTS = "block_digests",
EVENTS = "events",
}

type BlockArgs =
Expand Down
Loading