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

Simple Introduction to Solidity|Lecture 22: Call

Read this article in 9 Minutes
This article describes how to use the low-level function call to call other contracts.
Original title: " A minimalist introduction to Solidity: 22. Call "
Original author: 0xAA     


I am re-learning solidity recently, consolidate the details, and write a "Solidity "Minimalist Introduction", for beginners (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


We In Lecture 17: Sending ETH, we introduced the use of call to send ETH. In this lecture, we will introduce how to use it to call the contract.


Call


call  is a low-level member function of address type, which is used to communicate with other Contract interaction. Its return value is (bool, data), corresponding to whether the call is successful or not and the return value of the target function.


call is the official solidity recommended method to send ETH by triggering the fallback or receive function. It is not recommended to use call to call another contract, because when you call a function of an unsafe contract, you give it the initiative. The recommended method is still to call functions after declaring contract variables, see Lecture 19: Calling other contracts. When we don't know the source code or ABI of the other party's contract, we can't generate contract variables; at this time, we can still call the function of the other party's contract through call.


The usage rules of call


The usage rules of call are as follows:



The binary encoding is obtained by using the structured encoding function abi.encodeWithSignature:



The function signature is "function name (parameter types separated by commas)". For example abi.encodeWithSignature("f(uint256,address)", _x, _addr).


In addition, call can specify the ETH amount and gas sent by the transaction when calling the contract:



It looks a bit complicated, let's give an example of a call application.


Target contract


We first write a simple target contract OtherContract and deploy it, the code is the same as It is basically the same as in Lecture 19, except that there is an extra fallback function.



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.


Use call to call the target contract


1. Response event


We write a Call contract to call the target contract function. First write a definition of the Response event, and output the success and data returned by the call, so that we can observe the return value.



2. Call the setX function


We define the callSetX function to call the setX() of the target contract, transfer the msg.value amount of ETH, and release the Response event to output success and data :



Connect Next, we call callSetX to change the state variable _x to 5, and the parameters are OtherContract address and 5. Since the target function setX() has no return value, the output data of the Response event is 0x, which is empty.


3. Call the getX function


Next we call the getX() function, which will return The value of the target contract _x, the type is uint256. We can use abi.decode to decode the return value data of call and read out the value.



From Response In the output of the event, we can see that the data is 0x0000000000000000000000000000000000000000000000000000000000000005. After abi.decode, the final return value is 5.


4. Call a function that does not exist


If the function we input to call does not exist For the target contract, the fallback function of the target contract will be triggered.



Example above , we call the non-existing foo function. The call can still execute successfully and return success, but it actually calls the fallback function of the target contract.


Summary


In this lecture, we introduced how to use the low-level function call to Call other contracts. call is not the recommended way to call a contract because it is not safe. But it allows us to call the target contract without knowing the source code and ABI, which is very useful.


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