Created
March 26, 2023 15:04
-
-
Save CJ42/4cb5fdd692a338735c2c28cf308b8832 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: GPL-3.0 | |
pragma solidity ^0.8.0; | |
contract TryCatchNewContract { | |
string public hello; | |
function tryDeployingContract() public { | |
try new Target() returns (Target newContract) { | |
// example 1: retrieve the address of the deployed contract | |
address deployedAt = address(newContract); | |
// example 2: make some external calls to this newly deployed address | |
hello = newContract.doSomething(); | |
// example 3: read new contract's balance | |
uint256 balance = deployedAt.balance; | |
} catch (bytes memory) { | |
} | |
} | |
} | |
contract Target { | |
uint256 _callCounter; | |
function doSomething() public returns (string memory) { | |
_callCounter++; | |
return "state updated successfully"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment