0

Trying to get the return function of a contract from another contract. Don't know what I am doing wrong.

I can see the return value when I call contract A directly but when I try calling the function from Contract A, it does not work.

contract A{
  function validateSchedule(uint scheduleId)
    public
    view
    returns(uint)
  {
    uint amount = schedules[scheduleId].amount;
    return amount;
  }
}

Here is the second contract

import "./A.sol";

Contract B is A{

  A Multisig;

  function verifySchedule(uint scheduleid)
        public
        view
        returns (uint)
        {
        uint amount = Multisig.validateSchedule(scheduleid);
        return amount;
         }
}
0

Since contract B is already inheriting from contract A, it has access to all the functionalities contract A has, so you can rewrite your contract B as :

import "./A.sol";

Contract B is A{

  function verifySchedule(uint scheduleid)
      public
      view
      returns (uint)
  {
      uint amount = validateSchedule(scheduleid);
      return amount;
  }
}

Doing the above should work perfectly.

Notice I took out your Multisig definition and also the Multisig in front of validateSchedule(scheduleid). That's because you're just defining Multisig as a type of contract A, meaning the Multisig variable you created should only be assigned an instance of contract A. So at that point you only just declared it empty.

0

contract B is A

No. That's inheritance. Just go contract B {

A Multisig;

Yes. An instance of contract A called multisig (should be camel case).

So, far, multisig is cast as type "contract A" but it isn't instantiated. Think about;

uint x;

versus

uint x = 10;

B knows how to communicate with an A but it's missing the address of the specific A that it should talk to which is A's address. A common approach is to deal with that in a constructor.

constructor(address a) public {
  A = A(a); // use the address to provide the missing piece
}

Or even

A a;    
constructor(A _a) public {
  a = _a;
}

You can instantiate on the fly as needed. For example, consider a contract designed to work with ERC20 tokens. You could say

import "./IERC20.sol"; // the interface

then carry on in a function and go:

function myBalance(IERC20 token) public returns(uint) {
   return token.balanceOf(msg.sender); // assume we got the address of an ERC20 contract and carry on

The function would take an address argument and implicitly work out that it should have the ERC20 functions described in the interface.

Hope it helps.

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.