gasPrice: 0 in Test Environment not working

I am trying to use Test Environment to run a test in which I want to compare a user balance before and after a couple of transactions excluding transaction fees.

The use case is a complaint within an escrow contract: a seller offers a product for X wei. Then, a buyer orders the product and sends X wei to the contract. However, there is a complaint, so the buyer gets the X wei back from the contract. If gasPrice is 0, the buyer should have exactly the same balance before and after the order and complaint transactions, but I am getting the transaction fees and therefore, the balances do not match at the end.

This is the test:

const { accounts, contract } = require('@openzeppelin/test-environment');
const {balance, BN } = require('@openzeppelin/test-helpers');
const Test = contract.fromArtifact('test');

const [admin, buyer1, seller1] = accounts;
let test;

beforeEach(async () => {
    test = await{ from: admin });

    // Seller1 offers a t-shirt for 10 wei
    await test.offer('t-shirt', 10, { from: seller1 });

it('should complain an order', async () => {
    // Buyer1 orders the t-shirt and transfers 10 wei to the escrow contract
    const balanceBuyerBefore = new BN(await balance.current(buyer1));
    await test.order('t-shirt', { from: buyer1, value: 10 });

    // Buyer1 makes a complaint, so receives back the 10 wei from the escrow contract
    await test.complain('t-shirt', { from: buyer1 });
    const balanceBuyerAfter = new BN(await balance.current(buyer1));

    // If gas price would be 0, both balances should be the same
    console.log('balance before', balanceBuyerBefore.toString());
    console.log('balance after ', balanceBuyerAfter.toString());

My test-environment.config.js is:

module.exports = {
    node: { 
        gasPrice: 0

The outcome is:

balance before 100000000000000000000
balance after   99998308000000000000

I was expecting that if gasPrice is 0, variables balanceBuyerBefore and balanceBuyerAfter should be exactly the same, but the second balance has been affected by the gas fees.

Is there anything I am missing?

1 Like

Hi @sjuanati,

We need to set the defaultGasPrice:

// test-environment.config.js

module.exports = {
    contracts: {
      // Options available since v0.1.2
      defaultGasPrice: 0, // Gas price for contract calls (when unspecified)
1 Like