Skip to content

Commit

Permalink
Merge pull request #117 from alephium/fix-pending-tx-when-not-in-mempool
Browse files Browse the repository at this point in the history
Remove Pending Tx When Neccessary
  • Loading branch information
h0ngcha0 authored Aug 3, 2023
2 parents 56ea7e6 + ea940ef commit 4ca4fc6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
19 changes: 15 additions & 4 deletions packages/extension/src/background/transactions/sources/onchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ export async function getTransactionsUpdate(transactions: Transaction[]) {
transactionsToCheck.map(async (transaction) => {
const network = await getNetwork(transaction.account.networkId)
const explorerProvider = new ExplorerProvider(network.explorerApiUrl)
const updatedTransaction = await explorerProvider.transactions.getTransactionsTransactionHash(transaction.hash)

const status = updatedTransaction.type === "Accepted" ? "ACCEPTED_ON_CHAIN" : "ACCEPTED_ON_MEMPOOL"
return { ...transaction, status }
const txDate = new Date(transaction.timestamp)
const txCreatedSinceInMinutes = (new Date().valueOf() - txDate.valueOf()) / 1000 / 60
try {
const updatedTransaction = await explorerProvider.transactions.getTransactionsTransactionHash(transaction.hash)
const status = updatedTransaction.type === "Accepted" ? "ACCEPTED_ON_CHAIN" : "ACCEPTED_ON_MEMPOOL"
return { ...transaction, status }
} catch (exception: any) {
// If the transaction is not found and it has been created for more than 2 minutes
// we can assume it has been removed from the mempool
if (exception.message.includes("not found") && txCreatedSinceInMinutes >= 2) {
return { ...transaction, status: 'REMOVED_FROM_MEMPOOL' }
} else {
throw exception
}
}
})
)

Expand Down
7 changes: 6 additions & 1 deletion packages/extension/src/background/transactions/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { accountsEqual } from "../../shared/wallet.service"
import { getTransactionsUpdate } from "./sources/onchain"
import { getTransactionHistory } from "./sources/voyager"
import { transactionsStore } from "./store"
import { partition } from "lodash"

export interface TransactionTracker {
loadHistory: (accountsToPopulate: WalletAccount[]) => Promise<void>
Expand All @@ -28,7 +29,11 @@ export const transactionTracker: TransactionTracker = {
// is smart enough to filter for just the pending transactions, as the rest needs no update
allTransactions,
)
await transactionsStore.push(updatedTransactions)
const [toBeRemoved, toBeKept] = partition(updatedTransactions, (tx) => tx.status === "REMOVED_FROM_MEMPOOL")
if (toBeRemoved.length > 0) {
await transactionsStore.remove((tx) => toBeRemoved.some((toBeRemovedTx) => tx.hash === toBeRemovedTx.hash))
}
await transactionsStore.push(toBeKept)
const hasPendingTransactions =
getInFlightTransactions(allTransactions).length > 0
return hasPendingTransactions
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/shared/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReviewTransactionResult } from "./actionQueue/types"
import { WalletAccount } from "./wallet.model"
import { AlephiumExplorerTransaction } from "./explorer/type"

export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_MEMPOOL' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_CHAIN' | 'REJECTED';
export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_MEMPOOL' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_CHAIN' | 'REJECTED' | 'REMOVED_FROM_MEMPOOL';

// Global Constants for Transactions
export const SUCCESS_STATUSES: Status[] = [
Expand Down

0 comments on commit 4ca4fc6

Please sign in to comment.