UNPKG

rubic-sdk

Version:
102 lines 5.55 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneInchTrade = void 0; const errors_1 = require("../../../../../../common/errors"); const native_tokens_1 = require("../../../../../../common/tokens/constants/native-tokens"); const decorators_1 = require("../../../../../../common/utils/decorators"); const evm_web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/evm-web3-pure"); const token_native_address_proxy_1 = require("../../../../../common/utils/token-native-address-proxy"); const constants_1 = require("./constants/constants"); const one_inch_api_service_1 = require("./one-inch-api-service"); const on_chain_trade_type_1 = require("../../common/models/on-chain-trade-type"); const aggregator_evm_on_chain_trade_abstract_1 = require("../../common/on-chain-aggregator/aggregator-evm-on-chain-trade-abstract"); class OneInchTrade extends aggregator_evm_on_chain_trade_abstract_1.AggregatorEvmOnChainTrade { /** @internal */ static async checkIfNeedApproveAndThrowError(from, toToken, fromWithoutFee, fromAddress, useProxy) { const needApprove = await new OneInchTrade({ from, to: toToken, fromWithoutFee, useProxy, path: [from, toToken] }, evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS).needApprove(fromAddress); if (needApprove) { throw new errors_1.RubicSdkError('Approve is needed'); } } get type() { return on_chain_trade_type_1.ON_CHAIN_TRADE_TYPE.ONE_INCH; } constructor(tradeStruct, providerAddress) { super(tradeStruct, providerAddress); this.dexContractAddress = tradeStruct.dexContractAddress; this.disableMultihops = tradeStruct.disableMultihops; this.transactionData = tradeStruct.data; this.availableProtocols = tradeStruct.availableProtocols; this.wrappedPath = (0, token_native_address_proxy_1.createTokenNativeAddressProxyInPathStartAndEnd)(this.path, constants_1.oneinchApiParams.nativeAddress); this.nativeSupportedFromWithoutFee = (0, token_native_address_proxy_1.createTokenNativeAddressProxy)(tradeStruct.fromWithoutFee, constants_1.oneinchApiParams.nativeAddress); this.nativeSupportedTo = (0, token_native_address_proxy_1.createTokenNativeAddressProxy)(tradeStruct.to, constants_1.oneinchApiParams.nativeAddress); } async getTransactionConfigAndAmount(options) { const fromTokenAddress = this.nativeSupportedFromWithoutFee.address; const toTokenAddress = this.nativeSupportedTo.address; const swapRequest = { params: { src: fromTokenAddress, dst: toTokenAddress, amount: this.nativeSupportedFromWithoutFee.stringWeiAmount, slippage: (this.slippageTolerance * 100).toString(), from: options.fromAddress || this.walletAddress, disableEstimate: true, ...(this.disableMultihops && { connectorTokens: `${fromTokenAddress},${toTokenAddress}` }), ...(options.receiverAddress && { receiver: options.receiverAddress }), ...(this.availableProtocols && { protocols: this.availableProtocols }) } }; const { tx, dstAmount } = await this.getResponseFromApiToTransactionRequest(swapRequest); return { tx: { ...tx, gas: String(tx.gas) }, toAmount: dstAmount }; } async getResponseFromApiToTransactionRequest(params) { return one_inch_api_service_1.OneInchApiService.oneInchHttpGetRequest('swap', this.from.blockchain, params); } specifyError(err) { const inchError = err?.error || err; if (inchError) { if ('message' in inchError) { if (inchError.message?.includes('cannot estimate')) { const nativeToken = native_tokens_1.nativeTokensList[this.from.blockchain]?.symbol; const message = `1inch sets increased costs on gas fee. For transaction enter less ${nativeToken} amount or top up your ${nativeToken} balance.`; return new errors_1.RubicSdkError(message); } if (inchError.message?.includes('insufficient funds for transfer')) { return new errors_1.InsufficientFundsOneinchError(this.from.blockchain); } } if ('description' in inchError && inchError.description?.includes('cannot estimate')) { return new errors_1.LowSlippageError(); } } return null; } } exports.OneInchTrade = OneInchTrade; __decorate([ (0, decorators_1.Cache)({ maxAge: 15000 }) ], OneInchTrade.prototype, "getResponseFromApiToTransactionRequest", null); //# sourceMappingURL=one-inch-trade.js.map