From e0669c72e1214d4b18d8505e777f09ed3417fa3b Mon Sep 17 00:00:00 2001 From: Eric Nordelo Date: Thu, 3 Nov 2022 23:03:18 +0100 Subject: [PATCH] Support starknet token for mainnet deployments (#263) * feat: add support for mainnet token * feat: update tests * fix: format * feat: add test coverage * feat: update README * fix: typo in README * Update README.md Co-authored-by: Andrew Fleming * Update README.md Co-authored-by: Andrew Fleming * Update src/nile/cli.py Co-authored-by: Andrew Fleming * feat: add tests Co-authored-by: Andrew Fleming --- README.md | 4 +++ src/nile/cli.py | 23 ++++++++++++-- src/nile/common.py | 5 +++ src/nile/core/account.py | 9 +++++- src/nile/core/declare.py | 2 ++ src/nile/core/deploy.py | 11 ++++++- src/nile/nre.py | 16 ++++++++-- tests/commands/test_account.py | 1 + tests/commands/test_declare.py | 4 +++ tests/commands/test_deploy.py | 4 +++ tests/test_common.py | 58 ++++++++++++++++++++++++++-------- 11 files changed, 116 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ce6e82cb..1fdf5ae3 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,8 @@ Creating artifacts/abis/ to store compilation artifacts > NOTICE: this method doesn't use an account, which will be deprecated very soon as StarkNet makes deployments from accounts mandatory. +> Token for deployments to Alpha Mainnet can be set with the `--token` option. + ```sh nile deploy contract --alias my_contract @@ -178,6 +180,8 @@ Some things to note: ### `declare` +> Token for declarations to Alpha Mainnet can be set with the `--token` option. + Very similar to `send`, but for declaring a contract based on its name through an account. ```sh diff --git a/src/nile/cli.py b/src/nile/cli.py index d9d6d81f..76bc2995 100644 --- a/src/nile/cli.py +++ b/src/nile/cli.py @@ -40,6 +40,14 @@ def network_option(f): )(f) +def mainnet_token_option(f): + """Configure TOKEN option for the cli.""" + return click.option( + "--token", + help="Used for deploying contracts in Alpha Mainnet.", + )(f) + + def _validate_network(_ctx, _param, value): """Normalize network values.""" # check if value is known @@ -81,9 +89,10 @@ def run(path, network): @network_option @click.option("--alias") @click.option("--abi") -def deploy(artifact, arguments, network, alias, abi=None): +@mainnet_token_option +def deploy(artifact, arguments, network, alias, abi=None, token=None): """Deploy StarkNet smart contract.""" - deploy_command(artifact, arguments, network, alias, abi=abi) + deploy_command(artifact, arguments, network, alias, abi=abi, mainnet_token=token) @cli.command() @@ -92,9 +101,16 @@ def deploy(artifact, arguments, network, alias, abi=None): @click.option("--max_fee", nargs=1) @click.option("--alias") @click.option("--overriding_path") +@mainnet_token_option @network_option def declare( - signer, contract_name, network, max_fee=None, alias=None, overriding_path=None + signer, + contract_name, + network, + max_fee=None, + alias=None, + overriding_path=None, + token=None, ): """Declare StarkNet smart contract.""" account = Account(signer, network) @@ -103,6 +119,7 @@ def declare( alias=alias, max_fee=max_fee, overriding_path=overriding_path, + mainnet_token=token, ) diff --git a/src/nile/common.py b/src/nile/common.py index d9473e19..0355454e 100644 --- a/src/nile/common.py +++ b/src/nile/common.py @@ -77,6 +77,7 @@ def run_command( max_fee=None, query_flag=None, overriding_path=None, + mainnet_token=None, ): """Execute CLI command with given parameters.""" command = ["starknet", operation] @@ -101,6 +102,10 @@ def run_command( command.append("--max_fee") command.append(max_fee) + if mainnet_token is not None: + command.append("--token") + command.append(mainnet_token) + if query_flag is not None: command.append(f"--{query_flag}") diff --git a/src/nile/core/account.py b/src/nile/core/account.py index 9f832eba..69861198 100644 --- a/src/nile/core/account.py +++ b/src/nile/core/account.py @@ -85,7 +85,13 @@ def deploy(self): return address, index def declare( - self, contract_name, max_fee=None, nonce=None, alias=None, overriding_path=None + self, + contract_name, + max_fee=None, + nonce=None, + alias=None, + overriding_path=None, + mainnet_token=None, ): """Declare a contract through an Account contract.""" if nonce is None: @@ -114,6 +120,7 @@ def declare( alias=alias, network=self.network, max_fee=max_fee, + mainnet_token=mainnet_token, ) def deploy_contract( diff --git a/src/nile/core/declare.py b/src/nile/core/declare.py index 4306fd8d..f51e65a4 100644 --- a/src/nile/core/declare.py +++ b/src/nile/core/declare.py @@ -14,6 +14,7 @@ def declare( alias=None, overriding_path=None, max_fee=None, + mainnet_token=None, ): """Declare StarkNet smart contracts.""" logging.info(f"🚀 Declaring {contract_name}") @@ -33,6 +34,7 @@ def declare( signature=signature, max_fee=max_fee, overriding_path=overriding_path, + mainnet_token=mainnet_token, ) class_hash, tx_hash = parse_information(output) diff --git a/src/nile/core/deploy.py b/src/nile/core/deploy.py index 0aedfdc6..0f63aaff 100644 --- a/src/nile/core/deploy.py +++ b/src/nile/core/deploy.py @@ -6,7 +6,15 @@ from nile.utils import hex_address -def deploy(contract_name, arguments, network, alias, overriding_path=None, abi=None): +def deploy( + contract_name, + arguments, + network, + alias, + overriding_path=None, + abi=None, + mainnet_token=None, +): """Deploy StarkNet smart contracts.""" logging.info(f"🚀 Deploying {contract_name}") base_path = ( @@ -20,6 +28,7 @@ def deploy(contract_name, arguments, network, alias, overriding_path=None, abi=N contract_name=contract_name, overriding_path=overriding_path, inputs=arguments, + mainnet_token=mainnet_token, ) address, tx_hash = parse_information(output) diff --git a/src/nile/nre.py b/src/nile/nre.py index c216aa99..0dc7806d 100644 --- a/src/nile/nre.py +++ b/src/nile/nre.py @@ -25,11 +25,23 @@ def compile(self, contracts, cairo_path=None): return compile(contracts, cairo_path=cairo_path) def deploy( - self, contract, arguments=None, alias=None, overriding_path=None, abi=None + self, + contract, + arguments=None, + alias=None, + overriding_path=None, + abi=None, + mainnet_token=None, ): """Deploy a smart contract.""" return deploy( - contract, arguments, self.network, alias, overriding_path, abi=abi + contract, + arguments, + self.network, + alias, + overriding_path, + abi=abi, + mainnet_token=mainnet_token, ) def call(self, address_or_alias, method, params=None): diff --git a/tests/commands/test_account.py b/tests/commands/test_account.py index c29bd63b..080b671d 100644 --- a/tests/commands/test_account.py +++ b/tests/commands/test_account.py @@ -116,6 +116,7 @@ def test_declare(mock_declare, mock_get_class, mock_deploy): network=NETWORK, alias=alias, max_fee=max_fee, + mainnet_token=None, ) diff --git a/tests/commands/test_declare.py b/tests/commands/test_declare.py index 07a77b64..b3448725 100644 --- a/tests/commands/test_declare.py +++ b/tests/commands/test_declare.py @@ -49,6 +49,7 @@ def test_alias_exists(): "arguments": ["--sender", hex_address(SENDER)], "overriding_path": None, "max_fee": "0", + "mainnet_token": None, }, [HASH, NETWORK, None], # expected register ), @@ -61,6 +62,7 @@ def test_alias_exists(): "arguments": ["--sender", hex_address(SENDER)], "overriding_path": None, "max_fee": "0", + "mainnet_token": None, }, [HASH, NETWORK, ALIAS], # expected register ), @@ -73,6 +75,7 @@ def test_alias_exists(): "arguments": ["--sender", hex_address(SENDER)], "overriding_path": PATH, "max_fee": "0", + "mainnet_token": None, }, [HASH, NETWORK, ALIAS], # expected register ), @@ -85,6 +88,7 @@ def test_alias_exists(): "arguments": ["--sender", hex_address(SENDER)], "overriding_path": PATH, "max_fee": str(MAX_FEE), + "mainnet_token": None, }, [HASH, NETWORK, ALIAS], # expected register ), diff --git a/tests/commands/test_deploy.py b/tests/commands/test_deploy.py index 56bde6fb..c117ec41 100644 --- a/tests/commands/test_deploy.py +++ b/tests/commands/test_deploy.py @@ -37,6 +37,7 @@ def tmp_working_dir(monkeypatch, tmp_path): "contract_name": CONTRACT, "network": NETWORK, "overriding_path": None, + "mainnet_token": None, }, ABI, # expected ABI ), @@ -46,6 +47,7 @@ def tmp_working_dir(monkeypatch, tmp_path): "contract_name": CONTRACT, "network": NETWORK, "overriding_path": PATH_OVERRIDE, + "mainnet_token": None, }, ABI, # expected ABI ), @@ -55,6 +57,7 @@ def tmp_working_dir(monkeypatch, tmp_path): "contract_name": CONTRACT, "network": NETWORK, "overriding_path": None, + "mainnet_token": None, }, ABI_OVERRIDE, # expected ABI ), @@ -64,6 +67,7 @@ def tmp_working_dir(monkeypatch, tmp_path): "contract_name": CONTRACT, "network": NETWORK, "overriding_path": PATH_OVERRIDE, + "mainnet_token": None, }, ABI_OVERRIDE, # expected ABI ), diff --git a/tests/test_common.py b/tests/test_common.py index 6c5d1fd9..8121e71c 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -14,25 +14,55 @@ LIST3 = [1, 2, 3, [4, 5, 6, [7, 8, 9]]] -@pytest.mark.parametrize("operation", ["invoke", "call"]) +@pytest.mark.parametrize("operation", ["invoke", "call", "deploy", "declare"]) +@pytest.mark.parametrize("signature", [None]) +@pytest.mark.parametrize("max_fee", [0, 5, None]) +@pytest.mark.parametrize("query_flag", ["simulate", "estimate_fee", None]) +@pytest.mark.parametrize("mainnet_token", ["token_test", None]) @patch("nile.common.subprocess.check_output") -def test_run_command(mock_subprocess, operation): +def test_run_command( + mock_subprocess, operation, signature, max_fee, query_flag, mainnet_token +): run_command( - contract_name=CONTRACT, network=NETWORK, operation=operation, inputs=ARGS + contract_name=CONTRACT, + network=NETWORK, + operation=operation, + inputs=ARGS, + signature=signature, + max_fee=max_fee, + query_flag=query_flag, + mainnet_token=mainnet_token, ) - mock_subprocess.assert_called_once_with( - [ - "starknet", - operation, - "--contract", - f"{BUILD_DIRECTORY}/{CONTRACT}.json", - "--inputs", - *ARGS, - "--no_wallet", - ] - ) + exp_command = [ + "starknet", + operation, + "--contract", + f"{BUILD_DIRECTORY}/{CONTRACT}.json", + "--inputs", + *ARGS, + ] + + # Add signature + if signature is not None: + exp_command.extend(["--signature", signature]) + + # Add max_fee + if max_fee is not None: + exp_command.extend(["--max_fee", max_fee]) + + # Add mainnet_token + if mainnet_token is not None: + exp_command.extend(["--token", mainnet_token]) + + # Add query_flag + if query_flag is not None: + exp_command.extend([f"--{query_flag}"]) + + exp_command.append("--no_wallet") + + mock_subprocess.assert_called_once_with(exp_command) @pytest.mark.parametrize(