feat: re-enable USDT and eip-1559 commits
This commit is contained in:
parent
c94d767f0b
commit
168efc923a
26 changed files with 667 additions and 241 deletions
|
|
@ -4,6 +4,8 @@ const _ = require('lodash/fp')
|
|||
const Web3 = require('web3')
|
||||
const web3 = new Web3()
|
||||
const hdkey = require('ethereumjs-wallet/hdkey')
|
||||
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
|
||||
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
|
||||
const Tx = require('ethereumjs-tx')
|
||||
const util = require('ethereumjs-util')
|
||||
const coins = require('@lamassu/coins')
|
||||
|
|
@ -35,9 +37,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
function connect (url) {
|
||||
if (!web3.isConnected()) {
|
||||
web3.setProvider(new web3.providers.HttpProvider(url))
|
||||
}
|
||||
web3.setProvider(new web3.providers.HttpProvider(url))
|
||||
}
|
||||
|
||||
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
|
||||
|
|
@ -53,7 +53,7 @@ function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
|
|||
function sendCoins (account, tx, settings, operatorId) {
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
|
||||
.then(pify(web3.eth.sendRawTransaction))
|
||||
.then(pify(web3.eth.sendSignedTransaction))
|
||||
.then(txid => {
|
||||
return pify(web3.eth.getTransaction)(txid)
|
||||
.then(tx => {
|
||||
|
|
@ -80,14 +80,16 @@ function balance (account, cryptoCode, settings, operatorId) {
|
|||
|
||||
const pendingBalance = (address, cryptoCode) => {
|
||||
const promises = [_balance(true, address, cryptoCode), _balance(false, address, cryptoCode)]
|
||||
return Promise.all(promises).then(([pending, confirmed]) => pending.minus(confirmed))
|
||||
return Promise.all(promises).then(([pending, confirmed]) => BN(pending).minus(confirmed))
|
||||
}
|
||||
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
|
||||
|
||||
function _balance (includePending, address, cryptoCode) {
|
||||
if (coins.utils.isErc20Token(cryptoCode)) {
|
||||
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
return contract.balanceOf(address.toLowerCase())
|
||||
const contract = new web3.eth.Contract(ABI.ERC20, coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
return contract.methods.balanceOf(address.toLowerCase()).call((_, balance) => {
|
||||
return contract.methods.decimals().call((_, decimals) => BN(balance).div(10 ** decimals))
|
||||
})
|
||||
}
|
||||
const block = includePending ? 'pending' : undefined
|
||||
return pify(web3.eth.getBalance)(address.toLowerCase(), block)
|
||||
|
|
@ -103,8 +105,8 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
|
||||
let contract, contractData
|
||||
if (isErc20Token) {
|
||||
contract = web3.eth.contract(ABI.ERC20).at(toAddress)
|
||||
contractData = isErc20Token && contract.transfer.getData(_toAddress.toLowerCase(), hex(toSend))
|
||||
contract = new web3.eth.Contract(ABI.ERC20, toAddress)
|
||||
contractData = isErc20Token && contract.methods.transfer(_toAddress.toLowerCase(), hex(toSend)).encodeABI()
|
||||
}
|
||||
|
||||
const txTemplate = {
|
||||
|
|
@ -115,29 +117,37 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
|
||||
if (isErc20Token) txTemplate.data = contractData
|
||||
|
||||
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
|
||||
|
||||
const promises = [
|
||||
pify(web3.eth.estimateGas)(txTemplate),
|
||||
pify(web3.eth.getGasPrice)(),
|
||||
pify(web3.eth.getTransactionCount)(fromAddress)
|
||||
pify(web3.eth.getTransactionCount)(fromAddress),
|
||||
pify(web3.eth.getBlock)('pending')
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([gas, gasPrice, txCount]) => [
|
||||
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
|
||||
BN(gas),
|
||||
BN(gasPrice),
|
||||
_.max([0, txCount, lastUsedNonces[fromAddress] + 1])
|
||||
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
|
||||
BN(baseFeePerGas)
|
||||
])
|
||||
.then(([gas, gasPrice, txCount]) => {
|
||||
.then(([gas, gasPrice, txCount, baseFeePerGas]) => {
|
||||
lastUsedNonces[fromAddress] = txCount
|
||||
|
||||
const toSend = includesFee
|
||||
? amount.minus(gasPrice.times(gas))
|
||||
: amount
|
||||
|
||||
const maxPriorityFeePerGas = new BN(web3.utils.toWei('2.5', 'gwei')) // web3 default value
|
||||
const maxFeePerGas = new BN(2).times(baseFeePerGas).plus(maxPriorityFeePerGas)
|
||||
|
||||
const rawTx = {
|
||||
chainId: 1,
|
||||
nonce: txCount,
|
||||
gasPrice: hex(gasPrice),
|
||||
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
|
||||
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
|
||||
gasLimit: hex(gas),
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
|
|
@ -148,12 +158,12 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
rawTx.data = contractData
|
||||
}
|
||||
|
||||
const tx = new Tx(rawTx)
|
||||
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
|
||||
tx.sign(privateKey)
|
||||
const signedTx = tx.sign(privateKey)
|
||||
|
||||
return '0x' + tx.serialize().toString('hex')
|
||||
return '0x' + signedTx.serialize().toString('hex')
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +184,7 @@ function sweep (account, cryptoCode, hdIndex, settings, operatorId) {
|
|||
if (r.eq(0)) return
|
||||
|
||||
return generateTx(defaultAddress(account), wallet, r, true, cryptoCode)
|
||||
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
|
||||
.then(signedTx => pify(web3.eth.sendSignedTransaction)(signedTx))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue