I'm trying to inherit and override a parent's payable function (and adding some modifier and logic) but so far seem unable to do so.
My first intuitive try was something like this:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function test() public payable returns (uint256) {
return super.test.value(msg.value / 2)();
}
}
and received compile error:
TypeError: Member "value" not found or not visible after argument-dependent lookup in function () - did you forget the "payable" modifier?
super.test.value(msg.value)();
Give the function a different name doesn't work either so it's not an overriding issue.
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function anotherTest() public payable returns (uint256) {
return super.test.value(msg.value / 2)();
}
}
I got slightly further by tricking compiler with a casting but this is obviously no good as would cause infinite loop at runtime:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function test() public payable returns (uint256) {
return foo(address(this)).test.value(msg.value / 2)();
}
}
What works is combining the two, i.e. giving it a different name and casting:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function anotherTest() public payable returns (uint256) {
return foo(address(this)).test.value(msg.value / 2)();
}
}
Now calling bar.test() with 6 wei returns 6 and calling bar.anotherTest() returns 3. But this isn't exactly what I want as it still exposes test() through bar.
So what am I doing wrong, or is this by design? Would really like to understand why Solidity behave this way and if there's a workaround to achieve what I want. thanks!