header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

A minimalist introduction to Solidity|Lecture 21: Calling other contracts

Read this article in 7 Minutes
This article introduces 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 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


Call Deployed contracts


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.


Target contract


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.


Call the OtherContract contract


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:


1. Pass in the contract address


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:



2. Incoming contract Variable


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.



3. Create a contract variable


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:



4. Call the contract and send ETH


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.



After the transfer, we can observe the change of the ETH balance of the target contract through the Log event and the getBalance() function.


Summary


< p>

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


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

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit