-
Notifications
You must be signed in to change notification settings - Fork 76
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
utils.py
Outdated
@@ -0,0 +1,195 @@ | |||
"""Utilities for Cairo contracts.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Utilities for Cairo contracts.""" | |
"""Utilities for Nile scripting.""" |
utils.py
Outdated
|
||
|
||
def felt_to_str(felt): | ||
"""Return a string from a given field element.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Return a string from a given field element.""" | |
"""Return a string from a given field element.""" | |
felt = int(felt, 16) if "0x" in felt else felt |
this added line provides hexadecimal support (something users have been asking for). I know this breaks the "cleanliness" of the code migration but I think it adds a lot of value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I take it back. I think the path should be different? It's sitting on the root directory right now.
utils.py
Outdated
def _get_path_from_name(name): | ||
"""Return the contract path by contract name.""" | ||
dirs = ["src", "tests/mocks"] | ||
for dir in dirs: | ||
for (dirpath, _, filenames) in os.walk(dir): | ||
for file in filenames: | ||
if file == f"{name}.cairo": | ||
return os.path.join(dirpath, file) | ||
|
||
raise FileNotFoundError(f"Cannot find '{name}'.") | ||
|
||
|
||
def get_contract_class(contract, is_path=False): | ||
"""Return the contract class from the contract name or path.""" | ||
if is_path: | ||
path = contract_path(contract) | ||
else: | ||
path = _get_path_from_name(contract) | ||
|
||
contract_class = compile_starknet_files(files=[path], debug_info=True) | ||
return contract_class | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Traceback (most recent call last):
File "/home/workshop/env/bin/nile", line 5, in <module>
from nile.cli import cli
File "/home/workshop/env/lib/python3.8/site-packages/nile/cli.py", line 20, in <module>
from nile.utils.debug import debug as debug_command
File "/home/workshop/env/lib/python3.8/site-packages/nile/utils/__init__.py", line 169, in <module>
class Account:
File "/home/workshop/env/lib/python3.8/site-packages/nile/utils/__init__.py", line 181, in Account
get_class = get_contract_class("Account")
File "/home/workshop/env/lib/python3.8/site-packages/nile/utils/__init__.py", line 136, in get_contract_class
path = _get_path_from_name(contract)
File "/home/workshop/env/lib/python3.8/site-packages/nile/utils/__init__.py", line 128, in _get_path_from_name
raise FileNotFoundError(f"Cannot find '{name}'.")
FileNotFoundError: Cannot find 'Account'.
I think some of these functions weren't easily portable since they depend on structure and contents from Contracts. We might be better off leaving them out of this beta.
Tunnel vision on my part for ease of use^. Where do you think is most appropriate? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me
This PR moves the
utils.py
from Contracts for Cairo to Nile in order to allow users to easily import the utility functions. This PR proposes to includeutils.py
in Nile's root thus:from nile.utils import *
.