diff --git a/safe_transaction_service/history/services/safe_service.py b/safe_transaction_service/history/services/safe_service.py index 4879bb02a..a467c995d 100644 --- a/safe_transaction_service/history/services/safe_service.py +++ b/safe_transaction_service/history/services/safe_service.py @@ -22,7 +22,13 @@ from safe_transaction_service.utils.abis.gelato import gelato_relay_1_balance_v2_abi from ..exceptions import NodeConnectionException -from ..models import EthereumTx, InternalTx, SafeLastStatus, SafeMasterCopy +from ..models import ( + EthereumTx, + InternalTx, + InternalTxType, + SafeLastStatus, + SafeMasterCopy, +) logger = logging.getLogger(__name__) @@ -116,7 +122,8 @@ def get_safe_creation_info( # Get first the actual creation transaction for the safe creation_internal_tx = ( InternalTx.objects.filter( - ethereum_tx__status=1 # Ignore Internal Transactions for failed Transactions + ethereum_tx__status=1, # Ignore Internal Transactions for failed Transactions + tx_type=InternalTxType.CREATE.value, ) .select_related("ethereum_tx__block") .get(contract_address=safe_address) diff --git a/safe_transaction_service/history/tests/test_safe_service.py b/safe_transaction_service/history/tests/test_safe_service.py index 702a8ca70..40b9e30c0 100644 --- a/safe_transaction_service/history/tests/test_safe_service.py +++ b/safe_transaction_service/history/tests/test_safe_service.py @@ -8,7 +8,7 @@ from safe_eth.eth.ethereum_client import TracingManager from safe_eth.safe.tests.safe_test_case import SafeTestCaseMixin -from ..models import SafeMasterCopy +from ..models import InternalTxType, SafeMasterCopy from ..services.safe_service import ( CannotGetSafeInfoFromBlockchain, CannotGetSafeInfoFromDB, @@ -36,6 +36,36 @@ class TestSafeService(SafeTestCaseMixin, TestCase): def setUp(self) -> None: self.safe_service = SafeServiceProvider() + def test_get_safe_creation_info(self): + """ + get_safe_creation_info should only return the TX of type CREATE + """ + random_address = Account.create().address + self.assertIsNone(self.safe_service.get_safe_creation_info(random_address)) + + InternalTxFactory( + contract_address=random_address, + tx_type=InternalTxType.CREATE.value, + ethereum_tx__status=0, + ) + + self.assertIsNone(self.safe_service.get_safe_creation_info(random_address)) + + InternalTxFactory( + contract_address=random_address, + ethereum_tx__status=1, + tx_type=InternalTxType.CREATE.value, + ) + + InternalTxFactory( + contract_address=random_address, + ethereum_tx__status=1, + tx_type=InternalTxType.CALL.value, + ) + + safe_creation_info = self.safe_service.get_safe_creation_info(random_address) + self.assertIsInstance(safe_creation_info, SafeCreationInfo) + def test_get_safe_creation_info_with_tracing(self): """ Traces are not stored on DB, so they must be recovered from the node @@ -43,7 +73,11 @@ def test_get_safe_creation_info_with_tracing(self): random_address = Account.create().address self.assertIsNone(self.safe_service.get_safe_creation_info(random_address)) - InternalTxFactory(contract_address=random_address, ethereum_tx__status=0) + InternalTxFactory( + contract_address=random_address, + tx_type=InternalTxType.CREATE.value, + ethereum_tx__status=0, + ) self.assertIsNone(self.safe_service.get_safe_creation_info(random_address)) with mock.patch.object( @@ -55,6 +89,7 @@ def test_get_safe_creation_info_with_tracing(self): InternalTxFactory( contract_address=random_address, ethereum_tx__status=1, + tx_type=InternalTxType.CREATE.value, trace_address="0", ) safe_creation_info = self.safe_service.get_safe_creation_info( @@ -73,7 +108,10 @@ def test_get_safe_creation_info_without_tracing_but_with_proxy_factory(self): self.assertIsNone(self.safe_service.get_safe_creation_info(random_address)) creation_trace = InternalTxFactory( - contract_address=random_address, ethereum_tx__status=1, trace_address="0" + contract_address=random_address, + tx_type=InternalTxType.CREATE.value, + ethereum_tx__status=1, + trace_address="0", ) safe_creation = self.safe_service.get_safe_creation_info(random_address) self.assertEqual(safe_creation.creator, creation_trace.ethereum_tx._from) @@ -105,6 +143,7 @@ def test_get_safe_creation_info_without_tracing_nor_proxy_factory(self): creation_trace = InternalTxFactory( contract_address=random_address, ethereum_tx__status=1, + tx_type=InternalTxType.CREATE.value, trace_address="0", ethereum_tx__data=None, ) @@ -124,7 +163,10 @@ def test_get_safe_creation_info_with_next_trace( ): random_address = Account.create().address InternalTxFactory( - contract_address=random_address, ethereum_tx__status=1, trace_address="" + contract_address=random_address, + tx_type=InternalTxType.CREATE.value, + ethereum_tx__status=1, + trace_address="", ) safe_creation_info = self.safe_service.get_safe_creation_info(random_address) self.assertIsInstance(safe_creation_info, SafeCreationInfo) diff --git a/safe_transaction_service/history/tests/test_views.py b/safe_transaction_service/history/tests/test_views.py index 8625b1cc7..be9ee1bf4 100644 --- a/safe_transaction_service/history/tests/test_views.py +++ b/safe_transaction_service/history/tests/test_views.py @@ -37,6 +37,7 @@ from ..helpers import DelegateSignatureHelper, DeleteMultisigTxSignatureHelper from ..models import ( IndexingStatus, + InternalTxType, MultisigConfirmation, MultisigTransaction, SafeContractDelegate, @@ -3274,6 +3275,7 @@ def test_safe_creation_view(self): contract_address=safe_address, trace_address="0,0", ethereum_tx__status=1, + tx_type=InternalTxType.CREATE.value, ) response = self.client.get( reverse("v1:history:safe-creation", args=(safe_address,)),