Search code examples
testingsoliditysmartcontractstruffleganache

Test for smart contract (solidity/truffle)


I have these lines of code

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

contract wasteManagement2 {
    struct Route{
        string date; //struct date{ uint day; uint month; uint year;}
        // eg. monday
        string vehicle;
        string driver; //struct driver { string name, string lname, uint id}
        string location;
        string routebinId;
    }

    mapping (uint => Route) public routes;
    uint public routeCount;

    constructor()  {
        routeCount = 0;
    }

    function setRoute(string memory _date,string memory _vehicle, string memory _driver, string memory _location, string memory _routebinId) public{
        routes[routeCount] = Route (_date,_vehicle, _driver, _location, _routebinId);
        routeCount++;
    }

    function getRoute(uint _routeCount) public view returns(Route memory){
        return routes[_routeCount];
    }
}

and I want to test the contract on how it's going to work if 6000+ different registries happen how much is going to cost. Thanks in advance.

This is the test file for now:

const Routes = artifacts.require("Routes");

contract ("Routes", (accounts) => {
    before(async () =>  {
        instance = await Routes.deployed()
    })

    it ('ensures that the array is empty', async () => {
        let count =  await instance.setRoute()
        assert.equal(count, 0, 'The array should be empty')
    })
})

Solution

  • I will show how to calculate the cost of calling a function and then you have to do for loop for 6000 registiries. Assuming that you initialize the contract correct:

    const result = await instance.setRoute()
    

    if you console.log(result you get this object

    result {
      tx: '0x1550f6f4f3e7abe0e2d39a43127714e4422e548e6a45d54a3fe12c2ed8b1c180',
      receipt: {
        transactionHash: '0x1550f6f4f3e7abe0e2d39a43127714e4422e548e6a45d54a3fe12c2ed8b1c180',
        transactionIndex: 0,
        blockHash: '0x6d13903f40a7b3c989b79accf70d5bb1f7ef673ee59a0eb534b09d375db1bd7e',
        blockNumber: 1249,
        from: '0xd76536f6b5722f78d444ba0c3b8aae84b7a226ba',
        to: '0xde7b6dd9d647e4249f85ac15e5f7c88e7e424fa',
        gasUsed: 31167,
        cumulativeGasUsed: 31167,
        contractAddress: null,
        logs: [],
        status: true,
        logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
        rawLogs: []
      },
      logs: []
    }
    

    To get the gas cost write a function:

    const getGas = async (result) => {
      const tx = await web3.eth.getTransaction(result.tx);
      const gasUsed = toBN(result.receipt.gasUsed);
      const gasPrice = toBN(tx.gasPrice);
      const gas = gasUsed.mul(gasPrice);
      return gas;
    };
    

    toBN is a helper function:

    const toBN = (value) => web3.utils.toBN(value);
    

    finally get the gas cost:

    const result = await instance.setRoute()
    const gas = await getGas(result);