Different approaches to call a smart contract function
Method-1
Let us assume that we have two smart contracts A and B. The smart contract A has already been deployed in the network and we have its contract address. Now lets say A has a function foo() that I need to call from the smart contract B. What can be done is that we can get the contract address of A inside the smart contract B and store this address inside a state variable. Now using the address we can use a call function in order to call the foo function of the smart contract B.
//The address of the test dhruv smart contract is passed as a function argument
function callfoo(address _testdhruv) external payable{
//The call function returns two arguments
(bool success, bytes memory _data)=_testdhruv.call{value:100}(abi.encodeWithSignature("foo(uint256)",5));
require(success==true,"The call to the function has failed");
data=_data;
}
Method-2
In this method again there are two smart contracts A and B. The smart contract A has been compiled and we have the ABI and the code available available. We import the smart contract. What we can do is create a state variable of this smart contract and using the address of the smart contract we can create an instance of the smart contract. Now we can call the functions of this smart contract.
An example is :
import "./DiceGame.sol";
contract RiggedRoll is Ownable {
DiceGame public diceGame;
constructor(address payable diceGameAddress) {
diceGame = DiceGame(diceGameAddress);
}
function tp() public
{
diceGame.foo{value:0.001 ether}(1);
}
}