UNPKG

rubic-sdk

Version:
88 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LifiEvmOnChainTrade = void 0; const errors_1 = require("../../../../../../../common/errors"); const updated_rates_error_1 = require("../../../../../../../common/errors/cross-chain/updated-rates-error"); const price_token_amount_1 = require("../../../../../../../common/tokens/price-token-amount"); const rubic_proxy_contract_address_1 = require("../../../../../../cross-chain/calculation-manager/providers/common/constants/rubic-proxy-contract-address"); const aggregator_evm_on_chain_trade_abstract_1 = require("../../../common/on-chain-aggregator/aggregator-evm-on-chain-trade-abstract"); const lifi_on_chain_api_service_1 = require("../services/lifi-on-chain-api-service"); class LifiEvmOnChainTrade extends aggregator_evm_on_chain_trade_abstract_1.AggregatorEvmOnChainTrade { get spenderAddress() { return this.useProxy ? rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].gateway : this.providerGateway; } get dexContractAddress() { throw new errors_1.RubicSdkError('Dex address is unknown before swap is started'); } get toTokenAmountMin() { return this._toTokenAmountMin; } constructor(tradeStruct, providerAddress) { super(tradeStruct, providerAddress); this._toTokenAmountMin = new price_token_amount_1.PriceTokenAmount({ ...this.to.asStruct, weiAmount: tradeStruct.toTokenWeiAmountMin }); this.type = tradeStruct.type; this.route = tradeStruct.route; this.providerGateway = this.route.steps[0].estimate.approvalAddress; } getSwapError(err) { if ('code' in err && [400, 500, 503].includes(err.code)) { throw new errors_1.SwapRequestError(); } if (this.isDeflationError()) { throw new errors_1.LowSlippageDeflationaryTokenError(); } if (err instanceof updated_rates_error_1.UpdatedRatesError || err instanceof errors_1.RubicSdkError) { throw err; } throw new errors_1.LifiPairIsUnavailableError(); } async getTransactionConfigAndAmount(options) { const firstStep = this.route.steps[0]; const step = { ...firstStep, action: { ...firstStep.action, fromAddress: options.fromAddress || this.walletAddress, toAddress: options.receiverAddress || this.walletAddress }, execution: { status: 'NOT_STARTED', process: [ { message: 'Preparing swap.', startedAt: Date.now(), status: 'STARTED', type: 'SWAP' } ] } }; try { const swapResponse = await lifi_on_chain_api_service_1.LifiOnChainApiService.getQuote(step.action.fromChainId, step.action.toChainId, step.action.fromToken.address, step.action.toToken.address, step.action.fromAmount, step.action.fromAddress, step.action.slippage); const { transactionRequest, estimate: { toAmount } } = swapResponse; return { tx: { data: transactionRequest.data, to: transactionRequest.to, value: transactionRequest.value, gas: transactionRequest.gasLimit, gasPrice: transactionRequest.gasPrice }, toAmount }; } catch (err) { if ('statusCode' in err && 'message' in err) { throw new errors_1.RubicSdkError(err.message); } throw err; } } } exports.LifiEvmOnChainTrade = LifiEvmOnChainTrade; //# sourceMappingURL=lifi-evm-on-chain-trade.js.map