Skip to content

Commit

Permalink
Fix: rename CHAINS_CONFIG_FILE to CONFIG_FILE to avoid getting issue …
Browse files Browse the repository at this point in the history
…by conf of chain
  • Loading branch information
1yam committed Sep 30, 2024
1 parent 59c845e commit 841a1a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/aleph/sdk/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def _load_account(
elif private_key_path and private_key_path.is_file():
if private_key_path:
try:
# Look for the account by private_key_path in CHAINS_CONFIG_FILE
with open(settings.CHAINS_CONFIG_FILE, "r") as file:
# Look for the account by private_key_path in CONFIG_FILE
with open(settings.CONFIG_FILE, "r") as file:
accounts = json.load(file)

matching_account = next(
Expand All @@ -159,13 +159,13 @@ def _load_account(

except FileNotFoundError:
logger.warning(
f"CHAINS_CONFIG_FILE not found, using default account type {account_type.__name__}"
f"CONFIG_FILE not found, using default account type {account_type.__name__}"
)
except json.JSONDecodeError:
logger.error(
f"Invalid format in CHAINS_CONFIG_FILE, unable to load account info."
f"Invalid format in CONFIG_FILE, unable to load account info."
)
raise ValueError(f"Invalid format in {settings.CHAINS_CONFIG_FILE}.")
raise ValueError(f"Invalid format in {settings.CONFIG_FILE}.")
except Exception as e:
logger.error(f"Error loading accounts from config: {e}")
raise ValueError(
Expand Down
6 changes: 3 additions & 3 deletions src/aleph/sdk/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Settings(BaseSettings):
description="Path to the mnemonic used to create Substrate keypairs",
)

CHAINS_CONFIG_FILE: Path = Field(
CONFIG_FILE: Path = Field(
default=Path("chains_config.json"),
description="Path to the JSON file containing chain account configurations",
)
Expand Down Expand Up @@ -167,8 +167,8 @@ class Config:
settings.PRIVATE_MNEMONIC_FILE = Path(
settings.CONFIG_HOME, "private-keys", "substrate.mnemonic"
)
if str(settings.CHAINS_CONFIG_FILE) == "chains_config.json":
settings.CHAINS_CONFIG_FILE = Path(
if str(settings.CONFIG_FILE) == "chains_config.json":
settings.CONFIG_FILE = Path(
settings.CONFIG_HOME, "configs", "chains_config.json"
)

Expand Down
16 changes: 8 additions & 8 deletions src/aleph/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,15 @@ async def save_json(file_path: Path, data: list):

async def add_chain_account(new_account: ChainAccount):
"""Add a new chain account to the JSON file asynchronously."""
accounts = await load_json(settings.CHAINS_CONFIG_FILE)
accounts = await load_json(settings.CONFIG_FILE)

for account in accounts:
if account["name"] == new_account.name:
logger.error(f"Account with name {new_account.name} already exists.")
raise ValueError(f"Account with name {new_account.name} already exists.")

accounts.append(new_account.dict())
await save_json(settings.CHAINS_CONFIG_FILE, accounts)
await save_json(settings.CONFIG_FILE, accounts)

logger.debug(
f"Added account for {new_account.name} with chain {new_account.chain} and path {new_account.path}."
Expand All @@ -485,7 +485,7 @@ async def add_chain_account(new_account: ChainAccount):

async def get_chain_account(name: str) -> ChainAccount:
"""Retrieve a chain account by name from the JSON file."""
accounts = await load_json(settings.CHAINS_CONFIG_FILE)
accounts = await load_json(settings.CONFIG_FILE)

for account in accounts:
if account["name"] == name:
Expand All @@ -498,7 +498,7 @@ async def get_chain_account(name: str) -> ChainAccount:

async def get_chain_account_from_path(path: str) -> ChainAccount:
"""Retrieve a chain account by name from the JSON file."""
accounts = await load_json(settings.CHAINS_CONFIG_FILE)
accounts = await load_json(settings.CONFIG_FILE)

for account in accounts:
if account["path"] == path:
Expand All @@ -511,12 +511,12 @@ async def get_chain_account_from_path(path: str) -> ChainAccount:

async def update_chain_account(updated_account: ChainAccount):
"""Update an existing chain account in the JSON file."""
accounts = await load_json(settings.CHAINS_CONFIG_FILE)
accounts = await load_json(settings.CONFIG_FILE)

for index, account in enumerate(accounts):
if account["name"] == updated_account.name:
accounts[index] = updated_account.dict()
await save_json(settings.CHAINS_CONFIG_FILE, accounts)
await save_json(settings.CONFIG_FILE, accounts)
logger.debug(f"Updated account with name {updated_account.name}.")
return

Expand All @@ -526,15 +526,15 @@ async def update_chain_account(updated_account: ChainAccount):

async def delete_chain_account(name: str):
"""Delete a chain account from the JSON file."""
accounts = await load_json(settings.CHAINS_CONFIG_FILE)
accounts = await load_json(settings.CONFIG_FILE)

updated_accounts = [account for account in accounts if account["name"] != name]

if len(updated_accounts) == len(accounts):
logger.error(f"No account found with name {name}.")
raise ValueError(f"No account found with name {name}.")

await save_json(settings.CHAINS_CONFIG_FILE, updated_accounts)
await save_json(settings.CONFIG_FILE, updated_accounts)
logger.debug(f"Deleted account with name {name}.")


Expand Down

0 comments on commit 841a1a3

Please sign in to comment.