Skip to content

Commit

Permalink
Fix get_safe_creation_info query (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
falvaradorodriguez authored Nov 18, 2024
1 parent f1021a7 commit a08d2dd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
11 changes: 9 additions & 2 deletions safe_transaction_service/history/services/safe_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)
Expand Down
50 changes: 46 additions & 4 deletions safe_transaction_service/history/tests/test_safe_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -36,14 +36,48 @@ 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
"""
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(
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions safe_transaction_service/history/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ..helpers import DelegateSignatureHelper, DeleteMultisigTxSignatureHelper
from ..models import (
IndexingStatus,
InternalTxType,
MultisigConfirmation,
MultisigTransaction,
SafeContractDelegate,
Expand Down Expand Up @@ -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,)),
Expand Down

0 comments on commit a08d2dd

Please sign in to comment.