feat: support eth EIP-1559
This commit is contained in:
parent
61c4ca9ae7
commit
44ee2250b5
3 changed files with 652 additions and 68 deletions
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue