Skip to content

Commit 1e01188

Browse files
authored
fix: support for named typed client exports for files with multiple contacts (#36)
* fix: support for named typed client exports for files with multiple contacts * chore: further simplifying the helpers * chore: regen artifacts
1 parent babd44b commit 1e01188

File tree

77 files changed

+638
-552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+638
-552
lines changed

examples/generators/production_python_smart_contract_python/.algokit/generators/create_contract/smart_contracts/{{ contract_name }}/deploy_config.py.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def deploy(
1414
app_spec: algokit_utils.ApplicationSpecification,
1515
deployer: algokit_utils.Account,
1616
) -> None:
17-
from smart_contracts.artifacts.{{ contract_name }}.client import (
17+
from smart_contracts.artifacts.{{ contract_name }}.{{ contract_name }}_client import (
1818
{{ contract_name.split('_')|map('capitalize')|join }}Client,
1919
)
2020

examples/generators/production_python_smart_contract_python/.tours/getting-started-with-your-algokit-project.tour

+5
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@
4747
"description": "Uncomment the following lines to enable complementary utilities that will generate artifacts required for the [AlgoKit AVM Debugger](https://github.com/algorandfoundation/algokit-avm-vscode-debugger) VSCode plugin available on the [VSCode Extension Marketplace](https://marketplace.visualstudio.com/items?itemName=algorandfoundation.algokit-avm-vscode-debugger). A new folder will be automatically created in the `.algokit` directory with source maps of all TEAL contracts in this workspace, as well as traces that will appear in a folder at the root of the workspace. You can then use the traces as entry points to trigger the debug extension. Make sure to have the `.algokit.toml` file available at the root of the workspace.",
4848
"line": 15
4949
}
50+
{
51+
"file": "smart_contracts/_helpers/__init__.py",
52+
"description": "This folder contains helper scripts for contract management. These automate tasks like compiling, generating clients, and deploying. Usually, you won't need to edit these files, but advanced users can expand them for custom needs.",
53+
"line": 1
54+
}
5055
]
5156
}

examples/generators/production_python_smart_contract_python/smart_contracts/__main__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
from dotenv import load_dotenv
66

7-
from smart_contracts.config import contracts
8-
from smart_contracts.helpers.build import build
9-
from smart_contracts.helpers.deploy import deploy
10-
from smart_contracts.helpers.util import find_app_spec_file
7+
from smart_contracts._helpers.build import build
8+
from smart_contracts._helpers.config import contracts
9+
from smart_contracts._helpers.deploy import deploy
1110

1211
# Uncomment the following lines to enable auto generation of AVM Debugger compliant sourcemap and simulation trace file.
1312
# Learn more about using AlgoKit AVM Debugger to debug your TEAL source codes and inspect various kinds of
@@ -36,7 +35,14 @@ def main(action: str) -> None:
3635
for contract in contracts:
3736
logger.info(f"Deploying app {contract.name}")
3837
output_dir = artifact_path / contract.name
39-
app_spec_file_name = find_app_spec_file(output_dir)
38+
app_spec_file_name = next(
39+
(
40+
file.name
41+
for file in output_dir.iterdir()
42+
if file.is_file() and file.suffixes == [".arc32", ".json"]
43+
),
44+
None,
45+
)
4046
if app_spec_file_name is None:
4147
raise Exception("Could not deploy app, .arc32.json file not found")
4248
app_spec_path = output_dir / app_spec_file_name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import logging
2+
import subprocess
3+
from pathlib import Path
4+
from shutil import rmtree
5+
6+
logger = logging.getLogger(__name__)
7+
deployment_extension = "py"
8+
9+
10+
def _get_output_path(output_dir: Path, deployment_extension: str) -> Path:
11+
return output_dir / Path(
12+
"{contract_name}"
13+
+ ("_client" if deployment_extension == "py" else "Client")
14+
+ f".{deployment_extension}"
15+
)
16+
17+
18+
def build(output_dir: Path, contract_path: Path) -> Path:
19+
output_dir = output_dir.resolve()
20+
if output_dir.exists():
21+
rmtree(output_dir)
22+
output_dir.mkdir(exist_ok=True, parents=True)
23+
logger.info(f"Exporting {contract_path} to {output_dir}")
24+
25+
build_result = subprocess.run(
26+
[
27+
"algokit",
28+
"--no-color",
29+
"compile",
30+
"python",
31+
contract_path.absolute(),
32+
f"--out-dir={output_dir}",
33+
"--output-arc32",
34+
],
35+
stdout=subprocess.PIPE,
36+
stderr=subprocess.STDOUT,
37+
text=True,
38+
)
39+
if build_result.returncode:
40+
raise Exception(f"Could not build contract:\n{build_result.stdout}")
41+
42+
app_spec_file_names = [file.name for file in output_dir.glob("*.arc32.json")]
43+
44+
for app_spec_file_name in app_spec_file_names:
45+
if app_spec_file_name is None:
46+
raise Exception(
47+
"Could not generate typed client, .arc32.json file not found"
48+
)
49+
print(app_spec_file_name)
50+
generate_result = subprocess.run(
51+
[
52+
"algokit",
53+
"generate",
54+
"client",
55+
output_dir,
56+
"--output",
57+
_get_output_path(output_dir, deployment_extension),
58+
],
59+
stdout=subprocess.PIPE,
60+
stderr=subprocess.STDOUT,
61+
text=True,
62+
)
63+
if generate_result.returncode:
64+
if "No such command" in generate_result.stdout:
65+
raise Exception(
66+
"Could not generate typed client, requires AlgoKit 2.0.0 or "
67+
"later. Please update AlgoKit"
68+
)
69+
else:
70+
raise Exception(
71+
f"Could not generate typed client:\n{generate_result.stdout}"
72+
)
73+
74+
return output_dir / app_spec_file_name

examples/generators/production_python_smart_contract_python/smart_contracts/cool_contract/deploy_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def deploy(
1414
app_spec: algokit_utils.ApplicationSpecification,
1515
deployer: algokit_utils.Account,
1616
) -> None:
17-
from smart_contracts.artifacts.cool_contract.client import (
17+
from smart_contracts.artifacts.cool_contract.cool_contract_client import (
1818
CoolContractClient,
1919
)
2020

examples/generators/production_python_smart_contract_python/smart_contracts/hello_world/deploy_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def deploy(
1414
app_spec: algokit_utils.ApplicationSpecification,
1515
deployer: algokit_utils.Account,
1616
) -> None:
17-
from smart_contracts.artifacts.hello_world.client import (
17+
from smart_contracts.artifacts.hello_world.hello_world_client import (
1818
HelloWorldClient,
1919
)
2020

examples/generators/production_python_smart_contract_python/smart_contracts/helpers/build.py

-63
This file was deleted.

examples/generators/production_python_smart_contract_python/smart_contracts/helpers/util.py

-8
This file was deleted.

examples/generators/production_python_smart_contract_python/tests/hello_world_client_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from algosdk.v2client.algod import AlgodClient
66
from algosdk.v2client.indexer import IndexerClient
77

8-
from smart_contracts.artifacts.hello_world.client import HelloWorldClient
8+
from smart_contracts.artifacts.hello_world.hello_world_client import HelloWorldClient
99

1010

1111
@pytest.fixture(scope="session")

examples/generators/production_python_smart_contract_typescript/.algokit/generators/create_contract/smart_contracts/{{ contract_name }}/deploy-config.ts.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as algokit from '@algorandfoundation/algokit-utils'
2-
import { {{ contract_name.split('_')|map('capitalize')|join }}Client } from '../artifacts/{{ contract_name }}/client'
2+
import { {{ contract_name.split('_')|map('capitalize')|join }}Client } from '../artifacts/{{ contract_name }}/{{ contract_name.split('_')|map('capitalize')|join }}Client'
33

44
// Below is a showcase of various deployment options you can use in TypeScript Client
55
export async function deploy() {

examples/generators/production_python_smart_contract_typescript/.tours/getting-started-with-your-algokit-project.tour

+5
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@
4242
"description": "Uncomment the following lines to enable complementary utilities that will generate artifacts required for the [AlgoKit AVM Debugger](https://github.com/algorandfoundation/algokit-avm-vscode-debugger) VSCode plugin available on the [VSCode Extension Marketplace](https://marketplace.visualstudio.com/items?itemName=algorandfoundation.algokit-avm-vscode-debugger). A new folder will be automatically created in the `.algokit` directory with source maps of all TEAL contracts in this workspace, as well as traces that will appear in a folder at the root of the workspace. You can then use the traces as entry points to trigger the debug extension. Make sure to have the `.algokit.toml` file available at the root of the workspace.",
4343
"line": 15
4444
}
45+
{
46+
"file": "smart_contracts/_helpers/__init__.py",
47+
"description": "This folder contains helper scripts for contract management. These automate tasks like compiling, generating clients, and deploying. Usually, you won't need to edit these files, but advanced users can expand them for custom needs.",
48+
"line": 1
49+
}
4550
]
4651
}

examples/generators/production_python_smart_contract_typescript/smart_contracts/__main__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from dotenv import load_dotenv
66

7-
from smart_contracts.config import contracts
8-
from smart_contracts.helpers.build import build
7+
from smart_contracts._helpers.build import build
8+
from smart_contracts._helpers.config import contracts
99

1010
logging.basicConfig(
1111
level=logging.DEBUG, format="%(asctime)s %(levelname)-10s: %(message)s"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import logging
2+
import subprocess
3+
from pathlib import Path
4+
from shutil import rmtree
5+
6+
logger = logging.getLogger(__name__)
7+
deployment_extension = "ts"
8+
9+
10+
def _get_output_path(output_dir: Path, deployment_extension: str) -> Path:
11+
return output_dir / Path(
12+
"{contract_name}"
13+
+ ("_client" if deployment_extension == "py" else "Client")
14+
+ f".{deployment_extension}"
15+
)
16+
17+
18+
def build(output_dir: Path, contract_path: Path) -> Path:
19+
output_dir = output_dir.resolve()
20+
if output_dir.exists():
21+
rmtree(output_dir)
22+
output_dir.mkdir(exist_ok=True, parents=True)
23+
logger.info(f"Exporting {contract_path} to {output_dir}")
24+
25+
build_result = subprocess.run(
26+
[
27+
"algokit",
28+
"--no-color",
29+
"compile",
30+
"python",
31+
contract_path.absolute(),
32+
f"--out-dir={output_dir}",
33+
"--output-arc32",
34+
],
35+
stdout=subprocess.PIPE,
36+
stderr=subprocess.STDOUT,
37+
text=True,
38+
)
39+
if build_result.returncode:
40+
raise Exception(f"Could not build contract:\n{build_result.stdout}")
41+
42+
app_spec_file_names = [file.name for file in output_dir.glob("*.arc32.json")]
43+
44+
for app_spec_file_name in app_spec_file_names:
45+
if app_spec_file_name is None:
46+
raise Exception(
47+
"Could not generate typed client, .arc32.json file not found"
48+
)
49+
print(app_spec_file_name)
50+
generate_result = subprocess.run(
51+
[
52+
"algokit",
53+
"generate",
54+
"client",
55+
output_dir,
56+
"--output",
57+
_get_output_path(output_dir, deployment_extension),
58+
],
59+
stdout=subprocess.PIPE,
60+
stderr=subprocess.STDOUT,
61+
text=True,
62+
)
63+
if generate_result.returncode:
64+
if "No such command" in generate_result.stdout:
65+
raise Exception(
66+
"Could not generate typed client, requires AlgoKit 2.0.0 or "
67+
"later. Please update AlgoKit"
68+
)
69+
else:
70+
raise Exception(
71+
f"Could not generate typed client:\n{generate_result.stdout}"
72+
)
73+
74+
return output_dir / app_spec_file_name

examples/generators/production_python_smart_contract_typescript/smart_contracts/cool_contract/deploy-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as algokit from '@algorandfoundation/algokit-utils'
2-
import { CoolContractClient } from '../artifacts/cool_contract/client'
2+
import { CoolContractClient } from '../artifacts/cool_contract/CoolContractClient'
33

44
// Below is a showcase of various deployment options you can use in TypeScript Client
55
export async function deploy() {

examples/generators/production_python_smart_contract_typescript/smart_contracts/hello_world/deploy-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as algokit from '@algorandfoundation/algokit-utils'
2-
import { HelloWorldClient } from '../artifacts/hello_world/client'
2+
import { HelloWorldClient } from '../artifacts/hello_world/HelloWorldClient'
33

44
// Below is a showcase of various deployment options you can use in TypeScript Client
55
export async function deploy() {

0 commit comments

Comments
 (0)