starknet-py


Namestarknet-py JSON
Version 0.24.2 PyPI version JSON
download
home_pagehttps://github.com/software-mansion/starknet.py
SummaryA python SDK for Starknet
upload_time2024-09-13 10:47:52
maintainerNone
docs_urlNone
authorTomasz Rejowski
requires_python<3.13,>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
    <img src="https://raw.githubusercontent.com/software-mansion/starknet.py/master/graphic.png" alt="starknet.py"/>
</div>
<h2 align="center">Starknet SDK for Python</h2>

<div align="center">

[![codecov](https://codecov.io/gh/software-mansion/starknet.py/branch/master/graph/badge.svg?token=3E54E8RYSL)](https://codecov.io/gh/software-mansion/starknet.py)
[![pypi](https://img.shields.io/pypi/v/starknet.py)](https://pypi.org/project/starknet.py/)
[![build](https://img.shields.io/github/actions/workflow/status/software-mansion/starknet.py/checks.yml)](https://github.com/software-mansion/starknet.py/actions)
[![docs](https://readthedocs.org/projects/starknetpy/badge/?version=latest)](https://starknetpy.readthedocs.io/en/latest/?badge=latest)
[![license](https://img.shields.io/badge/license-MIT-black)](https://github.com/software-mansion/starknet.py/blob/master/LICENSE.txt)
[![stars](https://img.shields.io/github/stars/software-mansion/starknet.py?color=yellow)](https://github.com/software-mansion/starknet.py/stargazers)
[![starkware](https://img.shields.io/badge/powered_by-StarkWare-navy)](https://starkware.co)

</div>

## 📘 Documentation

- [Installation](https://starknetpy.rtfd.io/en/latest/installation.html)
- [Quickstart](https://starknetpy.rtfd.io/en/latest/quickstart.html)
- [Guide](https://starknetpy.rtfd.io/en/latest/guide.html)
- [API](https://starknetpy.rtfd.io/en/latest/api.html)
- [Migration guide](https://starknetpy.readthedocs.io/en/latest/migration_guide.html)

## ⚙️ Installation

Installation varies between operating systems.

[See our documentation on complete instructions](https://starknetpy.rtfd.io/en/latest/installation.html)


## 💨 Quickstart
### Using FullNodeClient
A [Client](https://starknetpy.readthedocs.io/en/latest/api/client.html#client) is a facade for interacting with Starknet. 
[FullNodeClient](https://starknetpy.readthedocs.io/en/latest/api/full_node_client.html#module-starknet_py.net.full_node_client) is a client which interacts with a Starknet full nodes like [Pathfinder](https://github.com/eqlabs/pathfinder), [Papyrus](https://github.com/starkware-libs/papyrus) or [Juno](https://github.com/NethermindEth/juno). 
It supports read and write operations, like querying the blockchain state or adding new transactions.


```python
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.

```python
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](https://starknetpy.readthedocs.io/en/latest/api/full_node_client.html#module-starknet_py.net.full_node_client).

### Creating Account
[Account](https://starknetpy.readthedocs.io/en/latest/api/account.html#starknet_py.net.account.account.Account) is the default implementation of [BaseAccount](https://starknetpy.readthedocs.io/en/latest/api/account.html#starknet_py.net.account.base_account.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 methods `Account.deploy_account_v1` or `Account.deploy_account_v3`

Additionally, you can use the [sncast](https://foundry-rs.github.io/starknet-foundry/starknet/index.html) tool to create an account, 
which will automatically be saved to a file.
There are some examples how to do it:
```python
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
from starknet_py.net.signer.stark_curve_signer import StarkCurveSigner

# Creates an instance of account which is already deployed
# Account using transaction version=1 (has __validate__ function)
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,
)

# 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.SEPOLIA)

account = Account(
    client=client, address="0x1234", signer=signer, chain=StarknetChainId.SEPOLIA
)
```

### Using Account
Example usage:

```python
from starknet_py.contract import Contract
from starknet_py.net.client_models import ResourceBounds
l1_resource_bounds = ResourceBounds(
    max_amount=int(1e5), max_price_per_unit=int(1e13)
)
# Declare and deploy an example contract which implements a simple k-v store.
# Contract.declare_v3 takes string containing a compiled contract (sierra) and
# a class hash (casm_class_hash) or string containing a compiled contract (casm)
declare_result = await Contract.declare_v3(
    account,
    compiled_contract=compiled_contract,
    compiled_class_hash=class_hash,
    l1_resource_bounds=l1_resource_bounds,
)

await declare_result.wait_for_acceptance()
deploy_result = await declare_result.deploy_v3(
    l1_resource_bounds=l1_resource_bounds,
)
# 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_v3(
        k,
        v,
        l1_resource_bounds=ResourceBounds(
            max_amount=int(1e5), max_price_per_unit=int(1e13)
        ),
    )
).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_invoke_v3(key=10, value=20),
    map_contract.functions["put"].prepare_invoke_v3(key=30, value=40),
]

# Executes only one transaction with prepared calls
transaction_response = await account.execute_v3(
    calls=calls,
    l1_resource_bounds=l1_resource_bounds,
)
await account.client.wait_for_tx(transaction_response.transaction_hash)
```

### Using Contract
[Contract](https://starknetpy.readthedocs.io/en/latest/api/contract.html#starknet_py.contract.Contract) makes interacting with contracts deployed on Starknet much easier:
```python
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,
    cairo_version=1,
)

# All exposed functions are available at contract.functions.
# Here we invoke a function, creating a new transaction.
invocation = await contract.functions["put"].invoke_v1(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
```

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:

```python
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_v1_sync(key, 7, max_fee=int(1e16))
invocation.wait_for_acceptance_sync()

(saved,) = contract.functions["get"].call_sync(key)  # 7
```

Contract automatically serializes values to Cairo calldata. This includes adding array lengths automatically. 
See more info in [Serialization](https://starknetpy.readthedocs.io/en/latest/guide/serialization.html#serialization).

Quickstart in docs - click [here](https://starknetpy.rtfd.io/en/latest/quickstart.html).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/software-mansion/starknet.py",
    "name": "starknet-py",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Tomasz Rejowski",
    "author_email": "tomasz.rejowski@swmansion.com",
    "download_url": "https://files.pythonhosted.org/packages/62/7f/a7126fb212ef90a6b1819c627136e14d3e087d245aec39117a17c1cff19b/starknet_py-0.24.2.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n    <img src=\"https://raw.githubusercontent.com/software-mansion/starknet.py/master/graphic.png\" alt=\"starknet.py\"/>\n</div>\n<h2 align=\"center\">Starknet SDK for Python</h2>\n\n<div align=\"center\">\n\n[![codecov](https://codecov.io/gh/software-mansion/starknet.py/branch/master/graph/badge.svg?token=3E54E8RYSL)](https://codecov.io/gh/software-mansion/starknet.py)\n[![pypi](https://img.shields.io/pypi/v/starknet.py)](https://pypi.org/project/starknet.py/)\n[![build](https://img.shields.io/github/actions/workflow/status/software-mansion/starknet.py/checks.yml)](https://github.com/software-mansion/starknet.py/actions)\n[![docs](https://readthedocs.org/projects/starknetpy/badge/?version=latest)](https://starknetpy.readthedocs.io/en/latest/?badge=latest)\n[![license](https://img.shields.io/badge/license-MIT-black)](https://github.com/software-mansion/starknet.py/blob/master/LICENSE.txt)\n[![stars](https://img.shields.io/github/stars/software-mansion/starknet.py?color=yellow)](https://github.com/software-mansion/starknet.py/stargazers)\n[![starkware](https://img.shields.io/badge/powered_by-StarkWare-navy)](https://starkware.co)\n\n</div>\n\n## \ud83d\udcd8 Documentation\n\n- [Installation](https://starknetpy.rtfd.io/en/latest/installation.html)\n- [Quickstart](https://starknetpy.rtfd.io/en/latest/quickstart.html)\n- [Guide](https://starknetpy.rtfd.io/en/latest/guide.html)\n- [API](https://starknetpy.rtfd.io/en/latest/api.html)\n- [Migration guide](https://starknetpy.readthedocs.io/en/latest/migration_guide.html)\n\n## \u2699\ufe0f Installation\n\nInstallation varies between operating systems.\n\n[See our documentation on complete instructions](https://starknetpy.rtfd.io/en/latest/installation.html)\n\n\n## \ud83d\udca8 Quickstart\n### Using FullNodeClient\nA [Client](https://starknetpy.readthedocs.io/en/latest/api/client.html#client) is a facade for interacting with Starknet. \n[FullNodeClient](https://starknetpy.readthedocs.io/en/latest/api/full_node_client.html#module-starknet_py.net.full_node_client) is a client which interacts with a Starknet full nodes like [Pathfinder](https://github.com/eqlabs/pathfinder), [Papyrus](https://github.com/starkware-libs/papyrus) or [Juno](https://github.com/NethermindEth/juno). \nIt supports read and write operations, like querying the blockchain state or adding new transactions.\n\n\n```python\nfrom starknet_py.net.full_node_client import FullNodeClient\n\nnode_url = \"https://your.node.url\"\nclient = FullNodeClient(node_url=node_url)\n\ncall_result = await client.get_block(block_number=1)\n```\nThe 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.\n\n```python\nnode_url = \"https://your.node.url\"\nclient = FullNodeClient(node_url=node_url)\ncall_result = client.get_block_sync(block_number=1)\n```\nYou can check out all of the FullNodeClient\u2019s methods here: [FullNodeClient](https://starknetpy.readthedocs.io/en/latest/api/full_node_client.html#module-starknet_py.net.full_node_client).\n\n### Creating Account\n[Account](https://starknetpy.readthedocs.io/en/latest/api/account.html#starknet_py.net.account.account.Account) is the default implementation of [BaseAccount](https://starknetpy.readthedocs.io/en/latest/api/account.html#starknet_py.net.account.base_account.BaseAccount) interface. \nIt supports an account contract which proxies the calls to other contracts on Starknet.\n\nAccount can be created in two ways:\n- By constructor (It is required to provide an `address` and either `key_pair` or `signer`).\n- By static methods `Account.deploy_account_v1` or `Account.deploy_account_v3`\n\nAdditionally, you can use the [sncast](https://foundry-rs.github.io/starknet-foundry/starknet/index.html) tool to create an account, \nwhich will automatically be saved to a file.\nThere are some examples how to do it:\n```python\nfrom starknet_py.net.account.account import Account\nfrom starknet_py.net.full_node_client import FullNodeClient\nfrom starknet_py.net.models.chains import StarknetChainId\nfrom starknet_py.net.signer.stark_curve_signer import KeyPair\nfrom starknet_py.net.signer.stark_curve_signer import StarkCurveSigner\n\n# Creates an instance of account which is already deployed\n# Account using transaction version=1 (has __validate__ function)\nclient = FullNodeClient(node_url=\"https://your.node.url\")\naccount = Account(\n    client=client,\n    address=\"0x4321\",\n    key_pair=KeyPair(private_key=654, public_key=321),\n    chain=StarknetChainId.SEPOLIA,\n)\n\n# There is another way of creating key_pair\nkey_pair = KeyPair.from_private_key(key=123)\n# or\nkey_pair = KeyPair.from_private_key(key=\"0x123\")\n\n# Instead of providing key_pair it is possible to specify a signer\nsigner = StarkCurveSigner(\"0x1234\", key_pair, StarknetChainId.SEPOLIA)\n\naccount = Account(\n    client=client, address=\"0x1234\", signer=signer, chain=StarknetChainId.SEPOLIA\n)\n```\n\n### Using Account\nExample usage:\n\n```python\nfrom starknet_py.contract import Contract\nfrom starknet_py.net.client_models import ResourceBounds\nl1_resource_bounds = ResourceBounds(\n    max_amount=int(1e5), max_price_per_unit=int(1e13)\n)\n# Declare and deploy an example contract which implements a simple k-v store.\n# Contract.declare_v3 takes string containing a compiled contract (sierra) and\n# a class hash (casm_class_hash) or string containing a compiled contract (casm)\ndeclare_result = await Contract.declare_v3(\n    account,\n    compiled_contract=compiled_contract,\n    compiled_class_hash=class_hash,\n    l1_resource_bounds=l1_resource_bounds,\n)\n\nawait declare_result.wait_for_acceptance()\ndeploy_result = await declare_result.deploy_v3(\n    l1_resource_bounds=l1_resource_bounds,\n)\n# Wait until deployment transaction is accepted\nawait deploy_result.wait_for_acceptance()\n\n# Get deployed contract\nmap_contract = deploy_result.deployed_contract\nk, v = 13, 4324\n# Adds a transaction to mutate the state of k-v store. The call goes through account proxy, because we've used\n# Account to create the contract object\nawait (\n    await map_contract.functions[\"put\"].invoke_v3(\n        k,\n        v,\n        l1_resource_bounds=ResourceBounds(\n            max_amount=int(1e5), max_price_per_unit=int(1e13)\n        ),\n    )\n).wait_for_acceptance()\n\n# Retrieves the value, which is equal to 4324 in this case\n(resp,) = await map_contract.functions[\"get\"].call(k)\n\n# There is a possibility of invoking the multicall\n\n# Creates a list of prepared function calls\ncalls = [\n    map_contract.functions[\"put\"].prepare_invoke_v3(key=10, value=20),\n    map_contract.functions[\"put\"].prepare_invoke_v3(key=30, value=40),\n]\n\n# Executes only one transaction with prepared calls\ntransaction_response = await account.execute_v3(\n    calls=calls,\n    l1_resource_bounds=l1_resource_bounds,\n)\nawait account.client.wait_for_tx(transaction_response.transaction_hash)\n```\n\n### Using Contract\n[Contract](https://starknetpy.readthedocs.io/en/latest/api/contract.html#starknet_py.contract.Contract) makes interacting with contracts deployed on Starknet much easier:\n```python\nfrom starknet_py.contract import Contract\n\ncontract_address = (\n    \"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b\"\n)\nkey = 1234\n\n# Create contract from contract's address - Contract will download contract's ABI to know its interface.\ncontract = await Contract.from_address(address=contract_address, provider=account)\n\n# If the ABI is known, create the contract directly (this is the preferred way).\ncontract = Contract(\n    address=contract_address,\n    abi=abi,\n    provider=account,\n    cairo_version=1,\n)\n\n# All exposed functions are available at contract.functions.\n# Here we invoke a function, creating a new transaction.\ninvocation = await contract.functions[\"put\"].invoke_v1(key, 7, max_fee=int(1e16))\n\n# Invocation returns InvokeResult object. It exposes a helper for waiting until transaction is accepted.\nawait invocation.wait_for_acceptance()\n\n# Calling contract's function doesn't create a new transaction, you get the function's result.\n(saved,) = await contract.functions[\"get\"].call(key)\n# saved = 7 now\n```\n\nTo check if invoke succeeded use `wait_for_acceptance` on InvokeResult and get its status.\n\nAlthough asynchronous API is recommended, you can also use Contract\u2019s synchronous API:\n\n```python\nfrom starknet_py.contract import Contract\n\ncontract_address = (\n    \"0x01336fa7c870a7403aced14dda865b75f29113230ed84e3a661f7af70fe83e7b\"\n)\n\nkey = 1234\ncontract = Contract.from_address_sync(address=contract_address, provider=account)\n\ninvocation = contract.functions[\"put\"].invoke_v1_sync(key, 7, max_fee=int(1e16))\ninvocation.wait_for_acceptance_sync()\n\n(saved,) = contract.functions[\"get\"].call_sync(key)  # 7\n```\n\nContract automatically serializes values to Cairo calldata. This includes adding array lengths automatically. \nSee more info in [Serialization](https://starknetpy.readthedocs.io/en/latest/guide/serialization.html#serialization).\n\nQuickstart in docs - click [here](https://starknetpy.rtfd.io/en/latest/quickstart.html).\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A python SDK for Starknet",
    "version": "0.24.2",
    "project_urls": {
        "Documentation": "https://starknetpy.rtfd.io/",
        "Homepage": "https://github.com/software-mansion/starknet.py",
        "Repository": "https://github.com/software-mansion/starknet.py"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "627fa7126fb212ef90a6b1819c627136e14d3e087d245aec39117a17c1cff19b",
                "md5": "9597661cb07265701f02727fa7f2ef1b",
                "sha256": "71bc4892aca3287903694988142a89307a83886229c80e443df7119f3329f33b"
            },
            "downloads": -1,
            "filename": "starknet_py-0.24.2.tar.gz",
            "has_sig": false,
            "md5_digest": "9597661cb07265701f02727fa7f2ef1b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.8",
            "size": 97547,
            "upload_time": "2024-09-13T10:47:52",
            "upload_time_iso_8601": "2024-09-13T10:47:52.664737Z",
            "url": "https://files.pythonhosted.org/packages/62/7f/a7126fb212ef90a6b1819c627136e14d3e087d245aec39117a17c1cff19b/starknet_py-0.24.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-13 10:47:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "software-mansion",
    "github_project": "starknet.py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "starknet-py"
}
        
Elapsed time: 0.34284s