Skip to content

Commit a726756

Browse files
authored
feat: adding default algorand network configs to use when no .env.{network} found (#533)
* feat: adding default algorand network configs to use when no .env.{network} found * chore: addressing pr comments * docs: expanding docs * chore: pip audit * docs: tweaking path to copier-answers * chore: minor tweak to account for answers being inside .algokit folder
1 parent 9fa5005 commit a726756

13 files changed

+92
-11
lines changed

docs/features/init.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ If you want to create a community template, you can use the [AlgoKit guidelines
101101

102102
Answers to specific template prompts can be provided with the `--answer {key} {value}` option, which can be used multiple times for each prompt. Quotes can be used for values with spaces e.g. `--answer author_name "Algorand Foundation"`.
103103

104-
To find out the key for a specific answer you can either look at `.copier-answers.yml` in the root folder of a project created via `algokit init` or in the `copier.yaml` file of a template repo e.g. for the [beaker template](https://github.com/algorandfoundation/algokit-beaker-default-template/blob/main/copier.yaml).
104+
To find out the key for a specific answer you can either look at `.algokit/.copier-answers.yml` in the root folder of a project created via `algokit init` or in the `copier.yaml` file of a template repo e.g. for the [beaker template](https://github.com/algorandfoundation/algokit-beaker-default-template/blob/main/copier.yaml).
105105

106106
## Non-interactive project initialization
107107

docs/features/project/deploy.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ The logic for loading environment variables is as follows:
4848
- If a `.env` file exists, the environment variables contained in it are loaded first.
4949
- If a `.env.[network_name]` file exists, the environment variables in it are loaded, overriding any previously loaded values from the `.env` file for the same variables.
5050

51+
### Default Network Configurations
52+
53+
The `deploy` command assumes default configurations for `mainnet`, `localnet`, and `testnet` environments. If you're deploying to one of these networks and haven't provided specific environment variables, AlgoKit will use these default values:
54+
55+
- **Localnet**:
56+
57+
- `ALGOD_TOKEN`: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
58+
- `ALGOD_SERVER`: "http://localhost"
59+
- `ALGOD_PORT`: "4001"
60+
- `INDEXER_TOKEN`: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
61+
- `INDEXER_SERVER`: "http://localhost"
62+
- `INDEXER_PORT`: "8980"
63+
64+
- **Mainnet**:
65+
66+
- `ALGOD_SERVER`: "https://mainnet-api.algonode.cloud"
67+
- `INDEXER_SERVER`: "https://mainnet-idx.algonode.cloud"
68+
69+
- **Testnet**:
70+
- `ALGOD_SERVER`: "https://testnet-api.algonode.cloud"
71+
- `INDEXER_SERVER`: "https://testnet-idx.algonode.cloud"
72+
73+
These default values are used when no specific `.env.[network_name]` file is present and the corresponding environment variables are not set. This feature simplifies the deployment process for these common networks, reducing the need for manual configuration in many cases.
74+
75+
If you need to override these defaults or add additional configuration for these networks, you can still do so by creating the appropriate `.env.[network_name]` file or setting the environment variables explicitly or via generic `.env` file.
76+
5177
## AlgoKit Configuration File
5278

5379
AlgoKit uses a configuration file called `.algokit.toml` in the root of your project. The configuration file can be created using the `algokit init` command. This file will define the deployment commands for the various network environments that you want to target.

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/algokit/cli/init.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,11 @@ def init_command( # noqa: PLR0913, C901, PLR0915
361361

362362
from algokit.core.init import populate_default_answers
363363

364+
answers_file = project_path / ".algokit" / ".copier-answers.yml"
364365
with Worker(
365366
src_path=template.url,
366367
dst_path=project_path,
368+
answers_file=answers_file if answers_file.exists() else None,
367369
data=answers_dict,
368370
quiet=True,
369371
vcs_ref=template.branch or template.commit,

src/algokit/cli/project/deploy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _execute_deploy_command( # noqa: PLR0913
100100
else:
101101
msg = (
102102
f"Deploy command for '{environment_name}' is not specified in '{ALGOKIT_CONFIG}' file, "
103-
"and no generic command."
103+
"and no generic command available."
104104
)
105105
raise click.ClickException(msg)
106106
resolved_command = resolve_command_path(config.command)

src/algokit/core/project/deploy.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,55 @@
44

55
import click
66
import dotenv
7+
from algokit_utils import get_algonode_config
78

89
from algokit.core.conf import ALGOKIT_CONFIG, get_algokit_config
10+
from algokit.core.sandbox import (
11+
DEFAULT_ALGOD_PORT,
12+
DEFAULT_ALGOD_SERVER,
13+
DEFAULT_ALGOD_TOKEN,
14+
DEFAULT_INDEXER_PORT,
15+
DEFAULT_INDEXER_SERVER,
16+
DEFAULT_INDEXER_TOKEN,
17+
)
918
from algokit.core.utils import load_env_file, split_command_string
1019

1120
logger = logging.getLogger(__name__)
1221

1322

23+
class _KnownEnvironments:
24+
LOCALNET = "localnet"
25+
MAINNET = "mainnet"
26+
TESTNET = "testnet"
27+
28+
29+
DEFAULT_MAINNET_ALGOD_SERVER = get_algonode_config("mainnet", config="algod", token="").server
30+
DEFAULT_TESTNET_ALGOD_SERVER = get_algonode_config("testnet", config="algod", token="").server
31+
DEFAULT_MAINNET_INDEXER_SERVER = get_algonode_config("mainnet", config="indexer", token="").server
32+
DEFAULT_TESTNET_INDEXER_SERVER = get_algonode_config("testnet", config="indexer", token="").server
33+
34+
35+
_ENVIRONMENT_CONFIG: dict[str, dict[str, str | None]] = {
36+
_KnownEnvironments.LOCALNET: {
37+
# this file should contain environment variables specific to algokit localnet
38+
"ALGOD_TOKEN": str(DEFAULT_ALGOD_TOKEN),
39+
"ALGOD_SERVER": str(DEFAULT_ALGOD_SERVER),
40+
"ALGOD_PORT": str(DEFAULT_ALGOD_PORT),
41+
"INDEXER_TOKEN": str(DEFAULT_INDEXER_TOKEN),
42+
"INDEXER_SERVER": str(DEFAULT_INDEXER_SERVER),
43+
"INDEXER_PORT": str(DEFAULT_INDEXER_PORT),
44+
},
45+
_KnownEnvironments.MAINNET: {
46+
"ALGOD_SERVER": DEFAULT_MAINNET_ALGOD_SERVER,
47+
"INDEXER_SERVER": DEFAULT_MAINNET_INDEXER_SERVER,
48+
},
49+
_KnownEnvironments.TESTNET: {
50+
"ALGOD_SERVER": DEFAULT_TESTNET_ALGOD_SERVER,
51+
"INDEXER_SERVER": DEFAULT_TESTNET_INDEXER_SERVER,
52+
},
53+
}
54+
55+
1456
def load_deploy_env_files(name: str | None, project_dir: Path) -> dict[str, str | None]:
1557
"""
1658
Load the deploy configuration for the given network.
@@ -20,9 +62,14 @@ def load_deploy_env_files(name: str | None, project_dir: Path) -> dict[str, str
2062
result = load_env_file(project_dir)
2163
if name is not None:
2264
specific_env_path = project_dir / f".env.{name}"
23-
if not specific_env_path.exists():
65+
if specific_env_path.exists():
66+
result |= dotenv.dotenv_values(specific_env_path, verbose=True)
67+
68+
if name in _ENVIRONMENT_CONFIG:
69+
logger.debug(f"Using default environment config for algod and indexer for network {name}")
70+
result |= _ENVIRONMENT_CONFIG[name]
71+
elif not specific_env_path.exists():
2472
raise click.ClickException(f"No such file: {specific_env_path}")
25-
result |= dotenv.dotenv_values(specific_env_path, verbose=True)
2673
return result
2774

2875

src/algokit/core/sandbox.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def check_docker_compose_for_new_image_versions(self) -> None:
276276

277277

278278
DEFAULT_ALGOD_SERVER = "http://localhost"
279+
DEFAULT_INDEXER_SERVER = "http://localhost"
279280
DEFAULT_ALGOD_TOKEN = "a" * 64
281+
DEFAULT_INDEXER_TOKEN = "a" * 64
280282
DEFAULT_ALGOD_PORT = 4001
281283
DEFAULT_INDEXER_PORT = 8980
282284
DEFAULT_WAIT_FOR_ALGOD = 60

tests/project/deploy/test_deploy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ def test_command_bad_exit_code(proc_mock: ProcMock, tmp_path: Path, which_mock:
182182

183183
def test_algokit_env_name_missing(tmp_path_factory: TempPathFactory, which_mock: WhichMock) -> None:
184184
config_with_override = """
185-
[project.deploy.localnet]
185+
[project.deploy.customnet]
186186
command = "command_a"
187187
""".strip()
188188
cwd = tmp_path_factory.mktemp("cwd")
189189
(cwd / ALGOKIT_CONFIG).write_text(config_with_override, encoding="utf-8")
190190
(cwd / ".env").touch()
191191

192192
which_mock.add("command_a")
193-
result = invoke(["project", "deploy", "localnet"], cwd=cwd)
193+
result = invoke(["project", "deploy", "customnet"], cwd=cwd)
194194

195195
assert result.exit_code == 1
196196
verify(result.output)

tests/project/deploy/test_deploy.test_algokit_config_name_no_base.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DEBUG: Loading deploy command from project config
44
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
55
Using deploy command: /bin/command_a
66
Loading deployment environment variables...
7+
DEBUG: Using default environment config for algod and indexer for network localnet
78
Deploying smart contracts from AlgoKit compliant repository 🚀
89
DEBUG: Running '/bin/command_a' in '{current_working_directory}'
910
/bin/command_a: picked localnet

tests/project/deploy/test_deploy.test_algokit_config_name_overrides.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DEBUG: Loading deploy command from project config
44
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
55
Using deploy command: /bin/command_c
66
Loading deployment environment variables...
7+
DEBUG: Using default environment config for algod and indexer for network testnet
78
Deploying smart contracts from AlgoKit compliant repository 🚀
89
DEBUG: Running '/bin/command_c' in '{current_working_directory}'
910
/bin/command_c: picked testnet

tests/project/deploy/test_deploy.test_algokit_env_and_name_correct_set.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DEBUG: Loading deploy command from project config
44
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
55
Using deploy command: /bin/command_b
66
Loading deployment environment variables...
7+
DEBUG: Using default environment config for algod and indexer for network localnet
78
Deploying smart contracts from AlgoKit compliant repository 🚀
89
DEBUG: Running '/bin/command_b' in '{current_working_directory}'
910
/bin/command_b: picked localnet

tests/project/deploy/test_deploy.test_algokit_env_name_missing.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ DEBUG: Loading deploy command from project config
44
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
55
Using deploy command: /bin/command_a
66
Loading deployment environment variables...
7-
Error: No such file: {current_working_directory}/.env.localnet
7+
Error: No such file: {current_working_directory}/.env.customnet

tests/project/deploy/test_deploy.test_deploy_custom_project_dir.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DEBUG: Loading deploy command from project config
55
DEBUG: Attempting to load project config from {current_working_directory}/custom_folder/.algokit.toml
66
Using deploy command: /bin/command_a
77
Loading deployment environment variables...
8+
DEBUG: Using default environment config for algod and indexer for network testnet
89
Deploying smart contracts from AlgoKit compliant repository 🚀
910
DEBUG: Running '/bin/command_a' in '{current_working_directory}/custom_folder'
1011
/bin/command_a: picked base deploy command

0 commit comments

Comments
 (0)