@settlemint/sdk-blockscout
Version:
Blockscout integration module for SettleMint SDK, enabling blockchain explorer and analytics functionality
113 lines (110 loc) • 4.04 kB
JavaScript
/* SettleMint Blockscout SDK - Blockchain Explorer */
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __settlemint_sdk_utils_http = __toESM(require("@settlemint/sdk-utils/http"));
const __settlemint_sdk_utils_runtime = __toESM(require("@settlemint/sdk-utils/runtime"));
const __settlemint_sdk_utils_validation = __toESM(require("@settlemint/sdk-utils/validation"));
const gql_tada = __toESM(require("gql.tada"));
const graphql_request = __toESM(require("graphql-request"));
const zod = __toESM(require("zod"));
//#region src/blockscout.ts
/**
* Schema for validating client options for the Blockscout client.
*/
const ClientOptionsSchema = zod.z.object({
instance: __settlemint_sdk_utils_validation.UrlOrPathSchema,
accessToken: __settlemint_sdk_utils_validation.ApplicationAccessTokenSchema.optional()
});
/**
* Creates a Blockscout GraphQL client with proper type safety using gql.tada
*
* @param options - Configuration options for the client
* @param clientOptions - Optional GraphQL client configuration options
* @returns An object containing the GraphQL client and initialized gql.tada function
* @throws Will throw an error if the options fail validation
* @example
* import { createBlockscoutClient } from '@settlemint/sdk-blockscout';
* import type { introspection } from "@schemas/blockscout-env";
* import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';
*
* const logger = createLogger();
*
* const { client, graphql } = createBlockscoutClient<{
* introspection: introspection;
* disableMasking: true;
* scalars: {
* AddressHash: string;
* Data: string;
* DateTime: string;
* Decimal: string;
* FullHash: string;
* Json: string;
* NonceHash: string;
* Wei: string;
* };
* }>({
* instance: process.env.SETTLEMINT_BLOCKSCOUT_ENDPOINT,
* accessToken: process.env.SETTLEMINT_ACCESS_TOKEN
* }, {
* fetch: requestLogger(logger, "blockscout", fetch) as typeof fetch,
* });
*
* // Making GraphQL queries
* const query = graphql(`
* query GetTransaction($hash: String!) {
* transaction(hash: $hash) {
* hash
* blockNumber
* value
* gasUsed
* }
* }
* `);
*
* const result = await client.request(query, {
* hash: "0x123abc..."
* });
*/
function createBlockscoutClient(options, clientOptions) {
(0, __settlemint_sdk_utils_runtime.ensureServer)();
const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ClientOptionsSchema, options);
const graphql = (0, gql_tada.initGraphQLTada)();
const fullUrl = new URL(validatedOptions.instance).toString();
return {
client: new graphql_request.GraphQLClient(fullUrl, {
...clientOptions,
headers: (0, __settlemint_sdk_utils_http.appendHeaders)(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
}),
graphql
};
}
//#endregion
exports.ClientOptionsSchema = ClientOptionsSchema;
exports.createBlockscoutClient = createBlockscoutClient;
Object.defineProperty(exports, 'readFragment', {
enumerable: true,
get: function () {
return gql_tada.readFragment;
}
});
//# sourceMappingURL=blockscout.cjs.map