0

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

2
  • Try to replace 5 and 6 with uint(5) and uint(6). Dec 23 '20 at 19:02
  • Thanks but i got the same result
    – Bob5421
    Dec 23 '20 at 20:53
0

The types uint and uint256 are identical (the former is just an alias of the latter).

However, the strings "uint" and "uint256" are obviously not identical.

Hence your attempt to call myfunction(uint256) using the encoded signature (i.e., the hash of) "myfunction(uint)" is wrong.

So to begin with, change the string "uint" to "uint256".

0

Is it a good practice to declare Contract1Interface this way?

This is acceptable.

Contract A can provide the interface and this can help ensure the interface description is correct:

contract IA {
  function doSomething() external view returns(unit);
}

contract A is IA {
  ...
}

Now, contract IA could be shared so that contract B will be confident they are using the correct interface.

We can imagine a situation where Contract2's developer do not have Contract1 source code. This developer only knows the signature of the function (interface). Or is there a best way to do that?

If contract B's author doesn't have the code for IA and A then, in practice, there are other, bigger concerns. What you have done is workable.

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 ?

These are separate questions, each of which is a little rabbit hole of its own. Please post one question at a time.

Hope it helps.

1
  • I think you've missed the main point (see my answer). Dec 23 '20 at 21:04

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.