UNPKG

rubic-sdk

Version:
163 lines • 7.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChangellyCcrProvider = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("../../../../../common/errors"); const tokens_1 = require("../../../../../common/tokens"); const blockchain_1 = require("../../../../../common/utils/blockchain"); const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee"); const cross_chain_trade_type_1 = require("../../models/cross-chain-trade-type"); const cross_chain_provider_1 = require("../common/cross-chain-provider"); const proxy_cross_chain_evm_trade_1 = require("../common/proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade"); const changelly_ccr_trade_1 = require("./changelly-ccr-trade"); const changelly_specific_chain_ticker_1 = require("./constants/changelly-specific-chain-ticker"); const changelly_supported_chains_1 = require("./constants/changelly-supported-chains"); const native_token_data_1 = require("./constants/native-token-data"); const changelly_api_service_1 = require("./services/changelly-api-service"); class ChangellyCcrProvider extends cross_chain_provider_1.CrossChainProvider { constructor() { super(...arguments); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.CHANGELLY; } isSupportedBlockchain(fromBlockchain) { return changelly_supported_chains_1.changellySupportedChains.some(chain => chain === fromBlockchain); } async calculate(from, toToken, options) { // provider doesn't support deposit from proxy contract const useProxy = false; try { const feeInfo = await this.getFeeInfo(from.blockchain, options.providerAddress, from, useProxy); const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(from, feeInfo.rubicProxy?.platformFee?.percent); const changellyTokens = await this.getFromToChangellyTokens(from, toToken); const response = await changelly_api_service_1.ChangellyApiService.getFixedRateEstimation({ from: changellyTokens.fromToken.ticker, to: changellyTokens.toToken.ticker, amountFrom: fromWithoutFee.tokenAmount.toFixed() }); if (!response.result && response.error) { if (response.error.message.includes('Invalid amount for')) { const tokenLimits = response.error.data.limits; const minMaxError = this.checkMinMaxErrors(from, tokenLimits.min.from, tokenLimits.max.from); if (minMaxError) { return { tradeType: this.type, trade: this.getEmptyTrade(from, toToken, feeInfo, options.providerAddress, changellyTokens), error: minMaxError }; } } throw new Error(response.error.message); } const quote = response.result[0]; const toAmount = new bignumber_js_1.default(quote.amountTo); const toAmountMin = toAmount; const rateId = quote.id; const to = new tokens_1.PriceTokenAmount({ ...toToken.asStruct, tokenAmount: toAmount }); const routePath = await this.getRoutePath(from, to); const trade = new changelly_ccr_trade_1.ChangellyCcrTrade({ from, to, changellyTokens, feeInfo, gasData: null, priceImpact: from.calculatePriceImpactPercent(to), providerAddress: options.providerAddress, useProxy, toTokenAmountMin: toAmountMin, routePath, onChainTrade: null, rateId }); return { trade, tradeType: this.type }; } catch (err) { return { tradeType: this.type, trade: null, error: err }; } } checkMinMaxErrors(fromToken, minFrom, maxFrom) { if (fromToken.tokenAmount.lt(minFrom)) { return new errors_1.MinAmountError(new bignumber_js_1.default(minFrom), fromToken.symbol); } if (fromToken.tokenAmount.gt(maxFrom)) { return new errors_1.MaxAmountError(new bignumber_js_1.default(maxFrom), fromToken.symbol); } return null; } async getFromToChangellyTokens(from, to) { const { result: tokenList } = await changelly_api_service_1.ChangellyApiService.fetchTokenList(); return { fromToken: this.getChangellyToken(tokenList, from), toToken: this.getChangellyToken(tokenList, to) }; } getChangellyToken(tokenList, token) { const tokenBlockchain = this.getBlockchain(token.blockchain); const changellytoken = tokenList.find(fetchedToken => fetchedToken.blockchain.toLowerCase() === tokenBlockchain && ((fetchedToken.contractAddress && (0, blockchain_1.compareAddresses)(fetchedToken.contractAddress, token.address)) || (!fetchedToken.contractAddress && token.isNative) || this.isNativeTokenWithAddress(fetchedToken))); if (!changellytoken) { throw new errors_1.NotSupportedTokensError(); } return changellytoken; } getBlockchain(blockchain) { const specificChainTicker = changelly_specific_chain_ticker_1.changellySpecificChainTickers[blockchain]; return specificChainTicker ? specificChainTicker : blockchain.toLowerCase(); } isNativeTokenWithAddress(currency) { return native_token_data_1.changellyNativeTokensData.some(nativeTokenData => nativeTokenData.ticker === currency.ticker && nativeTokenData.blockchain === currency.blockchain.toLowerCase() && nativeTokenData.address === currency.contractAddress); } async getRoutePath(from, to) { return [ { type: 'cross-chain', provider: cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.CHANGELLY, path: [from, to] } ]; } getEmptyTrade(from, toToken, feeInfo, providerAddress, changellyTokens) { const to = new tokens_1.PriceTokenAmount({ ...toToken.asStruct, tokenAmount: new bignumber_js_1.default(0) }); const trade = new changelly_ccr_trade_1.ChangellyCcrTrade({ from, to, toTokenAmountMin: new bignumber_js_1.default(0), priceImpact: 0, feeInfo, changellyTokens, // rateId: '', routePath: [], useProxy: false, providerAddress, gasData: null, onChainTrade: null, rateId: '' }); return trade; } async getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy) { return proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy); } } exports.ChangellyCcrProvider = ChangellyCcrProvider; //# sourceMappingURL=changelly-ccr-provider.js.map