Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion page/docs/guide/json-rpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 3

# JSON-RPC API

Devnet also supports JSON-RPC API v0.2.0: [specifications](https://github.com/starkware-libs/starknet-specs/releases/tag/v0.2.0) . It can be reached under `/rpc`. For an example:
Devnet also supports JSON-RPC API v0.2.1: [specifications](https://github.com/starkware-libs/starknet-specs/releases/tag/v0.2.1) . It can be reached under `/rpc`. For an example:

```
POST /rpc
Expand Down
2 changes: 2 additions & 0 deletions starknet_devnet/blueprints/rpc/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
add_invoke_transaction,
add_declare_transaction,
add_deploy_transaction,
add_deploy_account_transaction,
)
from starknet_devnet.blueprints.rpc.utils import rpc_response, rpc_error
from starknet_devnet.blueprints.rpc.structures.types import RpcError, RpcErrorCode
Expand Down Expand Up @@ -64,6 +65,7 @@
"addInvokeTransaction": add_invoke_transaction,
"addDeclareTransaction": add_declare_transaction,
"addDeployTransaction": add_deploy_transaction,
"addDeployAccountTransaction": add_deploy_account_transaction,
}

rpc = Blueprint("rpc", __name__, url_prefix="/rpc")
Expand Down
76 changes: 69 additions & 7 deletions starknet_devnet/blueprints/rpc/structures/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
DeclareSpecificInfo,
L1HandlerSpecificInfo,
FeeEstimationInfo,
DeployAccountSpecificInfo,
)
from starkware.starknet.services.api.gateway.transaction import (
InvokeFunction,
Declare,
Deploy,
DeployAccount,
)
from starkware.starknet.services.api.gateway.transaction_utils import (
compress_program,
Expand Down Expand Up @@ -153,9 +155,20 @@ class RpcBroadcastedDeployTxn(TypedDict):
constructor_calldata: List[Felt]


class RpcBroadcastedDeployAccountTxn(RpcBroadcastedTxnCommon):
"""TypedDict for BroadcastedDeployAccountTxn"""

contract_address_salt: Felt
constructor_calldata: List[Felt]
class_hash: Felt


# rpc transaction's representation when it's sent to the sequencer (but not yet in a block)
RpcBroadcastedTxn = Union[
RpcBroadcastedDeployTxn, RpcBroadcastedDeclareTxn, RpcBroadcastedInvokeTxn
RpcBroadcastedDeployTxn,
RpcBroadcastedDeclareTxn,
RpcBroadcastedInvokeTxn,
RpcBroadcastedDeployAccountTxn,
]


Expand Down Expand Up @@ -210,12 +223,21 @@ class RpcDeployTransaction(TypedDict):
constructor_calldata: List[Felt]


class RpcDeployAccountTransaction(RpcTransactionCommon):
"""TypedDict for rpc deploy account transaction"""

contract_address_salt: Felt
constructor_calldata: List[Felt]
class_hash: Felt


RpcTransaction = Union[
RpcInvokeTransactionV0,
RpcInvokeTransactionV1,
RpcL1HandlerTransaction,
RpcDeclareTransaction,
RpcDeployTransaction,
RpcDeployAccountTransaction,
]


Expand All @@ -226,6 +248,7 @@ def rpc_transaction(transaction: TransactionSpecificInfo) -> RpcTransaction:
TransactionType.INVOKE_FUNCTION: rpc_invoke_transaction,
TransactionType.DECLARE: rpc_declare_transaction,
TransactionType.L1_HANDLER: rpc_l1_handler_transaction,
TransactionType.DEPLOY_ACCOUNT: rpc_deploy_account_transaction,
}
return tx_mapping[transaction.tx_type](transaction)

Expand Down Expand Up @@ -314,6 +337,28 @@ def rpc_deploy_transaction(transaction: DeploySpecificInfo) -> RpcDeployTransact
return txn


def rpc_deploy_account_transaction(
transaction: DeployAccountSpecificInfo,
) -> RpcDeployAccountTransaction:
"""
Convert gateway deploy account transaction to rpc format
"""
txn: RpcDeployAccountTransaction = {
"contract_address_salt": rpc_felt(transaction.contract_address_salt),
"constructor_calldata": [
rpc_felt(data) for data in transaction.constructor_calldata
],
"class_hash": rpc_felt(transaction.class_hash),
"transaction_hash": rpc_felt(transaction.transaction_hash),
"type": rpc_txn_type(transaction.tx_type.name),
"max_fee": rpc_felt(transaction.max_fee),
"version": hex(transaction.version),
"signature": [rpc_felt(value) for value in transaction.signature],
"nonce": rpc_felt(transaction.nonce),
}
return txn


def rpc_l1_handler_transaction(
transaction: L1HandlerSpecificInfo,
) -> RpcL1HandlerTransaction:
Expand Down Expand Up @@ -438,6 +483,28 @@ def make_deploy(deploy_transaction: RpcDeployTransaction) -> Deploy:
return deploy_tx


def make_deploy_account(
deploy_account_transaction: RpcDeployAccountTransaction,
) -> DeployAccount:
"""
Convert RpcDeployAccountTransaction to DeployAccount
"""
deploy_account_tx = DeployAccount(
class_hash=int(deploy_account_transaction["class_hash"], 16),
contract_address_salt=int(
deploy_account_transaction["contract_address_salt"], 16
),
constructor_calldata=[
int(data, 16) for data in deploy_account_transaction["constructor_calldata"]
],
version=int(deploy_account_transaction["version"], 16),
nonce=int(deploy_account_transaction["nonce"], 16),
max_fee=int(deploy_account_transaction["max_fee"], 16),
signature=[int(sig, 16) for sig in deploy_account_transaction["signature"]],
)
return deploy_account_tx


class EntryPoint(TypedDict):
"""TypedDict for rpc contract class entry point"""

Expand Down Expand Up @@ -543,20 +610,15 @@ def rpc_abi_entry(abi_entry: AbiEntryType) -> AbiEntry:
"""
Convert gateway abi entry to rpc AbiEntry
"""
type_map = {
"constructor": "function",
}

function_map = {
"l1_handler": function_abi_entry,
"function": function_abi_entry,
"struct": struct_abi_entry,
"event": event_abi_entry,
"constructor": function_abi_entry,
}

if abi_entry["type"] in type_map:
abi_entry["type"] = type_map[abi_entry["type"]]

return function_map[abi_entry["type"]](abi_entry)


Expand Down
31 changes: 26 additions & 5 deletions starknet_devnet/blueprints/rpc/structures/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class RpcDeployTransactionResult(TypedDict):
contract_address: Felt


class RpcDeployAccountTransactionResult(TypedDict):
"""TypedDict for rpc deploy account transaction result"""

transaction_hash: TxnHash
contract_address: Felt


class MessageToL1(TypedDict):
"""TypedDict for rpc message from l2 to l1"""

Expand Down Expand Up @@ -87,28 +94,34 @@ class RpcBaseTransactionReceipt(TypedDict):


class RpcDeployReceipt(RpcBaseTransactionReceipt):
"""TypedDict for rpc declare transaction receipt"""
"""TypedDict for rpc deploy transaction receipt"""

contract_address: Felt


class RpcDeployAccountReceipt(RpcBaseTransactionReceipt):
"""TypedDict for rpc deploy account transaction receipt"""

contract_address: Felt


def rpc_invoke_receipt(txr: TransactionReceipt) -> RpcInvokeReceipt:
"""
Convert rpc invoke transaction receipt to rpc format
Convert gateway invoke transaction receipt to rpc format
"""
return rpc_base_transaction_receipt(txr)


def rpc_declare_receipt(txr: TransactionReceipt) -> RpcDeclareReceipt:
"""
Convert rpc declare transaction receipt to rpc format
Convert gateway declare transaction receipt to rpc format
"""
return rpc_base_transaction_receipt(txr)


def rpc_deploy_receipt(txr: TransactionReceipt) -> RpcDeployReceipt:
"""
Convert rpc deploy transaction receipt to rpc format
Convert gateway deploy transaction receipt to rpc format
"""
base_receipt = rpc_base_transaction_receipt(txr)
transaction = state.starknet_wrapper.transactions.get_transaction(
Expand All @@ -122,9 +135,16 @@ def rpc_deploy_receipt(txr: TransactionReceipt) -> RpcDeployReceipt:
return receipt


def rpc_deploy_account_receipt(txr: TransactionReceipt) -> RpcDeployAccountReceipt:
"""
Convert gateway deploy account transaction receipt to rpc format
"""
return rpc_deploy_receipt(txr)


def rpc_l1_handler_receipt(txr: TransactionReceipt) -> RpcL1HandlerReceipt:
"""
Convert rpc l1 handler transaction receipt to rpc format
Convert gateway l1 handler transaction receipt to rpc format
"""
return rpc_base_transaction_receipt(txr)

Expand Down Expand Up @@ -196,6 +216,7 @@ def rpc_transaction_receipt(txr: TransactionReceipt) -> dict:
TransactionType.INVOKE_FUNCTION: rpc_invoke_receipt,
TransactionType.DECLARE: rpc_declare_receipt,
TransactionType.L1_HANDLER: rpc_l1_handler_receipt,
TransactionType.DEPLOY_ACCOUNT: rpc_deploy_account_receipt,
}
transaction = state.starknet_wrapper.transactions.get_transaction(
hex(txr.transaction_hash)
Expand Down
5 changes: 2 additions & 3 deletions starknet_devnet/blueprints/rpc/structures/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def rpc_block_status(block_status: str) -> RpcBlockStatus:
Address = Felt
NumAsHex = str

# Pending transactions will not be supported since it
# doesn't make much sense with the current implementation of devnet
RpcTxnType = Literal["DECLARE", "DEPLOY", "INVOKE", "L1_HANDLER"]
RpcTxnType = Literal["DECLARE", "DEPLOY", "INVOKE", "L1_HANDLER", "DEPLOY_ACCOUNT"]


def rpc_txn_type(transaction_type: str) -> RpcTxnType:
Expand All @@ -69,6 +67,7 @@ def rpc_txn_type(transaction_type: str) -> RpcTxnType:
TransactionType.DECLARE.name: "DECLARE",
TransactionType.INVOKE_FUNCTION.name: "INVOKE",
TransactionType.L1_HANDLER.name: "L1_HANDLER",
TransactionType.DEPLOY_ACCOUNT.name: "DEPLOY_ACCOUNT",
}
if transaction_type not in txn_type_map:
raise RpcError(
Expand Down
32 changes: 32 additions & 0 deletions starknet_devnet/blueprints/rpc/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
RpcBroadcastedTxn,
make_declare,
make_deploy,
make_deploy_account,
RpcBroadcastedDeployAccountTxn,
)
from starknet_devnet.blueprints.rpc.structures.responses import (
rpc_transaction_receipt,
RpcInvokeTransactionResult,
RpcDeclareTransactionResult,
RpcDeployTransactionResult,
RpcDeployAccountTransactionResult,
)
from starknet_devnet.blueprints.rpc.structures.types import (
TxnHash,
Expand Down Expand Up @@ -143,6 +146,33 @@ async def add_deploy_transaction(deploy_transaction: RpcBroadcastedDeployTxn) ->
)


async def add_deploy_account_transaction(
deploy_account_transaction: RpcBroadcastedDeployAccountTxn,
) -> dict:
"""
Submit a new deploy account transaction
"""
deploy_account_tx = make_deploy_account(deploy_account_transaction)

contract_address, transaction_hash = await state.starknet_wrapper.deploy_account(
external_tx=deploy_account_tx
)

status_response = state.starknet_wrapper.transactions.get_transaction_status(
hex(transaction_hash)
)
if (
status_response["tx_status"] == "REJECTED"
and "is not declared" in status_response["tx_failure_reason"].error_message
):
raise RpcError(code=28, message="Class hash not found")

return RpcDeployAccountTransactionResult(
transaction_hash=rpc_felt(transaction_hash),
contract_address=rpc_felt(contract_address),
)


def make_transaction(txn: RpcBroadcastedTxn) -> AccountTransaction:
"""
Convert RpcBroadcastedTxn to AccountTransaction
Expand All @@ -154,6 +184,8 @@ def make_transaction(txn: RpcBroadcastedTxn) -> AccountTransaction:
return make_declare(txn)
if txn_type == "DEPLOY":
return make_deploy(txn)
if txn_type == "DEPLOY_ACCOUNT":
return make_deploy_account(txn)
raise NotImplementedError(f"Unexpected type {txn_type}.")


Expand Down
Loading