I'm studying about call
function in soldiity and got stucked. Below is the code I'm working on and what I want is to call bar
function from Foo
in Loo
contract.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Loo {
Foo foo;
function callWithInstance(address payable _t, uint a, uint b) public payable returns(uint) {
foo = Foo(_t);
return foo.bar(a,b);
}
function callWithEncodeSignature(address _t, uint a, uint b) public returns(uint) {
bytes memory data = abi.encodeWithSignature("bar(uint, uint)", a, b);
(bool success, bytes memory returnData) = _t.call( data);
require(success);
uint c = abi.decode(returnData, (uint));
return c;
}
function callWithEncode(address _t, uint a, uint b) public returns(uint) {
(bool success, bytes memory returnData) = _t.call( bytes4(keccak256("bar(uint, uint)")), a, b);
require(success);
uint c = abi.decode(returnData, (uint));
return c;
}
}
contract Foo {
function bar(uint a, uint b) public pure returns(uint) {
return a+b;
}
}
call
function instead of calling the function directly from contract instance. What is the advantage of using it?callWithEncodeSignature
fails to get pass the require(success)
. I cannot find out the reason.callWithEncode
function gives "TypeError: Wrong argument count for function call". What did I missed?1- The call, callcode, and delegatecall functions are provided in order to interact with functions that do not have an ABI. These functions are not safe to use due to the impact on type safety and security of the contracts.
2- you should call it like this:
_t.call( data)("")
3- call
expects only one argument but you are passing 3 arguments
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.