2

Consider the following file

contract A {
}
contract B{
}
contract C{
}

Is a file like that valid? Can a .sol file contain multiple contracts as above? If yes, which contract would the deploy address point to?

Now consider the following file

contract A{
}
contract B{
}
contract C is A, B {
}
contract D is A, B, C {
}

Solidity docs say,
When a contract inherits from other contracts, only a single contract is created on the blockchain, and the code from all the base contracts is compiled into the created contract.
So how does solidity identify which contract to compile the bytecode for, is it the most derived contract? If someone interacts with the deployed address, which contract would be serving them?

1 Answer 1

0

You have to choose which one to compile & deploy. Or, you can of course compile everything and only choose the one to deploy. Or, if needed, you can deploy all of them.

The used tooling also influences this. For example command-line utilities such as Hardhat and Truffle always compile everything (unless told otherwise). But for obvious reasons you always choose which contract to deploy.

2
  • So my assumption that a contract address contains only one contract correct? And if I decided to deploy say contract D, then the code of A,B,C is implicitly copied into D and bytecode is generated?
    – Pranay
    Jan 3, 2022 at 13:17
  • Yes, inherited contracts are flattened into the contract which is being compiled & deployed. So, from the bytecode's point of view, there is no inheritance. Jan 3, 2022 at 13:34

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.