Calling another contract (without any knowledge of its source code) and getting a typed return value

pipermerriampipermerriam Member Posts: 10
I know that with shared source code, you can instantiate an instance of another contract and call it's functions as documented in the solidity tutorial [1]

However, there are situations where a contract may not have access to the source code of the contract it needs to interact with. Consider the situation of a primary contract A that represents an organization, and a secondary contract B that handles the membership of the organization. Contract A stores an update-able reference to the current address of the membership contract. As the organization evolves, the membership contract (B) may change through various iterations while the primary contract A could remain static, only having it's reference to the membership contract updated.

One useful function would be for the primary contract A to be able to query the membership status of the sender of incoming transactions in order to handle permissions. In this case, the ideal situation is for contract A to be able to instantiate contract B and just call the `checkMembership(msg.sender)` function. However this approach is not possible without access to the source code of contract B.

So what's a developer to do.

I've thought up the following scheme to enable calling another contract via the `` api.

In the linked gist, there is a `Master` contract and a `Sub` contract. The function `Master.delegate` offloads the function call to a known API on the `Sub` contract. The `Sub` contract knows that it should report the answer back to the calling contract using the provided `key`, and the `Master` contract knows to then look for the answer using the provided `key`.

Problem is that it doesn't work, and I'm unsure why. I've deployed these contracts and as far as I can tell, my calls to the `delegate` method on the `Master` contract don't successfully pass the call along to the `Sub` contract. Can anyone see what is wrong with this? It seems like it should work.

1 -


  • SmithgiftSmithgift Member Posts: 64
    edited August 2015
    IIRC, all Solidity "knows" when calling another contract is the hash of the signature of the function it's calling, and compiled Solidity contracts have some kind of dispatcher that sends the right hash to the right code. So I think you can actually create a stub function for checkMembership with the correct signature on an "abstract" contract for contract B, and then have contract A call that stub. When B is a derived contract and implements checkMembership in whatever way, A should call the derived checkMembership just fine.
  • pipermerriampipermerriam Member Posts: 10
    Wow, so simple and yes, it works.
Sign In or Register to comment.