-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
packages/transport-http/src/subscribe/handlers/block-headers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {SdkTransport} from "@onflow/typedefs" | ||
import {BlockArgsModel, createSubscriptionHandler} from "./types" | ||
|
||
type BlockHeaderArgs = | ||
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCK_HEADERS> | ||
|
||
type BlockHeaderData = | ||
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCK_HEADERS> | ||
|
||
type BlockHeaderArgsModel = BlockArgsModel | ||
|
||
type BlockHeaderDataModel = { | ||
// TODO: We do not know the data model types yet | ||
header: { | ||
id: string | ||
height: number | ||
timestamp: string | ||
chain_id: string | ||
parent_id: string | ||
collection_guarantees: { | ||
collection_id: string | ||
signer_ids: string[] | ||
}[] | ||
block_seals: { | ||
block_id: string | ||
result_id: string | ||
}[] | ||
} | ||
} | ||
|
||
export const blockHeaderHandler = createSubscriptionHandler<{ | ||
Topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS | ||
Args: BlockHeaderArgs | ||
Data: BlockHeaderData | ||
ArgsModel: BlockHeaderArgsModel | ||
DataModel: BlockHeaderDataModel | ||
}>({ | ||
topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS, | ||
createSubscriber: (initialArgs, onData, onError) => { | ||
let resumeArgs: BlockHeaderArgs = { | ||
...initialArgs, | ||
} | ||
|
||
return { | ||
sendData(data: BlockHeaderDataModel) { | ||
// Parse the raw data | ||
const parsedData: BlockHeaderData = { | ||
blockHeader: { | ||
id: data.header.id, | ||
height: data.header.height, | ||
timestamp: data.header.timestamp, | ||
chainId: data.header.chain_id, | ||
}, | ||
} | ||
|
||
// Update the resume args | ||
resumeArgs = { | ||
blockStatus: resumeArgs.blockStatus, | ||
startBlockHeight: data.header.height + 1, | ||
} | ||
|
||
onData(parsedData) | ||
}, | ||
sendError(error: Error) { | ||
onError(error) | ||
}, | ||
encodeArgs(args: BlockHeaderArgs) { | ||
let encodedArgs: BlockHeaderArgsModel = { | ||
block_status: args.blockStatus, | ||
} | ||
|
||
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 | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters