Skip to content

Commit 7c38fae

Browse files
authored
[indexer] Use unsafeApi to query evm state for tron networks (#716)
1 parent 56416d0 commit 7c38fae

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

sdk/packages/indexer/scripts/generate-compose.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath } from "node:url"
66
import Handlebars from "handlebars"
77
import { getEnv, getValidChains } from "../src/configs"
88

9-
const EVM_IMAGE = "polytopelabs/subql-node-ethereum:v6.3.1"
9+
const EVM_IMAGE = "polytopelabs/subql-node-ethereum:v6.3.4-0"
1010
const SUBSTRATE_IMAGE = "subquerynetwork/subql-node-substrate:v5.9.1"
1111

1212
// Setup paths

sdk/packages/indexer/src/configs/config-mainnet.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"chainId": "1",
4141
"startBlock": 21099382,
4242
"stateMachineId": "EVM-1",
43+
"blockConfirmations": 2,
4344
"contracts": {
4445
"ethereumHost": "0x792A6236AF69787C40cF76b69B4c8c7B28c4cA20",
4546
"erc6160ext20": "0x6B175474E89094C44Da98b954EedeAC495271d0F",

sdk/packages/indexer/src/configs/config-testnet.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"chainId": "11155111",
2929
"startBlock": 6876874,
3030
"stateMachineId": "EVM-11155111",
31+
"blockConfirmations": 5,
3132
"contracts": {
3233
"ethereumHost": "0x2EdB74C269948b60ec1000040E104cef0eABaae8",
3334
"handlerV1": "0xBf814fb3fD3eAaEB6a08C5A245c92da8b586f670",

sdk/packages/indexer/src/types/global.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Store } from "@subql/types-core"
2-
import { Provider, Signer } from "ethers"
2+
import { Provider, Signer, providers } from "ethers"
33
import { Logger } from "@subql/types"
44
import { ApiPromise } from "@polkadot/api"
55

@@ -8,6 +8,7 @@ import "@types/node-fetch"
88
declare global {
99
const store: Store
1010
const api: Provider | Signer | ApiPromise
11+
const unsafeApi: providers.JsonRpcProvider | undefined
1112
const logger: Logger
1213
const chainId: string
1314
}

sdk/packages/indexer/src/utils/state-machine.helper.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ApiPromise } from "@polkadot/api"
55
import { Option as PolkadotOption } from "@polkadot/types"
66
import { logger } from "ethers"
77
import { TextEncoder } from "util"
8-
import { CHAINS_BY_ISMP_HOST } from "@/constants"
8+
import { CHAINS_BY_ISMP_HOST, TRON_CHAIN_IDS } from "@/constants"
99
import { providers } from "ethers"
1010
import { Codec } from "@polkadot/types/types"
1111

@@ -146,12 +146,26 @@ export async function fetchStateCommitmentsEVM(params: {
146146
// Generate keys for timestamp, overlay, and state root
147147
const [timestampKey, overlayKey, stateRootKey] = generateStateCommitmentKeys(paraId, height)
148148

149-
// For all other EVM chains, use SubQuery's api provider which correctly
150-
// queries storage at the specific block height being indexed.
151-
const provider = api as providers.Provider
152-
const timestampValue = await provider.getStorageAt(hostContract, bytesToHex(timestampKey))
153-
const overlayRootValue = await provider.getStorageAt(hostContract, bytesToHex(overlayKey))
154-
const stateRootValue = await provider.getStorageAt(hostContract, bytesToHex(stateRootKey))
149+
const isTron = TRON_CHAIN_IDS.has(chainId)
150+
if (isTron && !unsafeApi) {
151+
logger.warn("unsafeApi is not available for Tron chain, cannot fetch state commitments")
152+
return null
153+
}
154+
const provider = (isTron ? unsafeApi! : api) as providers.Provider
155+
156+
let timestampValue: string | null
157+
let overlayRootValue: string | null
158+
let stateRootValue: string | null
159+
160+
if (isTron) {
161+
timestampValue = await provider.getStorageAt(hostContract, bytesToHex(timestampKey), "latest")
162+
overlayRootValue = await provider.getStorageAt(hostContract, bytesToHex(overlayKey), "latest")
163+
stateRootValue = await provider.getStorageAt(hostContract, bytesToHex(stateRootKey), "latest")
164+
} else {
165+
timestampValue = await provider.getStorageAt(hostContract, bytesToHex(timestampKey))
166+
overlayRootValue = await provider.getStorageAt(hostContract, bytesToHex(overlayKey))
167+
stateRootValue = await provider.getStorageAt(hostContract, bytesToHex(stateRootKey))
168+
}
155169

156170
if (!timestampValue) {
157171
return null

0 commit comments

Comments
 (0)