Skip to content

Commit

Permalink
hexToNumber: return BigInt if result is bigger than max integer (#5157)
Browse files Browse the repository at this point in the history
* In hexToNumber return BigInt if result is bigger than max integer

* Add some BigInt test

* Update CHANGELOG

* Add hex format as option for output in transaction receipt

* Revert hexToNumber and it's tests to initial state

* Remove unecessary logs

* Add test for formatter

* Remove unecessary prettierignore

* Update CHANGELOG

* Update docs
  • Loading branch information
nikoulai authored Jul 4, 2022
1 parent 555aa0d commit 9e0d9d1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,10 @@ Released with 1.0.0-beta.37 code base.
- Documentation details about `maxFeePerGas` and `maxPriorityFeePerGas` (#5121)

### Fixed
- Fix typos in web3-eth-accounts.rst & TESTING.md (#5047)
- Improve README.md & Fix typos (#4848)
- Fix typos in web3-eth-accounts.rst & TESTING.md (#5047)
- Improve README.md & Fix typos (#4848)
- Add optional hex formatting parameter for getTransactionrReceipt (#5153)

### Security
- Updated `got` lib version and fixed other libs using npm audit fix
- Updated `got` lib version and fixed other libs using npm audit fix

29 changes: 23 additions & 6 deletions docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,8 @@ Parameters
----------

1. ``String`` - The transaction hash.
2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
2. ``String`` - (optional) The ``hex`` keyword can be passed as second argument, in order to format in hex, values that would be ``Number`` otherwise.
3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.


.. _eth-gettransactionreceipt-return:
Expand All @@ -1415,16 +1416,16 @@ Returns

- ``status`` - ``Boolean``: ``TRUE`` if the transaction was successful, ``FALSE`` if the EVM reverted the transaction.
- ``blockHash`` 32 Bytes - ``String``: Hash of the block where this transaction was in.
- ``blockNumber`` - ``Number``: Block number where this transaction was in.
- ``blockNumber`` - ``Number`` (or ``hex String``): Block number where this transaction was in.
- ``transactionHash`` 32 Bytes - ``String``: Hash of the transaction.
- ``transactionIndex``- ``Number``: Integer of the transactions index position in the block.
- ``transactionIndex``- ``Number`` (or ``hex String``): Integer of the transactions index position in the block.
- ``from`` - ``String``: Address of the sender.
- ``to`` - ``String``: Address of the receiver. ``null`` when it's a contract creation transaction.
- ``contractAddress`` - ``String``: The contract address created, if the transaction was a contract creation, otherwise ``null``.
- ``cumulativeGasUsed`` - ``Number``: The total amount of gas used when this transaction was executed in the block.
- ``gasUsed`` - ``Number``: The amount of gas used by this specific transaction alone.
- ``cumulativeGasUsed`` - ``Number`` (or ``hex String``): The total amount of gas used when this transaction was executed in the block.
- ``gasUsed`` - ``Number`` (or ``hex String``): The amount of gas used by this specific transaction alone.
- ``logs`` - ``Array``: Array of log objects, which this transaction generated.
- ``effectiveGasPrice`` - ``Number``: The actual value per gas deducted from the senders account. Before EIP-1559, this is equal to the transaction's gas price. After, it is equal to baseFeePerGas + min(maxFeePerGas - baseFeePerGas, maxPriorityFeePerGas).
- ``effectiveGasPrice`` - ``Number`` (or ``hex String``): The actual value per gas deducted from the senders account. Before EIP-1559, this is equal to the transaction's gas price. After, it is equal to baseFeePerGas + min(maxFeePerGas - baseFeePerGas, maxPriorityFeePerGas).

-------
Example
Expand All @@ -1449,6 +1450,22 @@ Example
}, ...]
}
var receipt = web3.eth.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b','hex')
.then(console.log);
> {
"status": true,
"transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"transactionIndex": '0x0',
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": '0x3',
"contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
"cumulativeGasUsed": '0x4cb2f',
"gasUsed": '0x761a',
"logs": [{
// logs as returned by getPastLogs, etc.
}, ...]
}
------------------------------------------------------------------------------

Expand Down
21 changes: 11 additions & 10 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,21 @@ var outputTransactionReceiptFormatter = function (receipt) {
throw new Error('Received receipt is invalid: ' + receipt);
}

if (receipt.blockNumber !== null)
receipt.blockNumber = utils.hexToNumber(receipt.blockNumber);
if (receipt.transactionIndex !== null)
receipt.transactionIndex = utils.hexToNumber(receipt.transactionIndex);
receipt.cumulativeGasUsed = utils.hexToNumber(receipt.cumulativeGasUsed);
receipt.gasUsed = utils.hexToNumber(receipt.gasUsed);

if(!this.hexFormat){
if (receipt.blockNumber !== null)
receipt.blockNumber = utils.hexToNumber(receipt.blockNumber);
if (receipt.transactionIndex !== null)
receipt.transactionIndex = utils.hexToNumber(receipt.transactionIndex);
receipt.cumulativeGasUsed = utils.hexToNumber(receipt.cumulativeGasUsed);
receipt.gasUsed = utils.hexToNumber(receipt.gasUsed);
if (receipt.effectiveGasPrice) {
receipt.effectiveGasPrice = utils.hexToNumber(receipt.effectiveGasPrice)
}
}
if (Array.isArray(receipt.logs)) {
receipt.logs = receipt.logs.map(outputLogFormatter);
}

if (receipt.effectiveGasPrice) {
receipt.effectiveGasPrice = utils.hexToNumber(receipt.effectiveGasPrice)
}
if (receipt.contractAddress) {
receipt.contractAddress = utils.toChecksumAddress(receipt.contractAddress);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,16 @@ Method.prototype.buildCall = function () {

// actual send function
var send = function () {

let args = Array.prototype.slice.call(arguments);

var defer = promiEvent(!isSendTx),
payload = method.toPayload(Array.prototype.slice.call(arguments));
payload = method.toPayload(args);

method.hexFormat = false;
if(method.call === 'eth_getTransactionReceipt'){
method.hexFormat = (payload.params.length < args.length && args[args.length - 1] === 'hex')
}
// CALLBACK function
var sendTxCallback = function (err, result) {
if (method.handleRevert && isCall && method.abiCoder) {
Expand Down
25 changes: 25 additions & 0 deletions test/eth.getTransactionReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ var tests = [{
"gasUsed": 520464,
},
call: 'eth_'+ method
},
{
args: ['0xdd960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265','hex'],
formattedArgs: ['0xdd960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265'],
result: {
"status": "0x0",
"blockHash": "0x6fd9e2a26ab",
"blockNumber": "0x15df",
"transactionHash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b",
"transactionIndex": "0x10",
"contractAddress":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"cumulativeGasUsed":"0x7f110",
"gasUsed": "0x7f110",
},
formattedResult: {
"status": false,
"blockHash": "0x6fd9e2a26ab",
"blockNumber": "0x15df",
"transactionHash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b",
"transactionIndex": "0x10",
"contractAddress":"0x407D73d8a49eeb85D32Cf465507dd71d507100c1", // checksum address
"cumulativeGasUsed":"0x7f110",
"gasUsed": "0x7f110",
},
call: 'eth_'+ method
}];


Expand Down

0 comments on commit 9e0d9d1

Please sign in to comment.