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 AccountStatuses DataProvider #2036

Draft
wants to merge 9 commits into
base: jribbink/events-topic
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"./packages/*"
],
"scripts": {
"build": "lerna run build",
"build": "lerna run build --stream",
"start": "npm run build && lerna run start --parallel",
"test": "jest",
"release": "npm run build && npm run changeset publish",
Expand Down
104 changes: 104 additions & 0 deletions packages/transport-http/src/subscribe/handlers/account-statuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {SdkTransport} from "@onflow/typedefs"
import {createSubscriptionHandler} from "./types"
import {EventsArgsDto} from "./events"

type AccountStatusesArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES>

type AccountStatusesData =
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES>

type AccountStatusesArgsDto = EventsArgsDto

type AccountStatusesDataDto = {
// TODO: We do not know the data model types yet
account_status: {
block_id: string
height: number
account_events: {
block_id: string
block_height: number
block_timestamp: string
type: string
transaction_id: string
transaction_index: number
event_index: number
payload: string
}[]
}
}

export const accountStatusesHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES
Args: SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES>
Data: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES>
ArgsDto: AccountStatusesArgsDto
DataDto: AccountStatusesDataDto
}>({
topic: SdkTransport.SubscriptionTopic.ACCOUNT_STATUSES,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: AccountStatusesArgs = {
...initialArgs,
}

return {
onData(data: AccountStatusesDataDto) {
// Parse the raw data
const parsedData: AccountStatusesData = {
accountStatus: {
blockId: data.account_status.block_id,
height: data.account_status.height,
accountEvents: data.account_status.account_events.map(event => ({
blockId: event.block_id,
blockHeight: event.block_height,
blockTimestamp: event.block_timestamp,
type: event.type,
transactionId: event.transaction_id,
transactionIndex: event.transaction_index,
eventIndex: event.event_index,
payload: event.payload,
})),
},
}

// Update the resume args
resumeArgs = {
...resumeArgs,
startBlockHeight: data.account_status.height + 1,
startBlockId: undefined,
}

onData(parsedData)
},
onError(error: Error) {
onError(error)
},
argsToDto(args: AccountStatusesArgs) {
let encodedArgs: AccountStatusesArgsDto = {
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
},
}
},
})
2 changes: 2 additions & 0 deletions packages/transport-http/src/subscribe/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {SubscriptionManager} from "./subscription-manager"
import {blocksHandler} from "./handlers/blocks"
import {blockDigestsHandler} from "./handlers/block-digests"
import {eventsHandler} from "./handlers/events"
import {accountStatusesHandler} from "./handlers/account-statuses"

const SUBSCRIPTION_HANDLERS = [
blocksHandler,
blockDigestsHandler,
eventsHandler,
accountStatusesHandler,
]

// Map of SubscriptionManager instances by access node URL
Expand Down
1 change: 1 addition & 0 deletions packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ export type NodeVersionInfo = {
*/
nodeRootBlockHeight: number
}

export interface StreamConnection<ChannelMap extends {[name: string]: any}> {
on<C extends keyof ChannelMap>(
channel: C,
Expand Down
24 changes: 24 additions & 0 deletions packages/typedefs/src/sdk-transport/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ export type SubscriptionSchema = {
blockDigest: BlockDigest
}
>
[SubscriptionTopic.ACCOUNT_STATUSES]: SchemaItem<
(
| {
startBlockId: string
}
| {
startBlockHeight: number
}
| {}
) & {
filter: EventFilter
},
{
// TODO: We do not know the data model types yet
accountStatus: {
blockId: string
height: number
accountEvents: (Omit<Event, "data"> & {
payload: string
})[]
}
}
>
[SubscriptionTopic.EVENTS]: SchemaItem<
// TODO: We do not know the data model types yet
(
Expand All @@ -37,6 +60,7 @@ export type SubscriptionSchema = {
export enum SubscriptionTopic {
BLOCKS = "blocks",
BLOCK_DIGESTS = "block_digests",
ACCOUNT_STATUSES = "account_statuses",
EVENTS = "events",
}

Expand Down
Loading