@settlemint/sdk-portal
Version:
Portal API client module for SettleMint SDK, providing access to smart contract portal services and APIs
246 lines (245 loc) • 8.95 kB
TypeScript
/* SettleMint Portal SDK - Blockchain Portal Optimized */
import { AbstractSetupSchema, FragmentOf, ResultOf, VariablesOf, initGraphQLTada, readFragment } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { z } from "zod";
import * as graphql_ws0 from "graphql-ws";
import { Address, Hex, TransactionReceipt as TransactionReceipt$1 } from "viem";
//#region src/utils/websocket-client.d.ts
/**
* Options for the GraphQL WebSocket client
*/
interface WebsocketClientOptions {
/**
* The GraphQL endpoint URL for the Portal API
*/
portalGraphqlEndpoint: string;
/**
* The access token for authentication with the Portal API
*/
accessToken?: string;
}
/**
* Creates a GraphQL WebSocket client for the Portal API
*
* @param {WebsocketClientOptions} options - The options for the client
* @returns {Client} The GraphQL WebSocket client
* @example
* import { getWebsocketClient } from "@settlemint/sdk-portal";
*
* const client = getWebsocketClient({
* portalGraphqlEndpoint: "https://portal.settlemint.com/graphql",
* accessToken: "your-access-token",
* });
*/
declare function getWebsocketClient({
portalGraphqlEndpoint,
accessToken
}: WebsocketClientOptions): graphql_ws0.Client;
//#endregion
//#region src/utils/wait-for-transaction-receipt.d.ts
/**
* Represents an event emitted during a transaction execution
*/
interface TransactionEvent {
/** The name of the event that was emitted */
eventName: string;
/** The arguments emitted by the event */
args: Record<string, unknown>;
/** Indexed event parameters used for filtering and searching */
topics: Hex[];
}
/**
* Represents the structure of a blockchain transaction receipt
*/
interface TransactionReceipt extends TransactionReceipt$1<string, number, "Success" | "Reverted"> {
/** The raw reason for transaction reversion, if applicable */
revertReason: string;
/** Human-readable version of the revert reason */
revertReasonDecoded: string;
/** Array of events emitted during the transaction */
events: TransactionEvent[];
/** The address of the contract deployed in the transaction */
contractAddress: Address;
}
/**
* Represents the structure of a blockchain transaction with its receipt
*/
interface Transaction {
receipt: TransactionReceipt;
/** The hash of the transaction (duplicate of receipt.transactionHash) */
transactionHash: string;
/** The sender address (duplicate of receipt.from) */
from: string;
/** Timestamp when the transaction was created */
createdAt: string;
/** The contract address involved in the transaction */
address: string;
/** The name of the function called in the transaction */
functionName: string;
/** Whether the transaction is a contract deployment */
isContract: boolean;
}
/**
* Options for waiting for a transaction receipt
*/
interface WaitForTransactionReceiptOptions extends WebsocketClientOptions {
/** Optional timeout in milliseconds before the operation fails */
timeout?: number;
}
/**
* Waits for a blockchain transaction receipt by subscribing to transaction updates via GraphQL.
* This function polls until the transaction is confirmed or the timeout is reached.
*
* @param transactionHash - The hash of the transaction to wait for
* @param options - Configuration options for the waiting process
* @returns The transaction details including receipt information when the transaction is confirmed
* @throws Error if the transaction receipt cannot be retrieved within the specified timeout
*
* @example
* import { waitForTransactionReceipt } from "@settlemint/sdk-portal";
*
* const transaction = await waitForTransactionReceipt("0x123...", {
* portalGraphqlEndpoint: "https://example.settlemint.com/graphql",
* accessToken: "your-access-token",
* timeout: 30000 // 30 seconds timeout
* });
*/
declare function waitForTransactionReceipt(transactionHash: string, options: WaitForTransactionReceiptOptions): Promise<Transaction>;
//#endregion
//#region src/utils/wallet-verification-challenge.d.ts
/**
* Custom error class for challenge-related errors
*/
declare class ChallengeError extends Error {
readonly code: string;
constructor(message: string, code: string);
}
/**
* Options for handling a wallet verification challenge
*/
interface HandleWalletVerificationChallengeOptions<Setup extends AbstractSetupSchema> {
/** The portal client instance */
portalClient: GraphQLClient;
/** The GraphQL query builder */
portalGraphql: initGraphQLTada<Setup>;
/** The ID of the verification challenge */
verificationId: string;
/** The wallet address to verify */
userWalletAddress: Address;
/** The verification code provided by the user */
code: string | number;
/** The type of verification being performed */
verificationType: "otp" | "secret-code" | "pincode";
}
/**
* Handles a wallet verification challenge by generating an appropriate response
*
* @param options - The options for handling the wallet verification challenge
* @returns Promise resolving to an object containing the challenge response and optionally the verification ID
* @throws {ChallengeError} If the challenge cannot be created or is invalid
* @example
* import { createPortalClient } from "@settlemint/sdk-portal";
* import { handleWalletVerificationChallenge } from "@settlemint/sdk-portal";
*
* const { client, graphql } = createPortalClient({
* instance: "https://portal.example.com/graphql",
* accessToken: "your-access-token"
* });
*
* const result = await handleWalletVerificationChallenge({
* portalClient: client,
* portalGraphql: graphql,
* verificationId: "verification-123",
* userWalletAddress: "0x123...",
* code: "123456",
* verificationType: "otp"
* });
*/
declare function handleWalletVerificationChallenge<const Setup extends AbstractSetupSchema>({
portalClient,
portalGraphql,
verificationId,
userWalletAddress,
code,
verificationType
}: HandleWalletVerificationChallengeOptions<Setup>): Promise<{
challengeResponse: string;
verificationId?: string;
}>;
//#endregion
//#region src/portal.d.ts
/**
* Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.
*/
type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
/**
* Schema for validating Portal client configuration options.
*/
declare const ClientOptionsSchema: z.ZodObject<{
instance: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
accessToken: z.ZodOptional<z.ZodString>;
cache: z.ZodOptional<z.ZodEnum<{
default: "default";
"force-cache": "force-cache";
"no-cache": "no-cache";
"no-store": "no-store";
"only-if-cached": "only-if-cached";
reload: "reload";
}>>;
}, z.core.$strip>;
/**
* Type representing the validated client options.
*/
type ClientOptions = z.infer<typeof ClientOptionsSchema>;
/**
* Creates a Portal GraphQL client with the provided configuration.
*
* @param options - Configuration options for the Portal client
* @param clientOptions - Additional GraphQL client configuration options
* @returns An object containing the configured GraphQL client and graphql helper function
* @throws If the provided options fail validation
*
* @example
* import { createPortalClient } from "@settlemint/sdk-portal";
* import { loadEnv } from "@settlemint/sdk-utils/environment";
* import { createLogger, requestLogger } from "@settlemint/sdk-utils/logging";
* import type { introspection } from "@schemas/portal-env";
*
* const env = await loadEnv(false, false);
* const logger = createLogger();
*
* const { client: portalClient, graphql: portalGraphql } = createPortalClient<{
* introspection: introspection;
* disableMasking: true;
* scalars: {
* // Change unknown to the type you are using to store metadata
* JSON: unknown;
* };
* }>(
* {
* instance: env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT!,
* accessToken: env.SETTLEMINT_ACCESS_TOKEN!,
* },
* {
* fetch: requestLogger(logger, "portal", fetch) as typeof fetch,
* },
* );
*
* // Making GraphQL queries
* const query = portalGraphql(`
* query GetPendingTransactions {
* getPendingTransactions {
* count
* }
* }
* `);
*
* const result = await portalClient.request(query);
*/
declare function createPortalClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
client: GraphQLClient;
graphql: initGraphQLTada<Setup>;
};
//#endregion
export { ClientOptions, ClientOptionsSchema, type FragmentOf, type HandleWalletVerificationChallengeOptions, RequestConfig, type ResultOf, type Transaction, type TransactionEvent, type TransactionReceipt, type VariablesOf, type WaitForTransactionReceiptOptions, type WebsocketClientOptions, createPortalClient, getWebsocketClient, handleWalletVerificationChallenge, readFragment, waitForTransactionReceipt };
//# sourceMappingURL=portal.d.ts.map