UNPKG

rubic-sdk

Version:
181 lines 8.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CbridgeCrossChainTrade = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const blockchain_id_1 = require("../../../../../core/blockchain/utils/blockchains-info/constants/blockchain-id"); 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 cross_chain_trade_type_1 = require("../../models/cross-chain-trade-type"); const cbridge_contract_abi_1 = require("./constants/cbridge-contract-abi"); const cbridge_contract_address_1 = require("./constants/cbridge-contract-address"); const cbridge_proxy_abi_1 = require("./constants/cbridge-proxy-abi"); const celer_transit_tokens_1 = require("../celer-provider/constants/celer-transit-tokens"); const evm_cross_chain_trade_1 = require("../common/emv-cross-chain-trade/evm-cross-chain-trade"); const bridge_type_1 = require("../common/models/bridge-type"); class CbridgeCrossChainTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade { /** @internal */ static async getGasData(from, to, onChainTrade) { const fromBlockchain = from.blockchain; const walletAddress = injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(fromBlockchain).address; if (!walletAddress) { return null; } try { const { contractAddress, contractAbi, methodName, methodArguments, value } = await new CbridgeCrossChainTrade({ from, to, gasData: null, priceImpact: 0, slippage: 0, feeInfo: {}, maxSlippage: 0, contractAddress: evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS, transitMinAmount: new bignumber_js_1.default(0), onChainTrade: onChainTrade }, evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS).getContractParams({}); const web3Public = injector_1.Injector.web3PublicService.getWeb3Public(fromBlockchain); const [gasLimit, gasPrice] = await Promise.all([ web3Public.getEstimatedGas(contractAbi, contractAddress, methodName, methodArguments, walletAddress, value), new bignumber_js_1.default(await injector_1.Injector.gasPriceApi.getGasPrice(from.blockchain)) ]); if (!gasLimit?.isFinite()) { return null; } const increasedGasLimit = web3_pure_1.Web3Pure.calculateGasMargin(gasLimit, 1.2); return { gasLimit: increasedGasLimit, gasPrice }; } catch (_err) { return null; } } get fromBlockchain() { return this.from.blockchain; } get fromContractAddress() { // return cbridgeContractAddress[this.fromBlockchain].rubicRouter; return cbridge_contract_address_1.cbridgeContractAddress[this.fromBlockchain].providerRouter; } constructor(crossChainTrade, providerAddress) { super(providerAddress); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.CELER_BRIDGE; this.isAggregator = false; this.bridgeType = bridge_type_1.BRIDGE_TYPE.CELER_BRIDGE; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.gasData = crossChainTrade.gasData; this.priceImpact = crossChainTrade.priceImpact; this.slippage = crossChainTrade.slippage; this.toTokenAmountMin = crossChainTrade.transitMinAmount.multipliedBy(1 - crossChainTrade.maxSlippage / 10000000); this.feeInfo = crossChainTrade.feeInfo; this.priceImpact = crossChainTrade.priceImpact; this.maxSlippage = crossChainTrade.maxSlippage; this.celerContractAddress = crossChainTrade.contractAddress; this.onChainSubtype = crossChainTrade.onChainTrade ? { from: crossChainTrade.onChainTrade.type, to: undefined } : { from: undefined, to: undefined }; this.onChainTrade = crossChainTrade.onChainTrade; } async swap(options = {}) { await this.checkTradeErrors(); await this.checkAllowanceAndApprove(options); const { onConfirm, gasLimit, gasPrice } = options; let transactionHash; const onTransactionHash = (hash) => { if (onConfirm) { onConfirm(hash); } transactionHash = hash; }; // eslint-disable-next-line no-useless-catch try { const { data, to, value } = this.getTransactionRequest(options.receiverAddress || this.walletAddress, this.maxSlippage); await this.web3Private.trySendTransaction(to, { data, value, onTransactionHash, gas: gasLimit, gasPrice }); return transactionHash; } catch (err) { throw err; } } async getContractParams(options) { const receiverAddress = options?.receiverAddress || this.walletAddress; const toChainId = blockchain_id_1.blockchainId[this.to.blockchain]; const fromContracts = cbridge_contract_address_1.cbridgeContractAddress[this.fromBlockchain]; const swapArguments = [ this.from.address, this.from.stringWeiAmount, toChainId, this.to.address, web3_pure_1.Web3Pure.toWei(this.toTokenAmountMin, this.to.decimals), receiverAddress, this.providerAddress ]; const methodArguments = []; if (this.onChainTrade) { methodArguments.push(celer_transit_tokens_1.celerTransitTokens[this.from.blockchain] .address); const encodedData = (await this.onChainTrade.encodeDirect({ fromAddress: options.fromAddress || this.walletAddress, receiverAddress: this.fromContractAddress, supportFee: false })).data; methodArguments.push(encodedData); swapArguments.push(this.onChainTrade.dexContractAddress); } else { swapArguments.push(evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS); } methodArguments.push(this.maxSlippage, swapArguments); const value = this.getSwapValue(); return { contractAddress: fromContracts.rubicRouter, contractAbi: cbridge_proxy_abi_1.cbridgeProxyAbi, methodName: this.getMethodName(), methodArguments, value }; } getTradeAmountRatio(fromUsd) { return fromUsd.dividedBy(this.to.tokenAmount); } getUsdPrice() { return this.from.price.multipliedBy(this.from.tokenAmount); } getTradeInfo() { return { estimatedGas: this.estimatedGas, feeInfo: this.feeInfo, priceImpact: this.priceImpact ? { total: this.priceImpact } : { total: 0 }, slippage: { total: this.maxSlippage / 10000 } }; } getMethodName() { if (this.from.isNative) { return this.onChainTrade ? 'swapNativeAndBridge' : 'bridgeNative'; } return this.onChainTrade ? 'swapAndBridge' : 'bridge'; } getTransactionRequest(receiverAddress, maxSlippage) { const params = [receiverAddress]; if (!this.from.isNative) { params.push(this.from.address); } params.push(this.from.stringWeiAmount, blockchain_id_1.blockchainId[this.to.blockchain], Date.now(), maxSlippage); const encode = evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(this.fromContractAddress, cbridge_contract_abi_1.cbridgeContractAbi, this.from.isNative ? 'sendNative' : 'send', params, this.from.isNative ? this.from.stringWeiAmount : '0'); return { data: encode.data, to: encode.to, value: encode.value }; } } exports.CbridgeCrossChainTrade = CbridgeCrossChainTrade; //# sourceMappingURL=cbridge-cross-chain-trade.js.map