fix: missing import
refactor: unify geth transaction between eth and erc20 tokens
This commit is contained in:
parent
8e099e3283
commit
de832e435b
2 changed files with 11 additions and 58 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue