|
22 | 22 | APINotImplementedError,
|
23 | 23 | BlockNotFoundError,
|
24 | 24 | ChainError,
|
| 25 | + ConversionError, |
25 | 26 | ProviderNotConnectedError,
|
26 | 27 | QueryEngineError,
|
27 | 28 | TransactionNotFoundError,
|
@@ -937,7 +938,39 @@ def mine(
|
937 | 938 | self.pending_timestamp += deltatime
|
938 | 939 | self.provider.mine(num_blocks)
|
939 | 940 |
|
940 |
| - def set_balance(self, account: Union[BaseAddress, AddressType], amount: Union[int, str]): |
| 941 | + def get_balance( |
| 942 | + self, address: Union[BaseAddress, AddressType, str], block_id: Optional["BlockID"] = None |
| 943 | + ) -> int: |
| 944 | + """ |
| 945 | + Get the balance of the given address. If ``ape-ens`` is installed, |
| 946 | + you can pass ENS names. |
| 947 | +
|
| 948 | + Args: |
| 949 | + address (BaseAddress, AddressType | str): An address, ENS, or account/contract. |
| 950 | + block_id (:class:`~ape.types.BlockID` | None): The block ID. Defaults to latest. |
| 951 | +
|
| 952 | + Returns: |
| 953 | + int: The account balance. |
| 954 | + """ |
| 955 | + if (isinstance(address, str) and not address.startswith("0x")) or not isinstance( |
| 956 | + address, str |
| 957 | + ): |
| 958 | + try: |
| 959 | + address = self.conversion_manager.convert(address, AddressType) |
| 960 | + except ConversionError: |
| 961 | + # Try to get the balance anyway; maybe the provider can handle it. |
| 962 | + address = address |
| 963 | + |
| 964 | + return self.provider.get_balance(address, block_id=block_id) |
| 965 | + |
| 966 | + def set_balance(self, account: Union[BaseAddress, AddressType, str], amount: Union[int, str]): |
| 967 | + """ |
| 968 | + Set an account balance, only works on development chains. |
| 969 | +
|
| 970 | + Args: |
| 971 | + account (BaseAddress, AddressType | str): The account. |
| 972 | + amount (int | str): The new balance. |
| 973 | + """ |
941 | 974 | if isinstance(account, BaseAddress):
|
942 | 975 | account = account.address
|
943 | 976 |
|
|
0 commit comments