Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

feat: adding set timestamp method to starknet provider #46

Merged
merged 24 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 4 additions & 1 deletion ape_starknet/accounts/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def _import(cli_ctx, alias, network, address, keyfile):
container = _get_container(cli_ctx)
if alias in container.aliases:
existing_account = container.load(alias)
if existing_account.get_deployment(network):

if existing_account.get_deployment(network) or not isinstance(
existing_account, StarknetKeyfileAccount
):
cli_ctx.abort(f"Account already imported with '{network}' network.")

click.echo(f"Importing existing account to network '{network}'.")
Expand Down
12 changes: 12 additions & 0 deletions ape_starknet/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urlparse
from urllib.request import urlopen

import requests
from ape.api import BlockAPI, ProviderAPI, ReceiptAPI, SubprocessProvider, TransactionAPI
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.contracts import ContractInstance
Expand Down Expand Up @@ -312,6 +313,17 @@ def prepare_transaction(self, txn: TransactionAPI) -> TransactionAPI:

return txn

def set_timestamp(self, new_timestamp: int):
pending_timestamp = self.get_block("pending").timestamp
seconds_to_increase = new_timestamp - pending_timestamp
response = requests.post(
url=f"{self.uri}/increase_time", json={"time": seconds_to_increase}
)
response.raise_for_status()
response_data = response.json()
if "timestamp_increased_by" not in response_data:
raise ProviderError(response_data)

def get_virtual_machine_error(self, exception: Exception) -> VirtualMachineError:
return get_virtual_machine_error(exception) or VirtualMachineError(base_err=exception)

Expand Down
9 changes: 9 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import time
from starkware.starknet.public.abi import get_selector_from_name # type: ignore

from ape_starknet.utils import is_checksum_address
Expand Down Expand Up @@ -76,3 +77,11 @@ def test_get_transactions_by_block(provider, account, contract):
expected_nonce,
]
assert transactions[0].data == expected_data


def test_set_timestamp(provider, account, contract):
start_time = provider.get_block("pending").timestamp
provider.set_timestamp(start_time + 8600)
contract.increase_balance(account.address, 123, sender=account)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just stole this from a test above

curr_time = time.time()
assert pytest.approx(curr_time + 8600) == provider.get_block("latest").timestamp
14 changes: 6 additions & 8 deletions tests/integration/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
NEW_ALIAS = f"{ALIAS}new"


@pytest.fixture
@pytest.fixture(scope="module")
def accounts_runner(ape_cli):
return ApeStarknetCliRunner(ape_cli, ["starknet", "accounts"])


@pytest.fixture
@pytest.fixture(scope="module")
def root_accounts_runner(ape_cli):
return ApeStarknetCliRunner(ape_cli, ["accounts"])


@pytest.fixture
def deployed_account(account_container):
account_container.deploy_account(NEW_ALIAS)
yield account_container.load(NEW_ALIAS)
account_container.delete_account(NEW_ALIAS)
@pytest.fixture(scope="module")
def dev_account(account_container):
return account_container.test_accounts[3]


@pytest.fixture
@pytest.fixture(scope="module")
def argent_x_backup(argent_x_key_file_account_data):
with tempfile.TemporaryDirectory() as temp_dir:
key_file_path = Path(temp_dir) / "argent-x-backup.json"
Expand Down