以这种方式使用revert是否正确?



我有一个抽象合约,我需要验证参数,如果无效则返回错误,并返回gas费。

我创建了一个名为checkNumber的修饰符,并在其中验证_number并使用require和revert。

//SPDX-License-Identifier: UNLICENSED
// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity >=0.7.0 <0.9.0;
struct Information {
uint number;
string avatar;
}
abstract contract AbstractCandidate {
uint[6] private _numbers = [1, 2, 3, 4, 5, 6];
string[6] private _avatars = [
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-1.png",
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-2.png",
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-3.png",
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-4.png",
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-5.png",
"https://raw.githubusercontent.com/thiagosaud/dApp-superior-electoral-court/main/temp/imgs/candidate-6.png"
];
modifier checkNumber(uint _number) {
string memory _errorMessage = "Candidate number is incorret!";
require(_number < 1 || _number > _numbers.length, _errorMessage);
if (_number < 1 || _number > _numbers.length) {
revert(_errorMessage);
}
_;
}
function getInformation(uint _number) external view checkNumber(_number) returns(Information memory) {
return Information({ avatar: _avatars[_number], number: _numbers[_number] });
}
}

根据这个逻辑,你将总是得到_errorMessage。例如,在require语句if _number 0中,你不会得到err,但你会在if语句中得到它,因为0 <</p>

最新更新