Skip to content

Commit b6453ba

Browse files
committed
Better handling of configuration files with file, local, global
1 parent 5a5c3ef commit b6453ba

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/soroban/invoke.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ def invoke(
1515
function_name: str,
1616
args: list[xdr.SCVal] | None = None,
1717
*,
18-
source_account: stellar_sdk.Keypair | str | None = None,
19-
network: soroban_models.NetworkConfig | None = None,
18+
source_account: soroban_models.Identity | stellar_sdk.Keypair | str | None = None,
19+
network: soroban_models.NetworkConfig | soroban_models.NetworkConfig | None = None,
2020
):
21-
identity = soroban_models.Identity.from_source_account(account=source_account)
22-
network = soroban_models.NetworkConfig() if network is None else network
21+
identity = (
22+
source_account
23+
if isinstance(source_account, soroban_models.Identity)
24+
else soroban_models.Identity.from_source_account(account=source_account)
25+
)
26+
network = (
27+
network
28+
if isinstance(network, soroban_models.NetworkConfig)
29+
else soroban_models.NetworkConfig.from_network(network=network)
30+
)
2331

2432
soroban_server = stellar_sdk.SorobanServer(network.rpc_url)
2533
source_account = soroban_server.load_account(identity.public_key)

src/soroban/models.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pathlib
2+
from typing import Literal
23

34
from pydantic import model_validator, HttpUrl
45
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -7,6 +8,21 @@
78
__all__ = ["Identity", "NetworkConfig"]
89

910

11+
def _load_configuration(id: str | pathlib.Path, kind: Literal["identity", "network"]):
12+
id = pathlib.Path(id)
13+
global_config = pathlib.Path.home() / ".config" / "soroban" / kind / id / ".toml"
14+
local_config = pathlib.Path(".soroban") / kind / id / ".toml"
15+
16+
id = pathlib.Path(id)
17+
18+
if id.is_file():
19+
return id
20+
elif local_config.is_file():
21+
return local_config
22+
elif global_config.is_file():
23+
return global_config
24+
25+
1026
class Identity(BaseSettings):
1127
secret_key: str | None = None
1228
public_key: str | None = None
@@ -32,11 +48,7 @@ def from_source_account(
3248
if account is None:
3349
identity = Identity()
3450
elif isinstance(account, (str, pathlib.Path)):
35-
fname = (
36-
account
37-
if pathlib.Path(account).is_file()
38-
else pathlib.Path(".soroban/identity") / account / ".toml"
39-
)
51+
fname = _load_configuration(account, "identity")
4052
identity = Identity(_env_file=fname)
4153
else:
4254
identity = Identity(keypair=account)
@@ -49,3 +61,12 @@ class NetworkConfig(BaseSettings):
4961
base_fee: int = 100
5062

5163
model_config = SettingsConfigDict(env_file="network.toml")
64+
65+
@classmethod
66+
def from_network(cls, network: str | pathlib.Path | None = None) -> "NetworkConfig":
67+
if network is None:
68+
network = NetworkConfig()
69+
elif isinstance(network, (str, pathlib.Path)):
70+
fname = _load_configuration(network, "network")
71+
network = NetworkConfig(_env_file=fname)
72+
return network

tests/test_models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ def test_raises(self):
3030
keypair = Keypair.random()
3131
with pytest.raises(ValueError, match="provide a secret key or a Keypair"):
3232
soroban.Identity(public_key=keypair.public_key)
33+
34+
35+
class TestNetworkConfig:
36+
37+
def test_from_network(self):
38+
testnet = pathlib.Path(__file__).parent / "testnet.toml"
39+
soroban.NetworkConfig.from_network(network=testnet)

tests/testnet.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rpc_url = "https://soroban-testnet.stellar.org:443"
2+
network_passphrase = "Test SDF Network ; September 2015"

0 commit comments

Comments
 (0)