feat: upgrade to type 2 ETH transactions
This commit is contained in:
parent
59abb20cd3
commit
3029255b0c
1 changed files with 22 additions and 15 deletions
|
|
@ -4,7 +4,8 @@ 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')
|
||||||
const Tx = require('ethereumjs-tx')
|
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
|
||||||
|
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
|
||||||
const util = require('ethereumjs-util')
|
const util = require('ethereumjs-util')
|
||||||
const coins = require('@lamassu/coins')
|
const coins = require('@lamassu/coins')
|
||||||
const pify = require('pify')
|
const pify = require('pify')
|
||||||
|
|
@ -35,13 +36,11 @@ module.exports = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect (url) {
|
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 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) {
|
function privateKey (account) {
|
||||||
return defaultWallet(account).getPrivateKey()
|
return defaultWallet(account).getPrivateKey()
|
||||||
|
|
@ -54,7 +53,7 @@ function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
|
||||||
function sendCoins (account, tx, settings, operatorId) {
|
function sendCoins (account, tx, settings, operatorId) {
|
||||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
|
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
|
||||||
.then(pify(web3.eth.sendRawTransaction))
|
.then(pify(web3.eth.sendSignedTransaction))
|
||||||
.then(txid => {
|
.then(txid => {
|
||||||
return pify(web3.eth.getTransaction)(txid)
|
return pify(web3.eth.getTransaction)(txid)
|
||||||
.then(tx => {
|
.then(tx => {
|
||||||
|
|
@ -116,29 +115,37 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||||
|
|
||||||
if (isErc20Token) txTemplate.data = contractData
|
if (isErc20Token) txTemplate.data = contractData
|
||||||
|
|
||||||
|
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.London })
|
||||||
|
|
||||||
const promises = [
|
const promises = [
|
||||||
pify(web3.eth.estimateGas)(txTemplate),
|
pify(web3.eth.estimateGas)(txTemplate),
|
||||||
pify(web3.eth.getGasPrice)(),
|
pify(web3.eth.getGasPrice)(),
|
||||||
pify(web3.eth.getTransactionCount)(fromAddress)
|
pify(web3.eth.getTransactionCount)(fromAddress),
|
||||||
|
pify(web3.eth.getBlock)('pending')
|
||||||
]
|
]
|
||||||
|
|
||||||
return Promise.all(promises)
|
return Promise.all(promises)
|
||||||
.then(([gas, gasPrice, txCount]) => [
|
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
|
||||||
bn2bn(gas),
|
bn2bn(gas),
|
||||||
bn2bn(gasPrice),
|
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
|
lastUsedNonces[fromAddress] = txCount
|
||||||
|
|
||||||
const toSend = includesFee
|
const toSend = includesFee
|
||||||
? amount.minus(gasPrice.times(gas))
|
? amount.minus(gasPrice.times(gas))
|
||||||
: amount
|
: amount
|
||||||
|
|
||||||
|
const maxPriorityFeePerGas = new BN(2.5) // web3 default value
|
||||||
|
const maxFeePerGas = new BN(2).times(baseFeePerGas).plus(maxPriorityFeePerGas)
|
||||||
|
|
||||||
const rawTx = {
|
const rawTx = {
|
||||||
chainId: 1,
|
chainId: 1,
|
||||||
nonce: txCount,
|
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),
|
gasLimit: hex(gas),
|
||||||
to: toAddress,
|
to: toAddress,
|
||||||
from: fromAddress,
|
from: fromAddress,
|
||||||
|
|
@ -149,12 +156,12 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||||
rawTx.data = contractData
|
rawTx.data = contractData
|
||||||
}
|
}
|
||||||
|
|
||||||
const tx = new Tx(rawTx)
|
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
|
||||||
const privateKey = wallet.getPrivateKey()
|
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
|
if (r.eq(0)) return
|
||||||
|
|
||||||
return generateTx(defaultAddress(account), wallet, r, true, cryptoCode)
|
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