Starknet SDK for Python
📘 Documentation
⚙️ Installation
Installation varies between operating systems.
See our documentation on complete instructions
💨 Quickstart
Using FullNodeClient
A Client is a facade for interacting with Starknet.
FullNodeClient is a client which interacts with a Starknet full nodes like Pathfinder, Papyrus or Juno.
It supports read and write operations, like querying the blockchain state or adding new transactions.
from starknet_py.net.full_node_client import FullNodeClient
node_url = "https://your.node.url"
client = FullNodeClient(node_url=node_url)
call_result = await client.get_block(block_number=1)
The default interface is asynchronous. Although it is the recommended way of using starknet.py, you can also use a synchronous version. It might be helpful to play with Starknet directly in python interpreter.
node_url = "https://your.node.url"
client = FullNodeClient(node_url=node_url)
call_result = client.get_block_sync(block_number=1)
You can check out all of the FullNodeClient’s methods here: FullNodeClient.
Creating Account
Account is the default implementation of BaseAccount interface.
It supports an account contract which proxies the calls to other contracts on Starknet.
Account can be created in two ways:
- By constructor (It is required to provide an
address
and either key_pair
or signer
).
- By static method
Account.deploy_account_v3
Additionally, you can use the sncast tool to create an account,
which will automatically be saved to a file.
There are some examples how to do it:
from starknet_py.net.account.account import Account
from starknet_py.net.full_node_client import FullNodeClient
from starknet_py.net.models.chains import StarknetChainId
from starknet_py.net.signer.key_pair import KeyPair
from starknet_py.net.signer.stark_curve_signer import StarkCurveSigner
client = FullNodeClient(node_url="https://your.node.url")
account = Account(
client=client,
address="0x4321",
key_pair=KeyPair(private_key=654, public_key=321),
chain=StarknetChainId.SEPOLIA,
)
key_pair = KeyPair.from_private_key(key=123)
key_pair = KeyPair.from_private_key(key="0x123")
signer = StarkCurveSigner("0x1234", key_pair, StarknetChainId.SEPOLIA)
account = Account(
client=client, address="0x1234", signer=signer, chain=StarknetChainId.SEPOLIA
)
Using Account
Example usage:
from starknet_py.contract import Contract
from starknet_py.net.client_models import ResourceBounds, ResourceBoundsMapping
resource_bounds = ResourceBoundsMapping(
l1_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l2_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l1_data_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
)
declare_result = await Contract.declare_v3(
account,
compiled_contract=compiled_contract,
compiled_class_hash=class_hash,
resource_bounds=resource_bounds,
)
await declare_result.wait_for_acceptance()
deploy_result = await declare_result.deploy_v3(
resource_bounds=resource_bounds,
)
await deploy_result.wait_for_acceptance()
map_contract = deploy_result.deployed_contract
k, v = 13, 4324
await (
await map_contract.functions["put"].invoke_v3(
k,
v,
resource_bounds=resource_bounds,
)
).wait_for_acceptance()
(resp,) = await map_contract.functions["get"].call(k)
calls = [
map_contract.functions["put"].prepare_invoke_v3(key=10, value=20),
map_contract.functions["put"].prepare_invoke_v3(key=30, value=40),
]
transaction_response = await account.execute_v3(
calls=calls,
resource_bounds=resource_bounds,
)
await account.client.wait_for_tx(transaction_response.transaction_hash)
Using Contract
Contract makes interacting with contracts deployed on Starknet much easier:
from starknet_py.contract import Contract
from starknet_py.net.client_models import ResourceBounds, ResourceBoundsMapping
contract_address = (
"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b"
)
key = 1234
contract = await Contract.from_address(address=contract_address, provider=account)
contract = Contract(
address=contract_address,
abi=abi,
provider=account,
cairo_version=1,
)
resource_bounds = ResourceBoundsMapping(
l1_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l2_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l1_data_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
)
invocation = await contract.functions["put"].invoke_v3(
key,
7,
resource_bounds=resource_bounds,
)
await invocation.wait_for_acceptance()
(saved,) = await contract.functions["get"].call(key)
To check if invoke succeeded use wait_for_acceptance
on InvokeResult and get its status.
Although asynchronous API is recommended, you can also use Contract’s synchronous API:
from starknet_py.contract import Contract
from starknet_py.net.client_models import ResourceBounds, ResourceBoundsMapping
contract_address = (
"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b"
)
key = 1234
contract = Contract.from_address_sync(address=contract_address, provider=account)
resource_bounds = ResourceBoundsMapping(
l1_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l2_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
l1_data_gas=ResourceBounds(max_amount=int(1e5), max_price_per_unit=int(1e13)),
)
invocation = contract.functions["put"].invoke_v3_sync(key, 7, resource_bounds=resource_bounds)
invocation.wait_for_acceptance_sync()
(saved,) = contract.functions["get"].call_sync(key)
Contract automatically serializes values to Cairo calldata. This includes adding array lengths automatically.
See more info in Serialization.
Quickstart in docs - click here.