⚠️Blast API (blastapi.io) ends Oct 31. Migrate to Dwellir and skip Alchemy's expensive compute units.
Switch Today →
Skip to main content

state_call - JSON-RPC Method

Description

Performs a runtime API call at a given block. This JSON-RPC method allows you to execute runtime functions to query information without submitting transactions. Common uses include querying account nonces, validating transactions, and accessing runtime-specific data.

Parameters

ParameterTypeRequiredDescription
methodstringYesName of the runtime API method to call
datastringYesHex-encoded parameters for the method
blockHashstringNoBlock hash to call at. If omitted, uses the latest block

Returns

FieldTypeDescription
resultstringHex-encoded result from the runtime API call

Request Example

{
"jsonrpc": "2.0",
"method": "state_call",
"params": [
"AccountNonceApi_account_nonce",
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"
],
"id": 1
}

Response Example

{
"jsonrpc": "2.0",
"result": "0x0000000000000000",
"id": 1
}

Common Runtime API Methods

MethodDescriptionParameters
AccountNonceApi_account_nonceGet account nonceAccount address
TransactionPaymentApi_query_infoQuery transaction feeEncoded extrinsic
TransactionPaymentApi_query_fee_detailsGet detailed fee breakdownEncoded extrinsic
BlockBuilder_apply_extrinsicValidate extrinsicEncoded extrinsic
Core_versionGet runtime versionNone
Metadata_metadataGet runtime metadataNone

Code Examples

import requests
import json
from substrateinterface import SubstrateInterface

def state_call(method, data, block_hash=None):
url = "https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY"
headers = {
"Content-Type": "application/json"
}

params = [method, data]
if block_hash:
params.append(block_hash)

payload = {
"jsonrpc": "2.0",
"method": "state_call",
"params": params,
"id": 1
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()["result"]

# Example: Get account nonce
def get_account_nonce(address):
# Encode address properly
substrate = SubstrateInterface(url="https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY")
account_id = substrate.ss58_decode(address)
data = "0x" + account_id

result = state_call("AccountNonceApi_account_nonce", data)
return int(result, 16)

# Example: Query runtime version
def get_runtime_version():
result = state_call("Core_version", "0x")
# Decode SCALE-encoded version info
return result

Fee Calculation Example

// Calculate fees before submission
async function calculateFees(api, transaction) {
const info = await api.call.transactionPaymentApi.queryInfo(
transaction.toHex(),
transaction.encodedLength
);

return {
weight: info.weight.toNumber(),
class: info.class.toString(),
partialFee: info.partialFee.toNumber()
};
}

Use Cases

  1. Fee Estimation: Calculate transaction costs before submission
  2. Nonce Management: Get current account nonce for transaction construction
  3. Validation: Verify transactions will succeed before submission
  4. Runtime Queries: Access runtime-specific information
  5. Custom Logic: Execute specialized runtime functions

Notes

  • Results are SCALE-encoded and need proper decoding
  • Method names are case-sensitive
  • Some methods require specific runtime APIs to be available
  • Use high-level APIs when available for automatic encoding/decoding
  • Results may vary based on the block state