3

I have a smart contact which have 2 owner address and 3 functions.

my set functions is set a users with msg.sender and my getUser functions is returns name,surname and age and I want to add a admin which can set users to . This setUsersbyAdmin function take an address parameter for set name,surname and ag.

My smart contract works well on remix but I try to truffle compile it failed.

here's the code :

pragma solidity >=0.4.0 <0.7.0;

contract NewHello{ 

    address owner;
    address newOwner;

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

    modifier onlyOwner (){
        if(msg.sender != owner){
            require(msg.sender == newOwner);
            _;
        }
        else{
            require(msg.sender == owner);
            _;
        }

    }

    struct user{
        string name;
        string surname;
        uint age;
    }
     event infoChanged(
        string name,
        string surname,
        uint age
    );

    mapping(address => user) public users; // contains a user for every address

    function setUser(string memory name,string memory surname,uint age) public payable{
        users[msg.sender] = user(name,surname,age);
        emit infoChanged(name,surname,age);
    }
    function setUserbyAdmin(address _address, string memory name,string memory surname,uint age) public payable onlyOwner{
        users[msg.sender] = user(name,surname,age);
        emit infoChanged(name,surname,age);
    }

    function getUser(address _address) public view returns(string memory name , string memory surname, uint age)  {

        return (users[_address].name,users[_address].surname,users[_address].age);        
    }
}

when I compile this with truffle I see an error on my window which says :

Warning: Unused function parameter. Remove or comment out the variable name to silence this warning. function setUserbyAdmin(address _address, string memory name,string memory surname,uint age) public payable onlyOwner{ ^--------------^

3 Answers 3

11

You will see the same warning in Remix if you look at the compiler page.

The warning is quite clear: you have a variable which is not used anywhere in the function logic. It's not an error but the compiler is trying to help you to save gas and to make the function cleaner by removing the unneeded parameter. If you want you can just ignore the warning.

There is a curious feature that if you remove the variable name the warning disappears. But the function still has the same signature. I actually don't know why such a thing has been implemented.

4
  • thanks but do you know how can I set my users using with setUsersbyAdmin function ? can you help me about this ? Commented Jul 2, 2019 at 13:36
  • 1
    just remove the first parameter, it's useless Commented Jul 2, 2019 at 13:53
  • 3
    The answer is, sometime you have to obey the callee rules such as flash loan callback. The signature is fixed but only some of parameters you need in your function logic.
    – Jolly23
    Commented Oct 14, 2021 at 3:28
  • @Jolly23 Exactly, specially when writing tests, sometimes you want to create dumb implementations of some protocol interface just to run your tests locally, in this cases you probably will not use many of the function parameters, and you might get a lot of warnings in the console Commented Oct 8, 2023 at 20:06
3

Sometimes you will want to override a function provided by a library e.g. this ERC721._beforeTokenTransfer hook.

Suppose that you want to override this hook to prevent a specific token 12345 from being transferred:

function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override {
    require(tokenId != 12345);
}

Since from and to are unused, the compiler will warn you about the unused variables from and to, but you cannot delete them from the parameter list altogether, as that would alter the function signature. In order to preserve the signature, you must leave the parameter type, but the unused parameter name may be either deleted (see from below) or commented out (see to below):

function _beforeTokenTransfer(address, address /*to*/, uint256 tokenId) internal override {
    require(tokenId != 12345);
}

Commenting out is preferable, as is simpler to understand what’s going on, and if you suddenly want to use that variable, you can just uncomment it back.

0

Hey guys I solved this problem. Here's the code:

pragma solidity >=0.4.0 <0.7.0;

contract NewHello {
    address owner;
    address newOwner;
    constructor()public{
        owner = msg.sender;
    }

    modifier onlyOwner() {
        if (msg.sender != owner) {
            require(msg.sender == newOwner);
        } else {
            require(msg.sender == owner);
            _;
        }
    }

    struct user {
        string name;
        string surname;
        uint age;
    }
    
    event infoChanged(
        string name,
        string surname,
        uint age
    );

    mapping(address => user) public users; // contains a user for every address

    function setUser(string memory name,string memory surname,uint age) public payable {
        users[msg.sender] = user(name,surname,age);
        emit infoChanged(name,surname,age);
    }
    
    function setUserbyAdmin(address _address,string memory name,string memory surname,uint age) public payable {
        users[_address] = user(name,surname,age);
        emit infoChanged(name,surname,age);
    }

    function getUser(address _address) public view returns(string memory name , string memory surname, uint age) {
        return (users[_address].name,users[_address].surname,users[_address].age);        
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.