Browse Source

Fix the 'SELF' to explode 2 txs & 'OUT' to include the fees

master
Gaëtan Renaudeau 7 years ago
parent
commit
042d1de9ad
  1. 58
      src/bridge/EthereumJSBridge.js
  2. 11
      src/bridge/RippleJSBridge.js

58
src/bridge/EthereumJSBridge.js

@ -2,6 +2,7 @@
import React from 'react' import React from 'react'
import EthereumKind from 'components/FeesField/EthereumKind' import EthereumKind from 'components/FeesField/EthereumKind'
import throttle from 'lodash/throttle' import throttle from 'lodash/throttle'
import flatMap from 'lodash/flatMap'
import uniqBy from 'lodash/uniqBy' import uniqBy from 'lodash/uniqBy'
import type { Account, Operation } from '@ledgerhq/live-common/lib/types' import type { Account, Operation } from '@ledgerhq/live-common/lib/types'
import { apiForCurrency } from 'api/Ethereum' import { apiForCurrency } from 'api/Ethereum'
@ -25,22 +26,43 @@ const EditFees = ({ account, onChange, value }: EditProps<Transaction>) => (
/> />
) )
const toAccountOperation = (account: Account) => (tx: Tx): $Exact<Operation> => { // in case of a SELF send, 2 ops are returned.
const sending = account.freshAddress.toLowerCase() === tx.from.toLowerCase() const txToOps = (account: Account) => (tx: Tx): Operation[] => {
const receiving = account.freshAddress.toLowerCase() === tx.to.toLowerCase() const freshAddress = account.freshAddress.toLowerCase()
const type = sending && receiving ? 'SELF' : sending ? 'OUT' : 'IN' const from = tx.from.toLowerCase()
return { const to = tx.to.toLowerCase()
id: `${account.id}-${tx.hash}-${type}`, const sending = freshAddress === from
hash: tx.hash, const receiving = freshAddress === to
type, const ops = []
value: tx.value, if (sending) {
blockHeight: tx.block && tx.block.height, ops.push({
blockHash: tx.block && tx.block.hash, id: `${account.id}-${tx.hash}-OUT`,
accountId: account.id, hash: tx.hash,
senders: [tx.from], type: 'OUT',
recipients: [tx.to], value: tx.value + tx.gas_price * tx.gas_used,
date: new Date(tx.received_at), blockHeight: tx.block && tx.block.height,
blockHash: tx.block && tx.block.hash,
accountId: account.id,
senders: [tx.from],
recipients: [tx.to],
date: new Date(tx.received_at),
})
}
if (receiving) {
ops.push({
id: `${account.id}-${tx.hash}-IN`,
hash: tx.hash,
type: 'IN',
value: tx.value,
blockHeight: tx.block && tx.block.height,
blockHash: tx.block && tx.block.hash,
accountId: account.id,
senders: [tx.from],
recipients: [tx.to],
date: new Date(tx.received_at),
})
} }
return ops
} }
function isRecipientValid(currency, recipient) { function isRecipientValid(currency, recipient) {
@ -63,7 +85,7 @@ const paginateMoreTransactions = async (
acc.length ? acc[acc.length - 1].blockHash : undefined, acc.length ? acc[acc.length - 1].blockHash : undefined,
) )
if (txs.length === 0) return acc if (txs.length === 0) return acc
return mergeOps(acc, txs.map(toAccountOperation(account))) return mergeOps(acc, flatMap(txs, txToOps(account)))
} }
const fetchCurrentBlock = (perCurrencyId => currency => { const fetchCurrentBlock = (perCurrencyId => currency => {
@ -158,7 +180,7 @@ const EthereumBridge: WalletBridge<Transaction> = {
unit: currency.units[0], unit: currency.units[0],
lastSyncDate: new Date(), lastSyncDate: new Date(),
} }
account.operations = mergeOps([], txs.map(toAccountOperation(account))) account.operations = mergeOps([], flatMap(txs, txToOps(account)))
return { account } return { account }
} }
@ -209,7 +231,7 @@ const EthereumBridge: WalletBridge<Transaction> = {
if (unsubscribed) return if (unsubscribed) return
next(a => { next(a => {
const currentOps = a.operations const currentOps = a.operations
const newOps = txs.map(toAccountOperation(a)) const newOps = flatMap(txs, txToOps(a))
const { length: newLength } = newOps const { length: newLength } = newOps
const { length } = currentOps const { length } = currentOps
if ( if (

11
src/bridge/RippleJSBridge.js

@ -120,11 +120,18 @@ type Tx = {
const txToOperation = (account: Account) => ({ const txToOperation = (account: Account) => ({
id, id,
sequence, sequence,
outcome: { deliveredAmount, ledgerVersion, timestamp }, outcome: { fee, deliveredAmount, ledgerVersion, timestamp },
specification: { source, destination }, specification: { source, destination },
}: Tx): Operation => { }: Tx): Operation => {
const type = source.address === account.freshAddress ? 'OUT' : 'IN' const type = source.address === account.freshAddress ? 'OUT' : 'IN'
const value = deliveredAmount ? parseAPICurrencyObject(deliveredAmount) : 0 let value = deliveredAmount ? parseAPICurrencyObject(deliveredAmount) : 0
if (type === 'OUT') {
const feeValue = parseAPIValue(fee)
if (!isNaN(feeValue)) {
value += feeValue
}
}
const op: $Exact<Operation> = { const op: $Exact<Operation> = {
id, id,
hash: id, hash: id,

Loading…
Cancel
Save