UNPKG

rubic-sdk

Version:
176 lines • 8.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SquidrouterCrossChainTrade = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("../../../../../common/errors"); const errors_2 = require("../../../../../common/utils/errors"); const evm_web3_pure_1 = require("../../../../../core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/evm-web3-pure"); const injector_1 = require("../../../../../core/injector/injector"); const squidrouter_api_service_1 = require("../../../../common/providers/squidrouter/services/squidrouter-api-service"); 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 bridge_type_1 = require("../common/models/bridge-type"); const proxy_cross_chain_evm_trade_1 = require("../common/proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade"); const squidrouter_contract_address_1 = require("./constants/squidrouter-contract-address"); const get_cross_chain_gas_data_1 = require("../../utils/get-cross-chain-gas-data"); /** * Calculated DeBridge cross-chain trade. */ class SquidrouterCrossChainTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade { /** @internal */ static async getGasData(from, toToken, transactionRequest, feeInfo, receiverAddress, providerAddress) { const fromBlockchain = from.blockchain; const walletAddress = injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(fromBlockchain).address; if (!walletAddress) { return null; } try { const trade = new SquidrouterCrossChainTrade({ from, to: toToken, gasData: null, priceImpact: 0, allowanceTarget: '', slippage: 0, feeInfo, cryptoFeeToken: from, onChainTrade: null, onChainSubtype: { from: undefined, to: undefined }, transactionRequest }, providerAddress || evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS, [], false); return (0, get_cross_chain_gas_data_1.getCrossChainGasData)(trade, receiverAddress); } catch (_err) { return null; } } get fromBlockchain() { return this.from.blockchain; } get fromContractAddress() { return this.isProxyTrade ? rubic_proxy_contract_address_1.rubicProxyContractAddress[this.fromBlockchain].gateway : squidrouter_contract_address_1.SquidrouterContractAddress[this.fromBlockchain].providerGateway; } get methodName() { return 'startBridgeTokensViaGenericCrossChain'; } constructor(crossChainTrade, providerAddress, routePath, useProxy) { super(providerAddress, routePath, useProxy); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.SQUIDROUTER; this.isAggregator = false; this.onChainSubtype = { from: undefined, to: undefined }; this.bridgeType = bridge_type_1.BRIDGE_TYPE.SQUIDROUTER; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.gasData = crossChainTrade.gasData; this.priceImpact = crossChainTrade.priceImpact; this.allowanceTarget = crossChainTrade.allowanceTarget; this.slippage = crossChainTrade.slippage; this.onChainTrade = crossChainTrade.onChainTrade; this.toTokenAmountMin = this.to.tokenAmount.multipliedBy(1 - crossChainTrade.slippage); this.feeInfo = crossChainTrade.feeInfo; this.cryptoFeeToken = crossChainTrade.cryptoFeeToken; this.onChainSubtype = crossChainTrade.onChainSubtype; this.transactionRequest = crossChainTrade.transactionRequest; } async swapDirect(options = {}) { this.checkWalletConnected(); await this.checkAllowanceAndApprove(options); let transactionHash; try { const { data, value, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress || this.walletAddress); const { onConfirm } = options; const onTransactionHash = (hash) => { if (onConfirm) { onConfirm(hash); } transactionHash = hash; }; await this.web3Private.trySendTransaction(to, { onTransactionHash, data, value, gasPriceOptions: options.gasPriceOptions }); return transactionHash; } catch (err) { if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) { return transactionHash; } throw (0, errors_2.parseError)(err); } } async getContractParams(options) { const { data, value: providerValue, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress || this.walletAddress); const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData(options, { walletAddress: this.walletAddress, fromTokenAmount: this.from, toTokenAmount: this.to, srcChainTrade: this.onChainTrade, providerAddress: this.providerAddress, type: `native:${this.type}`, fromAddress: this.walletAddress }); 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(to, data, this.fromBlockchain, to, 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 }; } getTradeAmountRatio(fromUsd) { const usdCryptoFee = this.cryptoFeeToken.price.multipliedBy(this.cryptoFeeToken.tokenAmount); return fromUsd.plus(usdCryptoFee.isNaN() ? 0 : usdCryptoFee).dividedBy(this.to.tokenAmount); } getTradeInfo() { return { estimatedGas: this.estimatedGas, feeInfo: this.feeInfo, priceImpact: this.priceImpact ?? null, slippage: this.slippage * 100, routePath: this.routePath }; } async getTransactionConfigAndAmount(receiverAddress) { const requestParams = { ...this.transactionRequest, toAddress: receiverAddress }; const res = await squidrouter_api_service_1.SquidRouterApiService.getRoute(requestParams); this.squidrouterRequestId = res['x-request-id']; const route = res.route; return { config: { data: route.transactionRequest.data, value: route.transactionRequest.value, to: route.transactionRequest.target }, amount: route.estimate.toAmount }; } } exports.SquidrouterCrossChainTrade = SquidrouterCrossChainTrade; //# sourceMappingURL=squidrouter-cross-chain-trade.js.map