Skip to content

Commit

Permalink
Don't default type = 0x0 for eth_sendTransaction and eth_sendRawTrans…
Browse files Browse the repository at this point in the history
…action (#4241)

* Remove type = 0x0 default

* Update gasPrice short circuit check

* Update tests - remove type: 0x0

* Update CHANGELOG

* Apply patch fix to web3-eth-accounts

* Remove tx type defaulting

* update CHANGELOG

* update failing test

* Remove unused import

* Add quick check that user can pass type, and it's retained
  • Loading branch information
spacesailor24 authored Aug 15, 2021
1 parent b383a8e commit e1556ff
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 92 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,8 @@ Released with 1.0.0-beta.37 code base.
## [Unreleased]

## [1.5.2]

### Fixed

- Remove transaction `type` defaulting for `eth.sendTransaction`, `eth.sendRawTransaction` (#4241)
- `type: 0x0` was being added to legacy transaction when using `eth.signTransaction` (#4241)
114 changes: 31 additions & 83 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var formatters = require('web3-core-helpers').formatters;
var utils = require('web3-utils');
var promiEvent = require('web3-core-promievent');
var Subscriptions = require('web3-core-subscriptions').subscriptions;
var HardForks = require('@ethereumjs/common').Hardfork;

var EthersTransactionUtils = require('@ethersproject/transactions');

Expand Down Expand Up @@ -782,9 +781,6 @@ Method.prototype.buildCall = function () {
)
)
) {
if (typeof payload.params[0].type === 'undefined')
payload.params[0].type = _handleTxType(payload.params[0]);

_handleTxPricing(method, payload.params[0]).then(txPricing => {
if (txPricing.gasPrice !== undefined) {
payload.params[0].gasPrice = txPricing.gasPrice;
Expand Down Expand Up @@ -830,46 +826,6 @@ Method.prototype.buildCall = function () {
return send;
};

function _handleTxType(tx) {
// Taken from https://github.com/ethers-io/ethers.js/blob/2a7ce0e72a1e0c9469e10392b0329e75e341cf18/packages/abstract-signer/src.ts/index.ts#L215
const hasEip1559 = (tx.maxFeePerGas !== undefined || tx.maxPriorityFeePerGas !== undefined);

let txType;

if (tx.type !== undefined) {
txType = utils.toHex(tx.type)
} else if (tx.type === undefined && hasEip1559) {
txType = '0x2'
} else {
txType = '0x0'
}

if (tx.gasPrice !== undefined && (txType === '0x2' || hasEip1559))
throw Error("eip-1559 transactions don't support gasPrice");
if ((txType === '0x1' || txType === '0x0') && hasEip1559)
throw Error("pre-eip-1559 transaction don't support maxFeePerGas/maxPriorityFeePerGas");

if (
hasEip1559 ||
(
(tx.common && tx.common.hardfork && tx.common.hardfork.toLowerCase() === HardForks.London) ||
(tx.hardfork && tx.hardfork.toLowerCase() === HardForks.London)
)
) {
txType = '0x2';
} else if (
tx.accessList ||
(
(tx.common && tx.common.hardfork && tx.common.hardfork.toLowerCase() === HardForks.Berlin) ||
(tx.hardfork && tx.hardfork.toLowerCase() === HardForks.Berlin)
)
) {
txType = '0x1';
}

return txType
}

function _handleTxPricing(method, tx) {
return new Promise((resolve, reject) => {
try {
Expand All @@ -889,47 +845,39 @@ function _handleTxPricing(method, tx) {
params: 0
})).createFunction(method.requestManager);

if (tx.type < '0x2' && tx.gasPrice !== undefined) {
// Legacy transaction, return provided gasPrice
resolve({ gasPrice: tx.gasPrice })
} else {
Promise.all([
getBlockByNumber(),
getGasPrice()
]).then(responses => {
const [block, gasPrice] = responses;
if (
(tx.type === '0x2') &&
block && block.baseFeePerGas
) {
// The network supports EIP-1559

// Taken from https://github.com/ethers-io/ethers.js/blob/ba6854bdd5a912fe873d5da494cb5c62c190adde/packages/abstract-provider/src.ts/index.ts#L230
let maxPriorityFeePerGas, maxFeePerGas;

if (tx.gasPrice) {
// Using legacy gasPrice property on an eip-1559 network,
// so use gasPrice as both fee properties
maxPriorityFeePerGas = tx.gasPrice;
maxFeePerGas = tx.gasPrice;
delete tx.gasPrice;
} else {
maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x3B9ACA00'; // 1 Gwei
maxFeePerGas = tx.maxFeePerGas ||
utils.toHex(
utils.toBN(block.baseFeePerGas)
.mul(utils.toBN(2))
.add(utils.toBN(maxPriorityFeePerGas))
);
}
resolve({ maxFeePerGas, maxPriorityFeePerGas });
Promise.all([
getBlockByNumber(),
getGasPrice()
]).then(responses => {
const [block, gasPrice] = responses;
if (block && block.baseFeePerGas) {
// The network supports EIP-1559

// Taken from https://github.com/ethers-io/ethers.js/blob/ba6854bdd5a912fe873d5da494cb5c62c190adde/packages/abstract-provider/src.ts/index.ts#L230
let maxPriorityFeePerGas, maxFeePerGas;

if (tx.gasPrice) {
// Using legacy gasPrice property on an eip-1559 network,
// so use gasPrice as both fee properties
maxPriorityFeePerGas = tx.gasPrice;
maxFeePerGas = tx.gasPrice;
delete tx.gasPrice;
} else {
if (tx.maxPriorityFeePerGas || tx.maxFeePerGas)
throw Error("Network doesn't support eip-1559")
resolve({ gasPrice });
maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x3B9ACA00'; // 1 Gwei
maxFeePerGas = tx.maxFeePerGas ||
utils.toHex(
utils.toBN(block.baseFeePerGas)
.mul(utils.toBN(2))
.add(utils.toBN(maxPriorityFeePerGas))
);
}
})
}
resolve({ maxFeePerGas, maxPriorityFeePerGas });
} else {
if (tx.maxPriorityFeePerGas || tx.maxFeePerGas)
throw Error("Network doesn't support eip-1559")
resolve({ gasPrice });
}
})
} catch (error) {
reject(error)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ function _handleTxType(tx) {
txType = utils.toHex(tx.type)
} else if (tx.type === undefined && hasEip1559) {
txType = '0x2'
} else {
txType = '0x0'
}

if (tx.gasPrice !== undefined && (txType === '0x2' || hasEip1559))
Expand Down Expand Up @@ -364,7 +362,10 @@ function _handleTxType(tx) {
function _handleTxPricing(_this, tx) {
return new Promise((resolve, reject) => {
try {
if (tx.type < '0x2' && tx.gasPrice !== undefined) {
if (
(tx.type === undefined || tx.type < '0x2')
&& tx.gasPrice !== undefined
) {
// Legacy transaction, return provided gasPrice
resolve({ gasPrice: tx.gasPrice })
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2678,8 +2678,8 @@ var runTests = function(contractFactory) {
'0000000000000000000000000000000000000000000000000000000000000011' ,
to: addressLowercase,
from: addressLowercase,
gasPrice: '0x45656456456456',
type: '0x0'
maxPriorityFeePerGas: '0x3B9ACA00',
maxFeePerGas: '0x3b9aca0e'
}]);

done();
Expand Down
3 changes: 1 addition & 2 deletions test/eth.sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ var tests = [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x1234567",
type: "0x0"
gasPrice: "0x1234567"
}],
result3: '0x1234567'
},{
Expand Down
5 changes: 3 additions & 2 deletions test/method.buildCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('lib/web3/method', function () {
to: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
data: '0xa123456',
gasPrice: '0x1234567453543456321456321',
type: '0x0'
type: '0x2'
}]);

done();
Expand All @@ -221,7 +221,8 @@ describe('lib/web3/method', function () {
send({
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
data: '0xa123456'
data: '0xa123456',
type: '0x2'
});

});
Expand Down

0 comments on commit e1556ff

Please sign in to comment.