UNPKG

@reservoir0x/relay-sdk

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

171 lines 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createClient = exports.getClient = exports.configureDynamicChains = exports.RelayClient = void 0; const chains_1 = require("viem/chains"); const logger_js_1 = require("./utils/logger.js"); const actions = require("./actions/index.js"); const utils = require("./utils/index.js"); const servers_js_1 = require("./constants/servers.js"); const version_js_1 = require("./version.js"); let _client; const _backupChains = [ chains_1.mainnet, chains_1.base, chains_1.zora, chains_1.optimism, chains_1.arbitrum, chains_1.arbitrumNova ].map((chain) => utils.convertViemChainToRelayChain(chain)); const _backupTestnetChains = [chains_1.sepolia, chains_1.baseGoerli].map((chain) => utils.convertViemChainToRelayChain(chain)); class RelayClient { log(message, level = logger_js_1.LogLevel.Info) { return (0, logger_js_1.log)(message, level, this.logLevel); } constructor(options) { Object.defineProperty(this, "version", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "uiVersion", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "baseApiUrl", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "source", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "logLevel", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "pollingInterval", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "maxPollingAttemptsBeforeTimeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "useGasFeeEstimations", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "chains", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "utils", { enumerable: true, configurable: true, writable: true, value: { ...utils } }); Object.defineProperty(this, "actions", { enumerable: true, configurable: true, writable: true, value: actions }); this.version = version_js_1.SDK_VERSION; this.uiVersion = options.uiVersion; this.baseApiUrl = options.baseApiUrl ?? servers_js_1.MAINNET_RELAY_API; this.logLevel = options.logLevel !== undefined ? options.logLevel : logger_js_1.LogLevel.None; this.pollingInterval = options.pollingInterval; this.maxPollingAttemptsBeforeTimeout = options.maxPollingAttemptsBeforeTimeout; this.useGasFeeEstimations = options.useGasFeeEstimations ?? true; if (options.chains) { this.chains = options.chains; } else if (options.baseApiUrl?.includes('testnets')) { this.chains = _backupTestnetChains; } else { this.chains = _backupChains; } if (!options.source) { if (typeof window !== 'undefined') { let host = location.hostname; if (host.indexOf('www.') === 0) { host = host.replace('www.', ''); } this.source = host; console.warn('RelaySDK automatically generated a source based on the url, we recommend providing a source when initializing the sdk. Refer to our docs for steps on how to do this: https://docs.relay.link/references/sdk/getting-started#configuration'); } } else { this.source = options.source; } } configure(options) { this.baseApiUrl = options.baseApiUrl ? options.baseApiUrl : this.baseApiUrl; this.source = options.source ? options.source : this.source; this.logLevel = options.logLevel !== undefined ? options.logLevel : logger_js_1.LogLevel.None; this.pollingInterval = options.pollingInterval ? options.pollingInterval : this.pollingInterval; this.maxPollingAttemptsBeforeTimeout = options.maxPollingAttemptsBeforeTimeout ? options.maxPollingAttemptsBeforeTimeout : this.maxPollingAttemptsBeforeTimeout; this.useGasFeeEstimations = options.useGasFeeEstimations !== undefined ? options.useGasFeeEstimations : this.useGasFeeEstimations; if (options.chains) { this.chains = options.chains; } } } exports.RelayClient = RelayClient; async function configureDynamicChains() { try { const chains = await utils.fetchChainConfigs(_client.baseApiUrl); _client.chains = chains; return chains; } catch (e) { _client.log(['Failed to fetch remote chain configuration, falling back', e], logger_js_1.LogLevel.Error); throw e; } } exports.configureDynamicChains = configureDynamicChains; function getClient() { return _client; } exports.getClient = getClient; function createClient(options) { if (!_client) { _client = new RelayClient({ ...options }); } else { _client.configure(options); } return _client; } exports.createClient = createClient; //# sourceMappingURL=client.js.map