Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for named typed client exports for files with multiple contacts #36

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def deploy(
app_spec: algokit_utils.ApplicationSpecification,
deployer: algokit_utils.Account,
) -> None:
from smart_contracts.artifacts.{{ contract_name }}.client import (
from smart_contracts.artifacts.{{ contract_name }}.{{ contract_name }}_client import (
{{ contract_name.split('_')|map('capitalize')|join }}Client,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@
"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.",
"line": 15
}
{
"file": "smart_contracts/_helpers/__init__.py",
"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.",
"line": 1
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from dotenv import load_dotenv

from smart_contracts.config import contracts
from smart_contracts.helpers.build import build
from smart_contracts.helpers.deploy import deploy
from smart_contracts.helpers.util import find_app_spec_file
from smart_contracts._helpers.build import build
from smart_contracts._helpers.config import contracts
from smart_contracts._helpers.deploy import deploy

# Uncomment the following lines to enable auto generation of AVM Debugger compliant sourcemap and simulation trace file.
# Learn more about using AlgoKit AVM Debugger to debug your TEAL source codes and inspect various kinds of
Expand Down Expand Up @@ -36,7 +35,14 @@ def main(action: str) -> None:
for contract in contracts:
logger.info(f"Deploying app {contract.name}")
output_dir = artifact_path / contract.name
app_spec_file_name = find_app_spec_file(output_dir)
app_spec_file_name = next(
(
file.name
for file in output_dir.iterdir()
if file.is_file() and file.suffixes == [".arc32", ".json"]
),
None,
)
if app_spec_file_name is None:
raise Exception("Could not deploy app, .arc32.json file not found")
app_spec_path = output_dir / app_spec_file_name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
import subprocess
from pathlib import Path
from shutil import rmtree

logger = logging.getLogger(__name__)
deployment_extension = "py"


def _get_output_path(output_dir: Path, deployment_extension: str) -> Path:
return output_dir / Path(
"{contract_name}"
+ ("_client" if deployment_extension == "py" else "Client")
+ f".{deployment_extension}"
)


def build(output_dir: Path, contract_path: Path) -> Path:
output_dir = output_dir.resolve()
if output_dir.exists():
rmtree(output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
logger.info(f"Exporting {contract_path} to {output_dir}")

build_result = subprocess.run(
[
"algokit",
"--no-color",
"compile",
"python",
contract_path.absolute(),
f"--out-dir={output_dir}",
"--output-arc32",
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if build_result.returncode:
raise Exception(f"Could not build contract:\n{build_result.stdout}")

app_spec_file_names = [file.name for file in output_dir.glob("*.arc32.json")]

for app_spec_file_name in app_spec_file_names:
if app_spec_file_name is None:
raise Exception(
"Could not generate typed client, .arc32.json file not found"
)
print(app_spec_file_name)
generate_result = subprocess.run(
[
"algokit",
"generate",
"client",
output_dir,
"--output",
_get_output_path(output_dir, deployment_extension),
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if generate_result.returncode:
if "No such command" in generate_result.stdout:
raise Exception(
"Could not generate typed client, requires AlgoKit 2.0.0 or "
"later. Please update AlgoKit"
)
else:
raise Exception(
f"Could not generate typed client:\n{generate_result.stdout}"
)

return output_dir / app_spec_file_name
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def deploy(
app_spec: algokit_utils.ApplicationSpecification,
deployer: algokit_utils.Account,
) -> None:
from smart_contracts.artifacts.cool_contract.client import (
from smart_contracts.artifacts.cool_contract.cool_contract_client import (
CoolContractClient,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def deploy(
app_spec: algokit_utils.ApplicationSpecification,
deployer: algokit_utils.Account,
) -> None:
from smart_contracts.artifacts.hello_world.client import (
from smart_contracts.artifacts.hello_world.hello_world_client import (
HelloWorldClient,
)

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from algosdk.v2client.algod import AlgodClient
from algosdk.v2client.indexer import IndexerClient

from smart_contracts.artifacts.hello_world.client import HelloWorldClient
from smart_contracts.artifacts.hello_world.hello_world_client import HelloWorldClient


@pytest.fixture(scope="session")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { {{ contract_name.split('_')|map('capitalize')|join }}Client } from '../artifacts/{{ contract_name }}/client'
import { {{ contract_name.split('_')|map('capitalize')|join }}Client } from '../artifacts/{{ contract_name }}/{{ contract_name.split('_')|map('capitalize')|join }}Client'

// Below is a showcase of various deployment options you can use in TypeScript Client
export async function deploy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
"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.",
"line": 15
}
{
"file": "smart_contracts/_helpers/__init__.py",
"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.",
"line": 1
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from dotenv import load_dotenv

from smart_contracts.config import contracts
from smart_contracts.helpers.build import build
from smart_contracts._helpers.build import build
from smart_contracts._helpers.config import contracts

logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(levelname)-10s: %(message)s"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
import subprocess
from pathlib import Path
from shutil import rmtree

logger = logging.getLogger(__name__)
deployment_extension = "ts"


def _get_output_path(output_dir: Path, deployment_extension: str) -> Path:
return output_dir / Path(
"{contract_name}"
+ ("_client" if deployment_extension == "py" else "Client")
+ f".{deployment_extension}"
)


def build(output_dir: Path, contract_path: Path) -> Path:
output_dir = output_dir.resolve()
if output_dir.exists():
rmtree(output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
logger.info(f"Exporting {contract_path} to {output_dir}")

build_result = subprocess.run(
[
"algokit",
"--no-color",
"compile",
"python",
contract_path.absolute(),
f"--out-dir={output_dir}",
"--output-arc32",
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if build_result.returncode:
raise Exception(f"Could not build contract:\n{build_result.stdout}")

app_spec_file_names = [file.name for file in output_dir.glob("*.arc32.json")]

for app_spec_file_name in app_spec_file_names:
if app_spec_file_name is None:
raise Exception(
"Could not generate typed client, .arc32.json file not found"
)
print(app_spec_file_name)
generate_result = subprocess.run(
[
"algokit",
"generate",
"client",
output_dir,
"--output",
_get_output_path(output_dir, deployment_extension),
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if generate_result.returncode:
if "No such command" in generate_result.stdout:
raise Exception(
"Could not generate typed client, requires AlgoKit 2.0.0 or "
"later. Please update AlgoKit"
)
else:
raise Exception(
f"Could not generate typed client:\n{generate_result.stdout}"
)

return output_dir / app_spec_file_name
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { CoolContractClient } from '../artifacts/cool_contract/client'
import { CoolContractClient } from '../artifacts/cool_contract/CoolContractClient'

// Below is a showcase of various deployment options you can use in TypeScript Client
export async function deploy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as algokit from '@algorandfoundation/algokit-utils'
import { HelloWorldClient } from '../artifacts/hello_world/client'
import { HelloWorldClient } from '../artifacts/hello_world/HelloWorldClient'

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