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

Move utils.py from Contracts to Nile #163

Merged
merged 8 commits into from
Aug 11, 2022

Conversation

andrew-fleming
Copy link
Contributor

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 include utils.py in Nile's root thus: from nile.utils import *.

Copy link
Contributor

@martriay martriay left a 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."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Utilities for Cairo contracts."""
"""Utilities for Nile scripting."""

utils.py Outdated


def felt_to_str(felt):
"""Return a string from a given field element."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""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.

Copy link
Contributor

@martriay martriay left a 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
Comment on lines 119 to 140
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

Copy link
Contributor

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.

@andrew-fleming
Copy link
Contributor Author

I take it back. I think the path should be different? It's sitting on the root directory right now.

Tunnel vision on my part for ease of use^. Where do you think is most appropriate? from nile.src.utils? This seemed a little weird from nile.src.nile.utils.utils

Copy link
Contributor

@martriay martriay left a 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

@martriay martriay merged commit 651f59d into OpenZeppelin:main Aug 11, 2022
@andrew-fleming andrew-fleming deleted the integrate-utils branch August 12, 2022 06:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants