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 TransactionStatuses DataProvider #2041

Draft
wants to merge 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {SdkTransport, Transaction} from "@onflow/typedefs"
import {createSubscriptionHandler} from "./types"

const STATUS_MAP = {
UNKNOWN: 0,
PENDING: 1,
FINALIZED: 2,
EXECUTED: 3,
SEALED: 4,
EXPIRED: 5,
}

type TransactionStatusesArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES>

type TransactionStatusesData =
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES>

type TransactionStatusesArgsDto = {
envelope_signatures: string[]
payload_signatures: string[]
authorizers: string[]
payer: string
proposer: string
key_id: number
gas_limit: number
proposal_key: {
key_id: number
sequence_number: number
}
cadence: string
reference_block_id: string
}

type TransactionStatusesDataDto = {
transaction_status: {
block_id: string
block_height: number
transaction_id: string
status: string
timestamp: string
events: {
type: string
transaction_id: string
transaction_index: string
event_index: string
payload: string
}[]
}
}

export const blockDigestsHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES
Args: SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES>
Data: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES>
ArgsDto: TransactionStatusesArgsDto
DataDto: TransactionStatusesDataDto
}>({
topic: SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: TransactionStatusesArgs = {
...initialArgs,
}

return {
onData(data: TransactionStatusesDataModel) {
// Parse the raw data
const parsedData: TransactionStatusesData = {
transactionStatus: {
blockId: data.transaction_status.block_id,
status:
STATUS_MAP[data.transaction_status.status.toUpperCase()] || "",
statusString: data.transaction_status.status.toUpperCase(),
statusCode: data.transaction_status.status_code,
errorMessage: data.transaction_status.error_message,
events: data.transaction_status.events.map(event => ({
type: event.type,
transactionId: event.transaction_id,
transactionIndex: Number(event.transaction_index),
eventIndex: Number(event.event_index),
payload: JSON.parse(
context.Buffer.from(event.payload, "base64").toString()
),
})),
},
}

onData(parsedData)
},
onError(error: Error) {
onError(error)
},
argsToDto(args: TransactionStatusesArgs) {
return {
envelope_signatures: args.envelopeSignatures,
payload_signatures: args.payloadSignatures,
authorizers: args.authorizers,
payer: args.payer,
proposer: args.proposer,
key_id: args.keyId,
gas_limit: args.gasLimit,
proposal_key: {
key_id: args.proposalKey.keyId,
sequence_number: args.proposalKey.sequenceNumber,
},
cadence: args.script,
reference_block_id: args.referenceBlockId,
}
},
get connectionArgs() {
return resumeArgs
},
}
},
})
4 changes: 0 additions & 4 deletions packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,6 @@ export type Transaction = {
* - The ID of the key in the account used by the proposer of this transaction.
*/
keyId: number
/**
* - The address of the proposer of this transaction.
*/
address: string
/**
* - Address of the payer of the transaction.
*/
Expand Down
24 changes: 23 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, Transaction, TransactionExecutionStatus} from ".."

export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
Expand All @@ -13,11 +13,25 @@ export type SubscriptionSchema = {
blockDigest: BlockDigest
}
>
[SubscriptionTopic.TRANSACTION_STATUSES]: SchemaItem<
Transaction,
{
transactionStatus: {
blockId: string
status: TransactionExecutionStatus
statusString: string
statusCode: 0 | 1
errorMessage: string
events: Array<RawEvent>
}
}
>
}

export enum SubscriptionTopic {
BLOCKS = "blocks",
BLOCK_DIGESTS = "block_digests",
TRANSACTION_STATUSES = "transaction_statuses",
}

type BlockArgs =
Expand Down Expand Up @@ -53,3 +67,11 @@ export type SubscribeFn = <T extends SubscriptionTopic>(
},
opts: {node: string}
) => Promise<Subscription>

export type RawEvent = {
type: string
transactionId: string
transactionIndex: number
eventIndex: number
payload: any
}