UNPKG

rubic-sdk

Version:
237 lines • 12.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CrossChainTransferTrade = void 0; const errors_1 = require("../../../../../../common/errors"); const options_1 = require("../../../../../../common/utils/options"); const chain_type_1 = require("../../../../../../core/blockchain/models/chain-type"); const blockchains_info_1 = require("../../../../../../core/blockchain/utils/blockchains-info/blockchains-info"); const erc_20_token_abi_1 = require("../../../../../../core/blockchain/web3-public-service/web3-public/evm-web3-public/constants/erc-20-token-abi"); 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 injector_1 = require("../../../../../../core/injector/injector"); const convert_gas_price_1 = require("../../../utils/convert-gas-price"); const rubic_proxy_contract_address_1 = require("../constants/rubic-proxy-contract-address"); const evm_common_cross_chain_abi_1 = require("../evm-cross-chain-trade/constants/evm-common-cross-chain-abi"); const gateway_rubic_cross_chain_abi_1 = require("../evm-cross-chain-trade/constants/gateway-rubic-cross-chain-abi"); const evm_cross_chain_trade_1 = require("../evm-cross-chain-trade/evm-cross-chain-trade"); const proxy_cross_chain_evm_trade_1 = require("../proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade"); const gas_limit_estimation_1 = require("./constans/gas-limit-estimation"); class CrossChainTransferTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade { static async getGasData(from) { const fromBlockchain = from.blockchain; if (!blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(fromBlockchain)) { return null; } try { let gasLimit = 0; if (from.isNative) { gasLimit = gas_limit_estimation_1.transferGasLimit.NATIVE_TRANSFER_GAS_LIMIT; } else { gasLimit = gas_limit_estimation_1.transferGasLimit.TOKEN_TRANSFER_GAS_LIMIT; } const gasDetails = (0, convert_gas_price_1.convertGasDataToBN)(await injector_1.Injector.gasPriceApi.getGasPrice(fromBlockchain)); const increasedGasLimit = web3_pure_1.Web3Pure.calculateGasMargin(gasLimit, 1.2); return { gasLimit: increasedGasLimit, ...gasDetails }; } catch { return null; } } get methodName() { return this.onChainTrade ? 'swapAndStartBridgeTokensViaTransfer' : 'startBridgeTokensViaTransfer'; } get web3Private() { if (!blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.from.blockchain)) { throw new errors_1.RubicSdkError('Cannot retrieve web3 private'); } return injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(this.from.blockchain); } constructor(crossChainTrade) { super(crossChainTrade.providerAddress, crossChainTrade.routePath, crossChainTrade.useProxy); this.paymentInfo = null; this.isAggregator = false; this.onChainTrade = crossChainTrade.onChainTrade; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.toTokenAmountMin = crossChainTrade.toTokenAmountMin; this.gasData = crossChainTrade.gasData; this.feeInfo = crossChainTrade.feeInfo; this.priceImpact = crossChainTrade.priceImpact; this.actualTokenAmount = crossChainTrade.to.tokenAmount; } async getTransferTrade(receiverAddress, refundAddress) { await this.setTransactionConfig(false, false, receiverAddress, refundAddress); if (!this.paymentInfo) { throw new Error('Deposit address is not set'); } const extraField = this.paymentInfo.depositExtraIdName ? { name: this.paymentInfo.depositExtraIdName, value: this.paymentInfo.depositExtraId } : null; return { id: this.paymentInfo.id, depositAddress: this.paymentInfo.depositAddress, toAmount: this.actualTokenAmount, ...(extraField && { extraField }) }; } async getTransactionConfigAndAmount(receiverAddress, refundAddress) { const res = await this.getPaymentInfo(receiverAddress || this.walletAddress, refundAddress); const toAmountWei = web3_pure_1.Web3Pure.toWei(res.toAmount, this.to.decimals); this.paymentInfo = res; const config = { to: '', data: '', value: '' }; if (this.from.isNative) { config.value = this.from.stringWeiAmount; config.data = '0x'; config.to = this.paymentInfo.depositAddress; } else { const blockchainType = blockchains_info_1.BlockchainsInfo.getChainType(this.from.blockchain); if (blockchainType === chain_type_1.CHAIN_TYPE.EVM) { const encodedConfig = evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(this.from.address, erc_20_token_abi_1.ERC20_TOKEN_ABI, 'transfer', [this.paymentInfo.depositAddress, this.from.stringWeiAmount], '0'); config.value = '0'; config.to = this.from.address; config.data = encodedConfig.data; } else { config.value = '0'; config.data = '0x'; config.to = this.paymentInfo.depositAddress; } } return { config, amount: toAmountWei }; } async encode(options) { if (!blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.from.blockchain)) { throw new errors_1.RubicSdkError('Cannot encode trade for non-evm blockchain'); } await this.checkFromAddress(options.fromAddress, true, this.type); await this.checkReceiverAddress(options.receiverAddress, !blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain), this.type); if (this.isProxyTrade) { const { contractAddress, contractAbi, methodName, methodArguments, value } = await this.getContractParams({ fromAddress: options.fromAddress, receiverAddress: options.receiverAddress || options.fromAddress }); return evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(contractAddress, contractAbi, methodName, methodArguments, value, { ...(0, options_1.getGasOptions)(options) }); } return this.setTransactionConfig(options?.skipAmountCheck || false, options?.useCacheData || false, options?.receiverAddress || this.walletAddress); } async getContractParams(options, skipAmountChangeCheck) { await this.setTransactionConfig(skipAmountChangeCheck || false, options.useCacheData || false, options.receiverAddress); if (!this.paymentInfo) { throw new Error('Deposit address is not set'); } const toToken = this.to.clone({ address: evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS }); const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData({ ...options, receiverAddress: this.walletAddress }, { walletAddress: this.walletAddress, fromTokenAmount: this.from, toTokenAmount: toToken, srcChainTrade: this.onChainTrade, providerAddress: this.providerAddress, type: `native:${this.bridgeType}`, fromAddress: this.walletAddress }); const providerData = [this.paymentInfo.depositAddress]; const swapData = this.onChainTrade && (await proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getSwapData(options, { walletAddress: this.walletAddress, contractAddress: rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].router, fromTokenAmount: this.from, toTokenAmount: this.onChainTrade.to, onChainEncodeFn: this.onChainTrade.encode.bind(this.onChainTrade) })); const methodArguments = swapData ? [bridgeData, swapData, providerData] : [bridgeData, providerData]; const value = this.getSwapValue(); 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 }; } encodeApprove() { throw new errors_1.RubicSdkError(`Cannot encode approve for ${this.type}`); } async needApprove() { if (this.isProxyTrade) { return super.needApprove(); } return false; } async swapDirect(options = {}) { if (!blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.from.blockchain)) { throw new errors_1.RubicSdkError("For non-evm chains use 'getTransferTrade' method"); } await this.checkTradeErrors(); await this.checkReceiverAddress(options.receiverAddress, !blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain), this.type); const { onConfirm, gasPriceOptions } = options; let transactionHash; const onTransactionHash = (hash) => { if (onConfirm) { onConfirm(hash); } transactionHash = hash; }; try { await this.setTransactionConfig(false, options.useCacheData || false, options?.receiverAddress || this.walletAddress); if (!this.paymentInfo) { throw new Error('Deposit address is not set'); } if (this.from.isNative) { await this.web3Private.trySendTransaction(this.paymentInfo.depositAddress, { value: this.from.weiAmount, onTransactionHash, gasPriceOptions }); } else { await this.web3Private.tryExecuteContractMethod(this.from.address, erc_20_token_abi_1.ERC20_TOKEN_ABI, 'transfer', [this.paymentInfo.depositAddress, this.from.stringWeiAmount], { onTransactionHash, gasPriceOptions }); } return transactionHash; } catch (err) { if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) { return transactionHash; } throw err; } } async setTransactionConfig(skipAmountChangeCheck, useCacheData, receiverAddress, refundAddress) { if (this.lastTransactionConfig && useCacheData) { return this.lastTransactionConfig; } const { config, amount } = await this.getTransactionConfigAndAmount(receiverAddress || this.walletAddress, refundAddress); this.lastTransactionConfig = config; setTimeout(() => { this.lastTransactionConfig = null; }, 15000); if (!skipAmountChangeCheck) { this.checkAmountChange(amount, this.to.stringWeiAmount); } return config; } } exports.CrossChainTransferTrade = CrossChainTransferTrade; //# sourceMappingURL=cross-chain-transfer-trade.js.map