Skip to content

Commit

Permalink
Format transaction.type to hex. Add empty accessList is `tx.type …
Browse files Browse the repository at this point in the history
…=== '0x1'` (#5979)

* Add type to numberToHex formatting for _txInputFormatter

* Add test for type formatting

* Add logic to add empty accesslist if type === '0x1' when sending transaction. Add test for same

* Add input tx formatter test case

* Add input tx formatter test case

* Update CHANGELOG

* Fix failing test cases

* Update packages/web3-core-method/src/index.js

Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>

* Update CHANGELOG.md

---------

Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>
  • Loading branch information
spacesailor24 and jdevcs committed Apr 4, 2023
1 parent 9238e10 commit 4e5afa1
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,8 @@ Released with 1.0.0-beta.37 code base.
### Fixed

- Improved the error propagation in `web3-providers-http` package to effectively propagate useful error infomation about failed HTTP connections (#5955)

### Changed

- `transaction.type` is now formatted to a hex string before being send to provider (#5979)
- When sending a transaction, if `transaction.type === '0x1' && transaction.accessList === undefined`, then `transaction.accessList` is set to `[]` (#5979)
2 changes: 1 addition & 1 deletion packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var _txInputFormatter = function (options) {
delete options.gasPrice;
}

['gasPrice', 'gas', 'value', 'maxPriorityFeePerGas', 'maxFeePerGas', 'nonce'].filter(function (key) {
['gasPrice', 'gas', 'value', 'maxPriorityFeePerGas', 'maxFeePerGas', 'nonce', 'type'].filter(function (key) {
return options[key] !== undefined;
}).forEach(function (key) {
options[key] = utils.numberToHex(options[key]);
Expand Down
16 changes: 13 additions & 3 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,20 @@ Method.prototype.buildCall = function () {
return method.requestManager.send(payload, sendTxCallback);
};

// Send the actual transaction
if (isSendTx
const hasSendTxObject = isSendTx
&& !!payload.params[0]
&& typeof payload.params[0] === 'object'
&& typeof payload.params[0] === 'object';

if (hasSendTxObject &&
payload.params[0].type === '0x1'
&& typeof payload.params[0].accessList === 'undefined'
) {
payload.params[0].accessList = [];
}


// Send the actual transaction
if (hasSendTxObject
&& (
typeof payload.params[0].gasPrice === 'undefined'
&& (
Expand Down
21 changes: 21 additions & 0 deletions test/e2e.method.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ describe('method.send [ @E2E ]', function () {
assert(web3.utils.isHexStrict(receipt.transactionHash));
});

it('should yield 0x1 for type and add accessList property', async function() {
// ganache does not support eth_signTransaction
if (process.env.GANACHE || global.window ) return

var nonceVal = await web3.eth.getTransactionCount(accounts[0]);
var receipt = await web3.eth.sendTransaction({
to: accounts[1],
from: accounts[0],
nonce: nonceVal,
value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
gas: web3.utils.toHex(21000),
type: 1
});

assert(receipt.status === true);
assert(receipt.type === '0x1');

var fetchedTransaction = await web3.eth.getTransaction(receipt.transactionHash);
assert(fetchedTransaction.accessList.length === 0);
});

it('returns a receipt (EIP-1559, maxFeePerGas and maxPriorityFeePerGas specified)', async function () {
// ganache does not support eth_signTransaction
if (process.env.GANACHE || global.window ) return
Expand Down
147 changes: 147 additions & 0 deletions test/eth.sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,153 @@ var tests = [{
},
call: 'eth_'+ method
},
// test type
{
args: [{
from: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
to: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
value: '1234567654321',
gasPrice: '324234234234',
// Testing 10 is formatted to '0xa'
type: 10
}],
formattedArgs: [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x4b7dddc97a",
type: '0xa'
}],
result: '0x1234567',
formattedResult: '0x1234567',
notification: {
method: 'eth_subscription',
params: {
subscription: '0x1234567',
result: {
blockNumber: '0x10'
}
}
},
call: 'eth_'+ method
},
{
args: [{
from: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
to: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
value: '1234567654321',
gasPrice: '324234234234',
// Testing '10' is formatted to '0xa'
type: '10'
}],
formattedArgs: [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x4b7dddc97a",
type: '0xa'
}],
result: '0x1234567',
formattedResult: '0x1234567',
notification: {
method: 'eth_subscription',
params: {
subscription: '0x1234567',
result: {
blockNumber: '0x10'
}
}
},
call: 'eth_'+ method
},
{
args: [{
from: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
to: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
value: '1234567654321',
gasPrice: '324234234234',
// Testing 0x10 is formatted to '0x10'
type: 0x10
}],
formattedArgs: [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x4b7dddc97a",
type: '0x10'
}],
result: '0x1234567',
formattedResult: '0x1234567',
notification: {
method: 'eth_subscription',
params: {
subscription: '0x1234567',
result: {
blockNumber: '0x10'
}
}
},
call: 'eth_'+ method
},
{
args: [{
from: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
to: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
value: '1234567654321',
gasPrice: '324234234234',
// Testing 1 is formatted to '0x1'
type: 1
}],
formattedArgs: [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x4b7dddc97a",
type: '0x1',
accessList: []
}],
result: '0x1234567',
formattedResult: '0x1234567',
notification: {
method: 'eth_subscription',
params: {
subscription: '0x1234567',
result: {
blockNumber: '0x10'
}
}
},
call: 'eth_'+ method
},
{
args: [{
from: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
to: '0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', // checksum address
value: '1234567654321',
gasPrice: '324234234234',
// Testing "0xc0" is formatted to '0xc0'
type: "0xc0"
}],
formattedArgs: [{
from: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
to: "0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6",
value: "0x11f71f76bb1",
gasPrice: "0x4b7dddc97a",
type: '0xc0'
}],
result: '0x1234567',
formattedResult: '0x1234567',
notification: {
method: 'eth_subscription',
params: {
subscription: '0x1234567',
result: {
blockNumber: '0x10'
}
}
},
call: 'eth_'+ method
},
// test with gasPrice missing
{
args: [{
Expand Down
90 changes: 90 additions & 0 deletions test/formatters.inputTransactionFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,96 @@ var tests = [{
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8'
},
input: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: new bn(1000),
maxFeePerGas: new bn(1000),
type: 10
},
result: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8',
type: '0xa'
},
input: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: new bn(1000),
maxFeePerGas: new bn(1000),
type: '10'
},
result: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8',
type: '0xa'
},
input: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: new bn(1000),
maxFeePerGas: new bn(1000),
type: 0x10
},
result: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8',
type: '0x10'
},
input: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: new bn(1000),
maxFeePerGas: new bn(1000),
type: "0xc0"
},
result: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8',
type: '0xc0'
},
input: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: new bn(1000),
maxFeePerGas: new bn(1000),
type: 1
},
result: {
data: '0x34234bf23bf4234',
value: '0x64',
from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8',
gas: '0x3e8',
maxPriorityFeePerGas: '0x3e8',
maxFeePerGas: '0x3e8',
type: '0x1'
}
}];

Expand Down

0 comments on commit 4e5afa1

Please sign in to comment.