I created an auction exchange smart contract using solidity programming language and I am using ganache for testing this is a test file written in JavaScript. seller is the creator of auction and buyer1 and buyer2 are the persons who create the offer on that auction. and the owner of the auction( seller ) will make the transaction and he will get the best offer price.

In this test file auction owner( seller ) is doing a transaction. when the transaction is complete best offer price will add to the seller's balance and in assert function we check beforeBalance equal to afterBalance.

Error: error is when the seller makes the transaction some gas price is used and the assert function fails. I have to add best offer value in the seller's balance and subtract the gas fee, to successfully run the assert function

const AuctionExchange = artifacts.require("AuctionExchange");
const {expectRevert} = require("@openzeppelin/test-helpers");

contract("Auction Exchange", (accounts)=> {
    let auctionExchange;
    beforeEach( async() => {
       auctionExchange = await AuctionExchange.new();

auction = {
    name: "car",
    description: "red",
    min: 10

const [seller, buyer1, buyer2] = [accounts[0], accounts[1], accounts[2]];

//rest of the code.

 it("should make transaction", async()=> {
    await auctionExchange.createAuction(auction.name, auction.description, auction.min);
    const bestOffer = web3.utils.toBN(auction.min + 10);

    // Create offers from buyers
    await auctionExchange.createOffer(1, {from: buyer1, value:auction.min});
    await auctionExchange.createOffer(1, {from: buyer2, value: bestOffer});
    // Get seller's balance before the transaction
    const balanceBefore = web3.utils.toBN(await 
    console.log("Seller's balance before:", balanceBefore.toString());

    const tx = await auctionExchange.transaction(1, 
     { from: accounts[0] 

    // Retrieve the transaction receipt to get gas used
    // tx.tx contains the transaction hash
    const receipt = await web3.eth.getTransactionReceipt(tx.tx); 
    const gasUsed = web3.utils.toBN(receipt.gasUsed);
    const gasPrice = web3.utils.toBN(await web3.eth.getGasPrice());
    const gasCost = gasUsed.mul(gasPrice);
    console.log("Gas Cost:", gasCost.toString());

    // Get seller's balance after the transaction
    const balanceAfter = web3.utils.toBN(await web3.eth.getBalance(seller));
    console.log("Seller's balance after:", balanceAfter.toString());

    const expectedPrice = await balanceBefore.add(bestOffer).sub(gasUsed);
    console.log("Expected Price:", expectedPrice.toString());

//Result //Result

Seller's balance before: 99600548937388730843

gas Used 80796

gas Price: 20000000000

gas cost: 1615920000000000

Seller's balance after: 99600346947388084495

Expected Price: 99600548937388650067


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.