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"
full_node_client = FullNodeClient(node_url=node_url)
call_result = await full_node_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"
full_node_client = FullNodeClient(node_url=node_url)
call_result = full_node_client.get_block_sync(block_number=1)
You can check out all of the FullNodeClient’s methods here: FullNodeClient.
Using GatewayClient#
Warning
Gateway / Feeder Gateway API will become deprecated in the future. As a result, GatewayClient won’t work and will eventually be removed. Consider migrating to FullNodeClient.
GatewayClient will make requests directly to Starknet sequencer through gateway or feeder_gateway endpoints. It can be used to either query the blockchain state or add new transactions. It requires information about used network:
from starknet_py.net.gateway_client import GatewayClient
from starknet_py.net.networks import MAINNET, TESTNET
# Use testnet for playing with Starknet
testnet_client = GatewayClient(TESTNET)
# or
testnet_client = GatewayClient("testnet")
mainnet_client = GatewayClient(MAINNET)
# or
mainnet_client = GatewayClient("mainnet")
# Local network
local_network_client = GatewayClient("http://localhost:5000")
call_result = await testnet_client.get_block(
"0x495c670c53e4e76d08292524299de3ba078348d861dd7b2c7cc4933dbc27943"
)
It also has async/sync interface:
synchronous_testnet_client = GatewayClient(TESTNET)
call_result = synchronous_testnet_client.get_block_sync(
"0x495c670c53e4e76d08292524299de3ba078348d861dd7b2c7cc4933dbc27943"
)
You can check out all of the GatewayClient’s methods here: GatewayClient.
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 (address, key_pair and net must be known).
By static method
Account.deploy_account
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.stark_curve_signer import KeyPair
# Creates an instance of account which is already deployed
# Account using transaction version=1 (has __validate__ function)
client = FullNodeClient(node_url="your.node.url")
account = Account(
client=client,
address="0x4321",
key_pair=KeyPair(private_key=654, public_key=321),
chain=StarknetChainId.TESTNET,
)
# There is another way of creating key_pair
key_pair = KeyPair.from_private_key(key=123)
# or
key_pair = KeyPair.from_private_key(key="0x123")
# Instead of providing key_pair it is possible to specify a signer
signer = StarkCurveSigner("0x1234", key_pair, StarknetChainId.TESTNET)
account = Account(client=client, address="0x1234", signer=signer)
Using Account#
Example usage:
from starknet_py.contract import Contract
# Declare and deploy an example contract which implements a simple k-v store.
declare_result = await Contract.declare(
account=account, compiled_contract=map_compiled_contract, max_fee=MAX_FEE
)
await declare_result.wait_for_acceptance()
deploy_result = await declare_result.deploy(max_fee=MAX_FEE)
# Wait until deployment transaction is accepted
await deploy_result.wait_for_acceptance()
# Get deployed contract
map_contract = deploy_result.deployed_contract
k, v = 13, 4324
# Adds a transaction to mutate the state of k-v store. The call goes through account proxy, because we've used
# Account to create the contract object
await (
await map_contract.functions["put"].invoke(k, v, max_fee=int(1e16))
).wait_for_acceptance()
# Retrieves the value, which is equal to 4324 in this case
(resp,) = await map_contract.functions["get"].call(k)
# There is a possibility of invoking the multicall
# Creates a list of prepared function calls
calls = [
map_contract.functions["put"].prepare(key=10, value=20),
map_contract.functions["put"].prepare(key=30, value=40),
]
# Executes only one transaction with prepared calls
transaction_response = await account.execute(calls=calls, max_fee=int(1e16))
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
contract_address = (
"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b"
)
key = 1234
# Create contract from contract's address - Contract will download contract's ABI to know its interface.
contract = await Contract.from_address(address=contract_address, provider=account)
# If the ABI is known, create the contract directly (this is the preferred way).
contract = Contract(
address=contract_address,
abi=abi,
provider=account,
)
# All exposed functions are available at contract.functions.
# Here we invoke a function, creating a new transaction.
invocation = await contract.functions["put"].invoke(key, 7, max_fee=int(1e16))
# Invocation returns InvokeResult object. It exposes a helper for waiting until transaction is accepted.
await invocation.wait_for_acceptance()
# Calling contract's function doesn't create a new transaction, you get the function's result.
(saved,) = await contract.functions["get"].call(key)
# saved = 7 now
Note
To check if invoke succeeds 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
contract_address = (
"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b"
)
key = 1234
contract = Contract.from_address_sync(address=contract_address, provider=account)
invocation = contract.functions["put"].invoke_sync(key, 7, max_fee=int(1e16))
invocation.wait_for_acceptance_sync()
(saved,) = contract.functions["get"].call_sync(key) # 7
Note
Contract automatically serializes values to Cairo calldata. This includes adding array lengths automatically. See more info in Serialization.