Quickstart#

This guide will help you get started with parsing Starknet ABI JSON and encode and decode transaction calldata. Refer to the Installation guide for instructions on installing the Starknet ABI library.

Parse Starknet ABI JSON#

  1. Get the Starknet-ETH ABI JSON from Voyager and save it to a file named abi.json.

  2. Create a StarknetABI instance from the ABI JSON file:

import json
from nethermind.starknet_abi.core import StarknetAbi

with open("abi.json") as f:
    raw_abi = json.load(f)

# Create a StarknetABI instance
starknet_eth_abi = StarknetAbi.from_json(raw_abi, "starknet_eth", b"")

# View the ABI functions
print(starknet_eth_abi.functions)

# View the ABI implemented interfaces
print(starknet_eth_abi.implemented_interfaces)

Decode transaction calldata#

  1. Decode core types:

>>> from nethermind.starknet_abi.decode import decode_core_type, StarknetCoreType
>>> decode_core_type(StarknetCoreType.Bool, [0])
False
>>> decode_core_type(StarknetCoreType.U256, [12345, 0])
12345
>>> decode_core_type(StarknetCoreType.Felt, [256])
'0x0000000000000000000000000000000000000000000000000000000000000100'
  1. Decode from parameters:

>>> from nethermind.starknet_abi.decode import decode_from_params, StarknetCoreType, AbiParameter
>>> decode_from_params(
...     [AbiParameter("a", StarknetCoreType.U32), AbiParameter("b", StarknetCoreType.U32)],
...     [123456, 654321]
... )
{'a': 123456, 'b': 654321}
  1. Decode from types:

>>> from nethermind.starknet_abi.decode import decode_from_types, StarknetCoreType, StarknetArray
>>> decode_from_types([StarknetArray(StarknetCoreType.U8), StarknetCoreType.Bool], [3, 123, 244, 210, 0])
[[123, 244, 210], False]
>>> decode_from_types(
...     [StarknetCoreType.ContractAddress, StarknetCoreType.U256, StarknetCoreType.Bool],
...     [0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7, 250_000, 0, 1]
... )
['0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7', 250000, True]

Encode transaction calldata#

  1. Encode core types:

>>> from nethermind.starknet_abi.encode import encode_core_type, StarknetCoreType
>>> encode_core_type(StarknetCoreType.Bool, False)
[0]
>>> encode_core_type(StarknetCoreType.U256, 12345)
[12345, 0]
>>> encode_core_type(StarknetCoreType.Felt, "0x0000000000000000000000000000000000000000000000000000000000000100")
[256]
  1. Encode from parameters:

>>> from nethermind.starknet_abi.encode import encode_from_params, StarknetCoreType, AbiParameter
>>> encode_from_params(
...     [AbiParameter("a", StarknetCoreType.U32), AbiParameter("b", StarknetCoreType.U32)],
...     {"a": 123456, "b": 654321}
... )
[123456, 654321]
  1. Encode from types:

>>> from nethermind.starknet_abi.encode import encode_from_types, StarknetCoreType, StarknetArray
>>> encode_from_types([StarknetArray(StarknetCoreType.U8), StarknetCoreType.Bool], [[123, 244, 210], False])
[3, 123, 244, 210, 0]
>>> encode_from_types(
...     [StarknetCoreType.ContractAddress, StarknetCoreType.U256, StarknetCoreType.Bool],
...     ["0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", 250000, True]
... )
[2087021424722619777119509474943472645767659996348769578120564519014510906823, 250000, 0, 1]