Skip to content

Commit

Permalink
feat(Eth Legacy): load legacy sweep, amt not calculated correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
plondon committed May 4, 2018
1 parent a41b56c commit d96160a
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 135 deletions.
119 changes: 76 additions & 43 deletions packages/blockchain-wallet-v4-frontend/src/assets/locales/en.json

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions packages/blockchain-wallet-v4-frontend/src/data/auth/sagas.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { call, put, select, take, fork } from 'redux-saga/effects'
import { delay } from 'redux-saga'
import { prop, assoc, toUpper } from 'ramda'
import { path, prop, assoc, toUpper } from 'ramda'
import Either from 'data.either'

import * as actions from '../actions.js'
Expand Down Expand Up @@ -29,12 +29,18 @@ export default ({ api, coreSagas }) => {
yield take(actionTypes.core.walletSync.SYNC_SUCCESS)
}

// const transferEtherSaga = function * () {
// const legacyAccountBalance = yield select(selectors.core.data.ethereum.getLegacyAccountBalance)
// if (parseFloat(legacyAccountBalance) > 0) {
// // yield put(actions.payment.ethereum.initTransferEther())
// }
// }
const transferEtherSaga = function * () {
const legacyAccount = yield select(selectors.core.kvStore.ethereum.getLegacyAccount)
const { data } = legacyAccount
// If needed, get the ethereum legacy account balance and prompt sweep
if (data && !data.correct) {
const balances = yield call(api.getEthereumBalances, data.addr)
const balance = path([data.addr, 'balance'], balances)
if (balance > 0) {
yield put(actions.modals.showModal('TransferEther', { balance, addr: data.addr }))
}
}
}

const loginRoutineSaga = function * (mobileLogin, firstLogin) {
try {
Expand All @@ -46,20 +52,20 @@ export default ({ api, coreSagas }) => {
yield put(actions.auth.authenticate())
yield put(actions.core.webSocket.bitcoin.startSocket())
yield call(coreSagas.kvStore.root.fetchRoot, askSecondPasswordEnhancer)
// yield call ethereum metadata
if (!firstLogin) {
yield put(actions.alerts.displaySuccess('Login successful'))
}
yield call(coreSagas.kvStore.ethereum.fetchMetadataEthereum)
yield call(coreSagas.kvStore.bch.fetchMetadataBch)
yield put(actions.router.push('/home'))
yield fork(transferEtherSaga)
yield put(actions.auth.startLogoutTimer())
yield put(actions.goals.runGoals())
yield fork(reportStats, mobileLogin)
yield fork(logoutRoutine, yield call(setLogoutEventListener))
// ETHER - Fix derivation
// yield call(transferEtherSaga)
if (!firstLogin) {
yield put(actions.alerts.displaySuccess('Login successful'))
}
} catch (e) {
// Redirect to error page instead of notification
yield put(actions.alerts.displayError('Critical error while fetching essential data !' + e.message))
yield put(actions.alerts.displayError('Critical error while fetching essential data! ' + e.message))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as AT from './actionTypes'

export const sendEthInitialized = () => ({ type: AT.SEND_ETH_INITIALIZED })
export const sendEthInitialized = payload => ({ type: AT.SEND_ETH_INITIALIZED, payload })
export const sendEthDestroyed = () => ({ type: AT.SEND_ETH_DESTROYED })
export const sendEthPaymentUpdated = payment => ({ type: AT.SEND_ETH_PAYMENT_UPDATED, payload: payment })
export const sendEthFirstStepInitialized = () => ({ type: AT.SEND_ETH_FIRST_STEP_INITIALIZED })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default ({ coreSagas }) => {
yield put(A.sendEthPaymentUpdated(Remote.Loading))
let payment = coreSagas.payment.eth.create(({ network: settings.NETWORK_ETHEREUM }))
payment = yield payment.init()
payment = yield payment.from()
payment = yield payment.from(action.payload.from, action.payload.type)
const initialValues = { coin: 'ETH' }
yield put(initialize('sendEth', initialValues))
yield put(A.sendEthPaymentUpdated(Remote.of(payment.value())))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@

export const INIT_TRANSFER_ETHER = 'INIT_TRANSFER_ETHER'

export const TRANSFER_ETHER = 'TRANSFER_ETHER'
export const CONFIRM_TRANSFER_ETH = 'CONFIRM_TRANSFER_ETH'
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as AT from './actionTypes'

export const initTransferEther = (balance) => ({ type: AT.INIT_TRANSFER_ETHER, payload: { balance } })

export const transferEther = () => ({ type: AT.TRANSFER_ETHER })
export const confirmTransferEth = payload => ({ type: AT.CONFIRM_TRANSFER_ETH, payload })
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { takeEvery, takeLatest } from 'redux-saga/effects'
import { takeLatest } from 'redux-saga/effects'
import * as AT from './actionTypes'
import sagas from './sagas'

export default ({ coreSagas }) => {
const transferEtherSagas = sagas({ coreSagas })
const transferEthSagas = sagas({ coreSagas })

return function * () {
yield takeEvery(AT.INIT_TRANSFER_ETHER, transferEtherSagas.initTransferEther)
yield takeLatest(AT.TRANSFER_ETHER, transferEtherSagas.transferEther)
yield takeLatest(AT.CONFIRM_TRANSFER_ETH, transferEthSagas.confirmTransferEth)
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import { put, call } from 'redux-saga/effects'
import { call, put, select } from 'redux-saga/effects'
import * as actions from '../../actions.js'
import * as selectors from './selectors'
import { promptForSecondPassword } from 'services/SagaService'
import settings from 'config'

export default ({ coreSagas }) => {
const initTransferEther = function * (action) {
const confirmTransferEth = function * (action) {
try {
const { balance } = action.payload
const { to, effectiveBalance } = action.payload
let p = yield select(selectors.getPayment)
let payment = coreSagas.payment.eth.create({ payment: p.getOrElse({}), network: settings.NETWORK_ETHEREUM })
payment = yield payment.to(to)
const password = yield call(promptForSecondPassword)
payment = yield payment.amount(effectiveBalance)
payment = yield payment.build()
payment = yield payment.sign(password)
yield payment.publish()
yield put(actions.modals.closeAllModals())
yield put(actions.modals.showModal('TransferEther', { balance }, { loading: true }))
yield call(coreSagas.data.ethereum.fetchFee)
yield put(actions.modals.updateModalOptions({ loading: false }))
yield put(actions.router.push('/eth/transactions'))
yield put(actions.alerts.displaySuccess('Ether transaction has been successfully published!'))
} catch (e) {
yield put(actions.alerts.displayError('Could not init transfer ether.'))
}
}

const transferEther = function * (action) {
try {
// const gasPrice = yield select(selectors.core.data.ethereum.getFeeRegular)
// const gasLimit = yield select(selectors.core.data.ethereum.getGasLimit)
// const legacyAccount = yield select(selectors.core.kvStore.ethereum.getLegacyAccount)
// const accounts = yield select(selectors.core.kvStore.ethereum.getAccounts)
// const defaultAccount = head(accounts)

// const transaction = yield call(coreSagas.data.ethereum.buildTx, { from, to, amount, gasPrice, gasLimit })
} catch (e) {
yield put(actions.alerts.displayError('Could not transfer ether.'))
yield put(actions.alerts.displayError('Ether transaction could not be published.'))
}
}

return {
initTransferEther,
transferEther
confirmTransferEth
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { path } from 'ramda'

export const getStep = path(['components', 'sendEth', 'step'])
export const getPayment = path(['components', 'sendEth', 'payment'])
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
import React from 'react'
import { connect } from 'react-redux'
import { bindActionCreators, compose } from 'redux'
import { head } from 'ramda'

import { actions, selectors } from 'data'
import { actions } from 'data'
import modalEnhancer from 'providers/ModalEnhancer'
import TransferEther from './template.js'
import { transactions } from 'blockchain-wallet-v4/src'
import Success from './template.success.js'
import { getData } from './selectors.js'
import { Remote } from 'blockchain-wallet-v4/src'

class TransferEtherContainer extends React.PureComponent {
constructor (props) {
super(props)
this.handleSubmit = this.handleSubmit.bind(this)
}

componentDidUpdate (prevProps) {
if (Remote.Success.is(this.props.data)) {
const { fee, effectiveBalance } = this.props.data.data
if (parseFloat(fee) > parseFloat(effectiveBalance)) this.props.modalActions.closeAllModals()
}
}

componentDidMount () {
this.props.sendEthActions.sendEthInitialized({ from: this.props.addr, type: 'LEGACY' })
}

handleSubmit () {
this.props.transferEtherActions.transferEther()
const { to, effectiveBalance } = this.props.data.data
this.props.transferEtherActions.confirmTransferEth({ to, effectiveBalance })
}

render () {
return <TransferEther handleSubmit={this.handleSubmit} {...this.props} />
const { addr, data } = this.props
return data.cata({
Success: (val) => <Success handleSubmit={this.handleSubmit} from={addr} val={val} {...this.props} />,
Loading: () => null,
NotAsked: () => null,
Failure: () => null
})
}
}

const mapStateToProps = state => {
const legacyAccount = selectors.core.data.ethereum.getLegacyAccount(state)
const legacyAccountAddress = legacyAccount.account
const defaultAccountAddress = head(selectors.core.kvStore.ethereum.getAccounts(state)).addr
const balance = legacyAccount.balance
const gasPrice = selectors.core.data.ethereum.getFeeRegular(state)
const gasLimit = selectors.core.data.ethereum.getGasLimit(state)
const fee = (gasPrice && gasLimit) ? transactions.ethereum.calculateFee(gasPrice, gasLimit) : 0

return {
from: legacyAccountAddress,
to: defaultAccountAddress,
balance,
fee
data: getData(state)
}
}

const mapDispatchToProps = (dispatch) => ({
modalActions: bindActionCreators(actions.modals, dispatch),
sendEthActions: bindActionCreators(actions.components.sendEth, dispatch),
transferEtherActions: bindActionCreators(actions.modules.transferEther, dispatch)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { lift, head, prop } from 'ramda'
const extractAddress = addr => prop('addr', head(addr))

export const getData = state => {
const paymentR = selectors.components.sendEth.getPayment(state)
const defaultAccountR = selectors.core.kvStore.ethereum.getAccounts(state).map(extractAddress)
const legacyAccountR = selectors.core.kvStore.ethereum.getLegacyAccount(state)

const transform = (defaultAccount, legacyAccount) => ({
to: defaultAccount,
from: legacyAccount
})
const transform = (defaultAccount, payment) => {
const effectiveBalance = prop('effectiveBalance', payment) || '0'
const fee = prop('fee', payment) || '0'

return lift(transform)(defaultAccountR, legacyAccountR)
return {
to: defaultAccount,
effectiveBalance,
fee
}
}

return lift(transform)(defaultAccountR, paymentR)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const Row = styled.div`
box-sizing: border-box;
`

const TransferEther = (props) => {
const Success = (props) => {
const { position, total, loading, ...rest } = props
const { handleSubmit, from, to, balance, fee } = rest
const { handleSubmit, from, val } = rest

return (
<Modal size='large' position={position} total={total}>
Expand All @@ -43,7 +43,7 @@ const TransferEther = (props) => {
<FormattedMessage id='modals.transferether.updated' defaultMessage="Because of this, we've updated your ether address and are requiring a transfer of your funds." />
</Text>
<Text size='14px' weight={300}>
<FormattedMessage id='modals.transferether.old' defaultMessage="Don't worry, your old address is still valid" />
<FormattedMessage id='modals.transferether.old' defaultMessage="Don't worry, your old address is still valid." />
</Text>
</TextGroup>
<Container>
Expand All @@ -60,24 +60,20 @@ const TransferEther = (props) => {
<FormattedMessage id='modals.transferether.label2' defaultMessage='To:' />
</Text>
<Text size='14px' weight={300}>
{to}
{val.to}
</Text>
</Row>
<Row>
<Text size='14px' weight={500}>
<FormattedMessage id='modals.transferether.label3' defaultMessage='Amount:' />
</Text>
<Text size='14px' weight={300}>
<CoinDisplay coin='ETH'>{balance}</CoinDisplay>
</Text>
<CoinDisplay size='14px' coin='ETH'>{val.effectiveBalance}</CoinDisplay>
</Row>
<Row>
<Text size='14px' weight={500}>
<FormattedMessage id='modals.transferether.label3' defaultMessage='Transaction Fee:' />
</Text>
<Text size='14px' weight={300}>
<CoinDisplay coin='ETH'>{fee}</CoinDisplay>
<FormattedMessage id='modals.transferether.label4' defaultMessage='Transaction Fee:' />
</Text>
<CoinDisplay size='14px' coin='ETH'>{val.fee}</CoinDisplay>
</Row>
</Container>
<Button nature='primary' fullwidth uppercase onClick={handleSubmit}>
Expand All @@ -88,4 +84,4 @@ const TransferEther = (props) => {
)
}

export default TransferEther
export default Success
8 changes: 4 additions & 4 deletions packages/blockchain-wallet-v4/src/redux/kvStore/sagas.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import whatsNew from './whatsNew/sagas'
// import ethereum from './ethereum/sagas'
// import bch from './bch/sagas'
import bch from './bch/sagas'
import ethereum from './ethereum/sagas'
import shapeShift from './shapeShift/sagas'
import buySell from './buySell/sagas'
import contacts from './contacts/sagas'
Expand All @@ -9,8 +9,8 @@ import root from './root/sagas'
export default ({ api }) => ({
root: root({ api }),
// whatsNew: whatsNew({ api }),
// ethereum: ethereum({ api }),
// bch: bch({ api }),
bch: bch({ api }),
ethereum: ethereum({ api }),
shapeShift: shapeShift({ api }),
buySell: buySell({ api }),
contacts: contacts({ api })
Expand Down
5 changes: 2 additions & 3 deletions packages/blockchain-wallet-v4/src/redux/payment/eth/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default ({ api }) => {
return makePayment(merge(p, { amount }))
},

* from (origin) {
* from (origin, type) {
let account = origin
if (origin === null || origin === undefined || origin === '') {
const accountR = (yield select(S.kvStore.ethereum.getDefaultAddress))
Expand All @@ -67,10 +67,9 @@ export default ({ api }) => {
const data = yield call(api.getEthereumBalances, account)
const balance = path([account, 'balance'], data)
const nonce = path([account, 'nonce'], data)

const effectiveBalance = calculateEffectiveBalance(balance, prop('fee', p))
const from = {
type: 'ACCOUNT',
type: type || 'ACCOUNT',
address: account,
nonce
}
Expand Down

0 comments on commit d96160a

Please sign in to comment.