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
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();
checkIfManager
? Formsg.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.