Skip to content

Instantly share code, notes, and snippets.

@CJ42
Created March 26, 2023 15:04
Show Gist options
  • Save CJ42/4cb5fdd692a338735c2c28cf308b8832 to your computer and use it in GitHub Desktop.
Save CJ42/4cb5fdd692a338735c2c28cf308b8832 to your computer and use it in GitHub Desktop.
// 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