Skip to content

Commit

Permalink
🪚 Return pending transactions from signAndSend (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jan 9, 2024
1 parent 9ecfd78 commit 2dac0da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-plants-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/devtools": patch
---

Return pending (unsubmitted) transactions when signing and sending
20 changes: 14 additions & 6 deletions packages/devtools/src/transactions/signer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { createModuleLogger, pluralizeNoun, pluralizeOrdinal } from '@layerzerolabs/io-devtools'
import type { OmniSignerFactory, OmniTransaction, OmniTransactionWithReceipt } from './types'
import type { OmniSignerFactory, OmniTransaction, OmniTransactionWithError, OmniTransactionWithReceipt } from './types'
import { formatOmniPoint } from '@/omnigraph/format'
import type { OmniError } from '@/omnigraph/types'

export type SignAndSendResult = [
// All the successful transactions
successful: OmniTransactionWithReceipt[],
// The failed transactions along with the errors
errors: OmniTransactionWithError[],
// All the transactions that have not been executed (including the failed ones)
pending: OmniTransaction[],
]

/**
* Creates a sign & send utility for a list of transaction
Expand All @@ -14,14 +22,14 @@ export const createSignAndSend =
async (
transactions: OmniTransaction[],
onProgress?: (result: OmniTransactionWithReceipt, results: OmniTransactionWithReceipt[]) => unknown
): Promise<[successful: OmniTransactionWithReceipt[], errors: OmniError[]]> => {
): Promise<SignAndSendResult> => {
const logger = createModuleLogger('sign & send')

// Put it here so that we don't need to type like seven toilet rolls of variable names
const n = transactions.length

// Just exit when there is nothing to sign
if (n === 0) return logger.debug(`No transactions to sign, exiting`), [[], []]
if (n === 0) return logger.debug(`No transactions to sign, exiting`), [[], [], []]

// Tell the user how many we are signing
logger.debug(`Signing ${n} ${pluralizeNoun(n, 'transaction')}`)
Expand Down Expand Up @@ -55,12 +63,12 @@ export const createSignAndSend =
} catch (error) {
logger.debug(`Failed to process ${ordinal} transaction: ${error}`)

return [successful, [{ point: transaction.point, error }]]
return [successful, [{ transaction, error }], transactions.slice(index)]
}
}

// Tell the inquisitive user what a good job we did
logger.debug(`Successfully signed ${n} ${pluralizeNoun(n, 'transaction')}`)

return [successful, []]
return [successful, [], []]
}
5 changes: 5 additions & 0 deletions packages/devtools/src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface OmniTransactionWithReceipt<TReceipt extends OmniTransactionRece
receipt: TReceipt
}

export interface OmniTransactionWithError<TError = unknown> {
transaction: OmniTransaction
error: TError
}

export interface OmniTransactionResponse<TReceipt extends OmniTransactionReceipt = OmniTransactionReceipt> {
transactionHash: string
wait: (confirmations?: number) => Promise<TReceipt>
Expand Down
15 changes: 9 additions & 6 deletions packages/devtools/test/transactions/signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('transactions/signer', () => {
const signerFactory: OmniSignerFactory = () => ({ signAndSend, sign })
const signAndSendTransactions = createSignAndSend(signerFactory)

expect(await signAndSendTransactions([])).toEqual([[], []])
expect(await signAndSendTransactions([])).toEqual([[], [], []])

expect(signAndSend).not.toHaveBeenCalled()
expect(sign).not.toHaveBeenCalled()
Expand All @@ -42,10 +42,11 @@ describe('transactions/signer', () => {
const signAndSendTransactions = createSignAndSend(signerFactory)

// Now we send all the transactions to the flow and observe the output
const [successful, errors] = await signAndSendTransactions(transactions)
const [successful, errors, pending] = await signAndSendTransactions(transactions)

expect(successful).toEqual(transactions.map((transaction) => ({ transaction, receipt })))
expect(errors).toEqual([])
expect(pending).toEqual([])

// We also check that the signer factory has been called with the eids
for (const transaction of transactions) {
Expand Down Expand Up @@ -100,10 +101,11 @@ describe('transactions/signer', () => {

// Now we send all the transactions to the flow and observe the output
const transactions = [...firstBatch, failedTransaction, ...secondBatch]
const [successful, errors] = await signAndSendTransactions(transactions)
const [successful, errors, pending] = await signAndSendTransactions(transactions)

expect(successful).toEqual(firstBatch.map((transaction) => ({ transaction, receipt })))
expect(errors).toEqual([{ point: failedTransaction.point, error }])
expect(errors).toEqual([{ transaction: failedTransaction, error }])
expect(pending).toEqual([failedTransaction, ...secondBatch])

// We also check that the signer factory has been called with the eids
expect(signerFactory).toHaveBeenCalledWith(failedTransaction.point.eid)
Expand Down Expand Up @@ -151,10 +153,11 @@ describe('transactions/signer', () => {

// Now we send all the transactions to the flow and observe the output
const transactions = [...firstBatch, failedTransaction, ...secondBatch]
const [successful, errors] = await signAndSendTransactions(transactions)
const [successful, errors, pending] = await signAndSendTransactions(transactions)

expect(successful).toEqual(firstBatch.map((transaction) => ({ transaction, receipt })))
expect(errors).toEqual([{ point: failedTransaction.point, error }])
expect(errors).toEqual([{ transaction: failedTransaction, error }])
expect(pending).toEqual([failedTransaction, ...secondBatch])

// We also check that the signer factory has been called with the eids
expect(signerFactory).toHaveBeenCalledWith(failedTransaction.point.eid)
Expand Down

0 comments on commit 2dac0da

Please sign in to comment.