Counter.sol:
pragma solidity >=0.5.0 <0.7.0;
contract Counter {
uint public _counter = 0;
function GenNextID() public returns (uint) {
return _counter++;
}
}
I use Truffle and Ganache for local development.
My test looks like the following. For some reason ctrInstance.GenNextID.call() does NOT mutate _counter
and it always remains equal to 0.
const Counter = artifacts.require("Counter");
contract("Counter", () => {
it("should increment ocunter", async () => {
const ctrInstance = await Counter.deployed();
for (let i = 0; i < 10; i++) {
const result = await ctrInstance.GenNextID.call();
const id = result.toNumber();
assert.equal(result, id); // this fails on second iteration
}
});
});
I figured out that call const r = await ctrInstance.GenNextID();
mutates internal state, but it always returns receipt
but not returned data.
{
tx: '0x9d0ef6530ba5808194612f7bf1b834b4ccab1c8e67aa58d44862d27dd777d57a',
receipt: {
transactionHash: '0x9d0ef6530ba5808194612f7bf1b834b4ccab1c8e67aa58d44862d27dd777d57a',
transactionIndex: 0,
blockHash: '0xe4883bcdfa9aecf0066228b1dc33ef61045f6a9a158fb0f331eb94afafaf08bf',
blockNumber: 13,
from: '0xe821f406192d16f9d7b74ece45e73ba92fb9ba64',
to: '0x147e70c0babb11232d3df6f750fa9acd28a86460',
gasUsed: 26700,
cumulativeGasUsed: 26700,
contractAddress: null,
logs: [],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
v: '0x1c',
r: '0xdb56e87a6f52c0d03e7e3ddc1248d9cf55c38d0b747bebb4de4b8d21c94a28a',
s: '0x41d2ba345d4f18be71aaceca1548a357f4ecdfbd6c51972bf6759e51035c67e5',
rawLogs: []
},
logs: []
}
Question is: how to invoke GenNextID
like a regular function, that is:
- call to this function mutates internal state
- it returns result returned from the contract's code