Test when a transaction is sent without enough gas using OpenZeppelin CLI --gas

:memo:Details
I’m attempting to test what happens when a transaction is sent without enough gas. I thought the way to do this would be to use the --gas <value> switch with oz, but the values I use don’t seem to make any difference (the transaction always succeeds and ganache shows Gas usage: with a variable amount).

I’m sure I’m being dumb, but I’m stuck on this one.

:computer: Environment
oz cli v2.8.2

:1234: Code to reproduce
npx oz send-tx --gas 1

1 Like

Hi @EvilJordan,

Using the Box.sol contract from the Learn guides I tried setting gas using the CLI.

A low gas amount didn’t fail using ganache-cli but did on rinkeby.

I looked at the ganache-cli flags but didn’t see anything obvious to allow this type of testing.

ganache-cli

$ npx oz send-tx --gas 40000
? Pick a network development
? Pick an instance Box at 0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab
? Select which function store(newValue: uint256)
? newValue: uint256: 23
✓ Transaction successful. Transaction hash: 0x8d33d5ddf118758a9ef97abe3e209a6538b7e0d574922b2430ae40feeac5acea
Events emitted:
 - ValueChanged(23)

Rinkeby not enough gas

$ npx oz send-tx --gas 40000
? Pick a network rinkeby
? Pick an instance Box at 0x5C266476E2425082AeB9Ab393Dbd9fC0fac6E871
? Select which function store(newValue: uint256)
? newValue: uint256: 23
✖ Calling: 'store' with:
- newValue (uint256): "23"
Error while trying to send transaction to 0x5C266476E2425082AeB9Ab393Dbd9fC0fac6E871. Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0xac1f091c51d8fbb6828681346f7fb118e0b925ef98f922f617802c065643f50a",
  "blockNumber": 6836012,
  "contractAddress": null,
  "cumulativeGasUsed": 40000,
  "from": "0x77737a65c296012c67f8c7f656d1df81827c9541",
  "gasUsed": 40000,
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x5c266476e2425082aeb9ab393dbd9fc0fac6e871",
  "transactionHash": "0x4e36107504bd485c25956807eceac0f2f426502fd077bd040f3525bb98822d45",
  "transactionIndex": 0,
  "events": {}
}

Rinkeby enough gas

$ npx oz send-tx --gas 50000
? Pick a network rinkeby
? Pick an instance Box at 0x5C266476E2425082AeB9Ab393Dbd9fC0fac6E871
? Select which function store(newValue: uint256)
? newValue: uint256: 23
✓ Transaction successful. Transaction hash: 0x37f20495d07625b339f492a29bc7bd43641aefaf6a60580a99d04c21ad5c8e64
Events emitted:
 - ValueChanged(23)

To test locally, I used OpenZeppelin Test Environment to check for a revert

Box.test.js

// test/Box.test.js

// Load dependencies
const { accounts, contract } = require('@openzeppelin/test-environment');
const { expect } = require('chai');

// Import utilities from Test Helpers
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

// Load compiled artifacts
const Box = contract.fromArtifact('Box');

// Start test block
describe('Box', function () {
  const [ owner ] = accounts;

  beforeEach(async function () {
    // Deploy a new Box contract for each test
    this.contract = await Box.new({ from: owner });
  });

  // Test case
  it('store without enough gas', async function () {
    // Store a value - recall that only the owner account can do this!
    await expectRevert(this.contract.store(42, { from: owner, gas: 40000 }), "out of gas");
  });
});

Hi @EvilJordan,

Were you able to test this?

I was never able to get oz to work correctly with ganache-cli, so, no.
But, my testing works absolutely fine when using NOT ganache… so, yeah, same results as you!

Odd, no?

1 Like

Hi @EvilJordan,

This was strange, I assume some quirk of OpenZeppelin CLI and ganache-cli but I didn’t dig too much to see if this was an issue of the ganache-cli side.