@@ -26,6 +26,7 @@ import {
2626 InputGenerateTransactionPayloadData ,
2727 Network ,
2828 NetworkToNetworkName ,
29+ TransactionWorker ,
2930 TransactionWorkerEventsEnum ,
3031 UserTransactionResponse ,
3132} from "@aptos-labs/ts-sdk" ;
@@ -38,11 +39,13 @@ const aptos = new Aptos(config);
3839async function main ( ) {
3940 const accountsCount = 2 ;
4041 const transactionsCount = 10 ;
41- const totalTransactions = accountsCount * transactionsCount ;
42+
43+ const totalExpectedTransactions = accountsCount * transactionsCount ;
4244
4345 const start = Date . now ( ) / 1000 ; // current time in seconds
4446
45- console . log ( "starting..." ) ;
47+ console . log ( "Starting..." ) ;
48+
4649 // create senders and recipients accounts
4750 const senders : Account [ ] = [ ] ;
4851 const recipients : Account [ ] = [ ] ;
@@ -61,9 +64,9 @@ async function main() {
6164 // fund sender accounts
6265 const funds : Array < Promise < UserTransactionResponse > > = [ ] ;
6366
64- for ( let i = 0 ; i < senders . length ; i += 1 ) {
67+ for ( const sender of senders ) {
6568 funds . push (
66- aptos . fundAccount ( { accountAddress : senders [ i ] . accountAddress . toStringWithoutPrefix ( ) , amount : 10000000000 } ) ,
69+ aptos . fundAccount ( { accountAddress : sender . accountAddress . toStringWithoutPrefix ( ) , amount : 10000000000 } ) ,
6770 ) ;
6871 }
6972
@@ -72,21 +75,19 @@ async function main() {
7275 console . log ( `${ funds . length } sender accounts funded in ${ Date . now ( ) / 1000 - last } seconds` ) ;
7376 last = Date . now ( ) / 1000 ;
7477
75- // read sender accounts
78+ // Read sender accounts to check their balances.
7679 const balances : Array < Promise < AccountData > > = [ ] ;
77- for ( let i = 0 ; i < senders . length ; i += 1 ) {
78- balances . push ( aptos . getAccountInfo ( { accountAddress : senders [ i ] . accountAddress } ) ) ;
80+ for ( const sender of senders ) {
81+ balances . push ( aptos . getAccountInfo ( { accountAddress : sender . accountAddress } ) ) ;
7982 }
8083 await Promise . all ( balances ) ;
8184
8285 console . log ( `${ balances . length } sender account balances checked in ${ Date . now ( ) / 1000 - last } seconds` ) ;
8386 last = Date . now ( ) / 1000 ;
8487
85- // create transactions
88+ // Create transaction payloads.
8689 const payloads : InputGenerateTransactionPayloadData [ ] = [ ] ;
87- // 100 transactions
8890 for ( let j = 0 ; j < transactionsCount ; j += 1 ) {
89- // 5 recipients
9091 for ( let i = 0 ; i < recipients . length ; i += 1 ) {
9192 const txn : InputGenerateTransactionPayloadData = {
9293 function : "0x1::aptos_account::transfer" ,
@@ -96,33 +97,62 @@ async function main() {
9697 }
9798 }
9899
99- console . log ( `sends ${ totalTransactions * senders . length } transactions to ${ aptos . config . network } ....` ) ;
100- // emit batch transactions
101- senders . map ( ( sender ) => aptos . transaction . batch . forSingleAccount ( { sender , data : payloads } ) ) ;
100+ console . log (
101+ `Sending ${ totalExpectedTransactions * senders . length } ( ${ totalExpectedTransactions } transactions per sender) transactions to ${ aptos . config . network } ...` ,
102+ ) ;
102103
103- aptos . transaction . batch . on ( TransactionWorkerEventsEnum . TransactionSent , async ( data ) => {
104- console . log ( "message:" , data . message ) ;
105- console . log ( "transaction hash:" , data . transactionHash ) ;
106- } ) ;
104+ const transactionWorkers : TransactionWorker [ ] = [ ] ;
105+ for ( const sender of senders ) {
106+ // Create a transaction worker for each sender.
107+ const transactionWorker = new TransactionWorker ( config , sender ) ;
108+ transactionWorkers . push ( transactionWorker ) ;
107109
108- aptos . transaction . batch . on ( TransactionWorkerEventsEnum . ExecutionFinish , async ( data ) => {
109- // log event output
110- console . log ( data . message ) ;
111-
112- // verify accounts sequence number
113- const accounts = senders . map ( ( sender ) => aptos . getAccountInfo ( { accountAddress : sender . accountAddress } ) ) ;
114- const accountsData = await Promise . all ( accounts ) ;
115- accountsData . forEach ( ( accountData ) => {
116- console . log (
117- `account sequence number is ${ ( totalTransactions * senders . length ) / 2 } : ${
118- accountData . sequence_number === "20"
119- } `,
120- ) ;
110+ // Register listeners for certain events.
111+ transactionWorker . on ( TransactionWorkerEventsEnum . TransactionSent , async ( data ) => {
112+ console . log ( `Transaction sent. Hash: ${ data . transactionHash } . Message: ${ data . message } ` ) ;
113+ } ) ;
114+ transactionWorker . on ( TransactionWorkerEventsEnum . TransactionExecuted , async ( data ) => {
115+ console . log ( `Transaction executed. Hash: ${ data . transactionHash } . Message: ${ data . message } ` ) ;
121116 } ) ;
122- // worker finished execution, we can now unsubscribe from event listeners
123- aptos . transaction . batch . removeAllListeners ( ) ;
124- process . exit ( 0 ) ;
117+ transactionWorker . on ( TransactionWorkerEventsEnum . TransactionExecutionFailed , async ( data ) => {
118+ console . log ( `Transaction execution failed. Message: ${ data . message } ` ) ;
119+ } ) ;
120+
121+ // Push the payloads to the transaction worker.
122+ transactionWorker . pushMany ( payloads . map ( ( payload ) => [ payload , undefined ] ) ) ;
123+
124+ // Start the transaction worker.
125+ transactionWorker . start ( ) ;
126+ }
127+
128+ // Wait for all transaction workers to finish, up to 45 seconds.
129+ const timeout = 45 * 1000 ;
130+ const startTime = Date . now ( ) ;
131+ await Promise . all (
132+ transactionWorkers . map ( async ( worker ) => {
133+ while ( worker . executedTransactions . length < totalExpectedTransactions ) {
134+ console . debug ( "Waiting for transaction worker to finish..." ) ;
135+
136+ // Check if we've exceeded the timeout
137+ if ( Date . now ( ) - startTime > timeout ) {
138+ console . error ( "Timeout waiting for transaction worker to finish" ) ;
139+ worker . stop ( ) ;
140+ break ;
141+ }
142+
143+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
144+ }
145+ } ) ,
146+ ) ;
147+
148+ // Verify the sequence numbers of the accounts.
149+ const accounts = senders . map ( ( sender ) => aptos . getAccountInfo ( { accountAddress : sender . accountAddress } ) ) ;
150+ const accountsData = await Promise . all ( accounts ) ;
151+ accountsData . forEach ( ( accountData ) => {
152+ console . log ( `Account sequence number is ${ accountData . sequence_number } , it should be ${ totalExpectedTransactions } ` ) ;
125153 } ) ;
154+
155+ process . exit ( 0 ) ;
126156}
127157
128158main ( ) ;
0 commit comments