Skip to content

Commit

Permalink
fix: do some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanLav committed Jul 31, 2024
1 parent 44a545a commit 67aab89
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions scripts/update_generic_ledger_app_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from tests.data.setting_data import get_substrate_chains

CHAINS_FILE_PATH_DEV = Path(
os.getenv("DEV_CHAINS_JSON_PATH", 'chains/v20/chains_dev.json'))
CHAINS_FILE_PATH_DEV = Path(os.getenv("DEV_CHAINS_JSON_PATH", 'chains/v20/chains_dev.json'))


def load_json_file(file_path):
Expand Down Expand Up @@ -41,6 +40,7 @@ def update_network_data(network):
additional['supportsGenericLedgerApp'] = True
additional.pop('disabledCheckMetadataHash', None)


def remove_generic_ledger_app_support(existing_data, chain_id):
existing_network = find_existing_network(existing_data, chain_id)
if existing_network:
Expand Down Expand Up @@ -78,32 +78,39 @@ def check_metadata_hash_exist(connection: SubstrateInterface) -> bool:
return False


def main():
existing_data_dev = load_json_file(CHAINS_FILE_PATH_DEV)

substrate_chains = get_substrate_chains(path=CHAINS_FILE_PATH_DEV.__str__())
def process_chains(chains, existing_data):
networks_with_metadata_hash = []

for chain in substrate_chains:
for chain in chains:
print(f'Checking {chain.name}')
try:
connection = chain.create_connection()
process_single_chain(chain, existing_data, networks_with_metadata_hash)
return networks_with_metadata_hash


def process_single_chain(chain, existing_data, networks_with_metadata_hash):
try:
with chain.create_connection() as connection:
if connection:
has_metadata_hash = check_metadata_hash_exist(connection)
if has_metadata_hash:
networks_with_metadata_hash.append({
'name': chain.name,
'chainId': chain.chainId
})
else:
remove_generic_ledger_app_support(existing_data_dev, chain.chainId)
chain.close_substrate_connection()
except Exception as e:
print(f"Error creating connection for chain {chain.name}: {e}")

update_existing_data_with_new_networks(
existing_data_dev, networks_with_metadata_hash)
handle_connection(connection, chain,existing_data, networks_with_metadata_hash)
except Exception as e:
print(f"Error creating connection for chain {chain.name}: {e}")


def handle_connection(connection, chain, existing_data, networks_with_metadata_hash):
has_metadata_hash = check_metadata_hash_exist(connection)
if has_metadata_hash:
networks_with_metadata_hash.append({
'name': chain.name,
'chainId': chain.chainId
})
else:
remove_generic_ledger_app_support(existing_data, chain.chainId)


def main():
existing_data_dev = load_json_file(CHAINS_FILE_PATH_DEV)
substrate_chains = get_substrate_chains(path=CHAINS_FILE_PATH_DEV.__str__())
networks_with_metadata_hash = process_chains(substrate_chains, existing_data_dev)
update_existing_data_with_new_networks(existing_data_dev, networks_with_metadata_hash)
save_json_file(CHAINS_FILE_PATH_DEV, existing_data_dev)


Expand Down

0 comments on commit 67aab89

Please sign in to comment.