Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
handle cancel state for ethereum txs
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirotw committed Oct 7, 2021
1 parent 20ca92a commit b529355
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/custom/hooks/useRecentActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ export function useActivityDescriptors({ chainId, id }: { chainId?: ChainId; id:
// setup variables accordingly...
const isReceiptConfirmed =
tx.receipt?.status === TxReceiptStatus.CONFIRMED || typeof tx.receipt?.status === 'undefined'
const isCancelTx = tx?.replacementType === 'cancel'
isPending = !tx?.receipt
isPresignaturePending = false
isConfirmed = !isPending && isReceiptConfirmed
// TODO: can't tell when it's cancelled from the network yet
isCancelling = false
isCancelled = false
isCancelling = isCancelTx && isPending
isCancelled = isCancelTx && !isPending && isReceiptConfirmed

activity = tx
type = ActivityType.TX
Expand All @@ -156,10 +156,10 @@ export function useActivityDescriptors({ chainId, id }: { chainId?: ChainId; id:
status = ActivityStatus.PENDING
} else if (isPresignaturePending) {
status = ActivityStatus.PRESIGNATURE_PENDING
} else if (isConfirmed) {
status = ActivityStatus.CONFIRMED
} else if (isCancelled) {
status = ActivityStatus.CANCELLED
} else if (isConfirmed) {
status = ActivityStatus.CONFIRMED
} else {
status = ActivityStatus.EXPIRED
}
Expand Down
6 changes: 2 additions & 4 deletions src/custom/state/enhancedTransactions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ export const checkedTransaction = createAction<{
blockNumber: number
}>('enhancedTransactions/checkedTransaction')

export const cancelTransaction = createAction<{
chainId: number
hash: string
}>('enhancedTransactions/cancelTransaction')
export type REPLACEMENT_TYPE = 'speedup' | 'cancel'

export const replaceTransaction = createAction<{
chainId: number
oldHash: string
newHash: string
type: REPLACEMENT_TYPE
}>('enhancedTransactions/replaceTransaction')
17 changes: 5 additions & 12 deletions src/custom/state/enhancedTransactions/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
clearAllTransactions,
checkedTransaction,
finalizeTransaction,
cancelTransaction,
replaceTransaction,
REPLACEMENT_TYPE,
// updateSafeTransactions,
} from 'state/enhancedTransactions/actions'
import { SerializableTransactionReceipt } from '@src/state/transactions/actions'
Expand All @@ -31,6 +31,8 @@ export interface EnhancedTransactionDetails {
// Operations
approval?: { tokenAddress: string; spender: string }
presign?: { orderId: string }

replacementType?: REPLACEMENT_TYPE // if the user cancelled or speedup the tx it will be reflected here
}

export interface EnhancedTransactionState {
Expand Down Expand Up @@ -93,22 +95,13 @@ export default createReducer(initialState, (builder) =>
tx.confirmedTime = now()
})

.addCase(cancelTransaction, (transactions, { payload: { chainId, hash } }) => {
if (!transactions[chainId]?.[hash]) {
console.error('Attempted to cancel an unknown transaction.')
return
}
const allTxs = transactions[chainId] ?? {}
delete allTxs[hash]
})

.addCase(replaceTransaction, (transactions, { payload: { chainId, oldHash, newHash } }) => {
.addCase(replaceTransaction, (transactions, { payload: { chainId, oldHash, newHash, type } }) => {
if (!transactions[chainId]?.[oldHash]) {
console.error('Attempted to replace an unknown transaction.')
return
}
const allTxs = transactions[chainId] ?? {}
allTxs[newHash] = { ...allTxs[oldHash], hash: newHash, addedTime: new Date().getTime() }
allTxs[newHash] = { ...allTxs[oldHash], hash: newHash, addedTime: new Date().getTime(), replacementType: type }
delete allTxs[oldHash]
})
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from 'react'
import { useAppDispatch } from 'state/hooks'
import { useActiveWeb3React } from 'hooks/web3'
import { sdk } from 'utils/blocknative'
import { cancelTransaction, replaceTransaction } from 'state/enhancedTransactions/actions'
import { replaceTransaction } from 'state/enhancedTransactions/actions'
import { useAllPendingHashes } from 'state/enhancedTransactions/hooks'
import { Dispatch } from 'redux'

Expand All @@ -11,19 +11,16 @@ function watchTxChanges(pendingHashes: string[], chainId: number, dispatch: Disp
try {
const { emitter } = sdk[chainId].transaction(hash)
const currentHash = hash
let isSpeedup = false

emitter.on('txSpeedUp', (e) => {
isSpeedup = true
if ('hash' in e && typeof e.hash === 'string') {
dispatch(replaceTransaction({ chainId, oldHash: currentHash, newHash: e.hash }))
dispatch(replaceTransaction({ chainId, oldHash: currentHash, newHash: e.hash, type: 'speedup' }))
}
})

emitter.on('txConfirmed', (e) => {
// canceled txs are automatically watched by bnc so if the confirmed tx hash is different than the previously tracked one, it means the user sent a cancel tx
if ('hash' in e && e.hash !== currentHash && !isSpeedup) {
dispatch(cancelTransaction({ chainId, hash: currentHash }))
emitter.on('txCancel', (e) => {
if ('hash' in e && typeof e.hash === 'string') {
dispatch(replaceTransaction({ chainId, oldHash: currentHash, newHash: e.hash, type: 'cancel' }))
}
})
} catch (error) {
Expand Down

0 comments on commit b529355

Please sign in to comment.