Created
July 18, 2023 13:45
-
-
Save TehilaFavourite/34406044291c28496b6551a718db4da4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract ExternalCallExample { | |
address public targetContract; | |
uint256 public lastResult; | |
constructor(address _targetContract) { | |
targetContract = _targetContract; | |
} | |
function doExternalCall(uint256 data) public { | |
// Use require to validate the target contract address | |
require(targetContract != address(0), "Target contract address not set"); | |
// Perform an external call to the target contract | |
(bool success, bytes memory result) = targetContract.call(abi.encodeWithSignature("processData(uint256)", data)); | |
// Check if the external call was successful | |
require(success, "External call failed"); | |
// Parse the returned data | |
lastResult = abi.decode(result, (uint256)); | |
} | |
} | |
// Example Target Contract | |
contract TargetContract { | |
function processData(uint256 data) external pure returns (uint256) { | |
return data * 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment