Skip to content

Commit

Permalink
Support Sepolia network (#1256)
Browse files Browse the repository at this point in the history
* Rename `TESTNET` to `GOERLI` and add `SEPOLIA` chain id

* Rename fixtures in tests_on_networks

* Set correct return type for `get_chain_id`

* Add basic tests for Sepolia

* Update docs and test-variables file

* Update env variables in github workflow

* Update `_default_token_address()` in `Account` class
  • Loading branch information
ddoktorski authored Jan 18, 2024
1 parent b12812e commit db18a88
Show file tree
Hide file tree
Showing 28 changed files with 297 additions and 192 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,14 @@ jobs:
strategy:
fail-fast: false
env:
INTEGRATION_RPC_URL: ${{ secrets.INTEGRATION_RPC_URL }}
TESTNET_RPC_URL: ${{ secrets.TESTNET_RPC_URL }}
INTEGRATION_ACCOUNT_PRIVATE_KEY: ${{ secrets.INTEGRATION_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_ACCOUNT_ADDRESS: ${{ secrets.INTEGRATION_ACCOUNT_ADDRESS }}
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
TESTNET_ACCOUNT_ADDRESS: ${{ secrets.TESTNET_ACCOUNT_ADDRESS }}
GOERLI_INTEGRATION_RPC_URL: ${{ secrets.INTEGRATION_RPC_URL }}
GOERLI_TESTNET_RPC_URL: ${{ secrets.TESTNET_RPC_URL }}
SEPOLIA_INTEGRATION_RPC_URL: ${{ secrets.SEPOLIA_INTEGRATION_RPC_URL }}
SEPOLIA_TESTNET_RPC_URL: ${{ secrets.SEPOLIA_TESTNET_RPC_URL }}
GOERLI_INTEGRATION_ACCOUNT_PRIVATE_KEY: ${{ secrets.INTEGRATION_ACCOUNT_PRIVATE_KEY }}
GOERLI_INTEGRATION_ACCOUNT_ADDRESS: ${{ secrets.INTEGRATION_ACCOUNT_ADDRESS }}
GOERLI_TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
GOERLI_TESTNET_ACCOUNT_ADDRESS: ${{ secrets.TESTNET_ACCOUNT_ADDRESS }}
steps:
- uses: actions/checkout@v3

Expand Down
25 changes: 17 additions & 8 deletions docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ If you choose to install `starknet-devnet-rs <https://github.com/0xSpaceShard/st

In order to be able to run tests on testnet and integration networks (``starknet_py/tests/e2e/tests_on_networks/``), you must set some environmental variables:

- ``INTEGRATION_RPC_URL``
- ``TESTNET_RPC_URL``
- ``INTEGRATION_ACCOUNT_PRIVATE_KEY``
- ``INTEGRATION_ACCOUNT_ADDRESS``
- ``TESTNET_ACCOUNT_PRIVATE_KEY``
- ``TESTNET_ACCOUNT_ADDRESS``

The best way to do that is to create ``test-variables.env`` file in ``starknet_py/tests/e2e/`` directory, so they can be loaded by the ``python-dotenv`` library.
- ``GOERLI_INTEGRATION_RPC_URL``
- ``GOERLI_TESTNET_RPC_URL``
- ``SEPOLIA_INTEGRATION_RPC_URL``
- ``SEPOLIA_TESTNET_RPC_URL``
- ``GOERLI_INTEGRATION_ACCOUNT_PRIVATE_KEY``
- ``GOERLI_INTEGRATION_ACCOUNT_ADDRESS``
- ``GOERLI_TESTNET_ACCOUNT_PRIVATE_KEY``
- ``GOERLI_TESTNET_ACCOUNT_ADDRESS``

The existing tests don't execute any invoke transactions on the Sepolia networks. If you plan to incorporate such tests, please also set the following environment variables:

- ``SEPOLIA_INTEGRATION_ACCOUNT_PRIVATE_KEY``
- ``SEPOLIA_INTEGRATION_ACCOUNT_ADDRESS``
- ``SEPOLIA_TESTNET_ACCOUNT_PRIVATE_KEY``
- ``SEPOLIA_TESTNET_ACCOUNT_ADDRESS``

The best way to set environment variables is to create ``test-variables.env`` file in ``starknet_py/tests/e2e/`` directory, so they can be loaded by the ``python-dotenv`` library.
You can find an example file ``test-variables.env.template`` in the same directory with the format of how it should look like.

.. code-block:: bash
Expand Down
8 changes: 4 additions & 4 deletions starknet_py/hash/transaction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_resource_bounds():
0x64,
[],
0x0,
StarknetChainId.TESTNET,
StarknetChainId.GOERLI,
],
0x7D260744DE9D8C55E7675A34512D1951A7B262C79E685D26599EDD2948DE959,
),
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_compute_invoke_transaction_hash(data, expected_hash):
(
{
"address": 0x52125C1E043126C637D1436D9551EF6C4F6E3E36945676BBD716A56E3A41B7A,
"chain_id": StarknetChainId.TESTNET,
"chain_id": StarknetChainId.GOERLI,
"nonce": 0x675,
"tip": 0x0,
"paymaster_data": [],
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_compute_declare_v3_transaction_hash(
(
{
"address": 0x35ACD6DD6C5045D18CA6D0192AF46B335A5402C02D41F46E4E77EA2C951D9A3,
"chain_id": StarknetChainId.TESTNET,
"chain_id": StarknetChainId.GOERLI,
"nonce": 0x5,
"tip": 0x0,
"paymaster_data": [],
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_compute_invoke_v3_transaction_hash(
(
{
"address": 0x2FAB82E4AEF1D8664874E1F194951856D48463C3E6BF9A8C68E234A629A6F50,
"chain_id": StarknetChainId.TESTNET,
"chain_id": StarknetChainId.GOERLI,
"nonce": 0x0,
"tip": 0x0,
"paymaster_data": [],
Expand Down
8 changes: 6 additions & 2 deletions starknet_py/net/account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,9 @@ async def deploy_account(
)

if chain in (
StarknetChainId.TESTNET,
StarknetChainId.SEPOLIA_TESTNET,
StarknetChainId.SEPOLIA_INTEGRATION,
StarknetChainId.GOERLI,
StarknetChainId.MAINNET,
):
balance = await account.get_balance()
Expand All @@ -683,7 +685,9 @@ def _default_token_address_for_chain(
self, chain_id: Optional[StarknetChainId] = None
) -> str:
if (chain_id or self._chain_id) not in [
StarknetChainId.TESTNET,
StarknetChainId.SEPOLIA_TESTNET,
StarknetChainId.SEPOLIA_INTEGRATION,
StarknetChainId.GOERLI,
StarknetChainId.MAINNET,
]:
raise ValueError(
Expand Down
16 changes: 8 additions & 8 deletions starknet_py/net/account/account_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
from starknet_py.net.account.account import Account
from starknet_py.net.full_node_client import FullNodeClient
from starknet_py.net.models import StarknetChainId, parse_address
from starknet_py.net.networks import MAINNET, TESTNET
from starknet_py.net.networks import GOERLI, MAINNET
from starknet_py.net.signer.stark_curve_signer import KeyPair, StarkCurveSigner
from starknet_py.tests.e2e.fixtures.constants import MAX_FEE


@pytest.mark.asyncio
@pytest.mark.parametrize("net", (TESTNET, MAINNET))
@pytest.mark.parametrize("net", (GOERLI, MAINNET))
async def test_get_balance_default_token_address(net):
client = FullNodeClient(node_url=net + "/rpc")
acc_client = Account(
client=client,
address="0x123",
key_pair=KeyPair(123, 456),
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)

with patch(
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_create_account():
address=0x1,
client=FullNodeClient(node_url=""),
key_pair=key_pair,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)

assert account.address == 0x1
Expand All @@ -69,7 +69,7 @@ def test_create_account_from_signer():
signer = StarkCurveSigner(
account_address=0x1,
key_pair=KeyPair.from_private_key(0x111),
chain_id=StarknetChainId.TESTNET,
chain_id=StarknetChainId.GOERLI,
)
account = Account(address=0x1, client=FullNodeClient(node_url=""), signer=signer)

Expand All @@ -94,7 +94,7 @@ def test_create_account_raises_on_no_keypair_and_signer():
Account(
address=0x1,
client=FullNodeClient(node_url=""),
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)


Expand All @@ -105,11 +105,11 @@ def test_create_account_raises_on_both_keypair_and_signer():
Account(
address=0x1,
client=FullNodeClient(node_url=""),
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
key_pair=KeyPair.from_private_key(0x111),
signer=StarkCurveSigner(
account_address=0x1,
key_pair=KeyPair.from_private_key(0x11),
chain_id=StarknetChainId.TESTNET,
chain_id=StarknetChainId.GOERLI,
),
)
2 changes: 1 addition & 1 deletion starknet_py/net/full_node_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ async def get_block_hash_and_number(self) -> BlockHashAndNumber:
res = await self._client.call(method_name="blockHashAndNumber", params={})
return cast(BlockHashAndNumber, BlockHashAndNumberSchema().load(res))

async def get_chain_id(self) -> int:
async def get_chain_id(self) -> str:
"""Return the currently configured Starknet chain id"""
return await self._client.call(method_name="chainId", params={})

Expand Down
16 changes: 13 additions & 3 deletions starknet_py/net/models/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from typing import Optional

from starknet_py.common import int_from_bytes
from starknet_py.net.networks import MAINNET, TESTNET, Network
from starknet_py.net.networks import (
GOERLI,
MAINNET,
SEPOLIA_INTEGRATION,
SEPOLIA_TESTNET,
Network,
)


class StarknetChainId(IntEnum):
Expand All @@ -11,15 +17,19 @@ class StarknetChainId(IntEnum):
"""

MAINNET = int_from_bytes(b"SN_MAIN")
TESTNET = int_from_bytes(b"SN_GOERLI")
GOERLI = int_from_bytes(b"SN_GOERLI")
SEPOLIA_TESTNET = int_from_bytes(b"SN_SEPOLIA")
SEPOLIA_INTEGRATION = int_from_bytes(b"SN_INTEGRATION_SEPOLIA")


def chain_from_network(
net: Network, chain: Optional[StarknetChainId] = None
) -> StarknetChainId:
mapping = {
MAINNET: StarknetChainId.MAINNET,
TESTNET: StarknetChainId.TESTNET,
GOERLI: StarknetChainId.GOERLI,
SEPOLIA_TESTNET: StarknetChainId.SEPOLIA_TESTNET,
SEPOLIA_INTEGRATION: StarknetChainId.SEPOLIA_INTEGRATION,
}

if isinstance(net, str) and net in mapping:
Expand Down
11 changes: 8 additions & 3 deletions starknet_py/net/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
from starknet_py.constants import FEE_CONTRACT_ADDRESS

MAINNET = "mainnet"
TESTNET = "testnet"
PredefinedNetwork = Literal["mainnet", "testnet"]
GOERLI = "goerli"
SEPOLIA_TESTNET = "sepolia_testnet"
SEPOLIA_INTEGRATION = "sepolia_integration"

PredefinedNetwork = Literal[
"mainnet", "goerli", "sepolia_testnet", "sepolia_integration"
]

Network = Union[PredefinedNetwork, str]


def default_token_address_for_network(net: Network) -> str:
if net not in [TESTNET, MAINNET]:
if net not in [MAINNET, GOERLI, SEPOLIA_TESTNET, SEPOLIA_INTEGRATION]:
raise ValueError(
"Argument token_address must be specified when using a custom net address"
)
Expand Down
2 changes: 1 addition & 1 deletion starknet_py/net/signer/test_stark_curve_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_sign_transaction(transaction):
signer = StarkCurveSigner(
account_address=0x1,
key_pair=KeyPair.from_private_key(0x1),
chain_id=StarknetChainId.TESTNET,
chain_id=StarknetChainId.GOERLI,
)

signature = signer.sign_transaction(transaction)
Expand Down
14 changes: 7 additions & 7 deletions starknet_py/tests/e2e/account/account_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ async def test_deploy_account(client, deploy_account_details_factory, map_contra
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=int(1e16),
)
await deploy_result.wait_for_acceptance()
Expand Down Expand Up @@ -493,7 +493,7 @@ async def test_deploy_account_raises_on_incorrect_address(
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=MAX_FEE,
)

Expand All @@ -519,7 +519,7 @@ async def test_deploy_account_raises_on_no_enough_funds(
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=MAX_FEE,
)

Expand All @@ -546,7 +546,7 @@ async def test_deploy_account_passes_on_enough_funds(
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=MAX_FEE,
)

Expand Down Expand Up @@ -579,7 +579,7 @@ async def test_deploy_account_uses_custom_calldata(
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
constructor_calldata=calldata,
max_fee=int(1e16),
)
Expand All @@ -601,7 +601,7 @@ async def test_sign_deploy_account_tx_for_fee_estimation(
address=address,
client=client,
key_pair=key_pair,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)

transaction = await account.sign_deploy_account_v1_transaction(
Expand Down Expand Up @@ -675,7 +675,7 @@ async def test_argent_cairo1_account_deploy(
key_pair=key_pair,
client=client,
constructor_calldata=[key_pair.public_key, 0],
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=int(1e16),
)
await deploy_result.wait_for_acceptance()
Expand Down
6 changes: 3 additions & 3 deletions starknet_py/tests/e2e/client/full_node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def test_get_transaction_receipt_deploy_account(
salt=salt,
key_pair=key_pair,
client=client,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
max_fee=int(1e16),
)
await deploy_result.wait_for_acceptance()
Expand Down Expand Up @@ -368,7 +368,7 @@ async def test_get_block_hash_and_number(client):
async def test_get_chain_id(client):
chain_id = await client.get_chain_id()

assert chain_id == hex(StarknetChainId.TESTNET.value)
assert chain_id == hex(StarknetChainId.GOERLI.value)


@pytest.mark.asyncio
Expand Down Expand Up @@ -543,7 +543,7 @@ async def test_simulate_transactions_deploy_account(
address=address,
client=client,
key_pair=key_pair,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)
deploy_account_tx = await account.sign_deploy_account_v1_transaction(
class_hash=class_hash,
Expand Down
4 changes: 2 additions & 2 deletions starknet_py/tests/e2e/deploy_account/deploy_account_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def test_general_flow(client, deploy_account_details_factory):
address=address,
client=client,
key_pair=key_pair,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)

deploy_account_tx = await account.sign_deploy_account_v1_transaction(
Expand All @@ -36,7 +36,7 @@ async def test_deploy_account_v3(client, deploy_account_details_factory):
address=address,
client=client,
key_pair=key_pair,
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)

deploy_account_tx = await account.sign_deploy_account_v3_transaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ async def test_deploy_prefunded_account(

# Define the client to be used to interact with Starknet
client = FullNodeClient(node_url="your.node.url")
chain = StarknetChainId.TESTNET
chain = StarknetChainId.GOERLI
# docs: end

client = full_node_client_fixture
chain = chain_from_network(net=network, chain=StarknetChainId.TESTNET)
chain = chain_from_network(net=network, chain=StarknetChainId.GOERLI)
# docs: start

# Use `Account.deploy_account` static method to deploy an account
Expand Down
2 changes: 1 addition & 1 deletion starknet_py/tests/e2e/docs/code_examples/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_init():
address=0x123,
client=FullNodeClient(node_url="your.node.url"),
key_pair=KeyPair(12, 34),
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
)
# docs-end: init

Expand Down
2 changes: 1 addition & 1 deletion starknet_py/tests/e2e/docs/code_examples/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_init():
address=0x321,
client=FullNodeClient(node_url="your.node.url"),
key_pair=KeyPair(12, 34),
chain=StarknetChainId.TESTNET,
chain=StarknetChainId.GOERLI,
),
)
# docs-end: init
Expand Down
Loading

0 comments on commit db18a88

Please sign in to comment.