|
2 | 2 |
|
3 | 3 | from eth_typing import ChecksumAddress |
4 | 4 | from safe_eth.eth import EthereumClient |
5 | | -from safe_eth.eth.contracts import get_safe_V1_5_0_contract |
| 5 | +from safe_eth.eth.contracts import get_safe_V1_4_1_contract, get_safe_V1_5_0_contract |
6 | 6 | from safe_eth.eth.utils import fast_to_checksum_address |
7 | | -from safe_eth.safe.proxy_factory import ProxyFactoryV150 |
| 7 | +from safe_eth.safe.proxy_factory import ProxyFactoryV141, ProxyFactoryV150 |
| 8 | +from safe_eth.safe.safe_deployments import default_safe_deployments |
8 | 9 |
|
9 | 10 |
|
10 | 11 | @dataclasses.dataclass(eq=True, frozen=True) |
@@ -41,12 +42,15 @@ def decode_init_code( |
41 | 42 | - The ``ProxyFactory`` then deploys a ``Safe Proxy`` and calls ``setup`` with all the configuration parameters. |
42 | 43 | :param ethereum_client: |
43 | 44 | :return: Decoded Init Code dataclass |
44 | | - :raises ValueError: Problem decoding |
| 45 | + :raises ValueError: Problem decoding or unknown factory address |
45 | 46 | """ |
46 | 47 | factory_address = fast_to_checksum_address(init_code[:20]) |
47 | 48 | factory_data = init_code[20:] |
48 | | - proxy_factory = ProxyFactoryV150(factory_address, ethereum_client) |
49 | | - safe_contract = get_safe_V1_5_0_contract(ethereum_client.w3) |
| 49 | + |
| 50 | + proxy_factory, safe_contract = get_contract_instances( |
| 51 | + factory_address, ethereum_client |
| 52 | + ) |
| 53 | + |
50 | 54 | _, data = proxy_factory.contract.decode_function_input(factory_data) |
51 | 55 | initializer = data.pop("initializer") |
52 | 56 | _, safe_deployment_data = safe_contract.decode_function_input(initializer) |
@@ -77,3 +81,35 @@ def decode_init_code( |
77 | 81 | ] |
78 | 82 | ), |
79 | 83 | ) |
| 84 | + |
| 85 | + |
| 86 | +def get_contract_instances( |
| 87 | + factory_address: ChecksumAddress, ethereum_client: EthereumClient |
| 88 | +): |
| 89 | + """ |
| 90 | + Find the appropriate proxy factory and safe contract instances for a given factory address. |
| 91 | +
|
| 92 | + :param factory_address: The address of the ProxyFactory contract |
| 93 | + :param ethereum_client: Ethereum client instance |
| 94 | + :return: Tuple of (proxy_factory, safe_contract) |
| 95 | + :raises ValueError: If factory address is not found in safe_deployments |
| 96 | + """ |
| 97 | + # Map versions to their factory and contract classes |
| 98 | + version_configs = { |
| 99 | + "1.4.1": (ProxyFactoryV141, get_safe_V1_4_1_contract), |
| 100 | + "1.5.0": (ProxyFactoryV150, get_safe_V1_5_0_contract), |
| 101 | + } |
| 102 | + |
| 103 | + # Find which version the factory address belongs to |
| 104 | + for version, (factory_class, safe_contract_fn) in version_configs.items(): |
| 105 | + factory_addresses = default_safe_deployments[version]["SafeProxyFactory"] |
| 106 | + if any(factory_address == address for address in factory_addresses): |
| 107 | + return ( |
| 108 | + factory_class(factory_address, ethereum_client), |
| 109 | + safe_contract_fn(ethereum_client.w3), |
| 110 | + ) |
| 111 | + |
| 112 | + raise ValueError( |
| 113 | + f"Unknown ProxyFactory address: {factory_address}. " |
| 114 | + "Factory address is not registered in safe_deployments." |
| 115 | + ) |
0 commit comments