|
| 1 | +from typing import TYPE_CHECKING, Optional, Union |
| 2 | + |
| 3 | +from ape.types import AddressType |
| 4 | +from ape.utils import ZERO_ADDRESS, ManagerAccessMixin |
| 5 | +from eth_utils import to_checksum_address |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from collections.abc import Iterator |
| 9 | + |
| 10 | + from ape.api import AccountAPI, ReceiptAPI |
| 11 | + from ape.contracts import ContractInstance |
| 12 | + |
| 13 | + from ape_safe.accounts import SafeAccount |
| 14 | + from ape_safe.client import SafeTxID |
| 15 | + |
| 16 | + |
| 17 | +class SafeModuleManager(ManagerAccessMixin): |
| 18 | + SENTINEL: AddressType = "0x0000000000000000000000000000000000000001" |
| 19 | + PAGE_SIZE: int = 100 |
| 20 | + |
| 21 | + def __init__(self, safe: "SafeAccount"): |
| 22 | + self._safe = safe |
| 23 | + |
| 24 | + def __repr__(self) -> str: |
| 25 | + return f"<{self.__class__.__qualname__} safe={self._safe.address}" |
| 26 | + |
| 27 | + def __contains__(self, module: Union[str, AddressType, "ContractInstance"]) -> bool: |
| 28 | + return self._safe.contract.isModuleEnabled(module) |
| 29 | + |
| 30 | + def enable( |
| 31 | + self, |
| 32 | + module: Union[str, AddressType, "ContractInstance"], |
| 33 | + submitter: Union["AccountAPI", AddressType, str, None] = None, |
| 34 | + propose: bool = False, |
| 35 | + **txn_kwargs, |
| 36 | + ) -> Union["ReceiptAPI", "SafeTxID"]: |
| 37 | + if propose: |
| 38 | + txn = self._safe.contract.enableModule.as_transaction(module, **txn_kwargs) |
| 39 | + return self._safe.propose(txn=txn, submitter=submitter) |
| 40 | + |
| 41 | + else: |
| 42 | + return self._safe.contract.enableModule( |
| 43 | + module, |
| 44 | + sender=self._safe, |
| 45 | + submitter=submitter, |
| 46 | + **txn_kwargs, |
| 47 | + ) |
| 48 | + |
| 49 | + def __iter__(self) -> "Iterator[ContractInstance]": |
| 50 | + start_module = self.SENTINEL |
| 51 | + while True: |
| 52 | + page, start_module = self._safe.contract.getModulesPaginated( |
| 53 | + start_module, self.PAGE_SIZE |
| 54 | + ) |
| 55 | + yield from map(self.chain_manager.contracts.instance_at, page) |
| 56 | + |
| 57 | + if start_module == self.SENTINEL: |
| 58 | + break |
| 59 | + |
| 60 | + def _get_previous_module( |
| 61 | + self, module: Union[str, AddressType, "ContractInstance"] |
| 62 | + ) -> "AddressType": |
| 63 | + prev_module = self.SENTINEL |
| 64 | + |
| 65 | + for next_module in self: |
| 66 | + if next_module == module: |
| 67 | + return prev_module |
| 68 | + |
| 69 | + prev_module = next_module |
| 70 | + |
| 71 | + raise AssertionError(f"Module {module} not in Safe modules for {self._safe}") |
| 72 | + |
| 73 | + def disable( |
| 74 | + self, |
| 75 | + module: Union[str, AddressType, "ContractInstance"], |
| 76 | + submitter: Union["AccountAPI", AddressType, str, None] = None, |
| 77 | + propose: bool = False, |
| 78 | + **txn_kwargs, |
| 79 | + ) -> Union["ReceiptAPI", "SafeTxID"]: |
| 80 | + if propose: |
| 81 | + txn = self._safe.contract.disableModule.as_transaction( |
| 82 | + self._get_previous_module(module), |
| 83 | + module, |
| 84 | + **txn_kwargs, |
| 85 | + ) |
| 86 | + return self._safe.propose(txn=txn, submitter=submitter) |
| 87 | + |
| 88 | + else: |
| 89 | + return self._safe.contract.disableModule( |
| 90 | + self._get_previous_module(module), |
| 91 | + module, |
| 92 | + sender=self._safe, |
| 93 | + **txn_kwargs, |
| 94 | + ) |
| 95 | + |
| 96 | + @property |
| 97 | + def guard(self) -> Optional["ContractInstance"]: |
| 98 | + if ( |
| 99 | + module_guard_address := to_checksum_address( |
| 100 | + self.provider.get_storage( |
| 101 | + self._safe.contract.address, |
| 102 | + # NOTE: `keccak256("module_manager.module_guard.address")` |
| 103 | + "0xb104e0b93118902c651344349b610029d694cfdec91c589c91ebafbcd0289947", |
| 104 | + )[12:] |
| 105 | + ) |
| 106 | + ) == ZERO_ADDRESS: |
| 107 | + return None |
| 108 | + |
| 109 | + return self.chain_manager.contracts.instance_at(module_guard_address) |
| 110 | + |
| 111 | + def set_guard( |
| 112 | + self, |
| 113 | + guard: Union[str, AddressType, "ContractInstance"], |
| 114 | + submitter: Union["AccountAPI", AddressType, str, None] = None, |
| 115 | + propose: bool = False, |
| 116 | + **txn_kwargs, |
| 117 | + ) -> Union["ReceiptAPI", "SafeTxID"]: |
| 118 | + if propose: |
| 119 | + txn = self._safe.contract.setModuleGuard.as_transaction(guard, **txn_kwargs) |
| 120 | + return self._safe.propose(txn=txn, submitter=submitter) |
| 121 | + |
| 122 | + else: |
| 123 | + return self._safe.contract.setModuleGuard(guard, sender=self._safe, **txn_kwargs) |
| 124 | + |
| 125 | + def remove_guard(self, **txn_kwargs) -> Union["ReceiptAPI", "SafeTxID"]: |
| 126 | + return self.set_guard(ZERO_ADDRESS, **txn_kwargs) |
0 commit comments