Skip to content

Commit ce9b066

Browse files
committed
resolving comment: improve fetching total supply
1 parent 7cfe9c9 commit ce9b066

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

mech_client/mech_tool_management.py

+13-30
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,32 @@
66

77
import requests
88
from aea_ledger_ethereum import EthereumApi
9-
from web3 import Web3
109

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
1211

1312

1413
def get_total_supply(chain_config: str = "gnosis") -> int:
1514
"""
16-
Get the total supply from the contract.
15+
Fetches the total supply of tokens from a contract using the chain configuration.
1716
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
2021
"""
22+
# Get the mech configuration
2123
mech_config = get_mech_config(chain_config)
2224
ledger_config = mech_config.ledger_config
2325

24-
# Setup Web3
25-
w3 = Web3(Web3.HTTPProvider(ledger_config.address))
26+
# Setup Ethereum API
27+
ledger_api = EthereumApi(**asdict(ledger_config))
2628

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)
4732

4833
# Call the totalSupply function
49-
total_supply = contract.functions.totalSupply().call()
50-
51-
return total_supply
34+
return contract.functions.totalSupply().call()
5235

5336

5437
def get_agent_tools(

0 commit comments

Comments
 (0)