Skip to main content

Generate ledger key parameters with a symbol key using the Python SDK

In the increment example contract stores an integer value in a ledger entry that is identified by a key with the symbol COUNTER. The value of this ledger key can be derived using the following code snippets.

from stellar_sdk import xdr
from stellar_sdk.strkey import StrKey
from stellar_sdk.soroban import Symbol

def get_ledger_key_symbol(contract_id: str, symbol_text: str) -> str:
ledger_key = xdr.LedgerKey(
type=xdr.LedgerEntryType.CONTRACT_DATA,
contract_data=xdr.LedgerKeyContractData(
contract_id=xdr.hash.Hash(StrKey.decode_contract(contract_id)),
key=Symbol(symbol_text).to_xdr_sc_val(),
),
)
return ledger_key.to_xdr()

print(
get_ledger_key_symbol(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE",
"COUNTER"
)
)