Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 1859b7e

Browse files
feat: remove blockscout dependency
1 parent 2264509 commit 1859b7e

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

scripts/choose_staking.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
STORE_PATH = Path(SCRIPT_PATH, "..", ".trader_runner")
3434
DOTENV_PATH = Path(STORE_PATH, ".env")
3535
RPC_PATH = Path(STORE_PATH, "rpc.txt")
36+
STAKING_TOKEN_INSTANCE_ABI_PATH = Path(SCRIPT_PATH, "..", "trader", "packages", "valory", "contracts", "staking_token", "build", "StakingToken.json")
37+
STAKING_TOKEN_IMPLEMENTATION_ABI_PATH = STAKING_TOKEN_INSTANCE_ABI_PATH
38+
ACTIVITY_CHECKER_ABI_PATH = Path(SCRIPT_PATH, "..", "trader", "packages", "valory", "contracts", "mech_activity", "build", "MechActivity.json")
3639

3740
IPFS_ADDRESS = "https://gateway.autonolas.tech/ipfs/f01701220{hash}"
3841
NEVERMINED_MECH_CONTRACT_ADDRESS = "0x327E26bDF1CfEa50BFAe35643B23D5268E41F7F9"
@@ -140,10 +143,21 @@ def _get_abi(contract_address: str) -> List:
140143
return abi if abi else []
141144

142145

146+
def _load_abi_from_file(path: Path) -> Dict[str, Any]:
147+
if not os.path.exists(path):
148+
print("Error: Contract airtfacts not found. Please execute 'run_service.sh' before executing this script.")
149+
sys.exit(1)
150+
151+
with open(path, "r", encoding="utf-8") as f:
152+
data = json.load(f)
153+
154+
return data.get("abi")
155+
156+
143157
contracts_cache: Dict[str, Any] = {}
144158

145159

146-
def _get_staking_token_contract(program_id: str) -> Any:
160+
def _get_staking_token_contract(program_id: str, use_blockscout: bool = False) -> Any:
147161
if program_id in contracts_cache:
148162
return contracts_cache[program_id]
149163

@@ -152,25 +166,31 @@ def _get_staking_token_contract(program_id: str) -> Any:
152166

153167
w3 = Web3(Web3.HTTPProvider(rpc))
154168
staking_token_instance_address = STAKING_PROGRAMS.get(program_id)
155-
abi = _get_abi(staking_token_instance_address)
169+
if use_blockscout:
170+
abi = _get_abi(staking_token_instance_address)
171+
else:
172+
abi = _load_abi_from_file(STAKING_TOKEN_INSTANCE_ABI_PATH)
156173
contract = w3.eth.contract(address=staking_token_instance_address, abi=abi)
157174

158175
if 'getImplementation' in [func.fn_name for func in contract.all_functions()]:
159176
# It is a proxy contract
160177
implementation_address = contract.functions.getImplementation().call()
161-
abi = _get_abi(implementation_address)
178+
if use_blockscout:
179+
abi = _get_abi(implementation_address)
180+
else:
181+
abi = _load_abi_from_file(STAKING_TOKEN_IMPLEMENTATION_ABI_PATH)
162182
contract = w3.eth.contract(address=staking_token_instance_address, abi=abi)
163183

164184
contracts_cache[program_id] = contract
165185
return contract
166186

167187

168-
def _get_staking_contract_metadata(program_id: str) -> Dict[str, str]:
188+
def _get_staking_contract_metadata(program_id: str, use_blockscout: bool = False) -> Dict[str, str]:
169189
try:
170190
if program_id == NO_STAKING_PROGRAM_ID:
171191
return NO_STAKING_PROGRAM_METADATA
172192

173-
staking_token_contract = _get_staking_token_contract(program_id=program_id)
193+
staking_token_contract = _get_staking_token_contract(program_id=program_id, use_blockscout=use_blockscout)
174194
metadata_hash = staking_token_contract.functions.metadataHash().call()
175195
ipfs_address = IPFS_ADDRESS.format(hash=metadata_hash.hex())
176196
response = requests.get(ipfs_address)
@@ -186,12 +206,12 @@ def _get_staking_contract_metadata(program_id: str) -> Dict[str, str]:
186206
}
187207

188208

189-
def _get_staking_env_variables(program_id: str) -> Dict[str, str]:
209+
def _get_staking_env_variables(program_id: str, use_blockscout: bool = False) -> Dict[str, str]:
190210
if program_id == NO_STAKING_PROGRAM_ID:
191211
return NO_STAKING_PROGRAM_ENV_VARIABLES
192212

193213
staking_token_instance_address = STAKING_PROGRAMS.get(program_id)
194-
staking_token_contract = _get_staking_token_contract(program_id=program_id)
214+
staking_token_contract = _get_staking_token_contract(program_id=program_id, use_blockscout=use_blockscout)
195215
agent_id = staking_token_contract.functions.agentIds(0).call()
196216
service_registry = staking_token_contract.functions.serviceRegistry().call()
197217
staking_token = staking_token_contract.functions.stakingToken().call()
@@ -201,7 +221,11 @@ def _get_staking_env_variables(program_id: str) -> Dict[str, str]:
201221

202222
if 'activityChecker' in [func.fn_name for func in staking_token_contract.all_functions()]:
203223
activity_checker = staking_token_contract.functions.activityChecker().call()
204-
abi = _get_abi(activity_checker)
224+
225+
if use_blockscout:
226+
abi = _get_abi(activity_checker)
227+
else:
228+
abi = _load_abi_from_file(ACTIVITY_CHECKER_ABI_PATH)
205229

206230
with open(RPC_PATH, 'r', encoding="utf-8") as file:
207231
rpc = file.read().strip()
@@ -265,6 +289,7 @@ def _get_nevermined_env_variables() -> Dict[str, str]:
265289
def main() -> None:
266290
parser = argparse.ArgumentParser(description="Set up staking configuration.")
267291
parser.add_argument("--reset", action="store_true", help="Reset USE_STAKING and STAKING_PROGRAM in .env file")
292+
parser.add_argument("--use_blockscout", action="store_true", help="Use Blockscout to retrieve contract data.")
268293
args = parser.parse_args()
269294

270295
if args.reset:
@@ -291,7 +316,7 @@ def main() -> None:
291316
program_id = _prompt_select_staking_program()
292317

293318
print(" - Populating staking program variables in the .env file")
294-
staking_env_variables = _get_staking_env_variables(program_id)
319+
staking_env_variables = _get_staking_env_variables(program_id, use_blockscout=args.use_blockscout)
295320
_set_dotenv_file_variables(staking_env_variables)
296321

297322
print(" - Populating Nevermined variables in the .env file")

scripts/get_agent_bond.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
import argparse
2222
import requests
2323
import json
24+
import os
2425
import sys
25-
from typing import List
26+
from pathlib import Path
27+
from typing import Any, List, Dict
2628
from web3 import Web3
2729

2830
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
31+
SCRIPT_PATH = Path(__file__).resolve().parent
32+
SERVICE_REGISTRY_TOKEN_UTILITY_ABI_PATH = Path(SCRIPT_PATH, "..", "contracts", "ServiceRegistryTokenUtility.json")
2933

3034

3135
def _get_abi(contract_address: str) -> List:
@@ -45,13 +49,25 @@ def _get_abi(contract_address: str) -> List:
4549
return abi if abi else []
4650

4751

52+
def _load_abi_from_file(path: Path) -> Dict[str, Any]:
53+
if not os.path.exists(path):
54+
print("Error: Contract airtfacts not found. Please execute 'run_service.sh' before executing this script.")
55+
sys.exit(1)
56+
57+
with open(path, "r", encoding="utf-8") as f:
58+
data = json.load(f)
59+
60+
return data.get("abi")
61+
62+
4863
def main() -> None:
4964
parser = argparse.ArgumentParser(description="Get agent bond from service registry token utility contract.")
5065
parser.add_argument('service_registry', type=str, help='Service registry contract address')
5166
parser.add_argument('service_registry_token_utility', type=str, help='Service registry token utility contract address')
5267
parser.add_argument('service_id', type=int, help='Service ID')
5368
parser.add_argument('agent_id', type=int, help='Agent ID')
5469
parser.add_argument('rpc', type=str, help='RPC')
70+
parser.add_argument("--use_blockscout", action="store_true", help="Use Blockscout to retrieve contract data.")
5571
args = parser.parse_args()
5672

5773
service_registry = args.service_registry
@@ -61,7 +77,12 @@ def main() -> None:
6177
rpc = args.rpc
6278

6379
w3 = Web3(Web3.HTTPProvider(rpc))
64-
abi = _get_abi(service_registry_token_utility)
80+
81+
if args.use_blockscout:
82+
abi = _get_abi(service_registry_token_utility)
83+
else:
84+
abi = _load_abi_from_file(SERVICE_REGISTRY_TOKEN_UTILITY_ABI_PATH)
85+
6586
contract = w3.eth.contract(address=service_registry_token_utility, abi=abi)
6687
token = contract.functions.mapServiceIdTokenDeposit(service_id).call()[0]
6788

0 commit comments

Comments
 (0)