update_files.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import streamlit as st
  2. import json
  3. import yaml
  4. EVM_CHAINS_YAML = "/Users/tejasbadadare/dev/pyth-crosschain/contract_manager/store/chains/EvmChains.yaml"
  5. RECEIVER_CHAINS_JSON = "/Users/tejasbadadare/dev/pyth-crosschain/governance/xc_admin/packages/xc_admin_common/src/receiver_chains.json"
  6. MAINNET_ID_MIN = 60000
  7. MAINNET_ID_MAX = 69999
  8. TESTNET_ID_MIN = 50000
  9. TESTNET_ID_MAX = 59999
  10. def update_evm_chains_yml(is_mainnet: bool, chain_name: str, rpc_url: str) -> None:
  11. """Updates EvmChains.yaml with a new chain entry"""
  12. new_chain = {
  13. "id": chain_name,
  14. "mainnet": is_mainnet,
  15. "rpcUrl": rpc_url,
  16. "networkId": 0, # TODO: fetch from network
  17. "type": "EvmChain",
  18. }
  19. with open(EVM_CHAINS_YAML, "r") as f:
  20. evm_chains: list[dict[str, str]] = yaml.safe_load(f)
  21. evm_chains.append(new_chain)
  22. with open(EVM_CHAINS_YAML, "w") as f:
  23. yaml.dump(evm_chains, f, sort_keys=False)
  24. st.success(f"✏️ Added {chain_name} to EvmChains.yaml")
  25. def update_receiver_chains_json(is_mainnet, chain_name):
  26. """Updates receiver_chains.json with a new chain entry using an auto-incremented ID"""
  27. with open(RECEIVER_CHAINS_JSON, "r") as f:
  28. receiver_chains = json.load(f)
  29. network_type = "mainnet" if is_mainnet else "non_mainnet"
  30. ids = receiver_chains[network_type]
  31. # Get next available ID based on network type
  32. def _get_next_chain_id(existing_ids, id_min, id_max):
  33. current_max = max(
  34. (id for id in existing_ids if id_min <= id <= id_max), default=(id_min - 1)
  35. )
  36. return max(id_min, current_max + 1)
  37. if is_mainnet:
  38. next_id = _get_next_chain_id(ids.values(), MAINNET_ID_MIN, MAINNET_ID_MAX)
  39. else:
  40. next_id = _get_next_chain_id(ids.values(), TESTNET_ID_MIN, TESTNET_ID_MAX)
  41. receiver_chains[network_type][chain_name] = next_id
  42. with open(RECEIVER_CHAINS_JSON, "w") as f:
  43. json.dump(receiver_chains, f, indent=2)
  44. st.success(f"✏️ Added {chain_name} to receiver_chains.json")
  45. def update_files(is_mainnet, chain_name, rpc_url):
  46. update_evm_chains_yml(is_mainnet, chain_name, rpc_url)
  47. update_receiver_chains_json(is_mainnet, chain_name)