chore: revert EIP-1559 commits

This commit is contained in:
Sérgio Salgado 2022-06-01 18:47:34 +01:00
parent 0e5aa0e58c
commit ef21fc8b9d
3 changed files with 31 additions and 629 deletions

View file

@ -4,8 +4,7 @@ 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')
const pify = require('pify')
@ -36,7 +35,9 @@ module.exports = {
}
function connect (url) {
web3.setProvider(new web3.providers.HttpProvider(url))
if (!web3.isConnected()) {
web3.setProvider(new web3.providers.HttpProvider(url))
}
}
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
@ -52,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.sendSignedTransaction))
.then(pify(web3.eth.sendRawTransaction))
.then(txid => {
return pify(web3.eth.getTransaction)(txid)
.then(tx => {
@ -116,37 +117,29 @@ 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.getBlock)('pending')
pify(web3.eth.getTransactionCount)(fromAddress)
]
return Promise.all(promises)
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
.then(([gas, gasPrice, txCount]) => [
BN(gas),
BN(gasPrice),
_.max([1, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas)
_.max([1, txCount, lastUsedNonces[fromAddress] + 1])
])
.then(([gas, gasPrice, txCount, baseFeePerGas]) => {
.then(([gas, gasPrice, txCount]) => {
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,
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
gasPrice: hex(gasPrice),
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
@ -157,12 +150,12 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
rawTx.data = contractData
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
const tx = new Tx(rawTx)
const privateKey = wallet.getPrivateKey()
const signedTx = tx.sign(privateKey)
tx.sign(privateKey)
return '0x' + signedTx.serialize().toString('hex')
return '0x' + tx.serialize().toString('hex')
})
}
@ -183,7 +176,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.sendSignedTransaction)(signedTx))
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
})
}