GithubHelp home page GithubHelp logo

software-mansion / starknet.py Goto Github PK

View Code? Open in Web Editor NEW
252.0 252.0 59.0 4.82 MB

โœจ ๐Ÿ Python SDK for Starknet.

Home Page: https://starknetpy.rtfd.io/

License: MIT License

Shell 0.28% Python 88.90% Cairo 10.82%
ethereum l2 python sdk starknet starkware zk-rollup

starknet.py's People

Contributors

antazoey avatar arcticae avatar ca11ab1e avatar cptartur avatar cremafr avatar ddoktorski avatar dependabot[bot] avatar drknzz avatar exomonk avatar hayden4r4 avatar jonatan-chaverri avatar maksymiliandemitraszek avatar omahs avatar pweglik avatar radinyn avatar solpatium avatar thenry14 avatar tkumor3 avatar tomek0123456789 avatar war-in avatar xiaolou86 avatar yoga-braavos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

starknet.py's Issues

Add default block number to client methods

Is your feature request related to a problem? Please describe.

As of 0.8.2.1 starknet allows for passing "latest" as blockNumber .

Since 0.9 or later, starknet will require explicitly providing blockNumber parameter in the following methods

  • get_code
  • get_full_contract
  • get_block
  • get_storage_at
  • get_block
  • call_contract

These methods in starknet.py must be modified to use a default block_number="latest" parameter to continue to work in future versions.

Change deploy method

In addition to the new declare method: #158 we will have to change contract's deploy method.

Describe the solution you'd like

Contract.deploy should take class_hash instead of compilation_source and compiled_contract.

Expose search paths when compiling contracts

We should change starknet_compile and all functions using it to allow passing search paths.
Currently we only search for files within active directory. It is very difficult to manage scripts from external resources, like git submodule.

[BUG] Proxy contracts are not able to find ABIs

When I interact with a smart-contract via calling a mutable method on the live Testnet (goerli alpha), I get an Unknown location error.

Python version

Python 3.7.9

Expected Behavior

I can call mutable methods on contracts from goerli

Current Behavior

I get the following error:

ERROR: (ContractLogicError) Error at pc=0:32:Got an exception while executing a hint.Cairo traceback (most recent call last):Unknown location (pc=0:497)Unknown location (pc=0:456)Unknown location (pc=0:513)Error in the called contract (0x7394cbe418daa16e42b87ba67372d4ab4a5df0b05c6e554d158458ce245bc10):Error at pc=0:6:Got an exception while executing a hint.Cairo traceback (most recent call last):Unknown location (pc=0:795)Unknown location (pc=0:772)Unknown location (pc=0:1232)Traceback (most recent call last):  File "<hint0>", line 3, in <module>AssertionError: assert_not_zero failed: 0 = 0.').

I am using the test token contract deployed to 0x07394cbe418daa16e42b87ba67372d4ab4a5df0b05c6e554d158458ce245bc10

When I deploy an ERC20 to my local starknet-devnet, I can call mutable methods and things work just fine.

Possible Solution

We need to grab the ABI from the implementation contract.

Steps to Reproduce

from starknet_py.contract import Contract
from starknet_py.net import AccountClient
from starknet_py.net import KeyPair

# This account comes from ArgentX and is a proxy contract.
# The error I get is that it cannot find the __execute__ ABI.
# The __execute__ method needs to be grabbed from the implementation contract.
account_address = 2976162931320574283751166485501184860501729793478009661243729394508001134570

# Create the account client (note: my pkey is omitted for obvious reasons)
key_pair = KeyPair(
    private_key=******,
    public_key=888158711641686699726990655481927574337047407568
)
account_client = AccountClient(
    account_address,
    key_pair,
    'https://alpha4.starknet.io',
    chain=1536727068981429685321,
)

my_other_account = 3037460148268451690947737348211282823053350676597561718645135357343745417603

# Init the ETH token contract from its address
token_address = "0x07394cbe418daa16e42b87ba67372d4ab4a5df0b05c6e554d158458ce245bc10"
token_contract = Contract.from_address_sync(token_address, account_client)

receipt = token_contract.functions["transfer"].invoke_sync(my_other_account, 100, max_fee=100)
print(receipt)  # Does not get here

Context (Environment)

Trying to interact with the ETH contract (which is a proxy)
Also, trying to use my Argent X account (which is a proxy)
In general, I have trouble with proxy contracts

Detailed Description

Fetch ABI from proxy first by calling implementation()

Possible Implementation

Provide easy way to check account balances

Is your feature request related to a problem? Please describe.

A way to check an account's balance in the token that fees are required to be paid in.

Describe the solution you'd like

account_client.get_balance() that makes a call to the ETH ERC20 contract to get the amount of tokens that account has.

Describe alternatives you've considered

A tokens lists type manager is probably bigger picture

Additional context

I might be missing something obvious.
I am going to have to implement this myself because I need this functionality but I am wondering if it would make better sense here.

[BUG] Lint does not run on tests

Python version

3.7.12

Expected Behavior

Tests should be linted

Current Behavior

They are not

Possible Solution

  • Add __init__.py to tests directory
  • Add separate method to run lint in tests directory

Make e2e tests use fees on devnet

Is your feature request related to a problem? Please describe.

Since this PR starknet-devnet added support for transactions with positive fee. Our e2e tests should be changed to use non 0 max_fee value.

Describe the solution you'd like

Use predeployed accounts from devnet to send fees with transactions

Describe alternatives you've considered

None

Additional context

Using fees in every tests that requires them will enable us to more easily use our tests on testnet.

[BUG] Fix parallel tests task

Python version

Any

Expected Behavior

Tests should run in parallel

Current Behavior

They do not run at all

Possible Solution

Add env configuration to poe test_e2e_parallel as it is missing devnet port.

[BUG] `estimate_fee_sync` always fails with `TRANSACTION_FAILED`

Python version

3.9.12

Expected Behavior

estimate_fee_sync should successfully complete

Current Behavior

It returns the following error:

{"code": "StarknetErrorCode.TRANSACTION_FAILED", "message": "Error at pc=0:392:\nUnknown value for memory cell at address 12:0.\nCairo traceback (most recent call last):\nUnknown location (pc=0:534)\nUnknown location (pc=0:468)"}

Possible Solution

Steps to Reproduce

(nonce,) = keeper_contract.functions["get_nonce"].call_sync()
task_address_felt = int(task["address"], 16)
args, signature = get_account_message_args_sig(keeper_address, registry, "executeTask", [task_address_felt], nonce, key)
unsigned_invocation = keeper_contract.functions["execute"].prepare(*args)

# This is where the error occurs
estimation = unsigned_invocation.estimate_fee_sync()

# This normally runs perfectly fine if I don't do gas estimation for all these transactions that are failing with `estimate_fee`
invocation = unsigned_invocation.invoke_sync(signature=signature, max_fee=0)

FYI - I'm using an older version of an account contract to issue transactions but I'm assuming that this should not impact estimate fee as the other starknet.py apis are working fine?

Context (Environment)

I've been using the excellent starknet.py library to power Yagi Keepers (http://www.yagi.fi/automation).
I'm trying to upgrade keepers now to use fees on testnet before they are officially mandated.

Detailed Description

Possible Implementation

'transaction_hash' module import requires updating

When installing in a fresh environment, I try to follow the README but I get the following error:

>>> from starknet_py.net.client import Client
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jules/PycharmProjects/starknet.py/starknet_py/net/__init__.py", line 1, in <module>
    from .client import Client
  File "/Users/jules/PycharmProjects/starknet.py/starknet_py/net/client.py", line 16, in <module>
    from starknet_py.net.models.address import BlockIdentifier
  File "/Users/jules/PycharmProjects/starknet.py/starknet_py/net/models/__init__.py", line 8, in <module>
    from .transaction import (
  File "/Users/jules/PycharmProjects/starknet.py/starknet_py/net/models/transaction.py", line 9, in <module>
    from starkware.starknet.services.api.gateway.transaction_hash import (
ModuleNotFoundError: No module named 'starkware.starknet.services.api.gateway.transaction_hash'

This is because this module was moved in this PR: starkware-libs/cairo-lang@ed6cf8d

Python version

Python 3.7.9

Expected Behavior

I can import the Client class.

Current Behavior

Errors about a module not being found.

Possible Solution

Change the import path (see linked PR)

Steps to Reproduce

  1. pip install starknet.py
  2. python
  3. from starknet_py.net.client import Client

Context (Environment)

I am trying to evaluate this library for usage in a plugin for our development framework for starknet.

Update the way of creating account client in tests

Is your feature request related to a problem? Please describe.
There is a better way of creating the account client

Describe the solution you'd like
Change this:

acc_client = await AccountClient.create_account(
net=run_devnet, chain=StarknetChainId.TESTNET
)

to this:

acc_client = await DevnetClientFactory(run_devnet).make_devnet_client()

Make e2e test easily runnable on testnet

Is your feature request related to a problem? Please describe.

Currently, to run e2e tests on testnet, parameters have to be manually changed in multiple places. Ideally there should be some kind of switch that allows easily changing between devnet and testnet.

Describe the solution you'd like

Custom pytest parameter to toggle between tests

Describe alternatives you've considered

Some simple code interface for selecting net address

Additional context

n/a

Show rejection reason in exception

Is your feature request related to a problem? Please describe.

I am unable to make useful assertions on rejection reasons because the exception that gets raised in a generic exception that does not reveal any of the information.

Describe the solution you'd like

In client.wait_for_tx(), raise a custom error TransactionRejected(result["message"]) that includes that failure reason from the result.

Describe alternatives you've considered

Implementing a custom wait_for_tx().

Additional context

I was failing to sign a transaction correctly but the error message was not telling me that. I had to set a breakpoint in the lib to find out the details of the failure.

Wait for successful deployment in Contract.deploy

Is your feature request related to a problem? Please describe.

Currently when deployment of the contract fails after deploy transaction was made (e.g. when constructor calldata provided was incorrect and assertion error was thrown), Contract.deploy still returns a DeploymentResult that hast be verified by the user, for example using Client.wait_for_tx(). I think it would be useful for Contract.deploy to be able to fail when deployment is unsuccessful.

Describe the solution you'd like

Provide a parameter like wait_for_deploy so Contract.deploy checks for successful deployment and raises an exception if it's not.

Upgrade web3.py dependency

Is your feature request related to a problem? Please describe.
I am trying to install this package in an environment using web3.py==5.27.0 but this package is pinned to web3.py==5.3.0.

Describe the solution you'd like
Upgrade the web3.py dependency to include 5.27.0 in its supported range as well addressing any migrations related to this upgrade.

Describe alternatives you've considered
Not using this library directly or forking it? I don't know, the starknet APIs currently seem tricky. This is an excellent resources nonetheless!

class_hash instead of implementation_address in proxy contracts

The proxy contract will work with a class rather than a contract, that is implementation_address would change to class_hash.

There is an implementation of ArgentProxyCheck and OpenZeppelinProxyCheck in our project, so we should check how they will manage this change.

Remove env var and default net parameter from DevnetClientFactory

Is your feature request related to a problem? Please describe.

With current setup it is impossible to run tests either through cli or IDE integrations without setting DEVNET_PORT env var before. This variable is no longer used so this setup is unnecessary.

Also it may lead to bugs in tests as not passing run_devnet to DevnetClientFactory, it will try to use a default parameter using devnet_port, and the connection will fail (since there'd be no devnet instance at this port).

Describe the solution you'd like

Remove DEVNET_PORT and related default value entirely

Support changes from starknet 0.8.2

Is your feature request related to a problem? Please describe.

Support changes from starknet 0.8.2 (e.g. endpoints with new parameters, "latest" block)

Fix tests related to hashing

Is your feature request related to a problem? Please describe.

Method of calculating a transaction hash has now changed, update tests to reflect those changes

New Contract.declare transaction

In StarkNet Alpha 0.9.0 there will be a new type of transaction.
Declare transaction will put contract's definition to StarkNet.

Describe the solution you'd like

New declare method in Contract class.

Checks should run in Pull Requests from forks

Is your feature request related to a problem? Please describe.

Currently checks do not run in external PRs

Describe the solution you'd like

Checks should run in PRs made from forks

Running tests on starknet testnet

Is your feature request related to a problem? Please describe.

The problem comes from PR #131 where we have functionality that should be tested on testnet, but as correctly pointed out by @Arcticae these tests probably shouldn't be run on every commit as they introduce significant overhead.

Describe the solution you'd like

Some change in how these tests are run should be introduced.

Describe alternatives you've considered

Here are some ideas:

  • Introduce manually triggered task that runs tests on testnet (most flexible, but requires most maintnance)
  • Run testnet tests only on commits to development and master (automatic but may result in detecting problems after merge)
  • Run testnet tests just before merging PRs (most optimal, but I don't know if it's event possible)

Change default local test configuration so docs test are not run

Is your feature request related to a problem? Please describe.

I think that running docs tests everytime you use poe test_e2e is unfavourable behavior. I think we should consider changing.

Describe the solution you'd like

More control over running docs tests

Describe alternatives you've considered

  • Run only e2e tests and add custom command for running docs tests
  • Add custom command for running e2e test without docs test

Similarly to how you can now disable tests that run on testnet.

Additional context

I think it unnecessarily increases run time of the tests, when realistically docs tests should only be run on CI/locally before finishing a change.

Support max_fee when creating transactions

Describe the solution you'd like

Add support for max_fee when creating transactions.
Especially AccountClient.add_transaction should either

  • calculate max_fee based on the estimated_fee (estimated * 1.1) if no max_fee is provided
  • compare max_fee against estimated_fee when it's provided

and it should use this fee when creating MultiCall

[BUG] `deploy_sync` seems to be blocking

Hey thanks for developing starknet.py, awesome lib we are starting to use at https://yagi.fi! Friendly (possible) bug report.

I'm trying to deploy an account contract using deploy_sync but it hangs rather than returning the pending tx (which is what invoke_sync does).

Python version

3.8

Expected Behavior

Should return pending tx.

Current Behavior

Hangs.

Possible Solution

Match invoke_sync behavior with deploy_sync.

Steps to Reproduce

source_code = Path("contracts/Account.cairo").read_text("utf-8")
result = Contract.deploy_sync(
    client=Client("testnet"),
    compilation_source=source_code,
    constructor_args={
        "_public_key": public_key
    }
)

Context (Environment)

Not able to deploy an account contract using starknet.py.

Detailed Description

Possible Implementation

Estimate fee in account client [BUG]

Expected Behavior

Estimate fee in the account contract should prepare all data like in add_transaction method (e.g. signature) and use estimate_fee from Client with this data.

Possible Solution

Create estimate_fee method in AccountClient which will check the fee in the context of AccountClient

##Current behavior

Now you have to provide a signature to estimate_fee which is not correct

Fix Transaction.compute_invoke_hash

Is your feature request related to a problem? Please describe.

Add support for version and max_fee arguments now required by calculate_transaction_hash_common()

Checks should fails if documentation build is unsuccessful

Is your feature request related to a problem? Please describe.

If documentation build ends with a failure, checks for whole commit should fail.

Currently if there are errors while generating docs (e.g. missing pages), we often end up unaware of these errors.

Describe the solution you'd like

Checks should fail

Parametrize the contract you can use `AccountClient.create_account`

Is your feature request related to a problem? Please describe.
It is not a problem, but I like the transaction management and private key establishment that happens in this method however it is limited to the OZ contract. It would be nice to be able to allow the user to specify any contract to use here (and have it default to the OZ one).

Describe the solution you'd like
Allow the user to specify a separate contract to use in AccountClient.create_account()

Describe alternatives you've considered
Implement outside of lib.

Additional context
Add any other context or screenshots about the feature request here.

Run e2e tests in separate devnet instances

Is your feature request related to a problem? Please describe.

Currently each e2e test is run in the single devnet instance. This may allow for unpredictable side effects of previous tests to influence results of the rest.

Describe the solution you'd like

Isolate tests from eachother

Describe alternatives you've considered

  • Run devnet instance per test (e.g. provide a decorator that runs a devnet instance for test)
  • Run devnet instance per test suite (e.g. file), this may be easier to implement since we can still have some sort of bash script that runs devnet and test for all files in e2e directory recursively.

Additional context

Test are already quite slow, so we may need to consider some optimisation or ignore this issue entirely if it's not possible to run multiple devnets efficiently.

Typo in pyproject.toml

pyproject.toml in line 25 should be:

test_e2e.shell = "starknet-devnet --host localhost --port 5000 ; DEVNET_PID=$! && sleep 1 && coverage run -m pytest starknet/tests/e2e; kill $DEVNET_PID"

[BUG] Error message has a serialized class and is difficult to parse programmatically

When an error occurs on a contract, the error message includes the class name and is difficult to parse as a string.

Python version

Python 3.7.9

Expected Behavior

The error message only contains str or JSON like dict instead of class

Current Behavior

The error message has a stringified class

Possible Solution

Convert class to JSON dict first? (fast solution)
Or grab the important fields from the class and compose a nicer error message (ideal solutioin)

Steps to Reproduce

Create a contract that you know will error (I am using starknet-devnet)
Call method that causes error
See error message.

It looks something like this:

Transaction was rejected with following starknet error: TransactionFailureReason(code='TRANSACTION_FAILED', error_message='Error message: Already initialized\nError at pc=0:118:\nAn ASSERT_EQ instruction failed: 1 != 0.\nCairo traceback (most recent call last):\nUnknown location (pc=0:132)').

Notice the part that says TransactionFailureReason, that is like a class.. I really just want the code and the error_message from it but I have to parse the whole thing as a string.

Context (Environment)

Makes parsing and forming custom errors challenging.

Detailed Description

Use more primitive data types.

Possible Implementation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.