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'
|
'use strict'
|
||||||
|
|
||||||
|
const _ = require('lodash/fp')
|
||||||
const Web3 = require('web3')
|
const Web3 = require('web3')
|
||||||
const web3 = new Web3()
|
const web3 = new Web3()
|
||||||
const hdkey = require('ethereumjs-wallet/hdkey')
|
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 hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
|
||||||
|
const bn2bn = bn => BN(bn.toString(16), 16)
|
||||||
|
|
||||||
function privateKey (account) {
|
function privateKey (account) {
|
||||||
return defaultWallet(account).getPrivateKey()
|
return defaultWallet(account).getPrivateKey()
|
||||||
|
|
@ -84,7 +86,7 @@ const pendingBalance = (address, cryptoCode) => {
|
||||||
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
|
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
|
||||||
|
|
||||||
function _balance (includePending, 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)
|
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||||
return contract.balanceOf(address.toLowerCase())
|
return contract.balanceOf(address.toLowerCase())
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +99,7 @@ function _balance (includePending, address, cryptoCode) {
|
||||||
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||||
const fromAddress = '0x' + wallet.getAddress().toString('hex')
|
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 toAddress = isErc20Token ? coins.utils.getErc20Token(cryptoCode).contractAddress : _toAddress.toLowerCase()
|
||||||
|
|
||||||
const txTemplate = {
|
const txTemplate = {
|
||||||
|
|
@ -113,33 +115,32 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||||
]
|
]
|
||||||
|
|
||||||
return Promise.all(promises)
|
return Promise.all(promises)
|
||||||
.then(arr => {
|
.then(([gas, gasPrice, txCount]) => [
|
||||||
const gas = arr[0]
|
bn2bn(gas),
|
||||||
const gasPrice = arr[1]
|
bn2bn(gasPrice),
|
||||||
const txCount = arr[2] <= lastUsedNonces[fromAddress]
|
_.max([1, txCount, lastUsedNonces[fromAddress] + 1])
|
||||||
? lastUsedNonces[fromAddress] + 1
|
])
|
||||||
: arr[2]
|
.then(([gas, gasPrice, txCount]) => {
|
||||||
|
|
||||||
lastUsedNonces[fromAddress] = txCount
|
lastUsedNonces[fromAddress] = txCount
|
||||||
|
|
||||||
const toSend = includesFee
|
const toSend = includesFee
|
||||||
? amount.minus(gasPrice.times(gas))
|
? amount.minus(gasPrice.times(gas))
|
||||||
: amount
|
: amount
|
||||||
|
|
||||||
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
|
||||||
|
|
||||||
const rawTx = {
|
const rawTx = {
|
||||||
chainId: 1,
|
chainId: 1,
|
||||||
nonce: txCount,
|
nonce: txCount,
|
||||||
gasPrice: hex(gasPrice),
|
gasPrice: hex(gasPrice),
|
||||||
gasLimit: gas,
|
gasLimit: hex(gas),
|
||||||
to: toAddress,
|
to: toAddress,
|
||||||
from: fromAddress,
|
from: fromAddress,
|
||||||
value: isErc20Token ? hex(BN(0)) : hex(toSend)
|
value: isErc20Token ? hex(BN(0)) : hex(toSend)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isErc20Token && contract) {
|
if (isErc20Token) {
|
||||||
rawTx.data = contract.transfer.getData(toAddress, hex(toSend))
|
const contract = web3.eth.contract(ABI.ERC20).at(toAddress)
|
||||||
|
if (contract)
|
||||||
|
rawTx.data = contract.transfer.getData(toAddress, hex(toSend))
|
||||||
}
|
}
|
||||||
|
|
||||||
const tx = new Tx(rawTx)
|
const tx = new Tx(rawTx)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue