From 3029255b0c93877cd95bd1af4bd125c24601ad3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Thu, 28 Apr 2022 16:07:25 +0100 Subject: [PATCH] feat: upgrade to type 2 ETH transactions --- lib/plugins/wallet/geth/base.js | 37 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 56f23669..3904b911 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -4,7 +4,8 @@ const _ = require('lodash/fp') const Web3 = require('web3') const web3 = new Web3() const hdkey = require('ethereumjs-wallet/hdkey') -const Tx = require('ethereumjs-tx') +const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx') +const { default: Common, Chain, Hardfork } = require('@ethereumjs/common') const util = require('ethereumjs-util') const coins = require('@lamassu/coins') const pify = require('pify') @@ -35,13 +36,11 @@ 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) -const bn2bn = bn => BN(bn.toString(16), 16) +const bn2bn = bn => new BN(bn.toString(16), 16) function privateKey (account) { return defaultWallet(account).getPrivateKey() @@ -54,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 => { @@ -116,29 +115,37 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) { if (isErc20Token) txTemplate.data = contractData + const common = new Common({ chain: Chain.Ropsten, 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 }]) => [ bn2bn(gas), bn2bn(gasPrice), - _.max([1, txCount, lastUsedNonces[fromAddress] + 1]) + _.max([1, txCount, lastUsedNonces[fromAddress] + 1]), + bn2bn(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(2.5) // 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(web3.utils.toWei(maxPriorityFeePerGas.toString(), 'gwei')), + maxFeePerGas: web3.utils.toHex(web3.utils.toWei(maxFeePerGas.toString(), 'gwei')), gasLimit: hex(gas), to: toAddress, from: fromAddress, @@ -149,12 +156,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') }) } @@ -175,7 +182,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)) }) }