How would I call one contract's function from within another contract? I tried to find documentation on this all morning and wasn't able to find anything.
-
Here? http://solidity.readthedocs.io/en/develop/contracts.html?highlight=transactions#creating-contracts – vincentLg Jun 07 '17 at 14:20
-
@vincentLg , so I can use JavaScript API inside a contract being written in Solidity language? – Luiz Fonseca Jun 07 '17 at 14:31
-
1web3js is for the frontend inwhich you could call the function in solidity contract (backend) – Badr Bellaj Jun 07 '17 at 15:08
-
2This looks like it was downvoted a bit, but I actually found this rather tough to find in the docs even after a thorough read – OneChillDude Sep 14 '17 at 23:17
2 Answers
Please find an example here (call part of this section) in solidity documentation where calling an already deployed contract from inside another contract is shown. We can call functions in other deployed contracts using
address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
nameReg.call(bytes4(keccak256("fun(uint256)")), a);
Here the first parameter is the function signature and the next parameters are arguments for the function.
- 1,019
- 6
- 11
-
Thank you for the answer. But it seems you showed me how I could do that with Javascript API. But actually I would like to do that in Solidity language. Something like https://ethereum.stackexchange.com/questions/15323/how-to-call-a-function-from-an-already-deployed-contract And I would like to find that in the docs http://solidity.readthedocs.io/en/develop/index.html. – Luiz Fonseca Jun 07 '17 at 14:27
-
Please see in the call member of the address here http://solidity.readthedocs.io/en/develop/types.html#members-of-addresses . You can see how they tried to call a function using its function signature inside a solidity contract using
nameReg.call(bytes4(keccak256("fun(uint256)")), a);– joifsi Jun 07 '17 at 14:53 -
Can you please give a reference to, how to call a function with 2 parameters? – Anupam Mar 10 '21 at 08:39
Try to employ this approach here as it is easier to read and is promoted for uses, such as ERC20 token interaction:
contract Called{
uint public myuint;
function set(uint _var) {
myuint = _var;
}
function get() view returns (uint){
return myuint;
}
}
interface Called{
function set(uint);
function get() view returns (uint);
}
contract Caller {
Called public called_address;
function set_address(address _addy) {
called_address = Called(_addy);
}
function set(uint256 _var) {
called_address.set(_var);
}
function set_call(address _called, uint256 _var) {
require(_called.call(bytes4(sha3("set(uint256)")), _var));
}
}
Here you have an already deployed contract "Called" and can access it with an interface block included as part of your contract file. The last function set_call is same to previous answers on this page and is left for comparison.
Be aware that all calls from your contract cost gas, even if the functions are marked view.
Credit to @eli-drion for providing code that I didn't need to drum up from scratch: https://ethereum.stackexchange.com/a/44384/36872
- 483
- 3
- 14