From 95f7196d33349ed9fc6940782c3861f582f83215 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Mon, 2 Dec 2024 12:58:52 -0800 Subject: [PATCH] stash --- .../handlers/transaction-statuses.ts | 115 ++++++++++++++++++ packages/typedefs/src/index.ts | 4 - .../src/sdk-transport/subscriptions.ts | 24 +++- 3 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 packages/transport-http/src/subscribe/handlers/transaction-statuses.ts diff --git a/packages/transport-http/src/subscribe/handlers/transaction-statuses.ts b/packages/transport-http/src/subscribe/handlers/transaction-statuses.ts new file mode 100644 index 000000000..f7cd75b05 --- /dev/null +++ b/packages/transport-http/src/subscribe/handlers/transaction-statuses.ts @@ -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 + +type TransactionStatusesData = + SdkTransport.SubscriptionData + +type TransactionStatusesArgsModel = { + 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 TransactionStatusesDataModel = { + 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 + Data: SdkTransport.SubscriptionData + ArgsModel: TransactionStatusesArgsModel + DataModel: TransactionStatusesDataModel +}>({ + topic: SdkTransport.SubscriptionTopic.TRANSACTION_STATUSES, + createSubscriber: (initialArgs, onData, onError) => { + let resumeArgs: TransactionStatusesArgs = { + ...initialArgs, + } + + return { + sendData(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) + }, + sendError(error: Error) { + onError(error) + }, + encodeArgs(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 + }, + } + }, +}) diff --git a/packages/typedefs/src/index.ts b/packages/typedefs/src/index.ts index 8a6728d8e..f6de1ba7c 100644 --- a/packages/typedefs/src/index.ts +++ b/packages/typedefs/src/index.ts @@ -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. */ diff --git a/packages/typedefs/src/sdk-transport/subscriptions.ts b/packages/typedefs/src/sdk-transport/subscriptions.ts index b0ecf2f15..4da3fcf95 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, Transaction, TransactionExecutionStatus} from ".." export type SubscriptionSchema = { [SubscriptionTopic.BLOCKS]: SchemaItem< @@ -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 + } + } + > } export enum SubscriptionTopic { BLOCKS = "blocks", BLOCK_DIGESTS = "block_digests", + TRANSACTION_STATUSES = "transaction_statuses", } type BlockArgs = @@ -53,3 +67,11 @@ export type SubscribeFn = ( }, opts: {node: string} ) => Promise + +export type RawEvent = { + type: string + transactionId: string + transactionIndex: number + eventIndex: number + payload: any +}