UNPKG

@chorus-one/substrate

Version:

All-in-one toolkit for building staking dApps on Substrate Network SDK blockchains(Polkadot, Kusama, etc.)

65 lines (64 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscanIndexer = void 0; /** * This class provides the functionality to interact with Subscan Indexer API. */ class SubscanIndexer { network; headers; /** * This creates a new SubscanIndexer instance. * * @param params - Initialization parameters * @param params.network - Substrate network name e.g. 'polkadot', 'kusama' * @param params.headers - (Optional) HTTP headers to include in requests * * @returns An instance of SubscanIndexer. */ constructor(params) { const { network, headers } = params; this.network = network; this.headers = new Headers(); this.headers.append('Content-Type', 'application/json'); if (headers) { headers.forEach((header) => { this.headers.append(header.key, header.value); }); } } async getTxStatus(txHash) { const raw = JSON.stringify({ hash: txHash, only_extrinsic_event: true }); const options = { method: 'POST', headers: this.headers, body: raw, redirect: 'follow' }; const response = await fetch(`https://${this.network}.api.subscan.io/api/scan/extrinsic`, options); const jsonResponse = (await response.json()); if (jsonResponse.code !== undefined && jsonResponse.code !== 0) { throw new Error(`Subscan API returned error: ${jsonResponse.message}`); } if (!jsonResponse.data) { return { status: 'unknown', receipt: jsonResponse }; } if (jsonResponse.data.pending) { return { status: 'pending', receipt: jsonResponse }; } if (jsonResponse.data.error !== null) { return { status: 'failure', receipt: jsonResponse }; } if (jsonResponse.data.success) { return { status: 'success', receipt: jsonResponse }; } if (jsonResponse.data.success !== null && !jsonResponse.data.success) { return { status: 'failure', receipt: jsonResponse }; } return { status: 'unknown', receipt: jsonResponse }; } } exports.SubscanIndexer = SubscanIndexer;