Original title: " A minimalist introduction to Solidity: 21. Calling other contracts 》
Original author: 0xAA
I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist "Getting Started" is for novices (programmers can find other tutorials), 1-3 lectures are updated every week.
All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity
Developers write smart contracts to call other contracts, which allows programs on the Ethereum network to be reused, thereby establishing a prosperous ecology. Many web3 projects rely on calling other contracts, such as yield farming. In this lecture, we introduce how to call the function of the target contract when the contract code (or interface) and address are known.
We first write a simple contract OtherContract to call.

This contract Contains a state variable x, an event Log triggered when ETH is received, and three functions:
getBalance(): returns the contract ETH balance.
setX(): external payable function, which can set the value of x and send ETH to the contract.
getX(): Read the value of x.
We can use the contract address and contract code (or interface) To create a contract reference: _Name(_Address), where _Name is the contract name and _Address is the contract address. Then use the contract's reference to call its function: _Name(_Address).f(), where f() is the function to call.
Here we introduce 4 examples of calling contracts:
We can pass in the address of the target contract in the function, generate a reference to the target contract, and then call the target function. Take calling the setX function of the OtherContract contract as an example, we write a callSetX function in the new contract, and pass in the deployed OtherContract contract address _Address and the parameter x of setX:

We can directly pass in the reference of the contract in the function, just change the address type of the above parameter to the target contract name, than OtherContract. The following example implements the getX() function of calling the target contract.

We can create a contract variable and then call the target function through it. In the following example, we store a reference to the OtherContract contract in the variable oc:

If the target contract The function is payable, so we can transfer money to the contract by calling it: _Name(_Address).f{value: _Value}(), where _Name is the contract name, _Address is the contract address, f is the target function name, and _Value is The amount of ETH to transfer (in wei).
The setX function of the OtherContract contract is payable. In the following example, we transfer money to and from the target contract by calling setX.

p>
After the transfer, we can observe the change of the ETH balance of the target contract through the Log event and the getBalance() function.
In this talk, we introduce how to create a contract reference through the target contract code (or interface) and address, so as to call the function of the target contract.
Original link blockquote>
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia