2020.코딩일지
[solidity] modifier (feat.솔리디티깨부수기) 본문
728x90
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract lec24 {
//함수는 다른데 require문이 같을때 modifier를 쓴다....일일히 수정할수없쟈나ㅏㅏㅏ
function onlyAdults112(uint256 _age) public pure returns (string memory) {
require(_age>25, "You are not allowed to pay for the cigarette");
return "You payment is succeeded";
}
function onlyAdults32(uint256 _age) public pure returns(string memory) {
require(_age>25, "You are not allowed to pay for the cigarette");
return "Your payment is succeeded";
}
//위의상황을 아래와같이 modifier로 개선-------------------------------------------
modifier onlyAdults{
revert("You are not allowed to pay for the cigarette");
_; //이 자리에 아래함수가 들어온다고 생각하면 됨.
}
function BuyCigarette() public onlyAdults returns(string memory) {
return "Your payment is succeeded";
}
//modifier에 파라미터있는경우---------------------------------------------------
modifier onlyAdults2(uint256 _age) {
require(_age>18, "You are not allowed to pay for the cigarette"); //18세미만은 에러가 난다
_;
}
function BuyCigarette2(uint256 _age) public onlyAdults2(_age) returns(string memory){
return "Your payment is succeeded";
}
//------------------초기설정 5였는데, 함수실행하면 15였다가 modifier에 의해서 10으로 최종출력됨!
uint256 public num = 5;
modifier numChange {
_;
num = 10;
}
function numChangeFunction() public numChange{
num = 15;
}
}
'Block Chain' 카테고리의 다른 글
[solidity] fallback과 receive (feat.솔리디티깨부수기) (0) | 2022.12.05 |
---|---|
[solidity] payable/balance와 msg.sender (feat.솔리디티깨부수기) (0) | 2022.12.05 |
[solidity] return/SPDX (feat.솔리디티깨부수기) (0) | 2022.12.05 |
[solidity] error/try-catch문 (feat.솔리디티깨부수기) (0) | 2022.12.05 |
[solidity] if문/반복문/continue와break/equal (feat.솔리디티깨부수기) (0) | 2022.12.05 |
Comments