-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor UserOperationReceipt into dataclass (#831)
- That way we can add custom methods - Add new methods for UserOperationReceipt - Calculate deposit to the entrypoint - Get Safe module address used
- Loading branch information
Showing
10 changed files
with
163 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from hexbytes import HexBytes | ||
|
||
# Entrypoint v0.6.0 and v0.7.0 deposited event | ||
# Deposited (index_topic_1 address account, uint256 totalDeposit) | ||
DEPOSIT_EVENT_TOPIC = HexBytes( | ||
"0x2da466a7b24304f47e87fa2e1e5a81b9831ce54fec19055ce277ca2f39ba42c4" | ||
) | ||
|
||
# Safe > 1.4.1 event | ||
# ExecutionFromModuleSuccess (index_topic_1 address module) | ||
EXECUTION_FROM_MODULE_SUCCESS_TOPIC = HexBytes( | ||
"0x6895c13664aa4f67288b25d7a21d7aaa34916e355fb9b6fae0a139a9085becb8" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import dataclasses | ||
from typing import Any, Dict, List, Optional | ||
|
||
from eth_typing import ChecksumAddress | ||
from hexbytes import HexBytes | ||
from web3 import Web3 | ||
from web3.types import LogReceipt | ||
|
||
from gnosis.eth.account_abstraction.constants import ( | ||
DEPOSIT_EVENT_TOPIC, | ||
EXECUTION_FROM_MODULE_SUCCESS_TOPIC, | ||
) | ||
|
||
|
||
@dataclasses.dataclass(eq=True, frozen=True) | ||
class UserOperationReceipt: | ||
user_operation_hash: bytes | ||
entry_point: ChecksumAddress | ||
sender: ChecksumAddress | ||
nonce: int | ||
paymaster: ChecksumAddress | ||
actual_gas_cost: int | ||
actual_gas_used: int | ||
success: bool | ||
reason: str | ||
logs: List[LogReceipt] | ||
|
||
@classmethod | ||
def from_bundler_response( | ||
cls, | ||
user_operation_receipt_response: Dict[str, Any], | ||
) -> "UserOperationReceipt": | ||
return cls( | ||
HexBytes(user_operation_receipt_response["userOpHash"]), | ||
user_operation_receipt_response["entryPoint"], | ||
user_operation_receipt_response["sender"], | ||
int(user_operation_receipt_response["nonce"], 16), | ||
user_operation_receipt_response["paymaster"], | ||
int(user_operation_receipt_response["actualGasCost"], 16), | ||
int(user_operation_receipt_response["actualGasUsed"], 16), | ||
user_operation_receipt_response["success"], | ||
user_operation_receipt_response["reason"], | ||
user_operation_receipt_response["logs"], | ||
) | ||
|
||
def get_deposit(self) -> int: | ||
""" | ||
:return: Deposited value on the entrypoint for running the UserOperationReceipt | ||
""" | ||
deposited = 0 | ||
for log in self.logs: | ||
if ( | ||
len(log["topics"]) == 2 | ||
and HexBytes(log["topics"][0]) == DEPOSIT_EVENT_TOPIC | ||
and Web3.to_checksum_address(log["address"]) == self.entry_point | ||
and Web3.to_checksum_address(log["topics"][1][-40:]) == self.sender | ||
): | ||
deposited += int(log["data"], 16) | ||
return deposited | ||
|
||
def get_module_address(self) -> Optional[ChecksumAddress]: | ||
""" | ||
Use Safe's `ExecutionFromModuleSuccess` event to get the 4337 module address | ||
:return: If using a ``Safe``, the ``4337 module address`` used, ``None`` otherwise | ||
""" | ||
for log in reversed(self.logs): | ||
if ( | ||
len(log["topics"]) == 2 | ||
and HexBytes(log["topics"][0]) == EXECUTION_FROM_MODULE_SUCCESS_TOPIC | ||
and Web3.to_checksum_address(log["address"]) == self.sender | ||
): | ||
return Web3.to_checksum_address(log["topics"][1][-40:]) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
gnosis/eth/tests/account_abstraction/test_user_operation_receipt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from unittest import TestCase | ||
|
||
from ...account_abstraction import UserOperationReceipt | ||
from ..mocks.mock_bundler import user_operation_receipt_mock | ||
|
||
|
||
class TestUserOperation(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.user_operation_receipt = UserOperationReceipt.from_bundler_response( | ||
user_operation_receipt_mock["result"] | ||
) | ||
|
||
def test_calculate_deposit(self): | ||
expected_value = 759_940_285_250_436 | ||
self.assertEqual(self.user_operation_receipt.get_deposit(), expected_value) | ||
|
||
def test_get_module_address(self): | ||
expected_value = "0xa581c4A4DB7175302464fF3C06380BC3270b4037" | ||
self.assertEqual( | ||
self.user_operation_receipt.get_module_address(), expected_value | ||
) |
Oops, something went wrong.