Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomek0123456789 committed Aug 25, 2023
2 parents 2b9bc95 + bbfc49d commit 25bcbcb
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 111 deletions.
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ updates:
schedule:
interval: "monthly"
versioning-strategy: increase-if-necessary
groups:
production-dependencies:
dependency-type: "production"
dev-dependencies:
dependency-type: "development"
199 changes: 100 additions & 99 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "starknet-py"
version = "0.18.0"
version = "0.18.1"
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
55 changes: 49 additions & 6 deletions starknet_py/net/account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ async def _prepare_invoke(
nonce: Optional[int] = None,
max_fee: Optional[int] = None,
auto_estimate: bool = False,
cairo_version: int = 0,
) -> Invoke:
"""
Takes calls and creates Invoke from them.
Expand All @@ -149,10 +150,16 @@ async def _prepare_invoke(
if nonce is None:
nonce = await self.get_nonce()

call_descriptions, calldata = _merge_calls(ensure_iterable(calls))
wrapped_calldata = _execute_payload_serializer.serialize(
{"call_array": call_descriptions, "calldata": calldata}
)
if cairo_version == 1:
parsed_calls = _parse_calls_v2(ensure_iterable(calls))
wrapped_calldata = _execute_payload_serializer_v2.serialize(
{"calls": parsed_calls}
)
else:
call_descriptions, calldata = _merge_calls(ensure_iterable(calls))
wrapped_calldata = _execute_payload_serializer.serialize(
{"call_array": call_descriptions, "calldata": calldata}
)

transaction = Invoke(
calldata=wrapped_calldata,
Expand Down Expand Up @@ -246,9 +253,14 @@ async def sign_invoke_transaction(
nonce: Optional[int] = None,
max_fee: Optional[int] = None,
auto_estimate: bool = False,
cairo_version: int = 0,
) -> Invoke:
execute_tx = await self._prepare_invoke(
calls, nonce=nonce, max_fee=max_fee, auto_estimate=auto_estimate
calls,
nonce=nonce,
max_fee=max_fee,
auto_estimate=auto_estimate,
cairo_version=cairo_version,
)
signature = self.signer.sign_transaction(execute_tx)
return _add_signature_to_transaction(execute_tx, signature)
Expand Down Expand Up @@ -375,9 +387,14 @@ async def execute(
nonce: Optional[int] = None,
max_fee: Optional[int] = None,
auto_estimate: bool = False,
cairo_version: int = 0,
) -> SentTransactionResponse:
execute_transaction = await self.sign_invoke_transaction(
calls, nonce=nonce, max_fee=max_fee, auto_estimate=auto_estimate
calls,
nonce=nonce,
max_fee=max_fee,
auto_estimate=auto_estimate,
cairo_version=cairo_version,
)
return await self._client.send_transaction(execute_transaction)

Expand Down Expand Up @@ -529,6 +546,19 @@ def _merge_calls(calls: Iterable[Call]) -> Tuple[List[Dict], List[int]]:
return call_descriptions, entire_calldata


def _parse_calls_v2(calls: Iterable[Call]) -> List[Dict]:
calls_parsed = []
for call in calls:
_data = {
"to": call.to_addr,
"selector": call.selector,
"calldata": call.calldata,
}
calls_parsed.append(_data)

return calls_parsed


_felt_serializer = FeltSerializer()
_call_description = StructSerializer(
OrderedDict(
Expand All @@ -538,9 +568,22 @@ def _merge_calls(calls: Iterable[Call]) -> Tuple[List[Dict], List[int]]:
data_len=_felt_serializer,
)
)
_call_description_v2 = StructSerializer(
OrderedDict(
to=_felt_serializer,
selector=_felt_serializer,
calldata=ArraySerializer(_felt_serializer),
)
)

_execute_payload_serializer = PayloadSerializer(
OrderedDict(
call_array=ArraySerializer(_call_description),
calldata=ArraySerializer(_felt_serializer),
)
)
_execute_payload_serializer_v2 = PayloadSerializer(
OrderedDict(
calls=ArraySerializer(_call_description_v2),
)
)
16 changes: 14 additions & 2 deletions starknet_py/net/gateway_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ async def get_state_update(
self,
block_hash: Optional[Union[Hash, Tag]] = None,
block_number: Optional[Union[int, Tag]] = None,
include_block: bool = False,
# TODO (#1166): revert to `bool = False`
include_block: Optional[bool] = None,
) -> Union[BlockStateUpdate, StateUpdateWithBlock]:
"""
Get the information about the result of executing the requested block.
Expand All @@ -157,14 +158,25 @@ async def get_state_update(
:param include_block: Flag deciding whether to include the queried block. Defaults to false.
:return: BlockStateUpdate object representing changes in the requested block.
"""
# TODO (#1166): remove that
if include_block is not None and self._net in [
"https://alpha-mainnet.starknet.io",
"mainnet",
]:
raise ValueError(
"Argument 'include_block' does not work on mainnet yet and will be working after v0.12.2 release."
)

block_identifier = get_block_identifier(
block_hash=block_hash, block_number=block_number
)

params = {
"includeBlock": str(include_block).lower(),
**block_identifier,
}
# TODO (#1166): bring back into params
if include_block is not None:
params["includeBlock"] = str(include_block).lower()

res = await self._feeder_gateway_client.call(
method_name="get_state_update", params=params
Expand Down
9 changes: 6 additions & 3 deletions starknet_py/net/schemas/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ class StarknetBlockSchema(Schema):
parent_block_hash = Felt(data_key="parent_block_hash", required=True)
block_number = fields.Integer(data_key="block_number")
status = BlockStatusField(data_key="status", required=True)
root = Felt(data_key="state_root")
# TODO (#1166): change nonprefixedhex to felt in line below
root = NonPrefixedHex(data_key="state_root")
transactions = fields.List(
fields.Nested(TypesOfTransactionsSchema(unknown=EXCLUDE)),
data_key="transactions",
Expand Down Expand Up @@ -423,8 +424,10 @@ def make_dataclass(self, data, **kwargs) -> GatewayStateDiff:

class BlockStateUpdateSchema(Schema):
block_hash = Felt(data_key="block_hash", required=True)
new_root = Felt(data_key="new_root", required=True)
old_root = Felt(data_key="old_root", required=True)
# TODO (#1166): change nonprefixedhex to felt in line below
new_root = NonPrefixedHex(data_key="new_root", required=True)
# TODO (#1166): change nonprefixedhex to felt in line below
old_root = NonPrefixedHex(data_key="old_root", required=True)
state_diff = fields.Nested(StateDiffSchema(), data_key="state_diff", required=True)

@post_load
Expand Down
24 changes: 24 additions & 0 deletions starknet_py/tests/e2e/integration_tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,27 @@ async def test_get_state_update_with_block(gateway_client_integration):

assert res.block == block
assert res.state_update is not None


# TODO (#1166): remove tests below after mainnet release
@pytest.mark.asyncio
async def test_get_block_different_starknet_versions():
mainnet = GatewayClient(net="mainnet")
testnet = GatewayClient(net="testnet")

_ = await mainnet.get_block(block_number=100000)
_ = await testnet.get_block(block_number=100000)


@pytest.mark.asyncio
async def test_get_state_update_different_starknet_versions():
mainnet = GatewayClient(net="mainnet")
testnet = GatewayClient(net="testnet")

_ = await mainnet.get_state_update(block_number=100000)

with pytest.raises(ValueError):
_ = await mainnet.get_state_update(block_number=100000, include_block=False)
_ = await mainnet.get_state_update(block_number=100000, include_block=True)

_ = await testnet.get_state_update(block_number=100000, include_block=True)

0 comments on commit 25bcbcb

Please sign in to comment.