Call the variable or function from other contract

Solidity Programming Language
3 min readMay 30, 2023

We try to call other contract function(or variable) from our contract. We learn all the concept about this issue. Firstly you need to learn the visibility in solidity

We have two main contracts and each of them assigns one price value to price variable.

contract PriceOracle1 {
uint256 public _price;

function setPrice(uint256 newPrice) public {
_price = newPrice;
}

function price() external view returns (uint256) {
return _price;
}
}

contract PriceOracle2 {
uint256 public _price;

function setPrice(uint256 newPrice) public {
_price = newPrice;
}

function price() external view returns (uint256) {
return _price;
}
}

We can assign price value with “setPrice function” and we see the result directly in “_price variable” or indirectly in “price function”.

We start to write the our code which we call these two contracts. We call price functions from these two contracts. The function below is to call the price function of PriceOracle1 and PriceOracle2 contracts below and return the lower of the two prices. We need the address of these contracts and we take these address in getLowerPrice function as a parameter.

contract CrossContract {

function getLowerPrice(
address _priceOracle1,
address _priceOracle2
) external view returns (uint256) {

if(PriceOracle1(_priceOracle1).price() < PriceOracle2(_priceOracle2).price()){
return PriceOracle1(_priceOracle1).price();
}

else {
return PriceOracle2(_priceOracle2).price();
}

}
}

How to call the price function in the PriceOracle1 contract from our CrossContract contract ?

PriceOracle1(_priceOracle1).price()

Contract Name is PriceOracle1, Contract’s address is _priceOracle1 and Contract’s function is price().

In the practice, you start to assign the your value to the price variable in PriceOracle1 and then you can call from CrossContract contract.

This is our first code.

Other method is not different our first method.

We create PriceOracle1 type contract, assign its address value in the callOracleContract function and use this oracle.

Step by step:

1- We create the types of the PriceOracle1 and PriceOracle2 contracts in the CrossContract contract.

 PriceOracle1 priceOracle1;
PriceOracle2 priceOracle2;

2- We assign their’s address values in the callOracleContract function in the CrossContract contract.

function callOracleContract( address _p1, address _p2) public {
priceOracle1 = PriceOracle1(_p1);
priceOracle2 = PriceOracle2(_p2);
}

3- We use these oracles in the CrossContract contract.

 function getLowerPrice() external view returns (uint256) {


if(priceOracle1.price() < priceOracle2.price()){
return priceOracle1.price();
}
else {
return priceOracle2.price();
}
}

This is second code.

The first solution:

The second solution:

Associate Professor Engin YILMAZ (VeriDelisi)

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps