starknet-py

repository·development·Indexed 42 days ago

https://github.com/software-mansion/starknet.py

A comprehensive Python SDK for interacting with the Starknet blockchain. It provides high-level abstractions for managing accounts, interacting with smart contracts, and communicating with Starknet full nodes via FullNodeClient. The SDK supports declaring and deploying contracts, executing multicalls for gas optimization, and parsing Starknet ABIs (v0, v1, and v2). Version 0.30.0.

Tokens
27.9K
Snippets
96
Records
191
Agent score
54%

What's inside starknet.py

  1. How proxy resolution works via ProxyChecks

    development

    Because Starknet proxy implementations vary, starknet.py uses a mechanism called ProxyCheck to identify and resolve proxies. Contract.from_address iterates through available proxy_checks to find the implementation's address or class_hash.

    By default, proxy_config includes:

    • ArgentProxyCheck: Resolves Argent Proxy implementations.
    • OpenZeppelinProxyCheck: Resolves OpenZeppelin Proxy implementations.

    Note: StarknetEthProxyCheck has been removed as StarkGate ETH Token is no longer a proxy.

  2. Use custom signing methods with Account

    development

    By default, an Account uses the signing method of OpenZeppelin's account contract. If you need to use a different signing algorithm, you can create an Account instance by providing a custom Signer implementation.

    # Example of creating an Account with a custom Signer implementation
    # (Refer to test_custom_signer.py for specific implementation details)
  3. Use the Universal Deployer Contract (UDC) for deployment

    development

    The Deployer class is used to deploy contracts if you already have an account. starknet.py assumes the UDC implementation is compatible with OpenZeppelin's UDC implementation.

    The Deployer is also designed to work with multicalls, allowing you to deploy a contract and call its methods within the same atomic transaction.

    # Example of deploying with UDC
    # (Refer to test_deploying_with_udc.py for implementation details)
    
    # Example of deploying and calling methods in a single multicall
    # (Refer to test_deploying_in_multicall.py for implementation details)
  4. Data serialization and deserialization

    development

    starknet.py provides mechanisms to serialize Python values into Cairo-compatible values and deserialize Cairo values back into Python types. This is essential for interacting with Starknet contracts via function calls or processing contract events.

    Serialization Mapping (Python to Cairo)

    Expected Cairo typeAccepted Python typesExample Python values
    feltint0, 1213124124 (must be in range [0;P))
    tupleany iterable of matching size(1, 2, (9, 8))
    named tupledict, NamedTuple, or TupleDataclass{"a": 1, "b": 2}
    structdict with keys matching struct{"key_1": 2, "key_2": (1, 2, 3)}
    pointer/arrayany iterable[1, 2, 3] (automatically fills parameter_len)
    uint256int or dict with "low" and "high" keys340282366920938463463374607431768211583 or {"low": 12, "high": 13}

    Deserialization Mapping (Cairo to Python)

    Cairo typePython type
    feltint
    tupletuple
    named tupleTupleDataclass
    structdict
    pointer/arraylist
    uint256int
  5. Use FullNodeClient for Starknet interactions

    development
    The Client class in starknet_py.net.client serves as the base class for all clients interacting with the Starknet network. For most standard use cases, you should use FullNodeClient, which is the primary implementation of this interface. It provides the necessary methods to communicate with a Starknet node to perform network operations.
  6. Prepare transactions with PreparedFunctionCall and PreparedFunctionInvokeV3

    development

    To execute state-changing transactions, you often need to prepare them before sending.

    • PreparedFunctionCall: Used for preparing a standard function call.
    • PreparedFunctionInvokeV3: Used for preparing function invokes specifically for transaction version 1 (which includes a __validate__ function).

    These objects allow you to construct the transaction parameters (calldata, etc.) before signing and broadcasting them via an Account.

  7. Understand the Abi v2 model structure

    development

    The starknet_py.abi.v2.Abi model is the most comprehensive representation of a contract's ABI. It provides access to the following components:

    • functions: The contract's callable functions.
    • constructor: The constructor definition.
    • l1_handler: L1 handlers for cross-layer communication.
    • events: The events emitted by the contract.
    • defined_structures: Custom structures defined in the ABI.
    • defined_enums: Custom enums defined in the ABI.
    • interfaces: Interfaces implemented or used.
    • implementations: Implementation details.

    Each Function and Event within the Abi object contains its own specific metadata and properties.

  8. Handle chain reorganization notifications

    development

    When subscribing to block headers, events, or transaction statuses, the network may undergo reorganizations (e.g., when a block is replaced by a different canonical block). To handle these updates, provide a custom callback function to the on_reorg parameter in your subscription methods.

    async def my_reorg_handler(reorg_info):
        print(f"Chain reorg detected: {reorg_info}")
    
    await client.subscribe_new_heads(on_reorg=my_reorg_handler)
  9. Alternative methods for account creation

    development

    If you cannot use the DeployAccount transaction, there are two other programmatic ways to create an account, though both require an existing account to initiate the process:

    1. Deploy through Cairo syscall: Uses a system call to deploy the account.
    2. Universal Deployer Contract (UDC): Uses a specialized contract to handle deployment.