Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Add abi parameter to deploy #206

Merged
merged 3 commits into from
Sep 27, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ A few things to notice here:
2. This created a `localhost.deployments.txt` file storing all data related to my deployment
3. The `--alias` parameter lets me create an unique identifier for future interactions, if no alias is set then the contract's address can be used as identifier
4. By default Nile works on local, but you can use the `--network` parameter to interact with `mainnet`, `goerli`, and the default `localhost`.
5. By default, the ABI corresponding to the contract will be registered with the deployment. To register a different ABI file, use the `--abi` parameter.

### `declare`

Expand Down
5 changes: 3 additions & 2 deletions src/nile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ def run(path, network):
@click.argument("arguments", nargs=-1)
@network_option
@click.option("--alias")
def deploy(artifact, arguments, network, alias):
@click.option("--abi")
def deploy(artifact, arguments, network, alias, abi=None):
"""Deploy StarkNet smart contract."""
deploy_command(artifact, arguments, network, alias)
deploy_command(artifact, arguments, network, alias, abi=abi)


@cli.command()
Expand Down
8 changes: 4 additions & 4 deletions src/nile/core/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
from nile.common import ABIS_DIRECTORY, BUILD_DIRECTORY, parse_information, run_command


def deploy(contract_name, arguments, network, alias, overriding_path=None):
def deploy(contract_name, arguments, network, alias, overriding_path=None, abi=None):
"""Deploy StarkNet smart contracts."""
logging.info(f"🚀 Deploying {contract_name}")
base_path = (
overriding_path if overriding_path else (BUILD_DIRECTORY, ABIS_DIRECTORY)
)
abi = f"{base_path[1]}/{contract_name}.json"
register_abi = abi if abi is not None else f"{base_path[1]}/{contract_name}.json"

output = run_command(contract_name, network, overriding_path, arguments=arguments)

address, tx_hash = parse_information(output)
logging.info(f"⏳ ️Deployment of {contract_name} successfully sent at {address}")
logging.info(f"🧾 Transaction hash: {tx_hash}")

deployments.register(address, abi, network, alias)
return address, abi
deployments.register(address, register_abi, network, alias)
return address, register_abi
8 changes: 6 additions & 2 deletions src/nile/nre.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def declare(self, contract, alias=None, overriding_path=None):
"""Declare a smart contract class."""
return declare(contract, self.network, alias)

def deploy(self, contract, arguments=None, alias=None, overriding_path=None):
def deploy(
self, contract, arguments=None, alias=None, overriding_path=None, abi=None
):
"""Deploy a smart contract."""
return deploy(contract, arguments, self.network, alias, overriding_path)
return deploy(
contract, arguments, self.network, alias, overriding_path, abi=abi
)

def call(self, contract, method, params=None):
"""Call a view function in a smart contract."""
Expand Down
23 changes: 19 additions & 4 deletions tests/commands/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def tmp_working_dir(monkeypatch, tmp_path):
NETWORK = "goerli"
ALIAS = "alias"
ABI = f"{ABIS_DIRECTORY}/{CONTRACT}.json"
ABI_OVERRIDE = f"{ABIS_DIRECTORY}/override.json"
BASE_PATH = (BUILD_DIRECTORY, ABIS_DIRECTORY)
PATH_OVERRIDE = ("artifacts2", ABIS_DIRECTORY)
RUN_OUTPUT = b"output"
Expand All @@ -26,32 +27,46 @@ def tmp_working_dir(monkeypatch, tmp_path):


@pytest.mark.parametrize(
"args, exp_command",
"args, exp_command, exp_abi",
[
(
[CONTRACT, ARGS, NETWORK, ALIAS], # args
[CONTRACT, NETWORK, None], # expected command
ABI, # expected ABI
),
(
[CONTRACT, ARGS, NETWORK, ALIAS, PATH_OVERRIDE], # args
[CONTRACT, NETWORK, PATH_OVERRIDE], # expected command
ABI, # expected ABI
),
(
[CONTRACT, ARGS, NETWORK, ALIAS, None, ABI_OVERRIDE], # args
[CONTRACT, NETWORK, None], # expected command
ABI_OVERRIDE, # expected ABI
),
(
[CONTRACT, ARGS, NETWORK, ALIAS, PATH_OVERRIDE, ABI_OVERRIDE], # args
[CONTRACT, NETWORK, PATH_OVERRIDE], # expected command
ABI_OVERRIDE, # expected ABI
),
],
)
@patch("nile.core.deploy.run_command", return_value=RUN_OUTPUT)
@patch("nile.core.deploy.parse_information", return_value=[ADDRESS, TX_HASH])
@patch("nile.core.deploy.deployments.register")
def test_deploy(mock_register, mock_parse, mock_run_cmd, caplog, args, exp_command):
def test_deploy(
mock_register, mock_parse, mock_run_cmd, caplog, args, exp_command, exp_abi
):
logging.getLogger().setLevel(logging.INFO)

# check return values
res = deploy(*args)
assert res == (ADDRESS, ABI)
assert res == (ADDRESS, exp_abi)

# check internals
mock_run_cmd.assert_called_once_with(*exp_command, arguments=ARGS)
mock_parse.assert_called_once_with(RUN_OUTPUT)
mock_register.assert_called_once_with(ADDRESS, ABI, NETWORK, ALIAS)
mock_register.assert_called_once_with(ADDRESS, exp_abi, NETWORK, ALIAS)

# check logs
assert f"🚀 Deploying {CONTRACT}" in caplog.text
Expand Down