'How to make a function limitation call 1000 times in solidity?

I would like to make a function which only allow users to call it 1000 times per 24 hours, logically is possible right? But how to make it happen in solidity? And is it possible to set the target country time zones? Please advise, million thanks!



Solution 1:[1]

You add a counter in your storage and increase it on every call and add a require inside your function that checks if the counter is less than 1000.

Counters.Counter private counter; // default is 0

function foo() public {
  require(counter.current() <= 1000, "Message");
  counter.increase()
  ...
}

Then you need to restart your counter every 24 hours, either you do it manually or you use Chainlink Keepers (not free) or OpenZeppelin defender (free)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1