Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Jan 19, 2024
1 parent f4240f2 commit 34493ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions examples/typescript/batch_funds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -119,7 +119,8 @@ async function main() {
}`,
);
});

// worker finished execution, we can now unsubscribe from event listeners
aptos.transaction.batch.removeAllListeners();
process.exit(0);
});
}
Expand Down
6 changes: 4 additions & 2 deletions examples/typescript/batch_mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,14 +80,16 @@ 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);

// verify account sequence number
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);
});
}
Expand Down
37 changes: 24 additions & 13 deletions src/transactions/management/transactionWorker.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -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<TransactionWorkerEvents> {
readonly aptosConfig: AptosConfig;

Expand Down

0 comments on commit 34493ca

Please sign in to comment.