I've deployed this very simple Contract:
contract Contract1
{
function myfunction(uint param1) public returns (uint)
{
return param1+1;
}
}
Now, I've deployed a second contract:
interface Contract1Interface
{
function myfunction(uint) external returns (uint);
}
contract Contract2
{
event log(string msg);
Contract1Interface c = Contract1Interface(0x...deploymentadress_of_contract1);
uint ret;
bytes public data;
function test1() public
{
// COMPILATION ERROR: try ret=c.myfunction(param) { } catch { }
try c.myfunction(param) { } catch { }
}
function test2() public
{
bool success;
(success, data) = address(c).call{value: 0 ether}(abi.encodeWithSignature("myfunction(uint)", 5));
if (success) {
emit log("ok");
} else {
emit log("nok");
}
}
function test3() public
{
bool success;
(success, data) = address(c).call(abi.encodeWithSignature("myfunction(uint)", 6));
if (success) {
emit log("ok");
} else {
emit log("nok");
}
}
I have 4 questions:
- Is it a good practice to declare Contract1Interface this way ? We can imagine a situation where Contract2's developper do not have Contract1 source code. This developper only knows the functions signature (interface). Or is there a best way to do that ?
- in test1 function, i cannot fetch returned value for myfunction. If i add ret=, i get a compilation error.
- in test2 and test3 functions, success is equal to false. I do not understand why the call fails
- In test2 and test3 functions, how can i "decode" data variable to uint ?
Thanks a lot
5
and6
withuint(5)
anduint(6)
.