简体   繁体   中英

Error :The transaction has been reverted to the initial state

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

contract modifierExample{

    address admin ;

    constructor() public {
        admin == msg.sender;
    }

    modifier isAdmin {
        require(admin == msg.sender,"you are not the owner");
        _;
    }

    modifier isExp(uint exp) {
        if(exp >= 5)
        _;
        else
        revert("you are not experienced");
    }

    struct employeeDetails{
        uint iD;
        string name;
        uint age;
    }

    mapping (uint => employeeDetails) getDetailsByNum;

    function enterDetails(uint number,
                          uint iD,
                          string memory name,
                          uint age) public isAdmin isExp(5) {
                              employeeDetails memory EmployeeDetails = employeeDetails(iD,name,age);
                              getDetailsByNum[number] = EmployeeDetails;
                          }

    function getDetailsByNumber(uint number) public view returns (employeeDetails memory) {
        return getDetailsByNum[number];
    }

}

Why did I encounter this error????? After entering the details it is throwing me an error as mentioned below

transact to modifierExample.enterDetails errored: VM error: revert.

revert The transaction has been reverted to the initial state. Reason provided by the contract: "you are not the owner". Debug the transaction to get more information.

Your enterDetails() function uses the isAdmin modifier.

As per the following code, any function that uses the isAdmin modifier, can be executed only by the address that deployed the contract.

address admin ;

constructor() public {
    admin == msg.sender;
}

modifier isAdmin {
    require(admin == msg.sender,"you are not the owner");
    _;
}

You're getting the you are not the owner error because you're trying to execute the function from some other address than the admin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM