Skip to content

Commit

Permalink
events as enum
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Jul 20, 2023
1 parent 8e2425a commit 19d1543
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
* Read more about it here {@link https://aptos.dev/guides/transaction-management}
*/

import { AptosAccount, BCS, TxnBuilderTypes, TransactionWorker, FaucetClient, Provider, Types } from "aptos";
import {
AptosAccount,
BCS,
TxnBuilderTypes,
TransactionWorker,
TransactionWorkerEvents,
FaucetClient,
Provider,
Types,
} from "aptos";
import { exit } from "process";
import { NODE_URL, FAUCET_URL } from "./common";

Expand Down Expand Up @@ -111,14 +120,14 @@ async function main() {
* on a success event, is the hash value of the processed transaction
* on a failure event, is the reason for the failure
*/
transactionWorker.on("transactionSent", async (data) => {
transactionWorker.on(TransactionWorkerEvents.TransactionSent, async (data) => {
// all expected transactions have been sent
if (data[0] === totalTransactions) {
console.log(`transactions sent in ${Date.now() / 1000 - last} seconds`);
}
});

transactionWorker.on("sentFailed", async (data) => {
transactionWorker.on(TransactionWorkerEvents.SentFailed, async (data) => {
/**
* transaction sent failed, up to the user to decide next steps.
* whether to stop the worker by transactionWorker.stop() and handle
Expand All @@ -129,15 +138,15 @@ async function main() {
console.log("sentFailed", data);
});

transactionWorker.on("transactionExecuted", async (data) => {
transactionWorker.on(TransactionWorkerEvents.TransactionExecuted, async (data) => {
// all expected transactions have been executed
if (data[0] === totalTransactions) {
console.log(`transactions executed in ${Date.now() / 1000 - last} seconds`);
await checkAccounts();
}
});

transactionWorker.on("executionFailed", async (data) => {
transactionWorker.on(TransactionWorkerEvents.ExecutionFailed, async (data) => {
/**
* transaction execution failed, up to the user to decide next steps.
* whether to stop the worker by transactionWorker.stop() and handle
Expand Down
1 change: 1 addition & 0 deletions ecosystem/typescript/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
],
"dependencies": {
"@aptos-labs/aptos-client": "^0.0.2",
"eventemitter3": "^5.0.1",
"@noble/hashes": "1.1.3",
"@scure/bip39": "1.1.0",
"form-data": "4.0.0",
Expand Down
17 changes: 14 additions & 3 deletions ecosystem/typescript/sdk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions ecosystem/typescript/sdk/src/transactions/transaction_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import { AsyncQueue, AsyncQueueCancelledError } from "./async_queue";

const promiseFulfilledStatus = "fulfilled";

// Events
const transactionSent = "transactionSent";
const sentFailed = "sentFailed";
export enum TransactionWorkerEvents {
TransactionSent = "transactionSent",
SentFailed = "sentFailed",
TransactionExecuted = "transactionExecuted",
ExecutionFailed = "executionFailed",
}

const transactionExecuted = "transactionExecuted";
const executionFailed = "executionFailed";
export class TransactionWorker extends EventEmitter {
export class TransactionWorker extends EventEmitter<TransactionWorkerEvents> {
readonly provider: Provider;

readonly account: AptosAccount;
Expand Down Expand Up @@ -139,13 +140,16 @@ export class TransactionWorker extends EventEmitter {
if (sentTransaction.status === promiseFulfilledStatus) {
// transaction sent to chain
this.sentTransactions.push([sentTransaction.value.hash, sequenceNumber, null]);
this.emit(transactionSent, [this.sentTransactions.length, sentTransaction.value.hash]);
this.emit(TransactionWorkerEvents.TransactionSent, [
this.sentTransactions.length,
sentTransaction.value.hash,
]);
// check sent transaction execution
await this.checkTransaction(sentTransaction, sequenceNumber);
} else {
// send transaction failed
this.sentTransactions.push([sentTransaction.status, sequenceNumber, sentTransaction.reason]);
this.emit(sentFailed, [this.sentTransactions.length, sentTransaction.reason]);
this.emit(TransactionWorkerEvents.SentFailed, [this.sentTransactions.length, sentTransaction.reason]);
}
}
}
Expand All @@ -172,11 +176,17 @@ export class TransactionWorker extends EventEmitter {
if (executedTransaction.status === promiseFulfilledStatus) {
// transaction executed to chain
this.executedTransactions.push([executedTransaction.value.hash, sequenceNumber, null]);
this.emit(transactionExecuted, [this.executedTransactions.length, executedTransaction.value.hash]);
this.emit(TransactionWorkerEvents.TransactionExecuted, [
this.executedTransactions.length,
executedTransaction.value.hash,
]);
} else {
// transaction execution failed
this.executedTransactions.push([executedTransaction.status, sequenceNumber, executedTransaction.reason]);
this.emit(executionFailed, [this.executedTransactions.length, executedTransaction.reason]);
this.emit(TransactionWorkerEvents.ExecutionFailed, [
this.executedTransactions.length,
executedTransaction.reason,
]);
}
}
}
Expand Down

0 comments on commit 19d1543

Please sign in to comment.