⚠️Blast API (blastapi.io) ends Oct 31. Migrate to Dwellir and skip the 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

JavaScript

const stateCall = async (method, data, blockHash = null) => {
const params = blockHash ? [method, data, blockHash] : [method, data];

const response = await fetch('https://api-polkadot.n.dwellir.com', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'state_call',
params: params,
id: 1
})
});

const result = await response.json();
return result.result;
};

// Get account nonce
const getNonce = async (address) => {
const data = '0x' + Buffer.from(address.slice(2), 'hex').toString('hex');
const result = await stateCall('AccountNonceApi_account_nonce', data);
return parseInt(result, 16);
};

// Query transaction fees
const queryFees = async (encodedTx) => {
const result = await stateCall(
'TransactionPaymentApi_query_info',
encodedTx
);
// Decode the result based on the expected format
return result;
};

Python

import requests
import json
from substrateinterface import SubstrateInterface

def state_call(method, data, block_hash=None):
url = "https://api-polkadot.n.dwellir.com"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"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-polkadot.n.dwellir.com")
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

TypeScript (@polkadot/api)

import { ApiPromise, WsProvider } from '@polkadot/api';

async function makeRuntimeCall() {
const provider = new WsProvider('wss://api-polkadot.n.dwellir.com');
const api = await ApiPromise.create({ provider });

// Using high-level API (preferred)
const nonce = await api.call.accountNonceApi.accountNonce(
'ADDRESS_HERE'
);
console.log('Account nonce:', nonce.toNumber());

// Using low-level state_call
const address = api.createType('AccountId', 'ADDRESS_HERE');
const result = await api.rpc.state.call(
'AccountNonceApi_account_nonce',
address.toHex()
);
console.log('Raw result:', result.toHex());

// Query transaction payment
const tx = api.tx.balances.transfer('RECIPIENT', 1000000000000);
const paymentInfo = await api.call.transactionPaymentApi.queryInfo(
tx.toHex(),
tx.length
);
console.log('Fee:', paymentInfo.toHuman());

await api.disconnect();
}

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