Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions packages/node/src/ethereum/api.ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ import {
import { JsonRpcBatchProvider } from './ethers/json-rpc-batch-provider';
import { JsonRpcProvider } from './ethers/json-rpc-provider';
import { OPFormatterMixin } from './ethers/op/op-provider';
import {
TronJsonRpcBatchProvider,
TronJsonRpcProvider,
TronWsProvider,
} from './ethers/tron/tron-provider';
import { TRON_CHAIN_IDS } from './ethers/tron/tron-utils';
import { ConnectionInfo } from './ethers/web';
import SafeEthProvider from './safe-api';
import {
Expand Down Expand Up @@ -178,10 +184,33 @@ export class EthereumApi implements ApiWrapper {
//celo
if (network.chainId === 42220) {
if (this.client instanceof WebSocketProvider) {
this.client = new CeloWsProvider(this.client.connection.url);
this.client = new CeloWsProvider(this.client.connection.url, network);
} else {
this.client = new CeloJsonRpcBatchProvider(this.client.connection);
this.nonBatchClient = new CeloJsonRpcProvider(this.client.connection);
this.client = new CeloJsonRpcBatchProvider(
this.client.connection,
network,
);
this.nonBatchClient = new CeloJsonRpcProvider(
this.client.connection,
network,
);
this.applyBatchSize(this.config?.batchSize);
}
}

//tron
if (TRON_CHAIN_IDS.includes(network.chainId)) {
if (this.client instanceof WebSocketProvider) {
this.client = new TronWsProvider(this.client.connection.url, network);
} else {
this.client = new TronJsonRpcBatchProvider(
this.client.connection,
network,
);
this.nonBatchClient = new TronJsonRpcProvider(
this.client.connection,
network,
);
this.applyBatchSize(this.config?.batchSize);
}
}
Expand Down Expand Up @@ -324,7 +353,19 @@ export class EthereumApi implements ApiWrapper {

const block = formatBlock(rawBlock);

block.stateRoot = this.client.formatter.hash(block.stateRoot);
// Tron sometimes returns '0x' as stateRoot, which fails the formatter
// We only want to apply this fix for Tron networks
// Mainnet: 728126428, Shasta: 2494104990, Nile: 3448148188
if (
this.chainId &&
TRON_CHAIN_IDS.includes(this.chainId) &&
block.stateRoot === '0x'
) {
block.stateRoot =
'0x0000000000000000000000000000000000000000000000000000000000000000';
} else {
block.stateRoot = this.client.formatter.hash(block.stateRoot);
}

return block;
}
Expand Down
57 changes: 57 additions & 0 deletions packages/node/src/ethereum/ethers/tron/tron-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { Networkish } from '@ethersproject/networks';
import { WebSocketProvider } from '@ethersproject/providers';
import { JsonRpcBatchProvider } from '../json-rpc-batch-provider';
import { JsonRpcProvider } from '../json-rpc-provider';
import { ConnectionInfo } from '../web';
import { applyTronParamTransforms } from './tron-utils';

/**
* Tron-specific JsonRpcProvider that applies parameter transformations
* to handle Tron RPC limitations.
*/
export class TronJsonRpcProvider extends JsonRpcProvider {
constructor(url: string | ConnectionInfo, network?: Networkish) {
super(url, network);
}

async send(method: string, params: Array<any>): Promise<any> {
const chainId = this.network?.chainId ?? 0;
const cleanedParams = applyTronParamTransforms(method, params, chainId);
return super.send(method, cleanedParams);
}
}

/**
* Tron-specific JsonRpcBatchProvider that applies parameter transformations
* to handle Tron RPC limitations.
*/
export class TronJsonRpcBatchProvider extends JsonRpcBatchProvider {
constructor(url: string | ConnectionInfo, network?: Networkish) {
super(url, network);
}

async send(method: string, params: Array<any>): Promise<any> {
const chainId = this.network?.chainId ?? 0;
const cleanedParams = applyTronParamTransforms(method, params, chainId);
return super.send(method, cleanedParams);
}
}

/**
* Tron-specific WebSocketProvider that applies parameter transformations
* to handle Tron RPC limitations.
*/
export class TronWsProvider extends WebSocketProvider {
constructor(url: string, network?: Networkish) {
super(url, network);
}

async send(method: string, params: Array<any>): Promise<any> {
const chainId = this.network?.chainId ?? 0;
const cleanedParams = applyTronParamTransforms(method, params, chainId);
return super.send(method, cleanedParams);
}
}
126 changes: 126 additions & 0 deletions packages/node/src/ethereum/ethers/tron/tron-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

// Tron chain IDs: Mainnet, Shasta testnet, Nile testnet
export const TRON_CHAIN_IDS = [728126428, 2494104990, 3448148188];

// Methods that accept transaction objects as parameters
export const TRON_TRANSACTION_METHODS = ['eth_call'];

/**
* Methods that require block number parameter and the index of that parameter
*
* IMPORTANT TRON LIMITATION:
* Tron RPC rejects numeric block tags with error code -32602 and message:
* "QUANTITY not supported, just support TAG as latest"
*
* We forcibly replace ALL numeric block parameters with 'latest' for Tron chains.
* This means all state-querying methods (eth_call, eth_getBalance, eth_getCode,
* eth_getTransactionCount, eth_getStorageAt) will return tip/current state rather
* than point-in-time state for historical indexing.
*
* OPERATIONAL IMPACT:
* SubQL indexers CANNOT obtain historical state via these calls on Tron chains.
* All state queries will always return the current/latest state regardless of
* the block number requested.
*/
export const TRON_BLOCK_NUMBER_METHODS: Record<string, number> = {
eth_call: 1,
eth_getStorageAt: 2,
eth_getBalance: 1,
eth_getCode: 1,
eth_getTransactionCount: 1,
};

/**
* Remove type and accessList from transaction objects in params for Tron chains
*/
export function cleanParamsForTron(
params: Array<any>,
chainId: number,
): Array<any> {
if (!TRON_CHAIN_IDS.includes(chainId)) {
return params;
}

return params.map((param) => {
if (param && typeof param === 'object' && !Array.isArray(param)) {
const cleaned = { ...param };
delete cleaned.type;
delete cleaned.accessList;
return cleaned;
}
return param;
});
}

/**
* Replace block number parameter with 'latest' for Tron chains
*
* CRITICAL TRON RPC LIMITATION:
* The Tron RPC implementation does not support numeric block tags (QUANTITY).
* When a numeric block number is provided, Tron returns JSON-RPC error -32602
* with message: "QUANTITY not supported, just support TAG as latest"
*
* This function forcibly replaces any block number parameter with 'latest' to
* prevent this error. However, this introduces a significant limitation:
*
* LIMITATION:
* All state-querying methods will return the CURRENT/TIP state rather than
* point-in-time historical state. This means:
* - eth_call: Will execute against current state, not historical state
* - eth_getBalance: Returns current balance, not historical balance
* - eth_getCode: Returns current contract code, not historical code
* - eth_getTransactionCount: Returns current nonce, not historical nonce
* - eth_getStorageAt: Returns current storage value, not historical value
*
* IMPACT ON INDEXERS:
* SubQL indexers running on Tron chains CANNOT query historical state via
* these RPC methods. Indexers must rely solely on event logs and block data
* for historical information. Any smart contract state queries will always
* reflect the current state, not the state at the block being indexed.
*/
export function replaceBlockNumberForTron(
method: string,
params: Array<any>,
chainId: number,
): Array<any> {
if (!TRON_CHAIN_IDS.includes(chainId)) {
return params;
}

const blockNumberIndex = TRON_BLOCK_NUMBER_METHODS[method];
if (blockNumberIndex === undefined || params.length <= blockNumberIndex) {
return params;
}

// Always replace with 'latest' for Tron chains
// This prevents the -32602 error: "QUANTITY not supported, just support TAG as latest"
const cleaned = [...params];
cleaned[blockNumberIndex] = 'latest';
return cleaned;
}

/**
* Apply all Tron-specific parameter transformations for the given method.
* This combines both transaction object cleaning and block number replacement.
*
* NOTE: eth_call intentionally appears in both TRON_TRANSACTION_METHODS and
* TRON_BLOCK_NUMBER_METHODS, so both cleanParamsForTron and
* replaceBlockNumberForTron are applied in sequence. This dual-pass behavior
* is intentional and not a duplication bug.
*/
export function applyTronParamTransforms(
method: string,
params: Array<any>,
chainId: number,
): Array<any> {
let result = params;
if (TRON_TRANSACTION_METHODS.includes(method)) {
result = cleanParamsForTron(result, chainId);
}
if (TRON_BLOCK_NUMBER_METHODS[method] !== undefined) {
result = replaceBlockNumberForTron(method, result, chainId);
}
return result;
}
Loading