From 1c893cc7473190562d9143f8ab1ddbc36aea2c99 Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:00:42 +0100 Subject: [PATCH] chore: format --- .../public/waitForTransactionReceipt.test.ts | 46 +++++++++++++------ src/utils/observe.test.ts | 18 +++++--- src/utils/observe.ts | 5 +- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/actions/public/waitForTransactionReceipt.test.ts b/src/actions/public/waitForTransactionReceipt.test.ts index 51fb543ec6..31a777b19a 100644 --- a/src/actions/public/waitForTransactionReceipt.test.ts +++ b/src/actions/public/waitForTransactionReceipt.test.ts @@ -11,13 +11,17 @@ import { sendTransaction } from '../wallet/sendTransaction.js' import { anvilMainnet } from '../../../test/src/anvil.js' -import { sendRawTransaction, setIntervalMining, signTransaction } from '../index.js' -import * as getBlock from './getBlock.js' -import { prepareTransactionRequest } from '../../actions/index.js' -import { waitForTransactionReceipt } from './waitForTransactionReceipt.js' -import * as getTransactionModule from './getTransaction.js' import { privateKeyToAccount } from '~viem/accounts/privateKeyToAccount.js' import { keccak256 } from '~viem/utils/index.js' +import { prepareTransactionRequest } from '../../actions/index.js' +import { + sendRawTransaction, + setIntervalMining, + signTransaction, +} from '../index.js' +import * as getBlock from './getBlock.js' +import * as getTransactionModule from './getTransaction.js' +import { waitForTransactionReceipt } from './waitForTransactionReceipt.js' const client = anvilMainnet.getClient() @@ -122,8 +126,9 @@ test('waits for transaction (polling many blocks while others waiting does not t await mine(client, { blocks: 1 }) await wait(200) - const template = await getTransactionModule.getTransaction(client, { hash: templateHash }) - + const template = await getTransactionModule.getTransaction(client, { + hash: templateHash, + }) // Prepare and calculate hash of problematic transaction. Will send it later @@ -135,7 +140,6 @@ test('waits for transaction (polling many blocks while others waiting does not t const problematicTx = await signTransaction(client, prepareProblematic) const problematicTxHash = keccak256(problematicTx) - // Prepare a good transaction. Will send it later const prepareGood = await prepareTransactionRequest(client, { @@ -149,20 +153,32 @@ test('waits for transaction (polling many blocks while others waiting does not t // important step: we need to mock the getTransaction to simulate a transaction that is in the mempool but // is not yet mined. - getTransaction.mockResolvedValueOnce({...template, hash: goodTxHash, nonce: 1233}) + getTransaction.mockResolvedValueOnce({ + ...template, + hash: goodTxHash, + nonce: 1233, + }) - // Start looking for the receipt of the good transaction but did not send it yet. Here it will start polling - const goodReceiptPromise = waitForTransactionReceipt(client, { hash: goodTxHash, timeout: 5000, retryCount: 0 }) + // Start looking for the receipt of the good transaction but did not send it yet. Here it will start polling + const goodReceiptPromise = waitForTransactionReceipt(client, { + hash: goodTxHash, + timeout: 5000, + retryCount: 0, + }) await wait(200) // to simulate a transaction that is in the mempool but not yet mined - getTransaction.mockResolvedValueOnce({...template, hash: problematicTxHash, nonce: 1234}) + getTransaction.mockResolvedValueOnce({ + ...template, + hash: problematicTxHash, + nonce: 1234, + }) // Start polling the problematic transaction receipt waitForTransactionReceipt(client, { hash: problematicTxHash, retryCount: 0 }) await mine(client, { blocks: 1 }) await wait(200) - + // Send the problematic transaction and mine it so we will have the receipt await sendRawTransaction(client, { serializedTransaction: problematicTx }) await wait(200) @@ -171,7 +187,7 @@ test('waits for transaction (polling many blocks while others waiting does not t // getting many receipt will trigger many unwatch from the same listener await mine(client, { blocks: 1000 }) await wait(200) - + // Send good transaction and mine, if the polling is working fine should get the receipt but if not we will get a timeout. await sendRawTransaction(client, { serializedTransaction: goodTx }) await mine(client, { blocks: 1 }) @@ -180,7 +196,7 @@ test('waits for transaction (polling many blocks while others waiting does not t await mine(client, { blocks: 1 }) await wait(200) - const {status} = await goodReceiptPromise + const { status } = await goodReceiptPromise expect(status).toBe('success') }) diff --git a/src/utils/observe.test.ts b/src/utils/observe.test.ts index 4194d43888..c5ae2fe747 100644 --- a/src/utils/observe.test.ts +++ b/src/utils/observe.test.ts @@ -178,7 +178,7 @@ test('cleans up emit function correctly', async () => { test('cleans up emit function when last listener unwatch', async () => { const id = 'mock' const callback = vi.fn() - const cleanup = vi.fn(); + const cleanup = vi.fn() const emitter = vi.fn(({ emit }) => { setTimeout(() => emit({ foo: 'bar' }), 100) return () => { @@ -186,11 +186,17 @@ test('cleans up emit function when last listener unwatch', async () => { } }) - const unwatch1 = observe(id, { emit: () => { - unwatch1(); - unwatch1(); - unwatch1(); - } }, emitter) + const unwatch1 = observe( + id, + { + emit: () => { + unwatch1() + unwatch1() + unwatch1() + }, + }, + emitter, + ) const unwatch2 = observe(id, { emit: callback }, emitter) diff --git a/src/utils/observe.ts b/src/utils/observe.ts index b93dd804e4..264e19a583 100644 --- a/src/utils/observe.ts +++ b/src/utils/observe.ts @@ -43,9 +43,10 @@ export function observe( } const unwatch = () => { - if (!getListeners().find((cb: any) => cb.id === callbackId)) return + const listeners = getListeners() + if (!listeners.some((cb: any) => cb.id === callbackId)) return const cleanup = cleanupCache.get(observerId) - if (getListeners().length === 1 && cleanup) cleanup() + if (listeners.length === 1 && cleanup) cleanup() unsubscribe() }