diff --git a/ecosystem/typescript/sdk/examples/typescript/transactions_management.ts b/ecosystem/typescript/sdk/examples/typescript/transactions_management.ts index 402fcb23f4391..fa974949602e3 100644 --- a/ecosystem/typescript/sdk/examples/typescript/transactions_management.ts +++ b/ecosystem/typescript/sdk/examples/typescript/transactions_management.ts @@ -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"; @@ -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 @@ -129,7 +138,7 @@ 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`); @@ -137,7 +146,7 @@ async function main() { } }); - 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 diff --git a/ecosystem/typescript/sdk/package.json b/ecosystem/typescript/sdk/package.json index 4104fefd1334d..212510b145934 100644 --- a/ecosystem/typescript/sdk/package.json +++ b/ecosystem/typescript/sdk/package.json @@ -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", diff --git a/ecosystem/typescript/sdk/pnpm-lock.yaml b/ecosystem/typescript/sdk/pnpm-lock.yaml index eaa9cfa4e2dc6..496f63f385002 100644 --- a/ecosystem/typescript/sdk/pnpm-lock.yaml +++ b/ecosystem/typescript/sdk/pnpm-lock.yaml @@ -10,9 +10,6 @@ dependencies: '@scure/bip39': specifier: 1.1.0 version: 1.1.0 - axios: - specifier: 0.27.2 - version: 0.27.2 eventemitter3: specifier: ^5.0.1 version: 5.0.1 @@ -2021,6 +2018,15 @@ packages: '@babel/types': 7.20.0 dev: true + /@types/cacheable-request@6.0.3: + resolution: {integrity: sha512-IQ3EbTzGxIigb1I3qPZc1rWJnH0BmSKv5QYTalEwweFvyBDLSAe24zP0le/hyi7ecGfZVlIVAg4BZqb8WBwKqw==} + dependencies: + '@types/http-cache-semantics': 4.0.1 + '@types/keyv': 3.1.4 + '@types/node': 18.6.2 + '@types/responselike': 1.0.0 + dev: false + /@types/graceful-fs@4.1.5: resolution: {integrity: sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==} dependencies: @@ -5783,6 +5789,11 @@ packages: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} dev: true + /quick-lru@5.1.1: + resolution: {integrity: sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==} + engines: {node: '>=10'} + dev: false + /react-is@18.2.0: resolution: {integrity: sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==} dev: true diff --git a/ecosystem/typescript/sdk/src/transactions/transaction_worker.ts b/ecosystem/typescript/sdk/src/transactions/transaction_worker.ts index 5af326889051d..6acd3e996914e 100644 --- a/ecosystem/typescript/sdk/src/transactions/transaction_worker.ts +++ b/ecosystem/typescript/sdk/src/transactions/transaction_worker.ts @@ -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 { readonly provider: Provider; readonly account: AptosAccount; @@ -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]); } } } @@ -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, + ]); } } }