I'm attempting to write a contract which a variety of classes can inherit that give the inherited class access to various state variables of the child when called by name.
I.e. if parent has a function "food()" which returns 'hamburger', the inherited function is able to access the value of food by calling getAttribute("food()");
I tried to do it like this, but the success
variable returns false consistently.
The child contract is as follows inherits from a contract called reader.
pragma solidity >=0.7.0 <0.9.0;
import "./reader.sol";
contract mymain is reader {
constructor() {
myAddress=address(this);
}
function getName() public pure returns(string memory) {
return "foo";
}
function getFood() public pure returns(string memory) {
return "hamburger";
}
}
contract reader {
address myAddress;
function getAttribute(string memory functionName) public view returns(string memory) {
bytes memory payload = abi.encodePacked(functionName);
bool success;
bytes memory b;
(success, b)=myAddress.staticcall(payload);
if (!success) return "failed";
return(string(b));
}
}
When I deploy mymain and call mymain.getAttribute("getFood()");
I expect it to return hamburger
but it returns failed
. I am unfamiliar with the right way to encode the function selector, but obviously doing something wrong.