-
Notifications
You must be signed in to change notification settings - Fork 2
feat(txm): added txm callbacks and promises when tx status change #719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GabrielMartinezRodriguez
merged 4 commits into
master
from
gabriel/txm-transaction-callbacks
May 21, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e3a8937
feat(txm): added txm callbacks and promises when tx status change
GabrielMartinezRodriguez a61e32b
feat(txm): implemented timeout mechanism for waitForFinalization
GabrielMartinezRodriguez 40a224e
chore: format
GabrielMartinezRodriguez e5b67a8
chore(txm): pr review
GabrielMartinezRodriguez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,8 +109,8 @@ async function getCurrentBlock(): Promise<Block> { | |
| }) | ||
| } | ||
|
|
||
| async function createCounterTransaction(deadline?: number): Promise<Transaction> { | ||
| return await txm.createTransaction({ | ||
| function createCounterTransaction(deadline?: number): Transaction { | ||
| return txm.createTransaction({ | ||
| address: deployment.HappyCounter, | ||
| functionName: "increment", | ||
| contractName: "HappyCounter", | ||
|
|
@@ -195,7 +195,7 @@ test("NewBlock hook works correctly", async () => { | |
| test("onTransactionStatusChanged hook works correctly", async () => { | ||
| let hookTriggered = false | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -225,7 +225,7 @@ test("TransactionSubmissionFailed hook works correctly", async () => { | |
|
|
||
| proxyServer.addBehavior(ProxyBehavior.Fail) | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -269,7 +269,7 @@ test("TransactionSaveFailed hook works correctly", async () => { | |
|
|
||
| methodSpy.mockResolvedValue(err(new Error("Test error"))) | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -288,7 +288,7 @@ test("TransactionSaveFailed hook works correctly", async () => { | |
| test("Simple transaction executed", async () => { | ||
| const previousCount = await getCurrentCounterValue() | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -329,7 +329,7 @@ test("A transaction created using calldata is executed correctly", async () => { | |
| args: [], | ||
| }) | ||
|
|
||
| const transaction = await txm.createTransaction({ | ||
| const transaction = txm.createTransaction({ | ||
| address: deployment.HappyCounter, | ||
| calldata, | ||
| }) | ||
|
|
@@ -364,7 +364,7 @@ test("A transaction created using calldata is executed correctly", async () => { | |
| test("Transaction retried", async () => { | ||
| const previousCount = await getCurrentCounterValue() | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| proxyServer.addBehavior(ProxyBehavior.NotAnswer) | ||
|
|
||
|
|
@@ -424,7 +424,7 @@ test("Transaction retried", async () => { | |
| test("Transaction failed", async () => { | ||
| const previousCount = await getCurrentCounterValue() | ||
|
|
||
| const transaction = await txm.createTransaction({ | ||
| const transaction = txm.createTransaction({ | ||
| address: deployment.MockRevert, | ||
| functionName: "intentionalRevert", | ||
| contractName: "MockRevert", | ||
|
|
@@ -477,7 +477,7 @@ test("Transaction failed", async () => { | |
| test("Transaction failed for out of gas", async () => { | ||
| const previousCount = await getCurrentCounterValue() | ||
|
|
||
| const transaction = await txm.createTransaction({ | ||
| const transaction = txm.createTransaction({ | ||
| address: deployment.MockRevert, | ||
| functionName: "intentionalRevertDueToGasLimit", | ||
| contractName: "MockRevert", | ||
|
|
@@ -521,6 +521,59 @@ test("Transaction failed for out of gas", async () => { | |
| expect(transactionReverted.collectionBlock).toBe(previousBlock.number! + 1n) | ||
| }) | ||
|
|
||
| test("Use transaction.waitForFinalization() to wait for a transaction to be finalized", async () => { | ||
| let promiseResolved = false | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transaction.waitForFinalization().then((result) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a timeout test to check that that functionality works as intended? |
||
| if (result.isErr()) { | ||
| throw result.error | ||
| } | ||
| promiseResolved = true | ||
| expect(result.value.status).toBe(TransactionStatus.Success) | ||
| }) | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
| await mineBlock(2) | ||
|
|
||
| expect(promiseResolved).toBe(true) | ||
| }) | ||
|
|
||
| test("Use transaction.waitForFinalization() to wait for a transaction to be finalized with a timeout", async () => { | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| const timeStart = Date.now() | ||
| const timeout = 1000 | ||
| const result = await transaction.waitForFinalization(timeout) | ||
| const timeEnd = Date.now() | ||
|
|
||
| expect(result.isErr()).toBe(true) | ||
| expect(timeEnd - timeStart).toBeLessThan(timeout + 250) | ||
| }) | ||
|
|
||
| test("Use transaction.on() to register a callback which is triggered when the transaction changes to a specific status", async () => { | ||
| let promiseResolved = false | ||
|
|
||
| const transaction = txm.createTransaction({ | ||
| address: deployment.MockRevert, | ||
| functionName: "intentionalRevert", | ||
| contractName: "MockRevert", | ||
| args: [], | ||
| }) | ||
|
|
||
| transaction.on(TransactionStatus.Failed, (transaction) => { | ||
| promiseResolved = true | ||
| expect(transaction.status).toBe(TransactionStatus.Failed) | ||
| }) | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
| await mineBlock(2) | ||
|
|
||
| expect(promiseResolved).toBe(true) | ||
| }) | ||
|
|
||
| test("Transaction cancelled due to deadline passing", async () => { | ||
| const previousLivenessThreshold = txm.livenessThreshold | ||
| Object.defineProperty(txm, "livenessThreshold", { | ||
|
|
@@ -532,7 +585,7 @@ test("Transaction cancelled due to deadline passing", async () => { | |
|
|
||
| const deadline = Math.round(Date.now() / 1000 + 2) | ||
|
|
||
| const transaction = await createCounterTransaction(deadline) | ||
| const transaction = createCounterTransaction(deadline) | ||
|
|
||
| proxyServer.addBehavior(ProxyBehavior.NotAnswer) | ||
|
|
||
|
|
@@ -611,7 +664,7 @@ test("Transaction cancelled due to deadline passing", async () => { | |
|
|
||
| test("Execute a transaction with value", async () => { | ||
| const value = BigInt(1000) | ||
| const transactionToSend = await txm.createTransaction({ | ||
| const transactionToSend = txm.createTransaction({ | ||
| address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", | ||
| calldata: "0x", | ||
| value, | ||
|
|
@@ -671,7 +724,7 @@ test("Correctly calculates baseFeePerGas after a block with high gas usage", asy | |
| hash: transactionBurnerExecuted.attempts[0].hash, | ||
| }) | ||
|
|
||
| const incrementerTransaction = await createCounterTransaction() | ||
| const incrementerTransaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(incrementerTransaction) | ||
|
|
||
|
|
@@ -825,7 +878,7 @@ test("Transaction manager successfully processes transactions despite random RPC | |
| const emittedTransactions: Transaction[] = [] | ||
| const numTransactions = 5 | ||
| for (let i = 0; i < numTransactions; i++) { | ||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
| transactionQueue.push(transaction) | ||
| emittedTransactions.push(transaction) | ||
|
|
||
|
|
@@ -870,7 +923,7 @@ test("Transaction succeeds in congested blocks", async () => { | |
|
|
||
| await sendBurnGasTransactionWithSecondWallet(2) | ||
|
|
||
| const incrementerTransaction = await createCounterTransaction() | ||
| const incrementerTransaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(incrementerTransaction) | ||
|
|
||
|
|
@@ -925,7 +978,7 @@ test("Finalized transactions are automatically purged from db after finalizedTra | |
| configurable: true, | ||
| }) | ||
|
|
||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -965,7 +1018,7 @@ test("RPC liveness monitor works correctly", async () => { | |
| proxyServer.setMode(ProxyMode.Deterministic) | ||
|
|
||
| while (!txm.rpcLivenessMonitor.isAlive) { | ||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -992,7 +1045,7 @@ test("RPC liveness monitor works correctly", async () => { | |
| expect(txm.rpcLivenessMonitor.isAlive).toBe(true) | ||
|
|
||
| while (txm.rpcLivenessMonitor.isAlive) { | ||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
@@ -1005,7 +1058,7 @@ test("RPC liveness monitor works correctly", async () => { | |
| proxyServer.setMode(ProxyMode.Deterministic) | ||
|
|
||
| while (!txm.rpcLivenessMonitor.isAlive) { | ||
| const transaction = await createCounterTransaction() | ||
| const transaction = createCounterTransaction() | ||
|
|
||
| transactionQueue.push(transaction) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran stats on the repo the other day and this is the biggest file in it :D
Not important, but we can probably consider splitting it up sometime in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://linear.app/happychain/issue/HAPPY-570/split-the-txm-test-into-multiple-files