Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.
Draft
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
35 changes: 22 additions & 13 deletions src/nile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import click

from nile.core.account import Account
from nile.core.call_or_invoke import call_or_invoke as call_or_invoke_command
from nile.core.clean import clean as clean_command
from nile.core.compile import compile as compile_command
from nile.core.declare import declare as declare_command
from nile.core.deploy import deploy as deploy_command
from nile.core.init import init as init_command
from nile.core.install import install as install_command
from nile.core.node import node as node_command
from nile.core.commands.call_or_invoke import call_or_invoke as call_or_invoke_command
from nile.core.commands.clean import clean as clean_command
from nile.core.commands.compile import compile as compile_command
from nile.core.commands.debug import debug as debug_command
from nile.core.commands.declare import declare as declare_command
from nile.core.commands.deploy import deploy as deploy_command
from nile.core.commands.get_accounts import get_accounts as get_accounts_command
from nile.core.commands.get_nonce import get_nonce as get_nonce_command
from nile.core.commands.init import init as init_command
from nile.core.commands.install import install as install_command
from nile.core.commands.node import node as node_command
from nile.core.commands.run import run as run_command
from nile.core.commands.test import test as test_command
from nile.core.commands.version import version as version_command
from nile.core.plugins import load_plugins
from nile.core.run import run as run_command
from nile.core.test import test as test_command
from nile.core.version import version as version_command
from nile.utils.debug import debug as debug_command
from nile.utils.get_accounts import get_accounts as get_accounts_command

logging.basicConfig(level=logging.DEBUG, format="%(message)s")

Expand Down Expand Up @@ -239,6 +240,14 @@ def get_accounts(network):
return get_accounts_command(network)


@cli.command()
@click.argument("contract_address")
@network_option
def get_nonce(contract_address, network):
"""Retrieve the nonce for a contract."""
return get_nonce_command(contract_address, network)


cli = load_plugins(cli)


Expand Down
12 changes: 6 additions & 6 deletions src/nile/core/account.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Command to call or invoke StarkNet smart contracts."""
"""Account class abstraction."""
import logging
import os

from dotenv import load_dotenv

from nile import accounts, deployments
from nile.common import get_nonce
from nile.core.call_or_invoke import call_or_invoke
from nile.core.deploy import deploy
from nile.core.commands.call_or_invoke import call_or_invoke
from nile.core.commands.deploy import deploy
from nile.core.commands.get_nonce import get_nonce
from nile.core.common import accounts, deployments

try:
from nile.signer import Signer
from nile.core.signer import Signer
except ImportError:
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import os
import subprocess

from nile import deployments
from nile.common import GATEWAYS, prepare_params
from nile.core.common import GATEWAYS, deployments, prepare_params


def call_or_invoke(
Expand Down
2 changes: 1 addition & 1 deletion src/nile/core/clean.py → src/nile/core/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import shutil

from nile.common import (
from nile.core.common import (
ACCOUNTS_FILENAME,
BUILD_DIRECTORY,
DECLARATIONS_FILENAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import subprocess

from nile.common import (
from nile.core.common import (
ABIS_DIRECTORY,
BUILD_DIRECTORY,
CONTRACTS_DIRECTORY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
import time

from nile.common import (
from nile.core.common import (
BUILD_DIRECTORY,
DEPLOYMENTS_FILENAME,
GATEWAYS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Command to declare StarkNet smart contracts."""
import logging

from nile import deployments
from nile.common import DECLARATIONS_FILENAME, parse_information, run_command
from nile.core.common import (
DECLARATIONS_FILENAME,
deployments,
parse_information,
run_command,
)


def declare(contract_name, network, alias=None, overriding_path=None):
Expand Down
9 changes: 7 additions & 2 deletions src/nile/core/deploy.py → src/nile/core/commands/deploy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Command to deploy StarkNet smart contracts."""
import logging

from nile import deployments
from nile.common import ABIS_DIRECTORY, BUILD_DIRECTORY, parse_information, run_command
from nile.core.common import (
ABIS_DIRECTORY,
BUILD_DIRECTORY,
deployments,
parse_information,
run_command,
)


def deploy(contract_name, arguments, network, alias, overriding_path=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import json
import logging

from nile.accounts import current_index
from nile.core.account import Account
from nile.core.common.accounts import current_index


def get_accounts(network):
Expand Down
25 changes: 25 additions & 0 deletions src/nile/core/commands/get_nonce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Retrieve and manage deployed accounts."""
import logging
import os
import subprocess

from nile.core.common import get_gateway

GATEWAYS = get_gateway()


def get_nonce(contract_address, network):
"""Get the current nonce for contract address in a given network."""
command = ["starknet", "get_nonce", "--contract_address", contract_address]

if network == "mainnet":
os.environ["STARKNET_NETWORK"] = "alpha-mainnet"
elif network == "goerli":
os.environ["STARKNET_NETWORK"] = "alpha-goerli"
else:
command.append(f"--feeder_gateway_url={GATEWAYS.get(network)}")

nonce = int(subprocess.check_output(command).strip())

logging.info(f"\nCurrent nonce for {contract_address} is {nonce}")
return nonce
2 changes: 1 addition & 1 deletion src/nile/core/init.py → src/nile/core/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from distutils.dir_util import copy_tree
from pathlib import Path

from nile.core.install import install
from nile.core.commands.install import install


def init():
Expand Down
2 changes: 1 addition & 1 deletion src/nile/core/node.py → src/nile/core/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import subprocess

from nile.common import NODE_FILENAME
from nile.core.common import NODE_FILENAME


def node(host="127.0.0.1", port=5050, seed=None, lite_mode=False):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/nile/core/test.py → src/nile/core/commands/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Command to run Cairo tests written in Cairo."""
import asyncio

from nile.common import CONTRACTS_DIRECTORY, get_all_contracts
from nile.core.common import CONTRACTS_DIRECTORY, get_all_contracts


def test(contracts):
Expand Down
18 changes: 2 additions & 16 deletions src/nile/common.py → src/nile/core/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
RETRY_AFTER_SECONDS = 30


def _get_gateway():
def get_gateway():
"""Get the StarkNet node details."""
try:
with open(NODE_FILENAME, "r") as f:
Expand All @@ -27,7 +27,7 @@ def _get_gateway():
f.write('{"localhost": "http://127.0.0.1:5050/"}')


GATEWAYS = _get_gateway()
GATEWAYS = get_gateway()


def get_all_contracts(ext=None, directory=None):
Expand Down Expand Up @@ -72,20 +72,6 @@ def run_command(
return subprocess.check_output(command)


def get_nonce(contract_address, network):
"""Get the current nonce for contract address in a given network."""
command = ["starknet", "get_nonce", "--contract_address", contract_address]

if network == "mainnet":
os.environ["STARKNET_NETWORK"] = "alpha-mainnet"
elif network == "goerli":
os.environ["STARKNET_NETWORK"] = "alpha-goerli"
else:
command.append(f"--feeder_gateway_url={GATEWAYS.get(network)}")

return int(subprocess.check_output(command).strip())


def parse_information(x):
"""Extract information from deploy/declare command."""
# address is 64, tx_hash is 64 chars long
Expand Down
2 changes: 1 addition & 1 deletion src/nile/accounts.py → src/nile/core/common/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os

from nile.common import ACCOUNTS_FILENAME
from nile.core.common import ACCOUNTS_FILENAME


def register(pubkey, address, index, alias, network):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os

from nile.common import DECLARATIONS_FILENAME, DEPLOYMENTS_FILENAME
from nile.core.common import DECLARATIONS_FILENAME, DEPLOYMENTS_FILENAME


def register(address, abi, network, alias):
Expand Down
File renamed without changes.
17 changes: 11 additions & 6 deletions src/nile/nre.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""nile runtime environment."""
from nile import deployments
from nile.core.account import Account
from nile.core.call_or_invoke import call_or_invoke
from nile.core.compile import compile
from nile.core.declare import declare
from nile.core.deploy import deploy
from nile.core.commands.call_or_invoke import call_or_invoke
from nile.core.commands.compile import compile
from nile.core.commands.declare import declare
from nile.core.commands.deploy import deploy
from nile.core.commands.get_accounts import get_accounts
from nile.core.commands.get_nonce import get_nonce as get_nonce_command
from nile.core.common import deployments
from nile.core.plugins import get_installed_plugins, skip_click_exit
from nile.utils.get_accounts import get_accounts


class NileRuntimeEnvironment:
Expand Down Expand Up @@ -55,3 +56,7 @@ def get_or_deploy_account(self, signer):
def get_accounts(self):
"""Retrieve and manage deployed accounts."""
return get_accounts(self.network)

def get_nonce(self, contract_address):
"""Retrieve the nonce for a contract."""
return get_nonce_command(contract_address, self.network)
16 changes: 8 additions & 8 deletions tests/commands/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import pytest

from nile.common import (
from nile.core.commands.clean import clean
from nile.core.common import (
ACCOUNTS_FILENAME,
BUILD_DIRECTORY,
DECLARATIONS_FILENAME,
DEPLOYMENTS_FILENAME,
)
from nile.core.clean import clean


@pytest.fixture(autouse=True)
Expand All @@ -19,8 +19,8 @@ def tmp_working_dir(monkeypatch, tmp_path):
return tmp_path


@patch("nile.core.clean.shutil.rmtree")
@patch("nile.core.clean.os.remove")
@patch("nile.core.commands.clean.shutil.rmtree")
@patch("nile.core.commands.clean.os.remove")
def test_clean_already_clean(mock_os_remove, mock_shutil_rmtree):
clean()
mock_os_remove.assert_not_called()
Expand All @@ -35,17 +35,17 @@ def test_clean_already_clean(mock_os_remove, mock_shutil_rmtree):
f"localhost.{DECLARATIONS_FILENAME}",
],
)
@patch("nile.core.clean.shutil.rmtree")
@patch("nile.core.clean.os.remove")
@patch("nile.core.commands.clean.shutil.rmtree")
@patch("nile.core.commands.clean.os.remove")
def test_clean_clean_files(mock_os_remove, mock_shutil_rmtree, fname):
Path(fname).touch()
clean()
mock_os_remove.assert_called_once_with(fname)
mock_shutil_rmtree.assert_not_called()


@patch("nile.core.clean.shutil.rmtree")
@patch("nile.core.clean.os.remove")
@patch("nile.core.commands.clean.shutil.rmtree")
@patch("nile.core.commands.clean.os.remove")
def test_clean_clean_build_dir(mock_os_remove, mock_shutil_rmtree):
Path(BUILD_DIRECTORY).mkdir()
clean()
Expand Down
12 changes: 6 additions & 6 deletions tests/commands/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import pytest

from nile.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY
from nile.core.compile import _compile_contract, compile
from nile.core.commands.compile import _compile_contract, compile
from nile.core.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY

CONTRACT = "foo.cairo"

Expand All @@ -25,7 +25,7 @@ def tmp_working_dir(monkeypatch, tmp_path):
# shell commands.
@pytest.fixture(autouse=True)
def mock_subprocess():
with patch("nile.core.compile.subprocess") as mock_subprocess:
with patch("nile.core.commands.compile.subprocess") as mock_subprocess:
yield mock_subprocess


Expand All @@ -35,7 +35,7 @@ def test_compile_create_abis_directory(tmp_working_dir):
assert (tmp_working_dir / ABIS_DIRECTORY).exists()


@patch("nile.core.compile.get_all_contracts")
@patch("nile.core.commands.compile.get_all_contracts")
def test_compile_get_all_contracts_called(mock_get_all_contracts):
compile([])
mock_get_all_contracts.assert_called_once()
Expand All @@ -45,15 +45,15 @@ def test_compile_get_all_contracts_called(mock_get_all_contracts):
mock_get_all_contracts.assert_not_called()


@patch("nile.core.compile._compile_contract")
@patch("nile.core.commands.compile._compile_contract")
def test_compile__compile_contract_called(mock__compile_contract):
compile([CONTRACT])
mock__compile_contract.assert_called_once_with(
CONTRACT, CONTRACTS_DIRECTORY, False, False
)


@patch("nile.core.compile._compile_contract")
@patch("nile.core.commands.compile._compile_contract")
def test_compile_failure_feedback(mock__compile_contract, caplog):
# make logs visible to test
logging.getLogger().setLevel(logging.INFO)
Expand Down
Loading