0

I am trying to write a function to determine if the address msg.sender is the same address as the wallet that deployed the contract. I know that this is typically done via the the OpenZeppelin Ownable contract. As an exercise I wanted to see if I could make some of the logic myself.

The essential pieces are

pragma solidity ^0.6.8;
contract MyFund {

    mapping(address => Investor) public investors;
    address public manager;

    constructor(
        string memory _fundName,
        uint256 _installmentAmount,
        uint256 _numOfInstallments,
        uint256 _noOfInvestors
    ) public {
        fundName = _fundName;
        numOfInstallments = _numOfInstallments;
        noOfInvestors = _noOfInvestors;
        manager = msg.sender;
        installmentAmount = _installmentAmount * 1e18;
        jackpot = SafeMath.mul(installmentAmount, noOfInvestors);
    }

    modifier isManager() {
        require(msg.sender == manager, "Only the manager can access this function");
        _;
    }

    function checkIfManager() public view returns (bool) {
        if(msg.sender == manager){
            return true;
        } else {
            return false;
        }
    }
.
.    
.

I looked at this question, and it looked like my approach should in theory work? comparing two addresses

I just want to compare msg.sender (which is of type address) with manager (which is also of type address, using == for comparison.

I would think, if I deployed the contract with a metamask wallet, and then called the checkIfManager() function from the same metamask address, that it would return true, but that is not the case and I have no idea why.

I deploy the contract out to rinkeby and I have some FE code to call the checkIfManager function, and everything LOOKS like it aligns, between the manager address and my own address

FE code saying that checkIfManager is returning false

But according to that code, where my current address is the in top right corner, and the manager address is on the bottom, that function is returning false, and I am very confused by this. Is there something I am missing for how to compare two addresses for equality?

Edit: based on a comment by Ismael, the FE sets up the call to the view func like this

    const accounts = await web3.eth.getAccounts();
    setAccount(accounts[0]);
    const networkId = await web3.eth.net.getId();
    const networkData = MyFund.networks[networkId];
    if (networkData) {
      const myfund = new web3.eth.Contract(
        MyFund.abi,
        networkData.address
      );
    viewIsManager = await myfund.methods.checkIfManager().call();
2
  • 1
    How are you calling checkIfManager? For msg.sender to work reliably in a call to a view function you have to explicitly set the from parameter. Some frameworks use as default the zero address, others fail if it is missing.
    – Ismael
    Feb 15 at 3:44
  • That is a good point. I am using the web3 package in a react app. I think this is the call you are referring to, yes? const viewIsManager = await myfund.methods.checkIfManager().call(); Which is set up like const accounts = await web3.eth.getAccounts(); setAccount(accounts[0]); const networkId = await web3.eth.net.getId(); const networkData = MyFund.networks[networkId]; if (networkData) { const myfund = new web3.eth.Contract( MyFund.abi, networkData.address );
    – aaron
    Feb 15 at 4:46

2 Answers 2

0

If you're using Node.js and @usedapp, you could do something like this:

export function IsManager(address: string | Falsy) {
    const [isManager]: any =
    useContractCall({
        abi: interfaceAbi,
        address: tokenAdrress,
        method: "manager",
        args: []
    }) ?? [];
    return isManager;
}

const { account } = useEthers();
const etherBalance = useEtherBalance(account);
const isManager = IsManager(account);
0

based on the comment by Ismael, I updated the FE function call from

const viewIsManager = await chitfund.methods.checkIfManager().call();

To

const viewIsManager = await chitfund.methods.checkIfManager().call({from: accounts[0]});

Which did fix the behavior. This was not an obvious assumption to make about calling view functions vs other kinds, but the solution makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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