Repository: eth-brownie/brownie
Issue: Exception when deploying a self-destructing contract

### Environment information

* `brownie` Version: 1.8.5

### What was wrong?

I'm experimenting with create2 and made a contract that does some things and then calls `selfdestruct` at the end of the constructor. brownie didn't like this:

```
Running 'scripts.dev-deploy.main'...
Transaction sent: 0x36f1599c9b5b5ab9f243f5d0c64e8f2637a355818bc4cf5e1a67168e9e22713e
  Gas price: 0.0 gwei   Gas limit: 6721975
  ArgobytesOwnedVaultDeployer.constructor confirmed - Block: 1   Gas used: 3545980 (52.75%)
  ArgobytesOwnedVaultDeployer deployed at: 0x04246fAA61004668E1C0a388A3bF60E67827A17E

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ski/code/argobytes-contracts-brownie/venv/lib/python3.8/site-packages/brownie/network/contract.py", line 190, in _add_from_tx
    self.at(tx.contract_address, tx.sender, tx)
  File "/home/ski/code/argobytes-contracts-brownie/venv/lib/python3.8/site-packages/brownie/network/contract.py", line 174, in at
    contract = ProjectContract(self._project, build, address, owner, tx)
  File "/home/ski/code/argobytes-contracts-brownie/venv/lib/python3.8/site-packages/brownie/network/contract.py", line 728, in __init__
    _DeployedContractBase.__init__(self, address, owner, tx)
  File "/home/ski/code/argobytes-contracts-brownie/venv/lib/python3.8/site-packages/brownie/network/contract.py", line 309, in __init__
    raise ContractNotFound(f"No contract deployed at {address}")
brownie.exceptions.ContractNotFound: No contract deployed at 0x04246fAA61004668E1C0a388A3bF60E67827A17E
```

### How can it be fixed?

I was expecting to get a transaction object back rather than a Contract. I'm not sure how that will interact with a contract failing to deploy for other reasons though.


Changed files:
tests/network/account/test_account_deploy.py, brownie/network/account.py, brownie/network/contract.py

diff --git a/brownie/network/account.py b/brownie/network/account.py
index ca229f5e..fccc127b 100644
--- a/brownie/network/account.py
+++ b/brownie/network/account.py
@@ -14,7 +14,12 @@ from hexbytes import HexBytes
 from brownie._config import CONFIG, _get_data_folder
 from brownie._singleton import _Singleton
 from brownie.convert import Wei, to_address
-from brownie.exceptions import IncompatibleEVMVersion, UnknownAccount, VirtualMachineError
+from brownie.exceptions import (
+    ContractNotFound,
+    IncompatibleEVMVersion,
+    UnknownAccount,
+    VirtualMachineError,
+)
 
 from .rpc import Rpc, _revert_register
 from .state import TxHistory
@@ -256,8 +261,9 @@ class _PrivateKeyAccount(PublicKeyAccount):
             nonce: Nonce to use for the transaction.
 
         Returns:
-            * Contract instance if the transaction confirms
-            * TransactionReceipt if the transaction is pending or reverts"""
+            * Contract instance if the transaction confirms and the contract exists
+            * TransactionReceipt if the transaction is pending or reverts
+        """
 
         evm = contract._build["compiler"]["evm_version"]
         if rpc.is_active() and not rpc.evm_compatible(evm):
@@ -308,7 +314,11 @@ class _PrivateKeyAccount(PublicKeyAccount):
             return receipt
 
         add_thread.join()
-        return contract.at(receipt.contract_address)
+        try:
+            return contract.at(receipt.contract_address)
+        except ContractNotFound:
+            # if the contract self-destructed during deployment
+            return receipt
 
     def estimate_gas(self, to: "Accounts" = None, amount: int = 0, data: str = None) -> int:
         """Estimates the gas cost for a transaction. Raises VirtualMachineError
diff --git a/brownie/network/contract.py b/brownie/network/contract.py
index 64b7085b..24e4fc52 100644
--- a/brownie/network/contract.py
+++ b/brownie/network/contract.py
@@ -187,7 +187,11 @@ class ContractContainer(_ContractBase):
     def _add_from_tx(self, tx: TransactionReceiptType) -> None:
         tx._confirmed.wait()
         if tx.status:
-            self.at(tx.contract_address, tx.sender, tx)
+            try:
+                self.at(tx.contract_address, tx.sender, tx)
+            except ContractNotFound:
+                # if the contract self-destructed during deployment
+                pass
 
 
 class ContractConstructor:
diff --git a/tests/network/account/test_account_deploy.py b/tests/network/account/test_account_deploy.py
index 8e23feba..d7c390a7 100644
--- a/tests/network/account/test_account_deploy.py
+++ b/tests/network/account/test_account_deploy.py
@@ -2,6 +2,7 @@
 
 import pytest
 
+from brownie import compile_source
 from brownie.exceptions import IncompatibleEVMVersion, VirtualMachineError
 from brownie.network.contract import ProjectContract
 from brownie.network.transaction import TransactionReceipt
@@ -122,3 +123,18 @@ def test_evm_version(BrownieTester, accounts, monkeypatch):
     monkeypatch.setattr("psutil.Popen.cmdline", lambda s: ["-k", "byzantium"])
     with pytest.raises(IncompatibleEVMVersion):
         accounts[0].deploy(BrownieTester, True)
+
+
+def test_selfdestruct_during_deploy(accounts):
+    foo = compile_source(
+        """
+pragma solidity 0.5.0;
+
+contract Foo {
+    constructor () public { selfdestruct(address(0)); }
+}
+    """
+    ).Foo
+
+    result = foo.deploy({"from": accounts[0]})
+    assert isinstance(result, TransactionReceipt)