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
Parameter | Type | Required | Description |
---|---|---|---|
method | string | Yes | Name of the runtime API method to call |
data | string | Yes | Hex-encoded parameters for the method |
blockHash | string | No | Block hash to call at. If omitted, uses the latest block |
Returns
Field | Type | Description |
---|---|---|
result | string | Hex-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
Method | Description | Parameters |
---|---|---|
AccountNonceApi_account_nonce | Get account nonce | Account address |
TransactionPaymentApi_query_info | Query transaction fee | Encoded extrinsic |
TransactionPaymentApi_query_fee_details | Get detailed fee breakdown | Encoded extrinsic |
BlockBuilder_apply_extrinsic | Validate extrinsic | Encoded extrinsic |
Core_version | Get runtime version | None |
Metadata_metadata | Get runtime metadata | None |
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
- Fee Estimation: Calculate transaction costs before submission
- Nonce Management: Get current account nonce for transaction construction
- Validation: Verify transactions will succeed before submission
- Runtime Queries: Access runtime-specific information
- 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
Related Methods
state_getMetadata
- Get runtime metadatastate_getRuntimeVersion
- Get runtime versionstate_getStorage
- Query storage directlyauthor_submitExtrinsic
- Submit transactions