I would like to know how do I limit the amount of times an address can interact with a function, as in saving the amount of times an address interacted with the function as an uint256 so that I could reset it to 0 with another function, thanks!
I would like to know how do I limit the amount of times an address can interact with a function, as in saving the amount of times an address interacted with the function as an uint256 so that I could reset it to 0 with another function, thanks!
For a single function, you can use a mapping where the key is the user address and the value is the amount of interactions.
If you need to expand the functionality to track separate interactions with multiple functions, the key should be a combination of the user address and the function selector. You can combine them for example in a keccak256 hash.
pragma solidity ^0.8;
contract MyContract {
uint256 constant MAX_INTERACTIONS = 10;
mapping(address => uint256) interactionCount;
modifier limit {
require(interactionCount[msg.sender] < MAX_INTERACTIONS);
interactionCount[msg.sender]++;
_;
}
function foo() external limit {
// your implementation
}
function resetLimit(address user) external {
// TODO you might want to restrict this function only to an authorized address
interactionCount[user] = 0;
}
}