Solidity Calling Contracts
Calling a contract means one contract asks another contract to run a function and uses the answer. Each contract keeps its own state, and the call crosses from one contract to the other.
A caller stores the helper's address
The helper is deployed first, meaning it is put on the blockchain. Its address is then handed to the caller at creation.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract Helper {
function answer() public pure returns (uint256) {
return 42;
}
}
contract Caller {
address public helperAddress;
constructor(address helper) {
helperAddress = helper;
}
}
A caller asks the helper for a value
The caller wraps its stored address with the Helper name. That typed address can call the helper's functions.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract Helper {
function answer() public pure returns (uint256) {
return 42;
}
}
contract Caller {
address public helperAddress;
constructor(address helper) {
helperAddress = helper;
}
function askHelper() public view returns (uint256) {
return Helper(helperAddress).answer();
}
}
A caller stores the helper's answer
The caller can keep the answer in its own state. Later calls can then read the stored copy.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract Helper {
function answer() public pure returns (uint256) {
return 42;
}
}
contract Caller {
address public helperAddress;
uint256 public storedAnswer;
constructor(address helper) {
helperAddress = helper;
}
function fetchAnswer() public {
storedAnswer = Helper(helperAddress).answer();
}
}
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract FeeHelper {
function fee() public pure returns (uint256) {
return 5;
}
}
contract Checkout {
address public helperAddress;
constructor(address helper) {
helperAddress = helper;
}
function total() public view returns (uint256) {
uint256 base = 20;
uint256 extra = FeeHelper(helperAddress).fee();
return base + extra;
}
}
Key points
- Helper contract: the contract that provides a function another contract wants to use.
- Caller contract: the contract that asks the helper for an answer.
- An address identifies a contract: a contract's address is its 20-byte id on the blockchain.
- Handed in at creation: the caller can receive the helper's address in its constructor.
- A typed address: writing the helper's contract name before an address lets Solidity call its functions.
- The answer crosses back: a value the helper returns becomes a value the caller can store or return.
In this example, FeeHelper provides a fee function that returns 5. Checkout receives the helper's address in its constructor and stores it. Its total function asks the helper for the fee, adds the fee to a base of 20, and returns 25.
Note
calling another contract does not copy its code or its state into the caller.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Remix IDE
Exercise
How does the Caller in these lessons learn the Helper's address?