add transaction status in selector

This commit is contained in:
Mikhail Mikheev 2019-07-04 15:09:34 +04:00
parent 8565164d85
commit e4c10d2a48
3 changed files with 29 additions and 25 deletions

View File

@ -22,26 +22,14 @@ type TxData = {
type: string, type: string,
date: string, date: string,
amount: number | string, amount: number | string,
status: string,
tx: Transaction, tx: Transaction,
status?: string,
} }
export const formatDate = (date: Date): string => format(date, 'MMM D, YYYY - h:m:s') export const formatDate = (date: Date): string => format(date, 'MMM D, YYYY - h:m:s')
export type TransactionRow = SortRow<TxData> export type TransactionRow = SortRow<TxData>
const getTxStatus = (tx: Transaction): string => {
let txStatus = 'awaiting'
if (tx.isExecuted) {
txStatus = 'success'
} else if (tx.cancelled) {
txStatus = 'cancelled'
}
return txStatus
}
export const getTxTableData = (transactions: List<Transaction>): List<TransactionRow> => { export const getTxTableData = (transactions: List<Transaction>): List<TransactionRow> => {
const rows = transactions.map((tx: Transaction) => { const rows = transactions.map((tx: Transaction) => {
const txDate = tx.isExecuted ? tx.executionDate : tx.submissionDate const txDate = tx.isExecuted ? tx.executionDate : tx.submissionDate
@ -52,7 +40,7 @@ export const getTxTableData = (transactions: List<Transaction>): List<Transactio
[TX_TABLE_DATE_ID]: formatDate(tx.isExecuted ? tx.executionDate : tx.submissionDate), [TX_TABLE_DATE_ID]: formatDate(tx.isExecuted ? tx.executionDate : tx.submissionDate),
[buildOrderFieldFrom(TX_TABLE_DATE_ID)]: getTime(txDate), [buildOrderFieldFrom(TX_TABLE_DATE_ID)]: getTime(txDate),
[TX_TABLE_AMOUNT_ID]: Number(tx.value) > 0 ? `${fromWei(toBN(tx.value), 'ether')} ${tx.symbol}` : 'n/a', [TX_TABLE_AMOUNT_ID]: Number(tx.value) > 0 ? `${fromWei(toBN(tx.value), 'ether')} ${tx.symbol}` : 'n/a',
[TX_TABLE_STATUS_ID]: getTxStatus(tx), [TX_TABLE_STATUS_ID]: tx.status,
[TX_TABLE_RAW_TX_ID]: tx, [TX_TABLE_RAW_TX_ID]: tx,
} }
}) })

View File

@ -17,7 +17,7 @@ import { sameAddress } from '~/logic/wallets/ethAddresses'
import { safeTransactionsSelector } from '~/routes/safe/store/selectors/index' import { safeTransactionsSelector } from '~/routes/safe/store/selectors/index'
import { orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors' import { orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors'
import { type Token } from '~/logic/tokens/store/model/token' import { type Token } from '~/logic/tokens/store/model/token'
import { type Transaction } from '~/routes/safe/store/models/transaction' import { type Transaction, type TransactionStatus } from '~/routes/safe/store/models/transaction'
import { type TokenBalance } from '~/routes/safe/store/models/tokenBalance' import { type TokenBalance } from '~/routes/safe/store/models/tokenBalance'
import { safeParamAddressSelector } from '../store/selectors' import { safeParamAddressSelector } from '../store/selectors'
import { getEthAsToken } from '~/logic/tokens/utils/tokenHelpers' import { getEthAsToken } from '~/logic/tokens/utils/tokenHelpers'
@ -33,6 +33,18 @@ export type SelectorProps = {
transactions: List<Transaction>, transactions: List<Transaction>,
} }
const getTxStatus = (tx: Transaction): TransactionStatus => {
let txStatus = 'awaiting'
if (tx.isExecuted) {
txStatus = 'success'
} else if (tx.cancelled) {
txStatus = 'cancelled'
}
return txStatus
}
export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = createSelector( export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = createSelector(
userAccountSelector, userAccountSelector,
safeSelector, safeSelector,
@ -94,21 +106,21 @@ const extendedTransactionsSelector: Selector<GlobalState, RouterProps, List<Tran
safeTransactionsSelector, safeTransactionsSelector,
(transactions) => { (transactions) => {
const extendedTransactions = transactions.map((tx: Transaction) => { const extendedTransactions = transactions.map((tx: Transaction) => {
if (tx.isExecuted) { let extendedTx = tx
return tx
}
// If transactions is not executed, but there's a transaction with the same nonce submitted later // If transactions is not executed, but there's a transaction with the same nonce submitted later
// it means that the transaction was cancelled (Replaced) and shouldn't get executed // it means that the transaction was cancelled (Replaced) and shouldn't get executed
const replacementTransaction = transactions.findLast( let replacementTransaction
transaction => transaction.nonce === tx.nonce && isAfter(transaction.submissionDate, tx.submissionDate), if (!tx.isExecuted) {
) replacementTransaction = transactions.findLast(
transaction => transaction.nonce === tx.nonce && isAfter(transaction.submissionDate, tx.submissionDate),
if (!replacementTransaction) { )
return tx if (replacementTransaction) {
extendedTx = tx.set('cancelled', true)
}
} }
return tx.set('cancelled', true) return extendedTx.set('status', getTxStatus(extendedTx))
}) })
return extendedTransactions return extendedTransactions

View File

@ -3,6 +3,8 @@ import { List, Record } from 'immutable'
import type { RecordFactory, RecordOf } from 'immutable' import type { RecordFactory, RecordOf } from 'immutable'
import { type Confirmation } from '~/routes/safe/store/models/confirmation' import { type Confirmation } from '~/routes/safe/store/models/confirmation'
export type TransactionStatus = 'awaiting' | 'success' | 'cancelled'
export type TransactionProps = { export type TransactionProps = {
name: string, name: string,
nonce: number, nonce: number,
@ -17,6 +19,7 @@ export type TransactionProps = {
creationTxHash: string, creationTxHash: string,
executionTxHash?: string, executionTxHash?: string,
cancelled?: boolean, cancelled?: boolean,
status?: TransactionStatus,
} }
export const makeTransaction: RecordFactory<TransactionProps> = Record({ export const makeTransaction: RecordFactory<TransactionProps> = Record({
@ -33,6 +36,7 @@ export const makeTransaction: RecordFactory<TransactionProps> = Record({
executionTxHash: undefined, executionTxHash: undefined,
creationTxHash: '', creationTxHash: '',
cancelled: false, cancelled: false,
status: 'awaiting',
}) })
export type Transaction = RecordOf<TransactionProps> export type Transaction = RecordOf<TransactionProps>