Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
war-in committed Sep 15, 2022
2 parents 7620f82 + 9161ad3 commit b925c64
Show file tree
Hide file tree
Showing 62 changed files with 14,731 additions and 412 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ persistent=yes

# Minimum Python version to use for version dependent checks. Will default to
# the version used to run pylint.
py-version=3.7
py-version=3.8

# When enabled, pylint would attempt to guess common misconfiguration and emit
# user-friendly hints instead of false-positive error messages.
Expand Down
2 changes: 1 addition & 1 deletion docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Development dependencies
- `poetry <https://python-poetry.org/>`_ - dependency manager.
- `pyenv <https://github.com/pyenv/pyenv>`_ - recommended for installing and switching python versions locally.

Make sure running ``poetry run python --version`` returns ``Python 3.7.12``.
Make sure running ``poetry run python --version`` returns ``Python 3.8.x``.

Setup
-----
Expand Down
16 changes: 14 additions & 2 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@ signing algorithm, it is possible to create ``AccountClient`` with custom

.. literalinclude:: ../starknet_py/tests/e2e/docs/guide/test_custom_signer.py
:language: python
:lines: 11-35
:lines: 11-41
:dedent: 4


Signing off-chain messages
-------------------------------

:ref:`Account Client` lets you sign an off-chain message by using encoding standard proposed `here <https://github.com/argentlabs/argent-x/discussions/14>`_.
You can also **verify a message**, which is done by a call to ``is_valid_signature`` endpoint in the account's contract (e.g. `OpenZeppelin's account contract <https://github.com/starkware-libs/cairo-lang/blob/4e233516f52477ad158bc81a86ec2760471c1b65/src/starkware/starknet/third_party/open_zeppelin/Account.cairo#L115>`_).

.. literalinclude:: ../starknet_py/tests/e2e/docs/guide/test_sign_offchain_message.py
:language: python
:lines: 9-44,51-59,66-76
:dedent: 4


Expand All @@ -57,7 +69,7 @@ Here's how you can deploy new contracts:

.. literalinclude:: ../starknet_py/tests/e2e/docs/guide/test_deploying_new_contracts.py
:language: python
:lines: 10-33,37-68
:lines: 10-33,37-47,51-57,61-72
:dedent: 4


Expand Down
6 changes: 3 additions & 3 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ It requires information about used network:

.. literalinclude:: ../starknet_py/tests/e2e/docs/quickstart/test_using_gateway_client.py
:language: python
:lines: 8-29
:lines: 8-25
:dedent: 4

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.

.. literalinclude:: ../starknet_py/tests/e2e/docs/quickstart/test_synchronous_gateway_client.py
:language: python
:lines: 11-14
:lines: 8-11
:dedent: 4

You can see all Gateway Client's methods :ref:`GatewayClient`.
Expand Down Expand Up @@ -80,5 +80,5 @@ Although asynchronous API is recommended, you can also use Contract's synchronou

.. literalinclude:: ../starknet_py/tests/e2e/docs/quickstart/test_synchronous_api.py
:language: python
:lines: 8-15,22-29
:lines: 6-13,20-27
:dedent: 4
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "starknet-py"
version = "0.5.2-alpha"
version = "0.6.0-alpha"
description = "A python SDK for StarkNet"
authors = ["Tomasz Rejowski <[email protected]>", "Jakub Ptak <[email protected]>"]
include = ["starknet_py", "starknet_py/utils/crypto/libcrypto_c_exports.*"]
Expand Down Expand Up @@ -29,18 +29,18 @@ docs = ["sphinx", "enum-tools"]
eth-tester = ["tester"]

[tool.poetry.group.dev.dependencies]
starknet-devnet = {git = "https://github.com/Shard-Labs/starknet-devnet.git", rev = "03e77ec828418ce158af2294926b62dadc406747"}
pytest = "^6.2.5"
black = "^22.3.0"
poethepoet = "^0.11.0"
coverage = "^6.1.2"
coverage = "^6.3.0"
pytest-asyncio = "^0.19.0"
sphinx-rtd-theme = "^1.0.0"
pylint = "2.13.5"
setuptools = "^65.3.0"
pytest-mock = "^3.6.1"
pytest-xdist = "^2.5.0"
web3 = {extras=["tester"], version="6.0.0b4"}
starknet-devnet = {git = "https://github.com/Shard-Labs/starknet-devnet.git", rev = "01f82adb1df88c801adb5bf37bdb453321c5348e"}

[tool.poe.tasks]
test = ["test_unit", "test_e2e", "test_docs"]
Expand Down
12 changes: 6 additions & 6 deletions starknet_py/compile/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
from starkware.starknet.compiler.validation_utils import PreprocessorError

from starknet_py.compile.compiler import Compiler, create_contract_class
from starknet_py.tests.e2e.conftest import directory_with_contracts
from starknet_py.tests.e2e.conftest import contracts_dir

directory = os.path.dirname(__file__)

test_file_path = directory_with_contracts / "map.cairo"
test_file_path = contracts_dir / "map.cairo"
test_file_content = test_file_path.read_text("utf-8")

base_contract_path = directory_with_contracts / "base.cairo"
base_contract_path = contracts_dir / "base.cairo"

mock_account_path = directory_with_contracts / "mock_account.cairo"
mock_account_path = contracts_dir / "mock_account.cairo"
mock_account_content = mock_account_path.read_text("utf-8")


Expand Down Expand Up @@ -55,15 +55,15 @@ def test_throws_on_compile_with_wrong_extension():
def test_compile_with_search_path():
output_file_str = Compiler(
contract_source=[base_contract_path.resolve().absolute()],
cairo_path=[str(directory_with_contracts)],
cairo_path=[str(contracts_dir)],
).compile_contract()
output_json = json.loads(output_file_str)

assert output_json.get("abi") != []


def test_compile_with_env_var(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setenv(LIBS_DIR_ENVVAR, str(directory_with_contracts))
monkeypatch.setenv(LIBS_DIR_ENVVAR, str(contracts_dir))
output_file_str = Compiler(
contract_source=[base_contract_path.resolve().absolute()]
).compile_contract()
Expand Down
53 changes: 14 additions & 39 deletions starknet_py/contract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import dataclasses
import sys
import warnings
from dataclasses import dataclass
from typing import (
Expand All @@ -10,9 +9,9 @@
TypeVar,
Union,
Dict,
Collection,
NamedTuple,
)
from typing import TypedDict

from starkware.cairo.lang.compiler.identifier_manager import IdentifierManager
from starkware.starknet.core.os.class_hash import compute_class_hash
Expand All @@ -24,30 +23,22 @@
)

from starknet_py.common import create_compiled_contract
from starknet_py.compile.compiler import StarknetCompilationSource
from starknet_py.net import AccountClient
from starknet_py.net.client import Client
from starknet_py.net.client_models import Hash, Tag

from starknet_py.proxy_check import ProxyCheck, ArgentProxyCheck, OpenZeppelinProxyCheck
from starknet_py.net.models import (
InvokeFunction,
AddressRepresentation,
parse_address,
compute_address,
)
from starknet_py.compile.compiler import StarknetCompilationSource
from starknet_py.proxy_check import ProxyCheck, ArgentProxyCheck, OpenZeppelinProxyCheck
from starknet_py.transactions.deploy import make_deploy_tx
from starknet_py.utils.crypto.facade import pedersen_hash, Call
from starknet_py.utils.data_transformer import FunctionCallSerializer
from starknet_py.utils.sync import add_sync_methods

from starknet_py.net.client import Client

if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict


ABI = list
ABIEntry = dict
TSentTransaction = TypeVar("TSentTransaction", bound="SentTransaction")
Expand Down Expand Up @@ -152,36 +143,27 @@ def __init__(

async def call_raw(
self,
signature: Optional[Collection[int]] = None,
block_hash: Optional[str] = None,
) -> List[int]:
"""
Calls a method without translating the result into python values.
:param signature: Signature to send
:param block_hash: Optional block hash
:return: list of ints
"""
if self.version == 1:
tx = self
else:
tx = self._make_invoke_function(signature)

return await self._client.call_contract(invoke_tx=tx, block_hash=block_hash)
return await self._client.call_contract(invoke_tx=self, block_hash=block_hash)

async def call(
self,
signature: Optional[Collection[int]] = None,
block_hash: Optional[str] = None,
) -> NamedTuple:
"""
Calls a method.
:param signature: Signature to send
:param block_hash: Optional block hash
:return: CallResult or List[int] if return_raw is used
"""
result = await self.call_raw(signature=signature, block_hash=block_hash)
result = await self.call_raw(block_hash=block_hash)
return self._payload_transformer.to_python(result)

async def invoke(
Expand All @@ -201,8 +183,11 @@ async def invoke(
if max_fee is not None:
self.max_fee = max_fee

transaction = await self._client.sign_transaction(
self, self.max_fee, auto_estimate, self.version
transaction = await self._client.sign_invoke_transaction(
calls=self,
max_fee=self.max_fee,
auto_estimate=auto_estimate,
version=self.version,
)
response = await self._client.send_transaction(transaction)

Expand Down Expand Up @@ -235,24 +220,14 @@ async def estimate_fee(
"Cannot estimate fee of PreparedFunctionCall with max_fee not None or 0."
)

tx = await self._client.sign_transaction(self, max_fee=0, version=self.version)
tx = await self._client.sign_invoke_transaction(
calls=self, max_fee=0, version=self.version
)

return await self._client.estimate_fee(
tx=tx, block_hash=block_hash, block_number=block_number
)

def _make_invoke_function(self, signature) -> InvokeFunction:
return InvokeFunction(
contract_address=self._contract_data.address,
entry_point_selector=None if self.version == 1 else self.selector,
calldata=self.calldata,
# List is required here
signature=[*signature] if signature else [],
max_fee=self.max_fee if self.max_fee is not None else 0,
version=self.version,
nonce=None,
)

def _assert_can_invoke(self):
if not isinstance(self._client, AccountClient):
raise ValueError(
Expand Down
4 changes: 2 additions & 2 deletions starknet_py/contract_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from starknet_py.contract import Contract
from starknet_py.tests.e2e.conftest import directory_with_contracts
from starknet_py.tests.e2e.conftest import contracts_dir

SOURCE = """
// Declare this file as a StarkNet contract and set the required
Expand Down Expand Up @@ -70,7 +70,7 @@
2215802380634391255523792220475859318374841631894501575098737573407681278580
)

search_path = directory_with_contracts
search_path = contracts_dir


def test_compute_hash():
Expand Down
Loading

0 comments on commit b925c64

Please sign in to comment.