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

Devnet predeployed accounts #192

Conversation

ericnordelo
Copy link
Member

Add get_predeployed_accounts method and logic to cli and nre (compatible with starknet-devnet).

@ericnordelo ericnordelo linked an issue Sep 15, 2022 that may be closed by this pull request
@ericnordelo ericnordelo marked this pull request as draft September 15, 2022 23:17
@ericnordelo ericnordelo marked this pull request as ready for review September 20, 2022 16:26
Copy link
Contributor

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Great work, @ericnordelo! I left some comments and questions :)

Comment on lines +397 to +401
You can use it either from the cli:

```sh
nile get-accounts --predeployed
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we send txs from a predeployed account through the CLI?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't now, but it is an interesting idea. Maybe it will be worth modifying the send command (with a predeployed flag or similar) to allow sending tx through predeployed accounts? I'm curious about both your opinions on this @andrew-fleming @martriay

Copy link
Contributor

Choose a reason for hiding this comment

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

I assumed as much, just confirming. What do you think about removing the CLI snippet and the You can use it either from the cli? I don't think there's any value from running the command in the CLI and it might avoid users (or me) asking if we can send txs from the predeployeds through the CLI

Maybe it will be worth modifying the send command (with a predeployed flag or similar) to allow sending tx through predeployed accounts

Hmm it sounds like it'd be great to consider. I wouldn't include it in this PR though, that seems like a down-the-road feature

Copy link
Member Author

Choose a reason for hiding this comment

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

But you can use it from the cli, just not to send transactions. You can list the predeployed accounts from the cli, as you can do with registered accounts running nile get-accounts, so I think thee snippet still makes sense.

@@ -234,9 +237,13 @@ def debug(tx_hash, network, contracts_file):

@cli.command()
@network_option
def get_accounts(network):
@click.option("--predeployed/--registered", default=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be a good idea to give a quick mention to the default registered flag in the docs—probably in the get-accounts command

Copy link
Member Author

Choose a reason for hiding this comment

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

I was checking Click, and as you can check here, Click always wants you to provide an enable and disable flag so that you can change the default later, but just for this, the disable flag is not necessary and is not required from CLI, so I thought that with documenting the --predeployed is enough, because is never needed to pass the --registered flag. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that's reasonable. Documenting --predeployed seems like enough

Comment on lines +79 to +81
# Starknet CLI requires an hex string for get nonce command
if not str(contract_address).startswith("0x"):
contract_address = hex(int(contract_address))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a reminder for us to remove get_nonce here after #197 (assuming we merge that PR first)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and also I just noticed that this method shouldn't receive anything not being an int in contract address (the design patter for hex and int we are trying to follow), so this parser should be refactored accordingly too.

Comment on lines -23 to +24
def __init__(self, signer, network):
def __init__(self, signer, network, predeployed_info=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be extremely helpful to others (as well as ourselves) if we make an issue for adding params and return values in our docstrings

Copy link
Member Author

@ericnordelo ericnordelo Sep 23, 2022

Choose a reason for hiding this comment

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

Agree, I've just created it #209 .

Comment on lines +43 to +45
self.abi_path = os.path.dirname(os.path.realpath(__file__)).replace(
"/core", "/artifacts/abis/Account.json"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

target_address, _ = next(deployments.load(to, self.network)) or to
target_address, _ = next(deployments.load(hex_address(to), self.network)) or to
# Work with integers internally
target_address = normalize_number(target_address)
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
target_address = normalize_number(target_address)
target_address = hex(target_address)

normalize_number gave me a lot of trouble here. I think using hex is the way to go

Copy link
Member Author

Choose a reason for hiding this comment

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

What troubles did you find? Using hex here is against the pattern we agreed, we should use int internally. Anyway, normalize_number is not necessary here because deployments.load always returns an int (being compliant with the pattern). I removed it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the issues were from how I was hacking the deployments.load when trying to get it to work locally. With the latest updates to the PR, everything's running well

@@ -84,7 +100,7 @@ def send(self, to, method, calldata, max_fee, nonce=None):
)

return call_or_invoke(
contract=self.address,
contract=self,
Copy link
Contributor

Choose a reason for hiding this comment

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

This threw me off for a bit 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I think is better to pass the account instance to avoid passing a lot of extra parameters like the ABI (that was required for predeployed accounts). All these params are in the instance already. Suggestions are welcome to address this, of course.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed on this being a better approach. Well done!

Copy link
Contributor

Choose a reason for hiding this comment

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

this is causing a circular dependency, i'm rolling it back in #268

____________________________________________________ ERROR collecting tests/commands/test_get_balance.py ____________________________________________________
ImportError while importing test module '/home/nile/tests/commands/test_get_balance.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/commands/test_get_balance.py:7: in <module>
    from nile.utils.get_balance import get_balance
.tox/default/lib/python3.8/site-packages/nile/utils/get_balance.py:3: in <module>
    from nile.core.call_or_invoke import call_or_invoke
.tox/default/lib/python3.8/site-packages/nile/core/call_or_invoke.py:6: in <module>
    from nile.core import account
.tox/default/lib/python3.8/site-packages/nile/core/account.py:20: in <module>
    from nile.core.call_or_invoke import call_or_invoke
E   ImportError: cannot import name 'call_or_invoke' from partially initialized module 'nile.core.call_or_invoke' (most likely due to a circular import) (/home/nile/.tox/default/lib/python3.8/site-packages/nile/core/call_or_invoke.py)

Comment on lines +18 to +19
else:
address, abi = next(deployments.load(contract, network))
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
else:
address, abi = next(deployments.load(contract, network))
else:
address, abi = next(deployments.load(contract, network))
address = hex(address)

I believe the address should also be converted to hex. When I try to script a basic get_balance query, I'm receiving a type error:

TypeError: expected str, bytes or os.PathLike object, not int

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it outside the if/else for both branches before calling the starknet cli.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect!



def hex_address(number):
"""Normalize hex or int to int."""
Copy link
Contributor

Choose a reason for hiding this comment

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

hex_address is trying to be a normalized number 🥸

Comment on lines 35 to 38
build_calls = []
for call in calls:
build_call = list(call)
build_call[0] = hex(build_call[0])
build_calls.append(build_call)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we don't need to hexify the first arg, we can remove all this and just pass calls to get_raw_invoke and sign_transaction

Also, let's remember to update MockSigner and MockEthSigner in Contracts before the next release since they use the same build_calls logic

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed test.

andrew-fleming
andrew-fleming previously approved these changes Sep 23, 2022
Copy link
Contributor

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Excellent improvements! Looks good to go

@andrew-fleming andrew-fleming merged commit acb1f58 into OpenZeppelin:main Sep 26, 2022
@ericnordelo ericnordelo deleted the feat/devnet-predeployed-accounts-#137 branch September 26, 2022 16:26
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.

Integrate pre-deployed devnet accounts
3 participants