3333STORE_PATH = Path (SCRIPT_PATH , ".." , ".trader_runner" )
3434DOTENV_PATH = Path (STORE_PATH , ".env" )
3535RPC_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
3740IPFS_ADDRESS = "https://gateway.autonolas.tech/ipfs/f01701220{hash}"
3841NEVERMINED_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+
143157contracts_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]:
265289def 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" )
0 commit comments