如果您发送价值并且您发送的价值应小于您当前的余额,则被调用的函数应该是可支付的
当我调用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IScore()
函数具有onlyTeacher
修饰符,有效地允许该函数仅从teacher
地址执行。请注意,在本例中,执行该函数的地址是教师合约地址。
解决方案:通过执行
addTeacher()
函数(将Teacher
传递给它),将Teacher
合约地址设置为teacher
变量> 合约地址)在Score
合约上。然后,您将能够通过Teacher
合约执行IScore()
函数。The
IScore()
function has aonlyTeacher
modifier, effectively allowing the function to be executed only from theteacher
address.Mind that the address executing the function is the
Teacher
contract address in this case.Solution: Set the
Teacher
contract address as theteacher
variable by executing theaddTeacher()
function (passing it theTeacher
contract address) on theScore
contract. Then you'll be able to execute theIScore()
function through theTeacher
contract.