WA-521 Refactor fetchTransactions action including txs's data from backend

This commit is contained in:
apanizo 2018-08-10 17:35:24 +02:00
parent a9df97f30c
commit cbab03ace9
1 changed files with 57 additions and 20 deletions

View File

@ -3,34 +3,71 @@ import { List, Map } from 'immutable'
import type { Dispatch as ReduxDispatch } from 'redux' import type { Dispatch as ReduxDispatch } from 'redux'
import { type GlobalState } from '~/store/index' import { type GlobalState } from '~/store/index'
import { makeOwner } from '~/routes/safe/store/model/owner' import { makeOwner } from '~/routes/safe/store/model/owner'
import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction' import { makeTransaction, type Transaction } from '~/routes/safe/store/model/transaction'
import { load, TX_KEY } from '~/utils/localStorage' import { load, TX_KEY } from '~/utils/localStorage'
import { type Confirmation, type ConfirmationProps, makeConfirmation } from '~/routes/safe/store/model/confirmation' import { makeConfirmation } from '~/routes/safe/store/model/confirmation'
import { loadSafeSubjects } from '~/utils/localStorage/transactions' import { loadSafeSubjects } from '~/utils/localStorage/transactions'
import { buildTxServiceUrlFrom, type TxServiceType } from '~/wallets/safeTxHistory'
import { enhancedFetch } from '~/utils/fetch'
import addTransactions from './addTransactions' import addTransactions from './addTransactions'
export const loadSafeTransactions = () => { type ConfirmationServiceModel = {
const safes = load(TX_KEY) || {} owner: string,
submissionDate: Date,
type: TxServiceType,
transactionHash: string,
}
return Map().withMutations((map: Map<string, List<Confirmation>>) => type TxServiceModel = {
Object.keys(safes).map((safe: string) => { to: string,
const safeTxs = safes[safe] value: number,
const safeSubjects = loadSafeSubjects(safe) data: string,
const safeTxsRecord = safeTxs.map((tx: TransactionProps) => { operation: number,
const { confirmations } = tx nonce: number,
submissionDate: Date,
executionDate: Date,
confirmations: ConfirmationServiceModel[],
isExecuted: boolean,
}
const buildTransactionFrom = (tx: TxServiceModel, safeSubjects: Map<string, string>) => {
const name = safeSubjects.get(String(tx.nonce)) || 'Unknown' const name = safeSubjects.get(String(tx.nonce)) || 'Unknown'
const txRecord = makeTransaction({ const confirmations = List(tx.confirmations.map((conf: ConfirmationServiceModel) =>
...tx, makeConfirmation({
owner: makeOwner({ address: conf.owner }),
type: conf.type,
hash: conf.transactionHash,
})))
return makeTransaction({
name, name,
confirmations: List(confirmations.map((conf: ConfirmationProps) => nonce: tx.nonce,
makeConfirmation({ ...conf, owner: makeOwner(conf.owner) }))), value: tx.value,
confirmations,
destination: tx.to,
data: tx.data,
isExecuted: tx.isExecuted,
}) })
}
return txRecord export const loadSafeTransactions = async () => {
}) const safes = load(TX_KEY) || {}
const safeAddresses: string[] = Object.keys(safes)
const transactions: TxServiceModel[][] = await Promise.all(safeAddresses.map(async (safeAddress: string) => {
const url = buildTxServiceUrlFrom(safeAddress)
return map.set(safe, List(safeTxsRecord)) return enhancedFetch(url, 'Error fetching txs information')
})) }))
return Map().withMutations((map: Map<string, List<Transaction>>) => {
transactions.map((safeTxs: TxServiceModel[], index) => {
const safeAddress = safeAddresses[index]
const safeSubjects = loadSafeSubjects(safeAddress)
const txsRecord = safeTxs.map((tx: TxServiceModel) => buildTransactionFrom(tx, safeSubjects))
return map.set(safeAddress, List(txsRecord))
})
})
} }
export default () => async (dispatch: ReduxDispatch<GlobalState>) => { export default () => async (dispatch: ReduxDispatch<GlobalState>) => {