Skip to content

Commit

Permalink
"Send all" coins option
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaranov committed Jun 11, 2020
1 parent 93e48e2 commit de6ab01
Show file tree
Hide file tree
Showing 28 changed files with 789 additions and 254 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Current Master

- [#388](https://github.com/poanetwork/nifty-wallet/pull/388) - (Feature) "Send all" option for simple coin transfers
- [#385](https://github.com/poanetwork/nifty-wallet/pull/385) - (Feature) Display value of current pending tx's nonce on send tx screen
- [#384](https://github.com/poanetwork/nifty-wallet/pull/384) - (Fix) placement of HW Connect button title
- [#383](https://github.com/poanetwork/nifty-wallet/pull/383) - (Chore) Replace POA-ETH Binance link to POA-BTC
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import abiDecoder from 'abi-decoder'
abiDecoder.addABI(abi)

import TransactionStateManager from './tx-state-manager'
const TxGasUtil = require('./tx-gas-utils')
import TxGasUtil from './tx-gas-utils'
const PendingTransactionTracker = require('./pending-tx-tracker')
import NonceTracker from 'nonce-tracker'
import * as txUtils from './lib/util'
Expand Down
52 changes: 29 additions & 23 deletions app/scripts/controllers/transactions/tx-gas-utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const EthQuery = require('ethjs-query')
const {
hexToBn,
BnMultiplyByFraction,
bnToHex,
} = require('../../lib/util')
const { addHexPrefix } = require('ethereumjs-util')
const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send.
import EthQuery from 'ethjs-query'
import { hexToBn, BnMultiplyByFraction, bnToHex } from '../../lib/util'
import { addHexPrefix } from 'ethereumjs-util'
import { MIN_GAS_LIMIT_HEX } from '../../../../ui/app/components/send/send.constants'
import log from 'loglevel'

/**
tx-gas-utils are gas utility methods for Transaction manager
Expand All @@ -14,24 +11,31 @@ and used to do things like calculate gas of a tx.
@param {Object} provider - A network provider.
*/

class TxGasUtil {
export default class TxGasUtil {

constructor (provider) {
this.query = new EthQuery(provider)
}

/**
@param txMeta {Object} - the txMeta object
@returns {object} the txMeta object with the gas written to the txParams
@param {Object} txMeta - the txMeta object
@returns {GasAnalysisResult} The result of the gas analysis
*/
async analyzeGasUsage (txMeta) {
const block = await this.query.getBlockByNumber('latest', false)
let estimatedGasHex

// fallback to block gasLimit
const blockGasLimitBN = hexToBn(block.gasLimit)
const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20)
let estimatedGasHex = bnToHex(saferGasLimitBN)
try {
estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit)
} catch (err) {
} catch (error) {
log.warn(error)
txMeta.simulationFails = {
reason: err.message,
reason: error.message,
errorKey: error.errorKey,
debug: { blockNumber: block.number, blockGasLimit: block.gasLimit },
}
return txMeta
}
Expand Down Expand Up @@ -63,9 +67,9 @@ class TxGasUtil {
if (recipient) code = await this.query.getCode(recipient)

if (hasRecipient && (!code || code === '0x' || code === '0x0')) {
txParams.gas = SIMPLE_GAS_COST
txParams.gas = MIN_GAS_LIMIT_HEX
txMeta.simpleSend = true // Prevents buffer addition
return SIMPLE_GAS_COST
return MIN_GAS_LIMIT_HEX
}

// if not, fall back to block gasLimit
Expand Down Expand Up @@ -103,9 +107,9 @@ class TxGasUtil {
/**
Adds a gas buffer with out exceeding the block gas limit
@param initialGasLimitHex {string} - the initial gas limit to add the buffer too
@param blockGasLimitHex {string} - the block gas limit
@returns {string} the buffered gas limit as a hex string
@param {string} initialGasLimitHex - the initial gas limit to add the buffer too
@param {string} blockGasLimitHex - the block gas limit
@returns {string} - the buffered gas limit as a hex string
*/
addGasBuffer (initialGasLimitHex, blockGasLimitHex) {
const initialGasLimitBn = hexToBn(initialGasLimitHex)
Expand All @@ -114,12 +118,14 @@ class TxGasUtil {
const bufferedGasLimitBn = initialGasLimitBn.muln(1.5)

// if initialGasLimit is above blockGasLimit, dont modify it
if (initialGasLimitBn.gt(upperGasLimitBn)) return bnToHex(initialGasLimitBn)
if (initialGasLimitBn.gt(upperGasLimitBn)) {
return bnToHex(initialGasLimitBn)
}
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
if (bufferedGasLimitBn.lt(upperGasLimitBn)) return bnToHex(bufferedGasLimitBn)
if (bufferedGasLimitBn.lt(upperGasLimitBn)) {
return bnToHex(bufferedGasLimitBn)
}
// otherwise use blockGasLimit
return bnToHex(upperGasLimitBn)
}
}

module.exports = TxGasUtil
5 changes: 2 additions & 3 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ module.exports = class MetamaskController extends EventEmitter {
setDProvider: this.setDProvider.bind(this),
markPasswordForgotten: this.markPasswordForgotten.bind(this),
unMarkPasswordForgotten: this.unMarkPasswordForgotten.bind(this),
getGasPrice: (cb) => cb(null, this.getGasPrice()),
getGasPrice: nodeify(this.getGasPrice, this),
getPendingNonce: nodeify(this.getPendingNonce, this),

// shapeshift
Expand Down Expand Up @@ -1870,10 +1870,9 @@ module.exports = class MetamaskController extends EventEmitter {
* @returns {string} A hex representation of the suggested wei gas price.
*/
async getGasPrice () {
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve) => {
const { networkController } = this


const networkIdStr = networkController.store.getState().network
const networkId = parseInt(networkIdStr)
const isETHC = networkId === CLASSIC_CODE || networkId === MAINNET_CODE
Expand Down
3 changes: 2 additions & 1 deletion mascara/src/app/first-time/confirm-seed-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class ConfirmSeedScreen extends Component {
}
}

componentWillMount () {
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
const { seedWords, history } = this.props

if (!seedWords) {
Expand Down
3 changes: 2 additions & 1 deletion mascara/src/app/first-time/create-password-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class CreatePasswordScreen extends Component {
this.animationEventEmitter = new EventEmitter()
}

componentWillMount () {
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
const { isInitialized, history } = this.props

if (isInitialized) {
Expand Down
3 changes: 2 additions & 1 deletion mascara/src/app/first-time/seed-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class BackupPhraseScreen extends Component {
}
}

componentWillMount () {
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
const { seedWords, history } = this.props

if (!seedWords) {
Expand Down
3 changes: 2 additions & 1 deletion mascara/src/app/shapeshift-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class ShapeShiftForm extends Component {
isLoading: false,
};

componentWillMount () {
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
this.props.shapeShiftSubview()
}

Expand Down
2 changes: 1 addition & 1 deletion old-ui/app/add-suggested-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ AddSuggestedTokenScreen.prototype.render = function () {
)
}

AddSuggestedTokenScreen.prototype.componentWillMount = function () {
AddSuggestedTokenScreen.prototype.UNSAFE_componentWillMount = function () {
if (typeof global.ethereumProvider === 'undefined') return
}

Expand Down
3 changes: 2 additions & 1 deletion old-ui/app/components/add-token/add-token.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ export default class AddTokenScreen extends Component {
])
}

componentWillMount () {
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
if (typeof global.ethereumProvider === 'undefined') return

this.eth = new Eth(global.ethereumProvider)
Expand Down
Loading

0 comments on commit de6ab01

Please sign in to comment.