Skip to content

Commit

Permalink
Merge f8a8ffc into 492336c
Browse files Browse the repository at this point in the history
  • Loading branch information
falvaradorodriguez committed May 31, 2024
2 parents 492336c + f8a8ffc commit f563ba1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions gnosis/safe/api/transaction_service_api/entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Any, List, TypedDict


class ParameterDecoded(TypedDict):
name: str
type: str
value: Any


class DataDecoded(TypedDict):
method: str
parameters: List[ParameterDecoded]
19 changes: 19 additions & 0 deletions gnosis/safe/api/transaction_service_api/transaction_service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from gnosis.safe import SafeTx

from ..base_api import SafeAPIException, SafeBaseAPI
from .entities import DataDecoded
from .transaction_service_messages import get_delegate_message
from .transaction_service_tx import TransactionServiceTx

Expand Down Expand Up @@ -416,3 +417,21 @@ def post_message_signature(
f"Error posting message signature: {response.content}"
)
return True

def decode_data(
self, data: Union[bytes, HexStr], to_address: Optional[ChecksumAddress] = None
) -> DataDecoded:
"""
Retrieve decoded information using tx service internal ABI information given the tx data.
:param data: tx data as a 0x prefixed hexadecimal string.
:param to_address: address of the contract. This will be used in case of more than one function identifiers matching.
:return:
"""
payload = {"data": HexBytes(data).hex()}
if to_address:
payload["to"] = to_address
response = self._post_request("/api/v1/data-decoder/", payload)
if not response.ok:
raise SafeAPIException(f"Cannot decode tx data: {response.content}")
return response.json()
34 changes: 33 additions & 1 deletion gnosis/safe/tests/api/test_transaction_service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.test import TestCase

from eth_typing import HexStr
from hexbytes import HexBytes

from gnosis.eth import EthereumClient, EthereumNetwork, EthereumNetworkNotSupported
Expand All @@ -15,7 +16,11 @@
)

from ...api import SafeAPIException
from ..mocks.mock_transactions import transaction_data_mock, transaction_mock
from ..mocks.mock_transactions import (
transaction_data_decoded_mock,
transaction_data_mock,
transaction_mock,
)


class TestTransactionServiceAPI(EthereumTestCaseMixin, TestCase):
Expand Down Expand Up @@ -183,3 +188,30 @@ def test_get_safe_transaction(self):
f"Cannot get transaction with safe-tx-hash={safe_tx_hash.hex()}:",
str(context.exception),
)

def test_decode_data(self):
with patch.object(TransactionServiceApi, "_post_request") as mock_post_request:
mock_post_request.return_value.ok = True
mock_post_request.return_value.json = MagicMock(
return_value=transaction_data_decoded_mock
)
data = HexStr(
"0x095ea7b3000000000000000000000000e6fc577e87f7c977c4393300417dcc592d90acf8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
)
to_address = "0x4127839cdf4F73d9fC9a2C2861d8d1799e9DF40C"
decoded_data = self.transaction_service_api.decode_data(data, to_address)
expected_url = "/api/v1/data-decoder/"
expected_payload = {"data": HexBytes(data).hex(), "to": to_address}
mock_post_request.assert_called_once_with(expected_url, expected_payload)
self.assertEqual(decoded_data.get("method"), "approve")
self.assertEqual(decoded_data.get("parameters")[0].get("name"), "spender")
self.assertEqual(decoded_data.get("parameters")[1].get("name"), "value")

# Test response not ok
mock_post_request.return_value.ok = False
with self.assertRaises(SafeAPIException) as context:
self.transaction_service_api.decode_data(data, to_address)
self.assertIn(
"Cannot decode tx data:",
str(context.exception),
)
16 changes: 16 additions & 0 deletions gnosis/safe/tests/mocks/mock_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@
"trusted": True,
"signatures": "0x000000000000000000000000c6b82ba149cfa113f8f48d5e3b1f78e933e16dfd000000000000000000000000000000000000000000000000000000000000000001",
}

transaction_data_decoded_mock = {
"method": "approve",
"parameters": [
{
"name": "spender",
"type": "address",
"value": "0xe6fC577E87F7c977c4393300417dCC592D90acF8",
},
{
"name": "value",
"type": "uint256",
"value": "115792089237316195423570985008687907853269984665640564039457584007913129639935",
},
],
}

0 comments on commit f563ba1

Please sign in to comment.