0

I have a Smart Contract that I deployed on the blockchain. I would like to create another smart contract that will call the first smart contract. I would like to create an interface containing method signatures of the first contract so I can use it in the caller smart contract because I don't want to copy the whole source code of the first contract to the second one.

I am not sure how to do that automatically

0

This is how you can create interface and read or update state of already existing contract, let's suppose contract A is already deployed contract on the blockchain and in contract B you create instance of contract A by passing the contract address in the constuctor when deploying :

ContractA.sol:

pragma solidity ^0.5.11;

contract A {
    uint256 public someVariable = 0;
    
    function updateSomeVariable (uint256 _someVariable) public {
        someVariable = _someVariable;
    }
    
    function getSomeVariable() public view returns(uint256) {
        return someVariable;
    }
}

ContractB.sol:

pragma solidity ^0.5.11;

contract B {
    A a_contract_instance;
    constructor(address _a_contract_address) public {
        a_contract_instance = A(_a_contract_address);
    }
    
    function updateSomeVariableContractA(uint256 _someVariable) public{
        a_contract_instance.updateSomeVariable(_someVariable);
    }
    
    function getSomeVariableContractA() public view returns(uint256) {
        return a_contract_instance.getSomeVariable();
    }
}

interface A {
    function updateSomeVariable (uint256 _someVariable) external;
    function getSomeVariable() view external returns (uint256);
}
2
  • Hello. I know how the interface should look like. But, is there a way to make the interface automatically. What if I have a huge smart contract? I'm trying to avoid to do everything manually Dec 25 '20 at 12:20
  • I don't really think generating new interfaces or new interface methods is possible dynamically. This would be contrary to the main principle of smart contracts - that they should not be changeable once they're deployed. One way you can achieve this is by using upgradeable smart contracts. Split the logic into 2 conracts ( state and proxy ) and the interface connect will be added to the proxy contract. So whenever you need to change something on the interface connection or add new interfaces just replace the proxy contract. If you're not familiar with upgradeable contract better read about it. Dec 26 '20 at 6:54

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.