Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding issue when sending max tokens #5695

Merged
merged 4 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions test/e2e/beta/metamask-beta-ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ describe('MetaMask', function () {
})

it('renders the balance for the new token', async () => {
const balance = await findElement(driver, By.css('.transaction-view-balance .transaction-view-balance__token-balance'))
const balance = await findElement(driver, By.css('.transaction-view-balance .transaction-view-balance__primary-balance'))
await driver.wait(until.elementTextMatches(balance, /^100\s*TST\s*$/))
const tokenAmount = await balance.getText()
assert.ok(/^100\s*TST\s*$/.test(tokenAmount))
Expand Down Expand Up @@ -894,8 +894,8 @@ describe('MetaMask', function () {
// test cancelled on firefox until https://github.com/mozilla/geckodriver/issues/906 is resolved,
// or possibly until we use latest version of firefox in the tests
if (process.env.SELENIUM_BROWSER !== 'firefox') {
const tokenBalanceAmount = await findElement(driver, By.css('.transaction-view-balance__token-balance'))
assert.equal(await tokenBalanceAmount.getText(), '43 TST')
const tokenBalanceAmount = await findElements(driver, By.css('.transaction-view-balance__primary-balance'))
await driver.wait(until.elementTextMatches(tokenBalanceAmount[0], /43\s*TST/))
}
})
})
Expand Down Expand Up @@ -1055,7 +1055,7 @@ describe('MetaMask', function () {
})

it('renders the balance for the chosen token', async () => {
const balance = await findElement(driver, By.css('.transaction-view-balance__token-balance'))
const balance = await findElement(driver, By.css('.transaction-view-balance__primary-balance'))
await driver.wait(until.elementTextMatches(balance, /0\s*BAT/))
await delay(regularDelayMs)
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ui/app/selectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('Selectors', function () {

it('#getSelectedTokenToFiatRate', () => {
const selectedTokenToFiatRate = selectors.getSelectedTokenToFiatRate(mockState)
assert.equal(selectedTokenToFiatRate, '0.21880988420033493')
assert.equal(selectedTokenToFiatRate, '0.21880988420033492152')
})

describe('#getSelectedTokenContract', () => {
Expand Down
2 changes: 1 addition & 1 deletion ui/app/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ function updateSendTokenBalance ({
.then(usersToken => {
if (usersToken) {
const newTokenBalance = calcTokenBalance({ selectedToken, usersToken })
dispatch(setSendTokenBalance(newTokenBalance.toString(10)))
dispatch(setSendTokenBalance(newTokenBalance))
}
})
.catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ const mergeProps = (stateProps, dispatchProps, ownProps) => {
currency,
denomination,
hideLabel,
displayValue: propsDisplayValue,
suffix: propsSuffix,
...restOwnProps
} = ownProps

const toCurrency = currency || currentCurrency
const convertedValue = getValueFromWeiHex({
value, fromCurrency: nativeCurrency, toCurrency, conversionRate, numberOfDecimals, toDenomination: denomination,
})
const displayValue = formatCurrency(convertedValue, toCurrency)
const suffix = hideLabel ? undefined : toCurrency.toUpperCase()

const displayValue = propsDisplayValue || formatCurrency(
getValueFromWeiHex({
value,
fromCurrency: nativeCurrency,
toCurrency, conversionRate,
numberOfDecimals,
toDenomination: denomination,
}),
toCurrency
)
const suffix = propsSuffix || (hideLabel ? undefined : toCurrency.toUpperCase())

return {
...restStateProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('CurrencyDisplay container', () => {
},
result: {
nativeCurrency: 'ETH',
displayValue: '1e-9',
displayValue: '0.000000001',
suffix: undefined,
},
},
Expand Down
2 changes: 2 additions & 0 deletions ui/app/components/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

@import './tabs/index';

@import './token-balance/index';

@import './transaction-activity-log/index';

@import './transaction-breakdown/index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default class AmountMaxButton extends Component {
setAmountToMax: PropTypes.func,
setMaxModeTo: PropTypes.func,
tokenBalance: PropTypes.string,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

setMaxAmount () {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ const {
const ethUtil = require('ethereumjs-util')

function calcMaxAmount ({ balance, gasTotal, selectedToken, tokenBalance }) {
const { decimals } = selectedToken || {}
const multiplier = Math.pow(10, Number(decimals || 0))
const { decimals } = selectedToken || {}
const multiplier = Math.pow(10, Number(decimals || 0))

return selectedToken
? multiplyCurrencies(tokenBalance, multiplier, {toNumericBase: 'hex'})
: subtractCurrencies(
ethUtil.addHexPrefix(balance),
ethUtil.addHexPrefix(gasTotal),
{ toNumericBase: 'hex' }
)
return selectedToken
? multiplyCurrencies(
tokenBalance,
multiplier,
{
toNumericBase: 'hex',
multiplicandBase: 16,
}
)
: subtractCurrencies(
ethUtil.addHexPrefix(balance),
ethUtil.addHexPrefix(gasTotal),
{ toNumericBase: 'hex' }
)
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('amount-max-button utils', () => {
selectedToken: {
decimals: 10,
},
tokenBalance: 100,
tokenBalance: '64',
}), 'e8d4a51000')
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export default class SendAmountRow extends Component {
updateSendAmount: PropTypes.func,
updateSendAmountError: PropTypes.func,
updateGas: PropTypes.func,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

validateAmount (amount) {
const {
Expand Down Expand Up @@ -58,7 +58,6 @@ export default class SendAmountRow extends Component {

if (selectedToken) {
updateGasFeeError({
amount,
amountConversionRate,
balance,
conversionRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ function mapDispatchToProps (dispatch) {
setMaxModeTo: bool => dispatch(setMaxModeTo(bool)),
updateSendAmount: newAmount => dispatch(updateSendAmount(newAmount)),
updateGasFeeError: (amountDataObject) => {
dispatch(updateSendErrors(getGasFeeErrorObject(amountDataObject)))
dispatch(updateSendErrors(getGasFeeErrorObject(amountDataObject)))
},
updateSendAmountError: (amountDataObject) => {
dispatch(updateSendErrors(getAmountErrorObject(amountDataObject)))
dispatch(updateSendErrors(getAmountErrorObject(amountDataObject)))
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ describe('SendAmountRow Component', function () {
assert.deepEqual(
propsMethodSpies.updateGasFeeError.getCall(0).args,
[{
amount: 'someAmount',
amountConversionRate: 'mockAmountConversionRate',
balance: 'mockBalance',
conversionRate: 7,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export default class FromDropdown extends Component {
onSelect: PropTypes.func,
openDropdown: PropTypes.func,
selectedAccount: PropTypes.object,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

render () {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export default class SendFromRow extends Component {
tokenContract: PropTypes.object,
updateSendFrom: PropTypes.func,
setSendTokenBalance: PropTypes.func,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

async handleFromChange (newFrom) {
const {
Expand All @@ -32,6 +32,7 @@ export default class SendFromRow extends Component {
const usersToken = await tokenContract.balanceOf(newFrom.address)
setSendTokenBalance(usersToken)
}

updateSendFrom(newFrom)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from './send-from-row.selectors.js'
import { calcTokenBalance } from '../../send.utils.js'
import {
updateSendFrom,
setSendTokenBalance,
updateSendFrom,
setSendTokenBalance,
} from '../../../../actions'
import {
closeFromDropdown,
Expand All @@ -37,10 +37,10 @@ function mapDispatchToProps (dispatch) {
openFromDropdown: () => dispatch(openFromDropdown()),
updateSendFrom: newFrom => dispatch(updateSendFrom(newFrom)),
setSendTokenBalance: (usersToken, selectedToken) => {
if (!usersToken) return
if (!usersToken) return

const tokenBalance = calcTokenBalance({ usersToken, selectedToken })
dispatch(setSendTokenBalance(tokenBalance))
const tokenBalance = calcTokenBalance({ usersToken, selectedToken })
dispatch(setSendTokenBalance(tokenBalance))
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export default class SendGasRow extends Component {
gasLoadingError: PropTypes.bool,
gasTotal: PropTypes.string,
showCustomizeGasModal: PropTypes.func,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

render () {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { connect } from 'react-redux'
import {
getConversionRate,
getCurrentCurrency,
getGasTotal,
getConversionRate,
getCurrentCurrency,
getGasTotal,
} from '../../send.selectors.js'
import { getGasLoadingError, gasFeeIsInError } from './send-gas-row.selectors.js'
import { showModal } from '../../../../actions'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export default class SendToRow extends Component {
updateSendTo: PropTypes.func,
updateSendToError: PropTypes.func,
scanQrCode: PropTypes.func,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

handleToChange (to, nickname = '', toError) {
const { hasHexData, updateSendTo, updateSendToError, updateGas } = this.props
Expand Down
4 changes: 2 additions & 2 deletions ui/app/components/send/send-footer/send-footer.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export default class SendFooter extends Component {
tokenBalance: PropTypes.string,
unapprovedTxs: PropTypes.object,
update: PropTypes.func,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

onCancel () {
this.props.clearSend()
Expand Down
6 changes: 2 additions & 4 deletions ui/app/components/send/send.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export default class SendTransactionScreen extends PersistentForm {
scanQrCode: PropTypes.func,
qrCodeDetected: PropTypes.func,
qrCodeData: PropTypes.object,
};
}

static contextTypes = {
t: PropTypes.func,
};
}

componentWillReceiveProps (nextProps) {
if (nextProps.qrCodeData) {
Expand Down Expand Up @@ -138,14 +138,12 @@ export default class SendTransactionScreen extends PersistentForm {
})
const gasFeeErrorObject = selectedToken
? getGasFeeErrorObject({
amount,
amountConversionRate,
balance,
conversionRate,
gasTotal,
primaryCurrency,
selectedToken,
tokenBalance,
})
: { gasFee: null }
updateSendErrors(Object.assign(amountErrorObject, gasFeeErrorObject))
Expand Down
6 changes: 2 additions & 4 deletions ui/app/components/send/send.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ function isTokenBalanceSufficient ({
const tokenBalanceIsSufficient = conversionGTE(
{
value: tokenBalance,
fromNumericBase: 'dec',
fromNumericBase: 'hex',
},
{
value: calcTokenAmount(amountInDec, decimals),
fromNumericBase: 'dec',
},
)

Expand Down Expand Up @@ -151,7 +150,6 @@ function getAmountErrorObject ({
}

function getGasFeeErrorObject ({
amount,
amountConversionRate,
balance,
conversionRate,
Expand Down Expand Up @@ -180,7 +178,7 @@ function getGasFeeErrorObject ({

function calcTokenBalance ({ selectedToken, usersToken }) {
const { decimals } = selectedToken || {}
return calcTokenAmount(usersToken.balance.toString(), decimals) + ''
return calcTokenAmount(usersToken.balance.toString(), decimals).toString(16)
}

function doesAmountErrorRequireUpdate ({
Expand Down
2 changes: 0 additions & 2 deletions ui/app/components/send/tests/send-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,12 @@ describe('Send Component', function () {
assert.deepEqual(
utilsMethodStubs.getGasFeeErrorObject.getCall(0).args[0],
{
amount: 'mockAmount',
amountConversionRate: 'mockAmountConversionRate',
balance: 'mockBalance',
conversionRate: 10,
gasTotal: 'mockGasTotal',
primaryCurrency: 'mockPrimaryCurrency',
selectedToken: 'mockSelectedToken',
tokenBalance: 'mockTokenBalance',
}
)
})
Expand Down
3 changes: 1 addition & 2 deletions ui/app/components/send/tests/send-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,10 @@ describe('send utils', () => {
[
{
value: 123,
fromNumericBase: 'dec',
fromNumericBase: 'hex',
},
{
value: 'calc:1610',
fromNumericBase: 'dec',
},
]
)
Expand Down
Loading