forked from 0xSpaceShard/starknet-devnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support predeploying custom accounts (0xSpaceShard#294)
- Loading branch information
Showing
11 changed files
with
226 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Starknet ContractClass wrapper utilities""" | ||
|
||
from dataclasses import dataclass | ||
import os | ||
|
||
from starkware.python.utils import to_bytes | ||
from starkware.starknet.services.api.contract_class import ContractClass | ||
|
||
|
||
@dataclass | ||
class ContractClassWrapper: | ||
"""Wrapper of ContractClass""" | ||
|
||
contract_class: ContractClass | ||
hash_bytes: bytes | ||
|
||
|
||
DEFAULT_ACCOUNT_PATH = os.path.abspath( | ||
os.path.join( | ||
__file__, | ||
os.pardir, | ||
"accounts_artifacts", | ||
"OpenZeppelin", | ||
"0.4.0b-fork", | ||
"Account.cairo", | ||
"Account.json", | ||
) | ||
) | ||
DEFAULT_ACCOUNT_HASH_BYTES = to_bytes( | ||
250058203962332945652607154704986145054927159797127109843768594742871092378 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""Test custom account""" | ||
|
||
import subprocess | ||
import os | ||
import pytest | ||
|
||
from starkware.starknet.core.os.class_hash import compute_class_hash | ||
from starkware.starknet.services.api.contract_class import ContractClass | ||
|
||
from .account import invoke | ||
from .shared import ( | ||
ABI_PATH, | ||
CONTRACT_PATH, | ||
PREDEPLOY_ACCOUNT_CLI_ARGS, | ||
PREDEPLOYED_ACCOUNT_ADDRESS, | ||
PREDEPLOYED_ACCOUNT_PRIVATE_KEY, | ||
) | ||
from .util import ( | ||
call, | ||
deploy, | ||
devnet_in_background, | ||
DevnetBackgroundProc, | ||
get_class_hash_at, | ||
load_file_content, | ||
) | ||
|
||
ACTIVE_DEVNET = DevnetBackgroundProc() | ||
|
||
NON_EXISTENT_PATH = "most-certainly-non-existent-path.txt" | ||
DIR_PATH = os.path.abspath(os.path.join(__file__, os.pardir)) | ||
MISSING_METHOD_PATH = os.path.join( | ||
__file__, os.pardir, "custom_account_missing_method.json" | ||
) | ||
CORRECT_PATH = os.path.join(__file__, os.pardir, "custom_account.json") | ||
|
||
|
||
@pytest.mark.account_custom | ||
@pytest.mark.parametrize( | ||
"class_path, expected_error", | ||
[ | ||
( | ||
NON_EXISTENT_PATH, | ||
f"Error: {os.path.abspath(NON_EXISTENT_PATH)} is not a valid file\n", | ||
), | ||
(DIR_PATH, f"Error: {DIR_PATH} is not a valid file\n"), | ||
(__file__, f"Error: {__file__} is not a valid JSON file\n"), | ||
( | ||
ABI_PATH, | ||
f"Error: {os.path.abspath(ABI_PATH)} is not a valid contract class artifact\n", | ||
), | ||
( | ||
MISSING_METHOD_PATH, | ||
f"Error: {os.path.abspath(MISSING_METHOD_PATH)} is missing account methods: __validate_declare__\n", | ||
), | ||
], | ||
) | ||
def test_invalid_path(class_path: str, expected_error: str): | ||
"""Test behavior on providing nonexistent path""" | ||
proc = ACTIVE_DEVNET.start("--account-class", class_path, stderr=subprocess.PIPE) | ||
assert proc.returncode == 1 | ||
assert proc.stderr.read().decode("utf-8") == expected_error | ||
|
||
|
||
@pytest.mark.account_custom | ||
@devnet_in_background("--account-class", CORRECT_PATH, *PREDEPLOY_ACCOUNT_CLI_ARGS) | ||
def test_providing_correct_account_class(): | ||
"""Test behavior if correct custom account provided""" | ||
fetched_class_hash = int(get_class_hash_at(PREDEPLOYED_ACCOUNT_ADDRESS), 16) | ||
|
||
expected_contract_class = ContractClass.loads( | ||
load_file_content("custom_account.json") | ||
) | ||
assert fetched_class_hash == compute_class_hash(expected_contract_class) | ||
|
||
deploy_info = deploy(CONTRACT_PATH, ["0"]) | ||
invoke( | ||
calls=[(deploy_info["address"], "increase_balance", [10, 20])], | ||
account_address=PREDEPLOYED_ACCOUNT_ADDRESS, | ||
private_key=PREDEPLOYED_ACCOUNT_PRIVATE_KEY, | ||
) | ||
increased_value = call( | ||
function="get_balance", address=deploy_info["address"], abi_path=ABI_PATH | ||
) | ||
|
||
assert increased_value == "30" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.