From 4820d51b141c54a438098fb93a8d850fe3507111 Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria Date: Tue, 14 Nov 2023 15:19:07 +0100 Subject: [PATCH] Predict Safe address before creating - Closes #303 --- requirements.txt | 2 +- safe_cli/safe_creator.py | 26 ++++++++++++++++++-------- tests/test_safe_creator.py | 4 ++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0ed998cd..00a67f65 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,6 @@ packaging>=23.1 prompt_toolkit==3.0.40 pygments==2.16.1 requests==2.31.0 -safe-eth-py==6.0.0b5 +safe-eth-py==6.0.0b6 tabulate==0.9.0 web3==6.11.3 diff --git a/safe_cli/safe_creator.py b/safe_cli/safe_creator.py index 101669bf..33d036a8 100644 --- a/safe_cli/safe_creator.py +++ b/safe_cli/safe_creator.py @@ -190,12 +190,22 @@ def main(*args, **kwargs) -> EthereumTxSent: ) proxy_factory = ProxyFactory(proxy_factory_address, ethereum_client) - ethereum_tx_sent = proxy_factory.deploy_proxy_contract_with_nonce( - account, safe_contract_address, safe_creation_tx_data, salt_nonce + expected_safe_address = proxy_factory.calculate_proxy_address( + safe_contract_address, safe_creation_tx_data, salt_nonce ) - print_formatted_text( - f"Tx with tx-hash={ethereum_tx_sent.tx_hash.hex()} " - f"will create safe={ethereum_tx_sent.contract_address}" - ) - print_formatted_text(f"Tx parameters={ethereum_tx_sent.tx}") - return ethereum_tx_sent + if ethereum_client.is_contract(expected_safe_address): + print_formatted_text(f"Safe on {expected_safe_address} is already deployed") + sys.exit(1) + + if yes_or_no_question( + f"Safe will be deployed on {expected_safe_address}, looks good?" + ): + ethereum_tx_sent = proxy_factory.deploy_proxy_contract_with_nonce( + account, safe_contract_address, safe_creation_tx_data, salt_nonce + ) + print_formatted_text( + f"Sent tx with tx-hash={ethereum_tx_sent.tx_hash.hex()} " + f"Safe={ethereum_tx_sent.contract_address} is being created" + ) + print_formatted_text(f"Tx parameters={ethereum_tx_sent.tx}") + return ethereum_tx_sent diff --git a/tests/test_safe_creator.py b/tests/test_safe_creator.py index 670b089a..99401be0 100644 --- a/tests/test_safe_creator.py +++ b/tests/test_safe_creator.py @@ -41,6 +41,10 @@ def test_main(self, mock_parse_args: MagicMock): safe_info.fallback_handler, self.compatibility_fallback_handler.address ) + # If contract is deployed script should exit with code 1 + with self.assertRaisesRegex(SystemExit, "1"): + main() + if __name__ == "__main__": unittest.main()