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

Feature/predeployed accounts endpoint #138

Merged
merged 11 commits into from
Jun 24, 2022
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,27 @@ const account = await starknet.getAccountFromAddress(
);
```

The balance of an account can be checked using:
### Fetch predeployed accounts

```
GET /predeployed_accounts
```

Response:

```
[
{
"initial_balance": 1e+21,
"address": "0x7c3e2...",
"private_key": "0x6160...",
"public_key": "0x6a5540..."
},
...
]
```

### Fetch account balance

```
GET /account_balance?address=<HEX_ADDRESS>
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ crypto-cpp-py = "~1.0.4"
pylint = "~2.12.2"
web3 = "~5.28.0"
psutil = "~5.9.1"
jsonschema = "~3.2.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
9 changes: 9 additions & 0 deletions starknet_devnet/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def get_contract_class(cls):
cls.CONTRACT_CLASS = ContractClass.load(load_nearby_contract(cls.CONTRACT_PATH))
return cls.CONTRACT_CLASS

def to_json(self):
"""Return json account"""
return {
"initial_balance": self.initial_balance,
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
"private_key": hex(self.private_key),
"public_key": hex(self.public_key),
"address": hex(self.address)
}

async def deploy(self, starknet: Starknet) -> StarknetContract:
"""Deploy this account."""
account_carried_state = starknet.state.state.contract_states[self.address]
Expand Down
6 changes: 6 additions & 0 deletions starknet_devnet/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ async def get_balance():
"amount": balance,
"unit": "wei"
})

@base.route("/predeployed_accounts", methods=["GET"])
def get_predeployed_accounts():
"""Get predeployed accounts"""
accounts = state.starknet_wrapper.accounts
return jsonify([account.to_json() for account in accounts])
22 changes: 22 additions & 0 deletions test/support/assertions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
from os.path import join, dirname, abspath
from jsonschema import validate, RefResolver
tabaktoni marked this conversation as resolved.
Show resolved Hide resolved


def assert_valid_schema(data, schema_file):
"""Checks whether the given data matches the schema"""

schema = _load_json_schema(schema_file)
schema_dir = join(dirname(__file__), "schemas")
resolver = RefResolver(base_uri="file://" + schema_dir + "/", referrer=schema)
return validate(data, schema, resolver=resolver)


def _load_json_schema(filename):
"""Loads the given schema file"""

relative_path = join("schemas", filename)
absolute_path = join(dirname(__file__), relative_path)

with open(absolute_path) as schema_file:
return json.loads(schema_file.read())
115 changes: 115 additions & 0 deletions test/support/schemas/predeployed_accounts_fixed_seed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Predeployed accounts fixed value response schema",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"address": {
"type": "string",
"enum": [
"0xc7608a2ba18f3af1a7d95ac07b7772ca5d2e84c6191e1ccb99a74521b4eda4"
]
},
"initial_balance": {
"type": "number",
"enum": [
1000
]
},
"private_key": {
"type": "string",
"enum": [
"0xc4da537c1651ddae44867db30d67b366"
]
},
"public_key": {
"type": "string",
"enum": [
"0x60dea6c1228f1db4ca1f9db11c01b6e9cce5e627f7181dcaa27d69cbdbe57b5"
]
}
},
"required": [
"address",
"initial_balance",
"private_key",
"public_key"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"address": {
"type": "string",
"enum": [
"0x5d13db75ca726e8b45484c7c5692493e41f883e20888baf6cfeb9444938ba89"
]
},
"initial_balance": {
"type": "number",
"enum": [
1000
]
},
"private_key": {
"type": "string",
"enum": [
"0xd6a82a951b923e0a443cdef36840ff07"
]
},
"public_key": {
"type": "string",
"enum": [
"0x5a91f0ea25312accb20d8041b12260bff31a3490e5730a690b0ec8fe10ffbb"
]
}
},
"required": [
"address",
"initial_balance",
"private_key",
"public_key"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"address": {
"type": "string",
"enum": [
"0x14271050ae36fb1a217a5d9a0eb73b76ccaee633c5acc7008ceeace3c18694f"
]
},
"initial_balance": {
"type": "number",
"enum": [
1000
]
},
"private_key": {
"type": "string",
"enum": [
"0x610e4ad509c47055dff4948fe6b4f832"
]
},
"public_key": {
"type": "string",
"enum": [
"0x2b45af00df833ea1a4895c49a18ebd84309b79d658cae05e274a7b1cac47016"
]
}
},
"required": [
"address",
"initial_balance",
"private_key",
"public_key"
],
"additionalProperties": false
}
]
}
27 changes: 26 additions & 1 deletion test/test_account_predeployed.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
"""Predeployed account tests"""

from test.settings import GATEWAY_URL

import pytest
import requests

from starkware.starknet.core.os.class_hash import compute_class_hash

from starknet_devnet.account import Account
from .util import assert_equal
from .util import assert_equal, devnet_in_background
from .support.assertions import assert_valid_schema

ACCOUNTS_SEED_DEVNET_ARGS = [
"--accounts",
"3",
"--seed",
"123",
"--gas-price",
"100",
"--initial-balance",
"1_000",
]


@pytest.mark.account_predeployed
def test_precomputed_contract_hash():
"""Test if the precomputed hash of the account contract is correct."""
recalculated_hash = compute_class_hash(contract_class=Account.get_contract_class())
assert_equal(recalculated_hash, Account.HASH)


@pytest.mark.account_predeployed
@devnet_in_background(*ACCOUNTS_SEED_DEVNET_ARGS)
def test_predeployed_accounts_predefined_values():
tabaktoni marked this conversation as resolved.
Show resolved Hide resolved
"""Test if --account --seed --initial-balance return exact calculated values"""
response = requests.get(f"{GATEWAY_URL}/predeployed_accounts")
assert response.status_code == 200
assert_valid_schema(response.json(), "predeployed_accounts_fixed_seed.json")