Merge pull request #1106 from siiky/fix/lam-332/ethereum-send
fix: Ethereum errors on send
This commit is contained in:
commit
47674d8c82
1 changed files with 15 additions and 14 deletions
|
|
@ -1,5 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const _ = require('lodash/fp')
|
||||
const Web3 = require('web3')
|
||||
const web3 = new Web3()
|
||||
const hdkey = require('ethereumjs-wallet/hdkey')
|
||||
|
|
@ -40,6 +41,7 @@ function connect (url) {
|
|||
}
|
||||
|
||||
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
|
||||
const bn2bn = bn => BN(bn.toString(16), 16)
|
||||
|
||||
function privateKey (account) {
|
||||
return defaultWallet(account).getPrivateKey()
|
||||
|
|
@ -84,7 +86,7 @@ const pendingBalance = (address, cryptoCode) => {
|
|||
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
|
||||
|
||||
function _balance (includePending, address, cryptoCode) {
|
||||
if (coins.utils.getCryptoCurrency(cryptoCode).type === 'erc-20') {
|
||||
if (coins.utils.isErc20Token(cryptoCode)) {
|
||||
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
return contract.balanceOf(address.toLowerCase())
|
||||
}
|
||||
|
|
@ -97,7 +99,7 @@ function _balance (includePending, address, cryptoCode) {
|
|||
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||
const fromAddress = '0x' + wallet.getAddress().toString('hex')
|
||||
|
||||
const isErc20Token = coins.utils.getCryptoCurrency(cryptoCode).type === 'erc-20'
|
||||
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
|
||||
const toAddress = isErc20Token ? coins.utils.getErc20Token(cryptoCode).contractAddress : _toAddress.toLowerCase()
|
||||
|
||||
const txTemplate = {
|
||||
|
|
@ -113,32 +115,31 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
]
|
||||
|
||||
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]
|
||||
|
||||
.then(([gas, gasPrice, txCount]) => [
|
||||
bn2bn(gas),
|
||||
bn2bn(gasPrice),
|
||||
_.max([1, txCount, lastUsedNonces[fromAddress] + 1])
|
||||
])
|
||||
.then(([gas, gasPrice, txCount]) => {
|
||||
lastUsedNonces[fromAddress] = txCount
|
||||
|
||||
const toSend = includesFee
|
||||
? amount.minus(gasPrice.times(gas))
|
||||
: amount
|
||||
|
||||
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
|
||||
const rawTx = {
|
||||
chainId: 1,
|
||||
nonce: txCount,
|
||||
gasPrice: hex(gasPrice),
|
||||
gasLimit: gas,
|
||||
gasLimit: hex(gas),
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
value: isErc20Token ? hex(BN(0)) : hex(toSend)
|
||||
}
|
||||
|
||||
if (isErc20Token && contract) {
|
||||
if (isErc20Token) {
|
||||
const contract = web3.eth.contract(ABI.ERC20).at(toAddress)
|
||||
if (contract)
|
||||
rawTx.data = contract.transfer.getData(toAddress, hex(toSend))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue