Let’s say you’re building a simple calculator in Solidity. You want it to handle basic math — addition, subtraction, multiplication, and division.
Things are smooth until a user says:
“Hey, can this also calculate square roots? Or raise numbers to powers?”
You could throw everything into one huge contract...
But there's a better approach: split responsibilities.
One contract will handle the basics.
Another contract will handle advanced stuff — like powers and square roots.
Then, we’ll make them talk to each other.
Let’s build this step by step.
We’re going to build two smart contracts that work together:
Calculator.sol
– This will be our main calculator. It handles basic math like addition, subtraction, multiplication, and division. When it needs help with more advanced math, it delegates the task.ScientificCalculator.sol
– This contract is where we’ll put the advanced stuff — like exponentiation (powers) and square root calculations.Each contract will live in its own separate file, but they’ll be connected.
<aside> 💻
Here is the complete code 👇🏼 1️⃣ Calculator.sol :
https://github.com/snehasharma76/30daysSolidity_Web3Compass/blob/master/Calculator.sol
2️⃣ ScientificCalculator.sol :
https://github.com/snehasharma76/30daysSolidity_Web3Compass/blob/master/ScientificCalculator.sol
</aside>
In order to call functions from ScientificCalculator
inside the Calculator
contract, we’ll import it and store its deployed address.
⚠️ Important: Keep both files in the same directory, so the import works without issues.