UNPKG

rubic-sdk

Version:
184 lines • 9.24 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LifiEvmCrossChainTrade = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("../../../../../../common/errors"); const decorators_1 = require("../../../../../../common/utils/decorators"); const blockchains_info_1 = require("../../../../../../core/blockchain/utils/blockchains-info/blockchains-info"); const evm_web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/evm-web3-pure"); 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 rubic_proxy_contract_address_1 = require("../../common/constants/rubic-proxy-contract-address"); const evm_common_cross_chain_abi_1 = require("../../common/evm-cross-chain-trade/constants/evm-common-cross-chain-abi"); const gateway_rubic_cross_chain_abi_1 = require("../../common/evm-cross-chain-trade/constants/gateway-rubic-cross-chain-abi"); const evm_cross_chain_trade_1 = require("../../common/evm-cross-chain-trade/evm-cross-chain-trade"); const proxy_cross_chain_evm_trade_1 = require("../../common/proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade"); const lifi_api_service_1 = require("../services/lifi-api-service"); /** * Calculated Celer cross-chain trade. */ class LifiEvmCrossChainTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade { get fromBlockchain() { return this.from.blockchain; } get fromContractAddress() { return this.isProxyTrade ? rubic_proxy_contract_address_1.rubicProxyContractAddress[this.fromBlockchain].gateway : this.providerGateway; } get methodName() { return 'startBridgeTokensViaGenericCrossChain'; } get amountToCheck() { return web3_pure_1.Web3Pure.toWei(this.toTokenAmountMin, this.to.decimals); } constructor(crossChainTrade, providerAddress, routePath, useProxy) { super(providerAddress, routePath, useProxy); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.LIFI; this.isAggregator = true; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.route = crossChainTrade.route; this.providerGateway = this.route.steps[0].estimate.approvalAddress; this.gasData = crossChainTrade.gasData; this.toTokenAmountMin = crossChainTrade.toTokenAmountMin; this.feeInfo = crossChainTrade.feeInfo; this.slippage = crossChainTrade.slippage; this.priceImpact = crossChainTrade.priceImpact; this.onChainSubtype = crossChainTrade.onChainSubtype; this.bridgeType = crossChainTrade.bridgeType; } async swapDirect(options = {}) { try { await this.checkTradeErrors(); if (options.receiverAddress) { throw new errors_1.RubicSdkError('Receiver address not supported'); } await this.checkAllowanceAndApprove(options); const { onConfirm, gasPriceOptions } = options; let transactionHash; const onTransactionHash = (hash) => { if (onConfirm) { onConfirm(hash); } transactionHash = hash; }; // eslint-disable-next-line no-useless-catch try { const { data, value, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress); await this.web3Private.trySendTransaction(to, { data, value, onTransactionHash, gasPriceOptions }); return transactionHash; } catch (err) { throw err; } } catch (err) { if ([400, 500, 503].includes(err.code)) { throw new errors_1.SwapRequestError(); } throw err; } } async getContractParams(options) { const { data, value: providerValue, to: providerRouter } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress); const isEvmDestination = blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain); const receivingAsset = isEvmDestination ? this.to.address : this.from.address; const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData(options, { walletAddress: this.walletAddress, fromTokenAmount: this.from, toTokenAmount: this.to, srcChainTrade: null, providerAddress: this.providerAddress, type: `lifi:${this.bridgeType}`, fromAddress: this.walletAddress, toAddress: receivingAsset }); const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(this.from, this.feeInfo?.rubicProxy?.platformFee?.percent); const extraNativeFee = this.from.isNative ? new bignumber_js_1.default(providerValue).minus(fromWithoutFee.stringWeiAmount).toFixed() : new bignumber_js_1.default(providerValue).toFixed(); const providerData = await proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getGenericProviderData(providerRouter, data, this.fromBlockchain, providerRouter, extraNativeFee); const methodArguments = [bridgeData, providerData]; const value = this.getSwapValue(providerValue); const transactionConfiguration = evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].router, evm_common_cross_chain_abi_1.evmCommonCrossChainAbi, this.methodName, methodArguments, value); const sendingToken = this.from.isNative ? [] : [this.from.address]; const sendingAmount = this.from.isNative ? [] : [this.from.stringWeiAmount]; return { contractAddress: rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].gateway, contractAbi: gateway_rubic_cross_chain_abi_1.gatewayRubicCrossChainAbi, methodName: 'startViaRubic', methodArguments: [sendingToken, sendingAmount, transactionConfiguration.data], value }; } async getTransactionConfigAndAmount(receiverAddress) { const firstStep = this.route.steps[0]; const step = { ...firstStep, action: { ...firstStep.action, fromAddress: this.walletAddress, toAddress: receiverAddress || this.walletAddress }, execution: { status: 'NOT_STARTED', process: [ { message: 'Preparing transaction.', startedAt: Date.now(), status: 'STARTED', type: 'CROSS_CHAIN' } ] } }; try { const swapResponse = await lifi_api_service_1.LifiApiService.getQuote(step.action.fromChainId, step.action.toChainId, step.action.fromToken.symbol, step.action.toToken.symbol, step.action.fromAmount, step.action.fromAddress, step.action.toAddress, step.action.slippage); return { config: swapResponse.transactionRequest, amount: swapResponse.estimate.toAmountMin }; } catch (err) { if ('statusCode' in err && 'message' in err) { throw new errors_1.RubicSdkError(err.message); } throw err; } } getTradeAmountRatio(fromUsd) { return fromUsd.dividedBy(this.to.tokenAmount); } getTradeInfo() { return { estimatedGas: this.estimatedGas, feeInfo: this.feeInfo, priceImpact: this.priceImpact || null, slippage: this.slippage * 100, routePath: this.routePath }; } } exports.LifiEvmCrossChainTrade = LifiEvmCrossChainTrade; __decorate([ (0, decorators_1.Cache)({ maxAge: 15000 }) ], LifiEvmCrossChainTrade.prototype, "getTradeAmountRatio", null); //# sourceMappingURL=lifi-evm-cross-chain-trade.js.map