UNPKG

rubic-sdk

Version:
97 lines 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AcrossCrossChainProvider = void 0; const errors_1 = require("../../../../../common/errors"); const tokens_1 = require("../../../../../common/tokens"); const wrapped_addresses_1 = require("../../../../../common/tokens/constants/wrapped-addresses"); const errors_2 = require("../../../../../common/utils/errors"); const blockchain_id_1 = require("../../../../../core/blockchain/utils/blockchains-info/constants/blockchain-id"); 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 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 across_cross_chain_trade_1 = require("./across-cross-chain-trade"); const across_ccr_supported_chains_1 = require("./constants/across-ccr-supported-chains"); const across_api_service_1 = require("./services/across-api-service"); class AcrossCrossChainProvider extends cross_chain_provider_1.CrossChainProvider { constructor() { super(...arguments); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.ACROSS; } isSupportedBlockchain(blockchain) { return across_ccr_supported_chains_1.acrossCcrSupportedChains.some(supportedChain => supportedChain === blockchain); } async calculate(from, toToken, options) { const fromBlockchain = from.blockchain; const useProxy = options?.useProxy?.[this.type] ?? true; try { 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 quoteRequestParams = { originChainId: blockchain_id_1.blockchainId[fromBlockchain], destinationChainId: blockchain_id_1.blockchainId[toToken.blockchain], inputToken: from.isNative ? wrapped_addresses_1.wrappedAddress[from.blockchain] : from.address, outputToken: toToken.isNative ? wrapped_addresses_1.wrappedAddress[toToken.blockchain] : toToken.address, amount: fromWithoutFee.stringWeiAmount, skipAmountLimit: true }; const feeQuote = await across_api_service_1.AcrossApiService.getFeeQuote(quoteRequestParams); const toAmount = fromWithoutFee.weiAmount.minus(feeQuote.totalRelayFee.total); const to = new tokens_1.PriceTokenAmount({ ...toToken.asStruct, weiAmount: toAmount }); const routePath = await this.getRoutePath(from, to); const trade = new across_cross_chain_trade_1.AcrossCrossChainTrade({ from, to, toTokenAmountMin: to.tokenAmount, priceImpact: from.calculatePriceImpactPercent(to), gasData: await this.getGasData(from), feeInfo, slippage: options.slippageTolerance, acrossFeeQuoteRequestParams: quoteRequestParams }, options.providerAddress, routePath, useProxy); try { this.checkMinMaxAmounts(from, feeQuote.limits); } catch (err) { return { trade, tradeType: this.type, error: err }; } return { trade, tradeType: this.type }; } catch (err) { return { trade: null, tradeType: this.type, error: (0, errors_2.parseError)(err) }; } } async getRoutePath(fromToken, toToken) { return [{ type: 'cross-chain', provider: this.type, path: [fromToken, toToken] }]; } async getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy) { return proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy); } checkMinMaxAmounts(from, limits) { if (from.weiAmount.lt(limits.minDeposit)) { throw new errors_1.MinAmountError(web3_pure_1.Web3Pure.fromWei(limits.minDeposit, from.decimals), from.symbol); } if (from.weiAmount.gt(limits.maxDeposit)) { throw new errors_1.MaxAmountError(web3_pure_1.Web3Pure.fromWei(limits.maxDeposit, from.decimals), from.symbol); } } } exports.AcrossCrossChainProvider = AcrossCrossChainProvider; //# sourceMappingURL=across-cross-chain-provider.js.map