diff --git a/lib/commission-math.js b/lib/commission-math.js index d228f3d8..d2f1dd84 100644 --- a/lib/commission-math.js +++ b/lib/commission-math.js @@ -6,7 +6,7 @@ function truncateCrypto (cryptoAtoms, cryptoCode) { const DECIMAL_PLACES = 3 if (cryptoAtoms.eq(0)) return cryptoAtoms - const scale = utils.getCryptoCurrency(cryptoCode).displayScale + const scale = coinUtils.getCryptoCurrency(cryptoCode).displayScale const scaleFactor = BN(10).pow(scale) return BN(cryptoAtoms).truncated().div(scaleFactor) diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 93fd43ce..ddf8fa46 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -91,64 +91,11 @@ function _balance (includePending, address, cryptoCode) { return pify(web3.eth.getBalance)(address.toLowerCase(), block) } -function generateTx (toAddress, wallet, amount, includesFee, cryptoCode) { - return coins.utils.getCryptoCurrency(cryptoCode).type === 'erc-20' - ? generateContractTx(toAddress, wallet, amount, includesFee, cryptoCode) - : generateCoinTx(toAddress, wallet, amount, includesFee) -} - -function generateContractTx (_toAddress, wallet, amount, includesFee, cryptoCode) { - const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress) - +function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) { const fromAddress = '0x' + wallet.getAddress().toString('hex') - const toAddress = _toAddress.toLowerCase() - const txTemplate = { - from: fromAddress, - to: coins.utils.getErc20Token(cryptoCode).contractAddress, - value: amount - } - - const promises = [ - pify(web3.eth.estimateGas)(txTemplate), - pify(web3.eth.getGasPrice)(), - pify(web3.eth.getTransactionCount)(fromAddress) - ] - - return Promise.all(promises) - .then(arr => { - const gas = arr[0] - const gasPrice = arr[1] - const txCount = arr[2] <= lastUsedNonces[fromAddress] - ? lastUsedNonces[fromAddress] + 1 - : arr[2] - - const toSend = includesFee - ? amount.minus(gasPrice.times(gas)) - : amount - - const rawTx = { - nonce: txCount, - gasPrice: hex(gasPrice), - gasLimit: gas, - to: coins.utils.getErc20Token(cryptoCode).contractAddress, - from: fromAddress, - value: hex(BN(0)), - data: contract.transfer.getData(toAddress, hex(toSend)) - } - - const tx = new Tx(rawTx) - const privateKey = wallet.getPrivateKey() - - tx.sign(privateKey) - - return '0x' + tx.serialize().toString('hex') - }) -} - -function generateCoinTx (_toAddress, wallet, amount, includesFee) { - const fromAddress = '0x' + wallet.getAddress().toString('hex') - const toAddress = _toAddress.toLowerCase() + const isErc20Token = coins.utils.getCryptoCurrency(cryptoCode).type === 'erc-20' + const toAddress = isErc20Token ? coins.utils.getErc20Token(cryptoCode).contractAddress : _toAddress.toLowerCase() const txTemplate = { from: fromAddress, @@ -176,13 +123,19 @@ function generateCoinTx (_toAddress, wallet, amount, includesFee) { ? amount.minus(gasPrice.times(gas)) : amount + const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress) + const rawTx = { nonce: txCount, gasPrice: hex(gasPrice), gasLimit: gas, to: toAddress, from: fromAddress, - value: hex(toSend) + value: isErc20Token ? hex(BN(0)) : hex(toSend) + } + + if (isErc20Token && contract) { + rawTx.data = contract.transfer.getData(toAddress, hex(toSend)) } const tx = new Tx(rawTx)