UNPKG

@f5i23q999d/cow-sdk

Version:

<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>

105 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BridgingSdk = void 0; const trading_1 = require("../../trading/index.js"); const common_1 = require("../../common/index.js"); const getQuoteWithoutBridge_1 = require("./getQuoteWithoutBridge.js"); const getQuoteWithBridging_1 = require("./getQuoteWithBridging.js"); const wallet_1 = require("../../common/utils/wallet.js"); const getErc20Decimals_1 = require("./getErc20Decimals.js"); const log_1 = require("../../common/utils/log.js"); /** * SDK for bridging for swapping tokens between different chains. */ class BridgingSdk { options; config; constructor(options) { this.options = options; const { providers, tradingSdk, ...restOptions } = options; // For simplicity, we support only a single provider in the initial implementation if (!providers || providers.length !== 1) { throw new Error('Current implementation only supports a single bridge provider'); } if (options.enableLogging !== undefined) { (0, log_1.enableLogging)(options.enableLogging); } this.config = { providers, ...restOptions, tradingSdk: tradingSdk ?? new trading_1.TradingSdk(), }; } get provider() { const { providers } = this.config; return providers[0]; } /** * Get the providers for the bridging. */ getProviders() { return this.config.providers; } /** * Get the available sources networks for the bridging. */ async getSourceNetworks() { return common_1.ALL_SUPPORTED_CHAINS; } /** * Get the available target networks for the bridging. */ async getTargetNetworks() { return this.provider.getNetworks(); } /** * Get the available buy tokens for buying in a specific target chain * * @param param * @returns */ async getBuyTokens(targetChainId) { return this.provider.getBuyTokens(targetChainId); } /** * Get quote details, including a callback function to post the order on-chain. * * This method support both, cross-chain swaps and single-chain swap. * * The return type will be either `QuoteAndPost` or `BridgeQuoteAndPost`. * * To safely assert the type in Typescript, you can use: * - `isBridgeQuoteAndPost(result)` utility. * - `isQuoteAndPost(result)` utility. * - `assertIsBridgeQuoteAndPost(result)` assertion. * - `assertIsQuoteAndPost(result)` assertion. * * @throws Error if no path is found */ async getQuote(quoteBridgeRequest, advancedSettings) { const { sellTokenChainId, buyTokenChainId } = quoteBridgeRequest; const tradingSdk = this.config.tradingSdk; if (sellTokenChainId !== buyTokenChainId) { const signer = (0, wallet_1.getSigner)(quoteBridgeRequest.signer); const getErc20Decimals = this.config.getErc20Decimals ?? (0, getErc20Decimals_1.factoryGetErc20Decimals)(signer); // Cross-chain swap return (0, getQuoteWithBridging_1.getQuoteWithBridge)({ swapAndBridgeRequest: quoteBridgeRequest, advancedSettings, tradingSdk, provider: this.provider, getErc20Decimals, }); } else { // Single-chain swap return (0, getQuoteWithoutBridge_1.getQuoteWithoutBridge)({ quoteBridgeRequest, advancedSettings, tradingSdk, }); } } } exports.BridgingSdk = BridgingSdk; //# sourceMappingURL=BridgingSdk.js.map