当前位置:网站首页>Detailed explanation of call function in solidity
Detailed explanation of call function in solidity
2022-07-26 19:40:00 【LEVI_ one hundred and four】
Catalog
Call the external contract code
call Functions and callback functions
call Function to modify the state variable of the external contract
call Function transfer and callback function details
Call the external contract code
We have seen it before , Use interface、library To call the code of the external contract .
Next , We will add the third form for you :
In the following code , Deploy cat After the contract , For example, the address is 0x345678.. In the deployment animal At the time of the contract , Pass this cat Contract address . Thus, the reference of the contract can be stored . call test Method can call the method of the external contract .

call function
Whether it's interface、library Or the form seen above , To call external code , It's all the bottom calls call Or is it delecall function .
call Basic usage of function
call How to use the function , First, you need the address of the external contract . In the following example animalCall contract , When deploying contracts , Delivered external contracts cat The address of 0x345678.. , Stored in address c among .
By contract address .call( Function flags ) To invoke the contract . The function identifier is the first... After the hash of the function declaration 4 Bytes of data .
In the following cases ,c.call(bytes4(keccak256(“eat()”))) Will call cat In the contract eat Method

call Function return value
call The return value of the function is true perhaps false. Only when this method can be found and executed successfully , Returns the true, If the function cannot be found or the execution fails, it will return false. So call test1 Method will return true, call test2 Method will return false, Because the function cannot be found .
call Functions and callback functions
call Function if the function cannot be found , The callback function will be called by default .
Callback functions are special functions , It has no function name .
In the form of : function(){}
For the following cat contract . Write the callback function . Assume that the contract address is c. Then call... Externally c.call(“abc”); This function will not be found , The callback function will be executed by default . Therefore, it is called externally c.call(“abc”) Will make cat The state variable of the contract becomes 999. and call Function will return true.
contract cat{
uint public a = 5;
function ear() public returns(uint){
a = 256;
return a;
}
function () {
a = 999;
}
}call Function and msg.data
Callback functions are very useful , For example, when the external call fails , Perform some operation .
For the following cat contract . Write the callback function . Assume that the contract address is c. Then call... Externally c.call(“abc”); This function will not be found , The callback function will be executed by default . In the callback function , take msg.data The value of is assigned to fail Variable . adopt getfail Function can be viewed call The complete data passed by the function .fail The value of the variable is 32 Bytes 0x6162630000000000000000000000000000000000000000000000000000000000, front 3 Bytes are parameter letters a、b、c Of ASCII code .61、62、63.
contract cat{
bytes fail;
function (){
fail = msg.data;
}
function getfail() returns(bytes){
return fail;
}
}
call Function to modify the state variable of the external contract
In the following example ,cat Contract and animalcall There are state variables in the contract. Let's deploy first cat contract , Get the address 0x3456.., Next , Deploy the contract address as a parameter anumalCall contract .
call test2 Method , The call cat The contract eat Method , Revised cat In contract a The value of is 256. call Function call external contract , Modify the state variables in the external contract .

dalegatecall
delegatecall Function and call The function is the same , By contract address .delegatecall( Function flags ) To invoke the contract . The function identifier is the first... After the hash of the function declaration 4 Bytes of data .
library The remote call of the library is exactly used delegatecall function .delegatecall And call The difference is ,delegatecall State variables in external contracts will not be modified , It seems that the code of the external function is loaded into the local contract for execution . The value of the local contract status variable will be modified .
For example, the following code , Deploy first cat contract , Get the address 0x3456.., Next , Deploy the contract address as a parameter anumalCall contract .
call test2 Method , The call cat The contract eat Method , But it was modified animalcall State variables in contracts a. Therefore, it is found that ,cat In the contract a There is no change ,animalCall Contract variables a Changed into 256.
call Function transfer and callback function details
call Function can transfer money , And is transfer And send The underlying function of .call The use method of function transfer is
Address .call.value( Transfer amount )()
It should be noted that , When performing transfer , If the address of the transfer is the contract , And there is a callback function in the transfer contract . Then the callback function will be executed by default .
But Ethereum in order to avoid reentry attacks , about transfer And send Function is restricted . When using transfer And send function , The maximum number of operations performed in the callback function cannot exceed 2300gas. This means that the transfer cannot be performed 、 Assignment, etc , It can only perform operations such as event triggering .
For example, the following code : Deploy first Receiver contract , Get the address 0x3456.., Pass it on again Receiver Address deployment Sender contract . When calling sendMoney Method time , Is the contract address 0x3456.. The transfer operation will trigger the callback function , Change state variables balance Increase in the number of . But because the operation of modifying state variables exceeds the maximum 2300gas The limitation of , So the following operations will not succeed .

call Function can make the above operation successful .call Function can specify gas The limitation of , exceed 2300gas The constraints of limits .
As shown in the following example :
Deploy first Receiver contract , Get the address 0x3456.., Pass it on again Receiver Address deployment Sender contract . When calling sendMoney Method transfer 100wei When , Is the contract address 0x3456.. The transfer operation will trigger the callback function , Change state variables balance Increase in the number of . because call Function specifies the maximum gas Limit to 20317, So trigger the callback function to balance The amount of is revised to 100. But be careful , Because of this ,call Functions are dangerous underlying functions , The problem of reentry attacks cannot be avoided .
边栏推荐
猜你喜欢

DDL,DQL,DML语句

cuda11.2对应pytorch安装

工作13年后,个人的一点软件测试经历及感想……

How to protect e-commerce websites from network attacks?
![[server data recovery] data recovery case of server storage shared folder loss](/img/bf/3f86a4e8abd4e045b022fd7574636f.png)
[server data recovery] data recovery case of server storage shared folder loss

Pyqt5 rapid development and practice 3.6 packaging resource files

【YOLOv5】--详细版训练自己的数据集 保姆级学习日志记录 手把手教程

香港高防IP优势及哪些行业适合使用

What is a server cluster? What are the advantages of overseas server clusters?

C语言-入门-语法-字符串(十一)
随机推荐
What is a server cluster? What are the advantages of overseas server clusters?
服务器内存故障预测居然可以这样做
How to solve the problem that win11 has been switched on after upgrading
SEO, client rendering ', server rendering, search engine understanding
Adjust the array order so that odd numbers precede even numbers and their relative positions remain the same
conda+pytorch环境教程
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Several ways to view containers
聊聊如何用 Redis 实现分布式锁?
Using MySQL master-slave replication delay to save erroneously deleted data
如果密钥忘记,多个设备分别不同的密钥,云端是如何同步
Save gas chitoken usage
conda转移项目虚拟环境必备技能+pip速度下载太慢解决办法
Software process that testers must know
3r平衡型理财产品会有风险吗?风险大吗?
关于接口测试你想知道的都在这儿了
时空预测4-graph wavenet
Do you know the difference between safety test, functional test and penetration test?
Conda+pytorch environment tutorial
C # create and read dat file cases