Question about contract.callStatic.methodName() #975
-
I am currently following the video at the 15:40:21 mark , It was mentioned that calling test portion for check up keep function describe("check up keep", function () {
it("returns false if no one is in the game", async function () {
//we want everything in check up keep to be true except for has players
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.send("evm_mine", []) //just want to mine one extra block
//calling check up keep would send a tx , we do not want that, we can use callstatic
const { upkeepNeeded } = await raffle.callStatic.checkUpkeep([])
assert(!upkeepNeeded)
})
}) code for checkUpKeep function checkUpkeep(
bytes memory /* checkData */
)
public
view
override
returns (
bool upkeepNeeded,
bytes memory /* performData */
)
{
bool isOpen = s_raffleState == RaffleState.OPEN;
// block.timeStamp gives the current time, in order to get the time passed
//we could do something like block.timeStamp - prevTimeStamp(we neeod a variable for this )
bool timepassed = ((block.timestamp - s_lastTimeStamp)) > c_interval;
bool hasplayers = (s_players.length > 0);
bool hasBalance = address(this).balance > 0;
upkeepNeeded = isOpen && timepassed && hasplayers && hasBalance;
}
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
I believe it is so that we do not change the state of our Contract (or, in this case, Raffle), which in this case would be upkeepNeeded. Quoted from docs below which should clarify it further:
This does not actually change any state, but is free. This in some cases can be used to determine if a transaction will fail or succeed. This otherwise functions the same as a Read-Only Method. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
@AlexK020908 See we want to test the |
Beta Was this translation helpful? Give feedback.
-
The reason Patrick uses
|
Beta Was this translation helpful? Give feedback.
@AlexK020908 See we want to test the
checkUpkeep
function but if we do a transaction it will then call theperformUpkeep
but we do not want that, we only want to test checkUpkeep. Because of that when we addcallStatic
with it it will execute all of the code but does not call other functions.