UNPKG

rubic-sdk

Version:
112 lines 7.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TeleSwapEvmCcrTrade = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); 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 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 teleswap_ccr_supported_chains_1 = require("../constants/teleswap-ccr-supported-chains"); const teleswap_contract_address_1 = require("../constants/teleswap-contract-address"); const teleswap_network_tickers_1 = require("../constants/teleswap-network-tickers"); const teleswap_swap_and_unwrap_abi_1 = require("../constants/teleswap-swap-and-unwrap-abi"); const teleswap_utils_service_1 = require("../services/teleswap-utils-service"); class TeleSwapEvmCcrTrade 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.from.blockchain].gateway : teleswap_contract_address_1.teleSwapContractAddresses[this.fromBlockchain]; } get methodName() { return 'startBridgeTokensViaGenericCrossChain'; } constructor(params) { const { routePath, useProxy, crossChainTrade, providerAddress } = params; super(providerAddress, routePath, useProxy); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.TELE_SWAP; this.onChainSubtype = { from: undefined, to: undefined }; this.bridgeType = bridge_type_1.BRIDGE_TYPE.TELE_SWAP; this.isAggregator = false; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.feeInfo = crossChainTrade.feeInfo; this.gasData = crossChainTrade.gasData; this.priceImpact = crossChainTrade.priceImpact; this.slippage = crossChainTrade.slippage; this.toTokenAmountMin = this.to.tokenAmount.multipliedBy(1 - this.slippage); this.teleSwapSdk = params.crossChainTrade.teleSwapSdk; this.fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(this.from, this.feeInfo.rubicProxy?.platformFee?.percent); } 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: 'teleswap', fromAddress: this.walletAddress, toAddress: receivingAsset }); const extraNativeFee = this.from.isNative ? new bignumber_js_1.default(providerValue).minus(this.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 fromTokenAddress = teleswap_utils_service_1.TeleSwapUtilsService.getTokenAddress(this.from); const swapParams = await this.teleSwapSdk.swapAndUnwrapInputs({ inputAmount: this.fromWithoutFee.stringWeiAmount, ...(fromTokenAddress && { inputToken: fromTokenAddress }) }, receiverAddress, teleswap_network_tickers_1.teleSwapNetworkTickers[this.fromBlockchain], this.toTokenAmountMin.toFixed()); const isFromBaseChain = teleswap_ccr_supported_chains_1.teleSwapBaseChains.includes(this.fromBlockchain); const args = this.getTxParams(swapParams.inputs.params, isFromBaseChain); const evmConfig = evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(teleswap_contract_address_1.teleSwapContractAddresses[this.fromBlockchain], isFromBaseChain ? teleswap_swap_and_unwrap_abi_1.teleswapSwapAndUnwrapAbi : teleswap_swap_and_unwrap_abi_1.teleswapSwapAndUwrapAbiForCcrChains, 'swapAndUnwrap', args, this.from.isNative ? swapParams.inputs.value : '0'); return { config: evmConfig, amount: this.to.stringWeiAmount }; } getTxParams(args, isFromBaseChain) { const toTokenAmountIndex = isFromBaseChain ? 1 : 2; const toTokenMinWeiAmount = web3_pure_1.Web3Pure.toWei(args[toTokenAmountIndex][1], this.to.decimals); args[toTokenAmountIndex][1] = toTokenMinWeiAmount; return args; } getTradeInfo() { return { estimatedGas: this.estimatedGas, feeInfo: this.feeInfo, priceImpact: this.priceImpact, slippage: this.slippage * 100, routePath: this.routePath }; } } exports.TeleSwapEvmCcrTrade = TeleSwapEvmCcrTrade; //# sourceMappingURL=teleswap-evm-ccr-trade.js.map