如果您发送价值并且您发送的价值应小于您当前的余额,则被调用的函数应该是可支付的

发布于 2025-01-14 09:46:17 字数 1197 浏览 2 评论 0原文

当我调用changeScore时,出现错误 [在此处输入图像描述][1]

交易已恢复到初始状态。 注意:如果您发送了值,则被调用的函数应该是付费的,并且您发送的值应该小于您当前的余额。 调试事务以获取更多信息。

代码

pragma solidity ^0.8.0;


// Student
contract Score{
    address public teacher;
    mapping (address => uint) StudentScore;


    modifier onlyTeacher(){
        require(msg.sender == teacher,"Don't Change Score");
        _;
    }

    function addTeacher(address _address) public{
        teacher = _address;
    }

    // external
    function IScore(address _account,uint _score) public onlyTeacher{
        require(_score <= 100,"Score more then 100");
        StudentScore[_account] = _score;
    }

    function getStudentScore(address _address) public view returns (uint){
        return StudentScore[_address];
    }
}

interface IScoreService{
    function IScore(address _account, uint _score) external;
}

// Teacher 
contract Teacher{
    address public selfAddress;

    IScoreService public score;

    constructor(){
        selfAddress = address(this);
    }
    
    function changeScore(address _account, uint _score) public {
        score.IScore(_account, _score);
    }

}


  [1]: https://i.sstatic.net/KwWHz.png

When I call changeScore, I get an error
[enter image description here][1]

The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

Code

pragma solidity ^0.8.0;


// Student
contract Score{
    address public teacher;
    mapping (address => uint) StudentScore;


    modifier onlyTeacher(){
        require(msg.sender == teacher,"Don't Change Score");
        _;
    }

    function addTeacher(address _address) public{
        teacher = _address;
    }

    // external
    function IScore(address _account,uint _score) public onlyTeacher{
        require(_score <= 100,"Score more then 100");
        StudentScore[_account] = _score;
    }

    function getStudentScore(address _address) public view returns (uint){
        return StudentScore[_address];
    }
}

interface IScoreService{
    function IScore(address _account, uint _score) external;
}

// Teacher 
contract Teacher{
    address public selfAddress;

    IScoreService public score;

    constructor(){
        selfAddress = address(this);
    }
    
    function changeScore(address _account, uint _score) public {
        score.IScore(_account, _score);
    }

}


  [1]: https://i.sstatic.net/KwWHz.png

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

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

发布评论

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

评论(1

唐婉 2025-01-21 09:46:17

IScore() 函数具有 onlyTeacher 修饰符,有效地允许该函数仅从 teacher 地址执行。

请注意,在本例中,执行该函数的地址是教师合约地址。

解决方案:通过执行 addTeacher() 函数(将 Teacher 传递给它),将 Teacher 合约地址设置为 teacher 变量> 合约地址)在 Score 合约上。然后,您将能够通过 Teacher 合约执行 IScore() 函数。

The IScore() function has a onlyTeacher modifier, effectively allowing the function to be executed only from the teacher address.

Mind that the address executing the function is the Teacher contract address in this case.

Solution: Set the Teacher contract address as the teacher variable by executing the addTeacher() function (passing it the Teacher contract address) on the Score contract. Then you'll be able to execute the IScore() function through the Teacher contract.

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