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

Commit

Permalink
Support starknet token for mainnet deployments (#263)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update README.md

Co-authored-by: Andrew Fleming <[email protected]>

* Update src/nile/cli.py

Co-authored-by: Andrew Fleming <[email protected]>

* feat: add tests

Co-authored-by: Andrew Fleming <[email protected]>
  • Loading branch information
ericnordelo and andrew-fleming authored Nov 3, 2022
1 parent 19ba063 commit e0669c7
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions src/nile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -103,6 +119,7 @@ def declare(
alias=alias,
max_fee=max_fee,
overriding_path=overriding_path,
mainnet_token=token,
)


Expand Down
5 changes: 5 additions & 0 deletions src/nile/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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}")

Expand Down
9 changes: 8 additions & 1 deletion src/nile/core/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -114,6 +120,7 @@ def declare(
alias=alias,
network=self.network,
max_fee=max_fee,
mainnet_token=mainnet_token,
)

def deploy_contract(
Expand Down
2 changes: 2 additions & 0 deletions src/nile/core/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/nile/core/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions src/nile/nre.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/commands/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
4 changes: 4 additions & 0 deletions tests/commands/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand All @@ -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
),
Expand All @@ -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
),
Expand All @@ -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
),
Expand Down
4 changes: 4 additions & 0 deletions tests/commands/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand All @@ -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
),
Expand All @@ -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
),
Expand All @@ -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
),
Expand Down
58 changes: 44 additions & 14 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e0669c7

Please sign in to comment.