I have multiple contract's addresses and their respective interfaces.

Name of contracts are:

 1. ContractA_v1
 2. ContractA_v2

Name of interfaces are:

 1. IContractA_v1
 2. IContractA_v2

allConractAddresses holds the addresses of my 2 contracts;

address [] allConractAddresses;
uint256 lastIndex = allConractAddresses.length - 1;
        while (lastIndex >= 0) {
            address cAddress = allConractAddresses[lastIndex];
            versionedSC = IContractA_v1(cAddress);

            //rest code here

My concern is how can I make the line versionedSC = IContractA_v1(cAddress); dynamic with interface name?

1 Answer 1


If the "rest code" works for every interface it means that these contracts shares a common subset of methods with the same signatures. You could simply create an interface, this is the idea:

interface ICommon {
    function foo();
    function bar();

interface IContract1 is ICommon {
    function exclusiveMethod1();

interface IContract2 is ICommon {
    function exclusiveMethod2();

(I haven't tried this code, it's only to grasp the idea)

This way that line simply become versionedSC = ICommon(cAddress);. Note that you may not even need the IContract<i> declarations in your contract if you always call only the common methods.

  • If I do versionedSC = ICommon(cAddress); then how can I execute exclusiveMethod1() method from IContract1?
    – NinjaMAN
    Jul 15, 2021 at 14:23
  • You can't and I don't understand why you would, if the "rest code here" part works for every contract in allContractAddresses this means that in that part only the common methods (foo() and bar()) are called. If the "rest code here" part calls different methods on different contracts it must use conditionals to distinguish them and therefore you can simply load the different addresses using different interfaces. However if you have a finite number of interfaces you can always transform your loop in a long sequence of instructions. Jul 15, 2021 at 14:38
  • That's the main problem I have. I am not sure over time how many interfaces there are going to be and for that how many if else conditions do I have to implement for picking up the interfaces. Thats the reason I want to have the versionedSC = IContractA_v1(cAddress); dynamic
    – NinjaMAN
    Jul 15, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.