Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 27, 2024
1 parent c576800 commit 1c893cc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
46 changes: 31 additions & 15 deletions src/actions/public/waitForTransactionReceipt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand All @@ -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, {
Expand All @@ -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)
Expand All @@ -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 })
Expand All @@ -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')
})

Expand Down
18 changes: 12 additions & 6 deletions src/utils/observe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,25 @@ 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 () => {
cleanup()
}
})

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)

Expand Down
5 changes: 3 additions & 2 deletions src/utils/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ export function observe<callbacks extends Callbacks>(
}

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()
}

Expand Down

0 comments on commit 1c893cc

Please sign in to comment.