2020.코딩일지

[solidity] modifier (feat.솔리디티깨부수기) 본문

Block Chain

[solidity] modifier (feat.솔리디티깨부수기)

개발하는라푼젤 2022. 12. 5. 13:31
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;
    }
}
Comments