Hi @weili,
Welcome to the community forum
.
Thanks for posting here.
I couldn’t find any documentation on what happens, so I tried it locally and when a contract factory creates a contract that reverts on creation, then the transaction count of the contract factory does not increment. The contract factory transaction count increments when a contract is successfully created.
The same happens when a proxy contract is created using Create2 and initialized, the transaction count is incremented. When a proxy contract is created using Create2 and initialization reverts, the transaction count does not increment.
I used the following contracts and obtained the following results:
MyFactory.sol
pragma solidity ^0.5.0;
import "@openzeppelin/upgrades/contracts/upgradeability/ProxyFactory.sol";
import "./MyContract.sol";
import "./MyContractRevert.sol";
contract MyFactory is ProxyFactory {
function deployMyContract() public {
new MyContract();
}
function deployMyContractRevert() public {
new MyContractRevert();
}
function deploy(uint256 salt, address logic, address admin, bool init) public returns (address) {
bytes memory payload = abi.encodeWithSignature("initialize(bool)", init);
deploy(salt, logic, admin, payload);
}
}
MyContract.sol
pragma solidity ^0.5.0;
contract MyContract {
function initialize(bool init) public {
require(init, "MyContract: false");
}
}
MyContractRevert.sol
pragma solidity ^0.5.0;
contract MyContractRevert {
constructor() public {
require(false, "MyContractRevert: false");
}
}
Setup
truffle(develop)> factory = await MyFactory.new()
truffle(develop)> myContract = await MyContract.new()
truffle(develop)> web3.eth.getTransactionCount(factory.address)
1
Factory creates a contract
truffle(develop)> await factory.deployMyContract()
{ tx: ...
truffle(develop)> web3.eth.getTransactionCount(factory.address)
2
Factory reverts creating a contract
> await factory.deployMyContractRevert()
Thrown:
{ Error: Returned error: VM Exception while processing transaction: revert MyContractRevert: false -- Reason given: MyContractRevert: false.
truffle(develop)> web3.eth.getTransactionCount(factory.address)
2
Factory creates a proxy contract using Create2 and initializes it
truffle(develop)> await factory.deploy(0, myContract.address, accounts[0], true)
{ tx:...
truffle(develop)> web3.eth.getTransactionCount(factory.address)
3
Factory creates a proxy contract using Create2 and initialize reverts
truffle(develop)> await factory.deploy(1, myContract.address, accounts[0], false)
Thrown:
{ Error: Returned error: VM Exception while processing transaction: revert
truffle(develop)> web3.eth.getTransactionCount(factory.address)
3