Skip to content

Commit

Permalink
fix: prevent losing information when a transaction is dropped and goe…
Browse files Browse the repository at this point in the history
…s back to pending
  • Loading branch information
cazala committed Oct 8, 2018
1 parent 8bbede3 commit 4f04e92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/modules/transaction/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,16 @@ function* handleReplaceTransactionRequest(

while (true) {
// check if tx has status, this is to recover from a tx that is dropped momentarily
const tx = yield call(() => txUtils.getTransaction(hash))
const tx: txUtils.Transaction = yield call(() =>
txUtils.getTransaction(hash)
)
if (tx != null) {
yield put(fetchTransactionRequest(account, hash, buildActionRef(tx)))
const txInState: Transaction = yield select(state =>
getTransaction(state, hash)
)
yield put(
fetchTransactionRequest(account, hash, buildActionRef(txInState))
)
break
}

Expand Down Expand Up @@ -195,7 +202,12 @@ function* handleReplaceTransactionRequest(
// could be due to a race condition when fetching the account nonce
// it will be sent back to the pending tx saga that will mark it as confirmed/reverted
if (hash === replacedBy.hash) {
yield put(fetchTransactionRequest(account, hash, buildActionRef(tx)))
const txInState: Transaction = yield select(state =>
getTransaction(state, hash)
)
yield put(
fetchTransactionRequest(account, hash, buildActionRef(txInState))
)
} else {
// replacement found!
yield put(replaceTransactionSuccess(hash, replacedBy.hash))
Expand Down
1 change: 1 addition & 0 deletions src/modules/transaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Transaction {
status: txUtils.Transaction['type'] | null
withReceipt?: boolean
receipt?: Receipt
payload?: any
}

export interface TransactionPayload {
Expand Down
7 changes: 2 additions & 5 deletions src/modules/transaction/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { AnyAction } from 'redux'
import { Transaction, TransactionPayload, FINISHED_TYPES } from './types'
import { txUtils } from 'decentraland-eth'

// Dummy action used as action ref when reinserting a pending transaction into the saga that watches pending txs
export const DUMMY_ACTION_REF = '_dummy_action_ref'

// Special flag used to determine transaction hashes to be monitored
export const TRANSACTION_ACTION_FLAG = '_watch_tx'

export function buildActionRef(transaction: Transaction) {
const { hash, events, withReceipt, ...payload } = transaction
const { actionType, events, withReceipt, payload } = transaction
return {
type: DUMMY_ACTION_REF,
type: actionType,
payload: (withReceipt
? buildTransactionWithReceiptPayload
: buildTransactionPayload)(transaction.hash, payload, events)
Expand Down

0 comments on commit 4f04e92

Please sign in to comment.