4

Let's say I have multiple smart contracts that inherit from one base contract:

Contract Base{
   function doSomething(){};
}

Contract A is Base{
}

Contract B is Base{
}

What I want to do is call doSomething without knowing which particular contract I'm dealing with, just having the address.

instance = web3.eth.contract(BaseABI).at(givenAddress);
instance.doSomething();

I believe that is polymorphism (correct me if I'm wrong). So, is that possible ? Just to be clear, the givenAddress can be the address of a contract A, B or any other contract that's been deployed and that inherits from Base. I want to be able to retrieve an instance of that contract without giving the specific ABI, just the Base one, since all I want to do in this case is calling the the Base doSomething.

Thank you for your advice.

PS: I did try that (with testrpc) which gives me an invalid op code.

  • Dont think this is possible, since the ABI is used to parse the bytecode at the address. – jojeyh Nov 26 '17 at 15:47
  • Thanks for answering! I don't see how this would be a limitation though: the only bytecode we need is the one implementing the function of the base contract, not the full bytecode giving access to the methods the contracts inheriting the base one would provide. Or are you saying that to retrieve an instance of a contract, you need the full ABI of that contract? (I have no idea how the web3.eth.contract and at methods are implmented) In this case, polymorphism isn't really implemented on Ethereum yet... I'd like a better insight if possible. – Hillfias Nov 26 '17 at 16:39
  • I'm saying that the bytecode is the raw data of the contract, and the ABI code is the framework that the bytecode goes in. If you are using the bytecode at a given address on the blockchain, but using a different ABI to structure it, how does the ABI know how to structure it? The ABI file maps the blockchain data to a contract object, but if you use a different ABI file it's like using an entirely different mapping function. – jojeyh Nov 26 '17 at 16:55
  • In fact, it is only necessary that signatures match. When you want, you even don't have to inherit from an interface and you can provide the implementation directly. The only thing that matters is the signature. – ivicaa Jun 13 '18 at 10:53
1

Yes, this should work. For example ERC-20 tokens, if your token implements the standard interface then any wallet can query your token.

For any contract it should work the same, if your contract implements the same abi then you can interact with it.

0

I think you're setting the instance wrong in testrpc. Try ContractA.deployed().then(function(i) { instance = i })

You should be able to call instance.doSomething() with that.

0

I'd like to answer my own question by saying that yes this works. Polymorphism works with Ethereum smart contracts (which is great).

The reason I was getting an error was because of testrpc. It's an otherwise great tool to get started but lacks (more than one, I stumbled on some other things not working as intended) some features that are actually provided by smart contracts.

Testrpc is not maintained (from what I recall) anymore, hence some (we could argue) advanced features it does not really support. I recommend for beginners to try it out. But I'd move to a real geth node and to Remix to really test out your contracts.

Hope this helps :) Thank you to the previous who answered.

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.