|
| 1 | +const { assert } = require('chai') |
| 2 | +const conf = require('./config') |
| 3 | +const helpers = require('./helpers') |
| 4 | +const web3 = conf.web3 |
| 5 | + |
| 6 | +let deployed = null |
| 7 | +let contractAddress = null |
| 8 | + |
| 9 | +before(async () => { |
| 10 | + deployed = await helpers.deployContract('storage') |
| 11 | + contractAddress = deployed.receipt.contractAddress |
| 12 | + |
| 13 | + assert.equal(deployed.receipt.status, conf.successStatus) |
| 14 | +}) |
| 15 | + |
| 16 | +it('should apply block overrides on eth_estimateGas', async () => { |
| 17 | + assert.equal(deployed.receipt.status, conf.successStatus) |
| 18 | + |
| 19 | + let receipt = await web3.eth.getTransactionReceipt(deployed.receipt.transactionHash) |
| 20 | + assert.equal(receipt.contractAddress, contractAddress) |
| 21 | + |
| 22 | + // Check the `block.number` value, without overrides |
| 23 | + let blockNumberSelector = deployed.contract.methods.blockNumber().encodeABI() |
| 24 | + let txArgs = { |
| 25 | + from: conf.eoa.address, |
| 26 | + to: contractAddress, |
| 27 | + gas: '0x75ab', |
| 28 | + gasPrice: web3.utils.toHex(conf.minGasPrice), |
| 29 | + value: '0x0', |
| 30 | + data: blockNumberSelector, |
| 31 | + } |
| 32 | + |
| 33 | + let response = await helpers.callRPCMethod( |
| 34 | + 'eth_estimateGas', |
| 35 | + [txArgs, 'latest', null, null] |
| 36 | + ) |
| 37 | + assert.equal(response.status, 200) |
| 38 | + assert.isDefined(response.body) |
| 39 | + assert.equal(web3.utils.hexToNumber(response.body.result), 21651n) |
| 40 | + |
| 41 | + // Override the `block.number` value to `2`. |
| 42 | + response = await helpers.callRPCMethod( |
| 43 | + 'eth_estimateGas', |
| 44 | + [txArgs, 'latest', null, { number: '0x2' }] |
| 45 | + ) |
| 46 | + assert.equal(response.status, 200) |
| 47 | + assert.isDefined(response.body) |
| 48 | + assert.equal(web3.utils.hexToNumber(response.body.result), 21651n) |
| 49 | + |
| 50 | + // Check the `block.timestamp` value, without overrides |
| 51 | + let blockTimeSelector = deployed.contract.methods.blockTime().encodeABI() |
| 52 | + txArgs.data = blockTimeSelector |
| 53 | + |
| 54 | + response = await helpers.callRPCMethod( |
| 55 | + 'eth_estimateGas', |
| 56 | + [txArgs, 'latest', null, null] |
| 57 | + ) |
| 58 | + assert.equal(response.status, 200) |
| 59 | + assert.isDefined(response.body) |
| 60 | + assert.equal(web3.utils.hexToNumber(response.body.result), 21607n) |
| 61 | + |
| 62 | + // Override the `block.timestamp` value to `0x674DB1E1`. |
| 63 | + response = await helpers.callRPCMethod( |
| 64 | + 'eth_estimateGas', |
| 65 | + [txArgs, 'latest', null, { time: '0x674DB1E1' }] |
| 66 | + ) |
| 67 | + assert.equal(response.status, 200) |
| 68 | + assert.isDefined(response.body) |
| 69 | + assert.equal(web3.utils.hexToNumber(response.body.result), 21607n) |
| 70 | + |
| 71 | + // Check the `block.prevrandao` value, without overrides |
| 72 | + let randomSelector = deployed.contract.methods.random().encodeABI() |
| 73 | + txArgs.data = randomSelector |
| 74 | + |
| 75 | + response = await helpers.callRPCMethod( |
| 76 | + 'eth_estimateGas', |
| 77 | + [txArgs, 'latest', null, null] |
| 78 | + ) |
| 79 | + assert.equal(response.status, 200) |
| 80 | + assert.isDefined(response.body) |
| 81 | + |
| 82 | + // Override the `block.prevrandao` value to `0x7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc0`. |
| 83 | + let random = '0x7914bb5b13bac6f621bc37bbf6e406fbf4472aaaaf17ec2f309a92aca4e27fc0' |
| 84 | + response = await helpers.callRPCMethod( |
| 85 | + 'eth_estimateGas', |
| 86 | + [txArgs, 'latest', null, { random: random }] |
| 87 | + ) |
| 88 | + assert.equal(response.status, 200) |
| 89 | + assert.isDefined(response.body) |
| 90 | + assert.equal(web3.utils.hexToNumber(response.body.result), 21584n) |
| 91 | +}) |
0 commit comments