diff --git a/examples/typescript-esm/transaction_with_predefined_abi.ts b/examples/typescript-esm/transaction_with_predefined_abi.ts index 981560e72..d74b8a5f4 100644 --- a/examples/typescript-esm/transaction_with_predefined_abi.ts +++ b/examples/typescript-esm/transaction_with_predefined_abi.ts @@ -14,6 +14,7 @@ import { Network, NetworkToNetworkName, parseTypeTag, + SimpleTransaction, TypeTagAddress, TypeTagU64, U64, @@ -48,13 +49,23 @@ const balance = async (aptos: Aptos, name: string, address: AccountAddress) => { return amount; }; -async function timeSubmission(aptos: Aptos, submit: () => Promise): Promise<[number, number]> { +async function timeSubmission( + aptos: Aptos, + signer: Account, + buildTxn: () => Promise, +): Promise { const start = Date.now(); - const hash = await submit(); + const rawTxn = await buildTxn(); + const buildTime = Date.now(); + const submittedTxn = await aptos.signAndSubmitTransaction({ signer, transaction: rawTxn }); const submitTime = Date.now(); - await aptos.waitForTransaction({ transactionHash: hash }); + await aptos.waitForTransaction({ transactionHash: submittedTxn.hash }); const endTime = Date.now(); - return [submitTime - start, endTime - start]; + const builtLatency = buildTime - start; + const submitLatency = submitTime - start; + const e2eLatency = endTime - start; + + console.log(`Time for building: ${builtLatency}ms | submission: ${submitLatency}ms | total E2E: ${e2eLatency}ms`); } const example = async () => { @@ -103,40 +114,32 @@ const example = async () => { console.log("\n=== Remote ABI, normal inputs ===\n"); const aliceAddressString = alice.accountAddress.toString(); const bobAddressString = alice.accountAddress.toString(); - const [submit1, e2e1] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: aliceAddressString, data: { function: "0x1::coin::transfer", typeArguments: [APTOS_COIN_TYPE], functionArguments: [bobAddressString, TRANSFER_AMOUNT], }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit1}ms | E2E: ${e2e1}ms`); + }), + ); console.log("\n=== Remote ABI, BCS inputs ===\n"); - const [submit2, e2e2] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::coin::transfer", typeArguments: [APTOS_COIN_TYPE], functionArguments: [bob.accountAddress, new U64(TRANSFER_AMOUNT)], }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit2}ms | E2E: ${e2e2}ms`); + }), + ); console.log("\n=== Local ABI, normal inputs ===\n"); - const [submit3, e2e3] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::coin::transfer", @@ -144,16 +147,12 @@ const example = async () => { functionArguments: [bobAddressString, TRANSFER_AMOUNT], abi: transferAbi, }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit3}ms | E2E: ${e2e3}ms`); + }), + ); console.log("\n=== Local ABI, BCS inputs ===\n"); - const [submit4, e2e4] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::coin::transfer", @@ -161,18 +160,14 @@ const example = async () => { functionArguments: [bob.accountAddress, new U64(TRANSFER_AMOUNT)], abi: transferAbi, }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit4}ms | E2E: ${e2e4}ms`); + }), + ); console.log("\n=== Local ABI, BCS inputs, sequence number already cached ===\n"); const accountData = await aptos.account.getAccountInfo({ accountAddress: alice.accountAddress }); const sequenceNumber = BigInt(accountData.sequence_number); - const [submit5, e2e5] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::coin::transfer", @@ -183,16 +178,12 @@ const example = async () => { options: { accountSequenceNumber: sequenceNumber, }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit5}ms | E2E: ${e2e5}ms`); + }), + ); console.log("\n=== Local ABI, BCS inputs, sequence number and gas already cached ===\n"); - const [submit6, e2e6] = await timeSubmission(aptos, async () => { - const txn = await aptos.transaction.build.simple({ + await timeSubmission(aptos, alice, async () => + aptos.transaction.build.simple({ sender: alice.accountAddress, data: { function: "0x1::coin::transfer", @@ -205,12 +196,8 @@ const example = async () => { gasUnitPrice: 100, maxGasAmount: 1000, }, - }); - - const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn }); - return committedTxn.hash; - }); - console.log(`Took for submission: ${submit6}ms | E2E: ${e2e6}ms`); + }), + ); }; example();