UNPKG

rubic-sdk-next

Version:
183 lines 7.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RubicApiService = void 0; const rxjs_1 = require("rxjs"); const socket_io_client_1 = require("socket.io-client"); const errors_1 = require("../../common/errors"); const unapproved_contract_error_1 = require("../../common/errors/proxy/unapproved-contract-error"); const unapproved_method_error_1 = require("../../common/errors/proxy/unapproved-method-error"); const unlisted_error_1 = require("../../common/errors/proxy/unlisted-error"); const trade_expired_error_1 = require("../../common/errors/swap/trade-expired.error"); const injector_1 = require("../../core/injector/injector"); const transform_utils_1 = require("./transform-utils"); const viem_1 = require("viem"); const rubic_api_link_mapping_1 = require("./constants/rubic-api-link-mapping"); class RubicApiService { get apiUrl() { const rubicApiLink = rubic_api_link_mapping_1.rubicApiLinkMapping[this.envType]; return rubicApiLink ? rubicApiLink : 'https://api-v2.rubic.exchange'; } constructor(envType) { this.envType = envType; this.client = this.getSocket(); this.latestQuoteParams = null; } getSocket() { const ioClient = (0, socket_io_client_1.io)(this.apiUrl, { reconnectionDelayMax: 10000, path: '/api/routes/ws/', transports: ['websocket'] }); return ioClient; } calculateAsync(params, attempt = 0) { this.latestQuoteParams = params; if (attempt > 2) { return; } if (this.client.connected) { this.client.emit('calculate', params); } else { const repeatInterval = 3000; setTimeout(() => { this.calculateAsync(params, attempt + 1); }, repeatInterval); } } stopCalculation() { if (this.latestQuoteParams) { this.client.emit('stopCalculation'); this.latestQuoteParams = null; } } async fetchSwapData(body) { try { const result = await injector_1.Injector.httpClient.post(`${this.apiUrl}/api/routes/swap`, body); if ('error' in result) { throw this.getApiError(result.error); } return result; } catch (err) { if (err instanceof errors_1.RubicSdkError) { throw err; } if ('error' in err) { throw this.getApiError(err.error.error); } throw this.getApiError(err); } } fetchRoutes(body) { return injector_1.Injector.httpClient.post(`${this.apiUrl}/api/routes/quoteAll`, body); } async fetchBestSwapData(body) { try { const result = await injector_1.Injector.httpClient.post(`${this.apiUrl}/api/routes/swapBest`, body); if ('error' in result) { throw this.getApiError(result.error); } return result; } catch (err) { if (err instanceof errors_1.RubicSdkError) { throw err; } if ('error' in err) { throw this.getApiError(err.error.error); } throw this.getApiError(err); } } fetchCelerRefundData() { // return Injector.httpClient.post<TransactionInterface>( // `${this.apiUrl}/api/routes/swap`, // body // ); } disconnectSocket() { this.client.disconnect(); } closetSocket() { this.client.close(); } handleQuotesAsync() { return (0, rxjs_1.fromEvent)(this.client, 'events').pipe((0, rxjs_1.concatMap)(wsResponse => { const { trade, total, calculated, data } = wsResponse; let promise = Promise.resolve(null); const rubicApiError = data ? { ...data, type: wsResponse.type } : null; if (this.latestQuoteParams) { promise = this.latestQuoteParams?.srcTokenBlockchain !== this.latestQuoteParams?.dstTokenBlockchain ? transform_utils_1.TransformUtils.transformCrossChain(trade, this.latestQuoteParams, rubicApiError) : transform_utils_1.TransformUtils.transformOnChain(trade, this.latestQuoteParams, rubicApiError); } return (0, rxjs_1.from)(promise).pipe((0, rxjs_1.catchError)(err => { console.log(err); return (0, rxjs_1.of)(null); }), (0, rxjs_1.map)(wrappedTrade => ({ total, calculated, wrappedTrade, ...(data && { tradeType: wsResponse.type }) }))); })); } fetchCrossChainTxStatus(srcTxHash) { return injector_1.Injector.httpClient.get(`${this.apiUrl}/api/info/status?srcTxHash=${srcTxHash}`); } fetchCrossChainTxStatusExtended(srcTxHash, rubicId) { return injector_1.Injector.httpClient.get(`${this.apiUrl}/api/info/statusExtended?srcTxHash=${srcTxHash}&rubicId=${rubicId}`); } getMessageToAuthWallet(walletAddress) { return injector_1.Injector.httpClient.get(`${this.apiUrl}/api/utility/authWalletMessage?walletAddress=${walletAddress}`); } getApiError(result) { switch (result.code) { case 3003: { return new errors_1.InsufficientFundsError(result.data.tokenSymbol); } case 3004: { return new errors_1.InsufficientFundsGasPriceValueError(); } case 3005: { return new viem_1.ExecutionRevertedError(); } case 3006: { return new errors_1.UnsupportedReceiverAddressError(); } case 4001: { return new errors_1.RubicSdkError('Meson only supports proxy swaps!'); } case 4002: { const method = result.reason.split('Selector - ')?.[1]?.slice(0, -1); return new unapproved_method_error_1.UnapprovedMethodError(method || 'Unknown'); } case 4003: { const contract = result.reason.split('Contract - ')[1]?.slice(0, -1); return new unapproved_contract_error_1.UnapprovedContractError(contract || 'Unknown'); } case 4004: { const contractAndSelector = result.reason.split('Selector - ')?.[1]?.slice(0, -1); const [method, contract] = contractAndSelector?.split('. Contract - ') || [ 'Unknown', 'Unknown' ]; return new unlisted_error_1.UnlistedError(contract || 'Unknown', method || 'Unknown'); } case 1004: { return new trade_expired_error_1.TradeExpiredError(); } } return new errors_1.RubicSdkError(result?.reason || 'Unknown error'); } } exports.RubicApiService = RubicApiService; //# sourceMappingURL=rubic-api-service.js.map