I'm starting with Solidity and Smart Contracts, and the static analysis in Remix is complaining about gas infinity for the function bet:
contract Forecast {
struct Bet {
address payable owner;
uint price;
}
// the bets
mapping(uint => Bet) bets;
// number of bets
uint public betCount;
//bet value
uint public betValue;
// Amount accumulated
uint public accPot;
/// Not enough funds to bet
error NotEnoughFunds();
constructor (uint _betValue) {
betValue = _betValue;
}
function bet(uint price) public payable {
if (betValue > msg.value)
revert NotEnoughFunds();
bets[betCount++] = Bet(payable(msg.sender), price);
accPot += msg.value;
}
}
Test:
pragma solidity >=0.4.22 <0.9.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "remix_accounts.sol";
import "../contracts/Forecast.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite {
Forecast forecast;
function beforeAll() public {
forecast = new Forecast(42);
}
/// #sender: account-1
/// #value: 42
function testBet() public payable {
Assert.equal(forecast.betValue(), 42, "Bet value should be 42");
Assert.equal(forecast.accPot(), 0, "Accumulated pot should start at 0");
Assert.equal(forecast.betCount(), 0, "Bet count should start at 0");
Assert.equal(msg.value, 42, "Msg value should be 42");
forecast.bet(42);
Assert.equal(forecast.betCount(), 1, "Bet count should be 1");
Assert.equal(forecast.accPot(), 42, "Accumulated pot should be 42");
}
}
In fact, when I run a test I get the error:
✘ Test bet Error Message: "Transaction has been reverted by the EVM: { "transactionHash": "0x2bd817dc53af5b2bf6cf5bba53207bf462283a8a229b5d66b3e5eae1f28fcc65", "transactionIndex": 0, "blockHash": "0xd19b57b71cc4948048baba6363ba841a56ddeedeaa65f1b4caffd7c5bd5e2e3f", "blockNumber": 47, "gasUsed": 5000000, "cumulativeGasUsed": 5000000, "status": false, "to": "0x3c725134d74D5c45B4E4ABd2e5e2a109b5541288", "events": {} }"
All documentation I read says too much gas might be spent on unbound loops and that kind of stuff, but here is just 2 assignment, so I really don't understand.
EDITED:
I modified the code to make a minimum test case.
The error I see during the test indeed comes from the revert NotEnoughFunds(); line (which I don't know why because the test has the correct values, but that's a different story). However, as per @xavier59 comment and the link he provided, revert reverts the transaction and doesn't consume all the gas.
revert SweepstakeAlreadyEnded();doesn't compile. What areSweepstakeAlreadyEnded()andNotEnoughFunds()? – Ismael Apr 25 '21 at 16:05