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

Added support for signer #33

Merged
merged 17 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include LICENSE
recursive-include src/nile/artifacts/ *
recursive-include src/nile/base_project/ *
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,56 @@ A few things to notice here:
3. The `--alias` parameter lets me create an unique identifier for future interactions, if no alias is set then the contract's address can be used as identifier
4. By default Nile works on local, but you can pass `--network mainnet` to deploy directly to a public chain! Notice that `mainnet` refers to StarkNet main chain, that's settled on Goerli testnet of Ethereum ([mainnet deployment this month!](https://medium.com/starkware/starknet-alpha-is-coming-to-mainnet-b825829eaf32))

### `setup`
You can find an exemple `.env` file in `example.env`. These are private keys only to be used for testing and never in production.
Copy link
Contributor

Choose a reason for hiding this comment

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

exemple -> example


```sh
nile setup PKEY1

🚀 Deploying Account
🌕 artifacts/Account.json successfully deployed to 0x07db6b52c8ab888183277bc6411c400136fe566c0eebfb96fffa559b2e60e794
📦 Registering deployment as account-0 in localhost.deployments.txt
Invoke transaction was sent.
Contract address: 0x07db6b52c8ab888183277bc6411c400136fe566c0eebfb96fffa559b2e60e794
Transaction hash: 0x17
```

A few things to notice here:

1. `nile set <env_var>` looks for an environement variable with the same name whose value is a private key
Copy link
Contributor

Choose a reason for hiding this comment

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

there's no nile set command :)

2. This created a `localhost.accounts.json` file storing all data related to accounts management

### `raw-execute`
Execute a transaction through the `Account` associated with the private key used. The syntax is:

```sh
nile raw-execute <env_signer> <contract_address> <contract_method> <args>
```

```sh
nile raw-execute PKEY1 0x0342e...4de4e0 transfer_ownership 0x07db6...60e794

Invoke transaction was sent.
Contract address: 0x03420417e09260947e3412d48952858a376f2d3ddde4e49f5981a2e41f4de4e0
Transaction hash: 0x1c
```

### `send`
Acts like `raw-execute` with the exception you can use it like you would use `nile invoke`.
Execute a transaction through the `Account` associated with the private key used. The syntax is:

```sh
nile send <env_signer> <contract_identifier> <contract_method> [PARAM_1, PARAM2...]
```

```sh
nile send PKEY1 ownable0 transfer_ownership 0x07db6...60e794

Invoke transaction was sent.
Contract address: 0x07db6b52c8ab888183277bc6411c400136fe566c0eebfb96fffa559b2e60e794
Transaction hash: 0x1c
```

### `call` and `invoke`
Using `call` and `invoke`, we can perform read and write operations against our local node (or public one using the `--network mainnet` parameter). The syntax is:

Expand Down
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PKEY1=1234
PKEY2=5678
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ python_requires = >=3.7
install_requires =
click>=8.0,<9.0
importlib-metadata>=4.0
python-dotenv>=0.19.2

[options.packages.find]
where = src
Expand Down
48 changes: 48 additions & 0 deletions src/nile/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""nile common module."""
import json
import os

from nile.common import ACCOUNTS_FILENAME


def register(pubkey, address, index, network):
"""Register a new account."""
file = f"{network}.{ACCOUNTS_FILENAME}"

if exists(pubkey, network):
raise Exception(f"account-{index} already exists in {file}")

with open(file, "r") as fp:
accounts = json.load(fp)
accounts[pubkey] = {"address": address, "index": index}
with open(file, "w") as file:
json.dump(accounts, file)


def exists(pubkey, network):
"""Return whether an account exists or not."""
foo = next(load(pubkey, network), None)
return foo is not None


def load(pubkey, network):
"""Load account that matches a pubkey."""
file = f"{network}.{ACCOUNTS_FILENAME}"

if not os.path.exists(file):
with open(file, "w") as fp:
json.dump({}, fp)

with open(file) as fp:
accounts = json.load(fp)
if pubkey in accounts:
yield accounts[pubkey]


def current_index(network):
"""Return the length of the accounts. Used as the next index."""
file = f"{network}.{ACCOUNTS_FILENAME}"

with open(file) as fp:
accounts = json.load(fp)
return len(accounts.keys())
Loading