import { Address } from '@safeblock/blockchain-utils'; import { Network } from 'ethers'; import { default as SafeBlock } from './index'; import { default as SdkException } from './sdk-exception'; import { default as SdkExtension, ExtractConfigExtensionsType, ExtractEvents } from './sdk-extension'; import { InternalMixinList, SdkMixins } from './sdk-mixins'; import { default as StateManager } from './state-manager'; import { ExchangeQuota, ExchangeRequest, SimulatedRoute } from '../types'; import { default as EventBus, EventIdentifier } from '../utils/event-bus'; type TAddressesList = { [p: string]: string; } & { default: string; }; type InstanceTypeOf any> = T extends new (...args: any) => infer R ? R : never; export interface ExtensionInitializeEnvironment> { sdk: SafeBlock; config: SdkConfig; eventBus: T; mixins: SdkMixins; } /** * SDK configuration */ export type SdkConfig = Partial<{ /** * Maximum allowed price‑impact difference between input * and output (%). Applies to valid routes only. * * @default 20 */ routePriceDifferenceLimit: number; /** * Custom handler for incoming SDK debug messages * * @param message debug message */ debugLogListener: (...message: any[]) => void; /** * Parameters for configuring the route simulation */ simulationLimit: { /** * Limit of the single chunk size in the simulation * * Each chunk represents a single multicall request to the node * * @default 40 */ chunkSizeLimit?: number; /** * Limit the maximum number of chunks that can be used for simulation per token * * @default unlimited */ chunksCountLimit?: number; }; /** * > Advanced option * * Provide custom mappings for network names */ customNetworkMappings: Record; /** * List of SDK extensions * * @param {ExtensionInitializeEnvironment} environment extension API surface * @returns {SdkExtension[]} array of extensions */ extensions: >(environment: ExtensionInitializeEnvironment) => SdkExtension[]; /** * If `true`, extensions that fail during initialization will * **not** cause an SDK init error. Such extensions are simply * disabled. * * > Use with caution: a direct call to a disabled extension will * > still raise an error. * * @default false */ allowExtensionsInitErrors: boolean; /** * > Advanced option * * Custom addresses for certain contracts */ contractAddresses: Partial<{ entryPoint: TAddressesList; quoter: TAddressesList; }>; /** * Custom backend URL and headers for route calculation */ backend: { url: string; headers?: Record; /** * Routes query cache lifetime in ms * @default 30_000 */ cacheTime?: number; }; }>; /** * SDK core */ export default class SdkCore extends StateManager { _extensions: ExtractConfigExtensionsType; protected readonly eventBus: EventBus, "">; protected readonly sdkConfig: SdkConfig; protected readonly mixins: SdkMixins<{ [key: string]: { [key: string]: { [key: string]: any; }; }; }, { [key: string]: { [key: string]: { [key: string]: any; }; }; } & InternalMixinList>; constructor(sdkConfig?: Configuration); /** * Register a mixin that mutates specific logic * * @param location global namespace the mixin belongs to * @param namespace local namespace the mixin belongs to * @param breakpoint mixin name (logical breakpoint) * @param callback function that mutates the value * @returns {string} mixin identifier */ addMixin(location: Location, namespace: Namespace, breakpoint: Breakpoint, callback: (value: Value) => Value): string; /** * Remove a mixin by its identifier * * @param {string} identifier mixin ID returned by `addMixin` */ removeMixin(identifier: string): void; /** * Add a new event listener * * @param event event to listen for * @param callback function invoked when the event fires * @param identifier optional listener ID * @returns {EventIdentifier} pointer to the registered listener */ addListener>(event: K, callback: (...args: ExtractEvents[K]) => any, identifier?: string): EventIdentifier; /** * Add an event listener that will fire only once * * @param event event to listen for * @param callback function invoked once when the event fires * @param identifier optional listener ID * @returns {EventIdentifier} pointer to the registered listener */ addListenerOnce>(event: K, callback: (...args: ExtractEvents[K]) => any, identifier?: string): EventIdentifier; /** * Remove an event listener * * @param event event name * @param callback registered function or its identifier * @returns {boolean} removal result */ removeListener>(event: K, callback?: ((...args: ExtractEvents[K]) => any) | EventIdentifier): boolean; /** * Find a swap route that satisfies the provided request * * > In many cases `createQuota` is more efficient, as it combines * > `findRoute` and `createQuotaFromRoute`. * * @param {ExchangeRequest} request swap request * @param signal abort controller signal * @returns {Promise} `SdkException` or a `SimulatedRoute` */ findRoute(request: ExchangeRequest, signal?: AbortSignal): Promise; /** * Create a quota from a pre‑computed swap route * * > In many cases `createQuota` is more efficient, as it combines * > `findRoute` and `createQuotaFromRoute`. * * @param from user address initiating the swap * @param route simulated swap route * @param signal abort controller signal * @returns {Promise} `SdkException` or an `ExchangeQuota` */ createQuotaFromRoute(from: Address, route: SimulatedRoute, signal?: AbortSignal): Promise; /** * Directly access a registered extension. * * > Use with caution: calling an extension that was not registered * > or was disabled during initialization will throw `SdkException`. * > This is one of the few methods that **throws** rather than returns errors. * * @param extension extension class to retrieve * @returns extension instance * @throws {SdkException} `ExtensionError` */ extension SdkExtension>(extension: T): InstanceTypeOf; /** * Safer alternative to `extension`. Executes the callback * only if the extension is available. * * @param extension extension class to retrieve * @param callback callback invoked with the extension instance * @returns callback result or `null` */ withExtension SdkExtension, R = any>(extension: T, callback: (instance: InstanceTypeOf) => R): R | null; /** * Internal routine that validates, initializes and registers * all provided extensions. * * @param extensions list of extensions * @param instance SDK instance * @param allowInitErrors allow initialization errors * @protected */ protected attachExtensions(extensions: ExtractConfigExtensionsType, instance: SafeBlock, allowInitErrors?: boolean): void; /** * Previously used to resolve the converter for route calculations. * Currently just a placeholder for future functionality. * * @returns {EvmConverter} converter instance * @private */ private resolveConverter; /** * Convert a simulated route into an exchange request * * @param route simulated route * @returns exchange request * @private */ private routeToRequest; } export {};