Browse Source

Attempt to serialize/deserialize account

master
meriadec 7 years ago
parent
commit
93206fda11
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 31
      src/reducers/accounts.js

31
src/reducers/accounts.js

@ -1,19 +1,20 @@
// @flow
import { handleActions } from 'redux-actions'
import { createAccountModel } from '@ledgerhq/wallet-common/lib/models/account'
import every from 'lodash/every'
import get from 'lodash/get'
import reduce from 'lodash/reduce'
import type { Account, AccountRaw } from '@ledgerhq/wallet-common/lib/types'
import { getDefaultUnitByCoinType, getCurrencyByCoinType } from '@ledgerhq/currencies'
import type { State } from 'reducers'
export type AccountsState = Account[]
const state: AccountsState = []
const accountModel = createAccountModel()
function orderAccountsOperations(account: Account) {
const { operations } = account
operations.sort((a, b) => new Date(b.date) - new Date(a.date))
@ -89,30 +90,12 @@ export function canCreateAccount(state: State): boolean {
return every(getAccounts(state), a => get(a, 'operations.length', 0) > 0)
}
export function serializeAccounts(accounts: Array<Object>) {
return accounts.map((account, key) => ({
...account,
currency: getCurrencyByCoinType(account.coinType),
name: account.name || `${key}`,
unit: account.unit || getDefaultUnitByCoinType(account.coinType),
}))
export function serializeAccounts(accounts: Account[]) {
return accounts.map(accountModel.decode)
}
export function deserializeAccounts(accounts: Account[]) {
return accounts.map(account => ({
id: account.id,
address: account.address,
addresses: account.addresses,
balance: account.balance,
balanceByDay: account.balanceByDay,
coinType: account.coinType,
index: account.index,
name: account.name,
operations: account.operations,
path: account.path,
rootPath: account.rootPath,
unit: account.unit,
}))
export function deserializeAccounts(accounts: AccountRaw[]) {
return accounts.map(accountModel.encode)
}
export default handleActions(handlers, state)

Loading…
Cancel
Save