UNPKG

rubic-sdk

Version:
127 lines • 6.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrbiterBridgeProvider = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("../../../../../common/errors"); const tokens_1 = require("../../../../../common/tokens"); const blockchain_name_1 = require("../../../../../core/blockchain/models/blockchain-name"); const web3_pure_1 = require("../../../../../core/blockchain/web3-pure/web3-pure"); const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee"); const orbiter_bridge_factory_1 = require("./orbiter-bridge-factory"); 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 orbiter_supported_blockchains_1 = require("./models/orbiter-supported-blockchains"); const orbiter_api_service_1 = require("./services/orbiter-api-service"); const orbiter_utils_1 = require("./services/orbiter-utils"); class OrbiterBridgeProvider extends cross_chain_provider_1.CrossChainProvider { constructor() { super(...arguments); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.ORBITER_BRIDGE; this.orbiterQuoteConfigs = []; } isSupportedBlockchain(blockchain) { return orbiter_supported_blockchains_1.orbiterSupportedBlockchains.some(supportedBlockchain => supportedBlockchain === blockchain); } async calculate(from, toToken, options) { const fromBlockchain = from.blockchain; let useProxy = options?.useProxy?.[this.type] ?? true; if (fromBlockchain === blockchain_name_1.BLOCKCHAIN_NAME.TRON) { useProxy = false; } try { this.orbiterQuoteConfigs = await orbiter_api_service_1.OrbiterApiService.getQuoteConfigs(); const feeInfo = await this.getFeeInfo(fromBlockchain, options.providerAddress, from, useProxy); const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(from, feeInfo.rubicProxy?.platformFee?.percent); const quoteConfig = orbiter_utils_1.OrbiterUtils.getQuoteConfig({ from, to: toToken, configs: this.orbiterQuoteConfigs }); const minAmountBN = new bignumber_js_1.default(quoteConfig.minAmt); const maxAmountBN = new bignumber_js_1.default(quoteConfig.maxAmt); try { this.checkAmountLimits(from, maxAmountBN, minAmountBN); } catch (error) { return { tradeType: this.type, error, trade: this.getEmptyTrade(from, toToken, feeInfo, quoteConfig, options.providerAddress) }; } const { result } = await orbiter_api_service_1.OrbiterApiService.getReceiveAmount({ line: quoteConfig.line, value: fromWithoutFee.stringWeiAmount }); const to = new tokens_1.PriceTokenAmount({ ...toToken.asStruct, tokenAmount: web3_pure_1.Web3Pure.fromWei(result.receiveAmount, toToken.decimals) }); const toAmountWithoutTradeFee = to.tokenAmount.minus(to.tokenAmount.multipliedBy(result.router.tradeFee)); const trade = orbiter_bridge_factory_1.OrbiterBridgeFactory.createTrade({ crossChainTrade: { feeInfo, from, gasData: await this.getGasData(from), to, priceImpact: from.calculatePriceImpactPercent(to), quoteConfig, toTokenAmountMin: toAmountWithoutTradeFee }, providerAddress: options.providerAddress, routePath: await this.getRoutePath(from, to), useProxy }); return { trade, tradeType: this.type }; } catch (err) { const rubicSdkError = cross_chain_provider_1.CrossChainProvider.parseError(err); return { trade: null, error: rubicSdkError, tradeType: this.type }; } } async getRoutePath(fromToken, toToken) { return [{ type: 'cross-chain', provider: this.type, path: [fromToken, toToken] }]; } checkAmountLimits(from, maxAmountBN, minAmountBN) { if (from.tokenAmount.gt(maxAmountBN)) { throw new errors_1.MaxAmountError(maxAmountBN, from.symbol); } if (from.tokenAmount.lt(minAmountBN)) { throw new errors_1.MinAmountError(minAmountBN, from.symbol); } } async getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy) { return proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy); } getEmptyTrade(from, toToken, feeInfo, quoteConfig, providerAddress) { const to = new tokens_1.PriceTokenAmount({ ...toToken.asStruct, tokenAmount: new bignumber_js_1.default(0) }); const trade = orbiter_bridge_factory_1.OrbiterBridgeFactory.createTrade({ crossChainTrade: { from, gasData: null, feeInfo, to, priceImpact: 0, quoteConfig, toTokenAmountMin: new bignumber_js_1.default(0) }, providerAddress, useProxy: false, routePath: [] }); return trade; } } exports.OrbiterBridgeProvider = OrbiterBridgeProvider; //# sourceMappingURL=orbiter-bridge-provider.js.map