|
6 | 6 |
|
7 | 7 | import requests
|
8 | 8 | from aea_ledger_ethereum import EthereumApi
|
9 |
| -from web3 import Web3 |
10 | 9 |
|
11 |
| -from mech_client.interact import fetch_tools, get_mech_config |
| 10 | +from mech_client.interact import fetch_tools, get_abi, get_contract, get_mech_config |
12 | 11 |
|
13 | 12 |
|
14 | 13 | def get_total_supply(chain_config: str = "gnosis") -> int:
|
15 | 14 | """
|
16 |
| - Get the total supply from the contract. |
| 15 | + Fetches the total supply of tokens from a contract using the chain configuration. |
17 | 16 |
|
18 |
| - :param chain_config: The chain configuration to use (default is "gnosis"). |
19 |
| - :return: The total supply as an integer. |
| 17 | + :param chain_config: The chain configuration to use. |
| 18 | + :type chain_config: str |
| 19 | + :return: The total supply of tokens. |
| 20 | + :rtype: int |
20 | 21 | """
|
| 22 | + # Get the mech configuration |
21 | 23 | mech_config = get_mech_config(chain_config)
|
22 | 24 | ledger_config = mech_config.ledger_config
|
23 | 25 |
|
24 |
| - # Setup Web3 |
25 |
| - w3 = Web3(Web3.HTTPProvider(ledger_config.address)) |
| 26 | + # Setup Ethereum API |
| 27 | + ledger_api = EthereumApi(**asdict(ledger_config)) |
26 | 28 |
|
27 |
| - # Fetch ABI from the contract_abi_url |
28 |
| - response = requests.get( |
29 |
| - mech_config.contract_abi_url.format( |
30 |
| - contract_address=mech_config.agent_registry_contract |
31 |
| - ) |
32 |
| - ) |
33 |
| - response_json = response.json() |
34 |
| - |
35 |
| - # Try to extract the ABI from different possible response structures |
36 |
| - if "result" in response_json and isinstance(response_json["result"], str): |
37 |
| - contract_abi = json.loads(response_json["result"]) |
38 |
| - elif "abi" in response_json: |
39 |
| - contract_abi = response_json["abi"] |
40 |
| - else: |
41 |
| - raise ValueError(f"Unexpected API response structure: {response_json}") |
42 |
| - |
43 |
| - # Create a contract instance |
44 |
| - contract = w3.eth.contract( |
45 |
| - address=mech_config.agent_registry_contract, abi=contract_abi |
46 |
| - ) |
| 29 | + # Fetch ABI and create contract instance |
| 30 | + abi = get_abi(mech_config.agent_registry_contract, mech_config.contract_abi_url) |
| 31 | + contract = get_contract(mech_config.agent_registry_contract, abi, ledger_api) |
47 | 32 |
|
48 | 33 | # Call the totalSupply function
|
49 |
| - total_supply = contract.functions.totalSupply().call() |
50 |
| - |
51 |
| - return total_supply |
| 34 | + return contract.functions.totalSupply().call() |
52 | 35 |
|
53 | 36 |
|
54 | 37 | def get_agent_tools(
|
|
0 commit comments