From 34493ca3c06229e276be7214d8b19fba5085ae1b Mon Sep 17 00:00:00 2001 From: maayan Date: Fri, 19 Jan 2024 14:16:14 -0500 Subject: [PATCH] address comments --- examples/typescript/batch_funds.ts | 5 ++- examples/typescript/batch_mint.ts | 6 ++- .../management/transactionWorker.ts | 37 ++++++++++++------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/examples/typescript/batch_funds.ts b/examples/typescript/batch_funds.ts index 8e565f0f1..103d069f5 100644 --- a/examples/typescript/batch_funds.ts +++ b/examples/typescript/batch_funds.ts @@ -30,7 +30,7 @@ import { UserTransactionResponse, } from "@aptos-labs/ts-sdk"; -const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.DEVNET; +const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.LOCAL; const config = new AptosConfig({ network: APTOS_NETWORK }); const aptos = new Aptos(config); @@ -119,7 +119,8 @@ async function main() { }`, ); }); - + // worker finished execution, we can now unsubscribe from event listeners + aptos.transaction.batch.removeAllListeners(); process.exit(0); }); } diff --git a/examples/typescript/batch_mint.ts b/examples/typescript/batch_mint.ts index 538b9111b..3fc60df2b 100644 --- a/examples/typescript/batch_mint.ts +++ b/examples/typescript/batch_mint.ts @@ -25,8 +25,8 @@ import { InputGenerateTransactionPayloadData, Network, NetworkToNetworkName, - TransactionWorkerEvents, UserTransactionResponse, + TransactionWorkerEventsEnum, } from "@aptos-labs/ts-sdk"; const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.DEVNET; @@ -80,7 +80,7 @@ async function main() { // batch mint token transactions aptos.transaction.batch.forSingleAccount({ sender, data: payloads }); - aptos.transaction.batch.on(TransactionWorkerEvents.ExecutionFinish, async (data: any) => { + aptos.transaction.batch.on(TransactionWorkerEventsEnum.ExecutionFinish, async (data) => { // log event output console.log(data); @@ -88,6 +88,8 @@ async function main() { const account = await aptos.getAccountInfo({ accountAddress: sender.accountAddress }); console.log(`account sequence number is 101: ${account.sequence_number === "101"}`); + // worker finished execution, we can now unsubscribe from event listeners + aptos.transaction.batch.removeAllListeners(); process.exit(0); }); } diff --git a/src/transactions/management/transactionWorker.ts b/src/transactions/management/transactionWorker.ts index 45c750361..d636e9b41 100644 --- a/src/transactions/management/transactionWorker.ts +++ b/src/transactions/management/transactionWorker.ts @@ -1,18 +1,5 @@ /* eslint-disable no-await-in-loop */ -/** - * TransactionWorker provides a simple framework for receiving payloads to be processed. - * - * Once one `start()` the process and pushes a new transaction, the worker acquires - * the current account's next sequence number (by using the AccountSequenceNumber class), - * generates a signed transaction and pushes an async submission process into the `outstandingTransactions` queue. - * At the same time, the worker processes transactions by reading the `outstandingTransactions` queue - * and submits the next transaction to chain, it - * 1) waits for resolution of the submission process or get pre-execution validation error - * and 2) waits for the resolution of the execution process or get an execution error. - * The worker fires events for any submission and/or execution success and/or failure. - */ - import EventEmitter from "eventemitter3"; import { AptosConfig } from "../../api/aptosConfig"; import { Account } from "../../core"; @@ -25,14 +12,22 @@ import { AsyncQueue, AsyncQueueCancelledError } from "./asyncQueue"; export const promiseFulfilledStatus = "fulfilled"; +// Event types the worker fires during execution and +// the dapp can listen to export enum TransactionWorkerEventsEnum { + // fired after a transaction gets sent to the chain TransactionSent = "transactionSent", + // fired if there is an error sending the transaction to the chain TransactionSendFailed = "transactionSendFailed", + // fired when a single transaction has executed successfully TransactionExecuted = "transactionExecuted", + // fired if a single transaction fails in execution TransactionExecutionFailed = "transactionExecutionFailed", + // fired when the worker has finished its job / when the queue has been emptied ExecutionFinish = "executionFinish", } +// Typed interface of the worker events export interface TransactionWorkerEvents { transactionSent: (data: SuccessEventData) => void; transactionSendFailed: (data: FailureEventData) => void; @@ -41,19 +36,35 @@ export interface TransactionWorkerEvents { executionFinish: (data: ExecutionFinishEventData) => void; } +// Type for when the worker has finished its job export type ExecutionFinishEventData = { message: string; }; +// Type for a success event export type SuccessEventData = { message: string; transactionHash: string; }; +// Type for a failure event export type FailureEventData = { message: string; error: string; }; + +/** + * TransactionWorker provides a simple framework for receiving payloads to be processed. + * + * Once one `start()` the process and pushes a new transaction, the worker acquires + * the current account's next sequence number (by using the AccountSequenceNumber class), + * generates a signed transaction and pushes an async submission process into the `outstandingTransactions` queue. + * At the same time, the worker processes transactions by reading the `outstandingTransactions` queue + * and submits the next transaction to chain, it + * 1) waits for resolution of the submission process or get pre-execution validation error + * and 2) waits for the resolution of the execution process or get an execution error. + * The worker fires events for any submission and/or execution success and/or failure. + */ export class TransactionWorker extends EventEmitter { readonly aptosConfig: AptosConfig;