我如何限制地址可以与功能交互的次数?

发布于 2025-01-23 14:10:39 字数 76 浏览 0 评论 0原文

我想知道如何限制地址可以与函数交互的次数,例如保存地址与函数作为UINT256相互作用的次数,以便我可以使用另一个功能将其重置为0,谢谢呢

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2025-01-30 14:10:39

对于单个函数,您可以使用映射其中键是用户地址,而值是交互的量。

如果您需要扩展功能以通过多个功能跟踪单独的交互,则密钥应是用户地址和功能选择器的组合。例如,您可以在keccak256哈希中组合它们。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文