UNPKG

rubic-sdk

Version:
107 lines 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TronBridgersCrossChainTrade = void 0; const errors_1 = require("../../../../../../common/errors"); const not_supported_region_1 = require("../../../../../../common/errors/swap/not-supported-region"); const blockchain_name_1 = require("../../../../../../core/blockchain/models/blockchain-name"); const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure"); const injector_1 = require("../../../../../../core/injector/injector"); const bridgers_native_address_1 = require("../../../../../common/providers/bridgers/constants/bridgers-native-address"); const to_bridgers_blockchain_1 = require("../../../../../common/providers/bridgers/constants/to-bridgers-blockchain"); const bridgers_contract_addresses_1 = require("../../../../../common/providers/bridgers/models/bridgers-contract-addresses"); const get_from_without_fee_1 = require("../../../../../common/utils/get-from-without-fee"); const token_native_address_proxy_1 = require("../../../../../common/utils/token-native-address-proxy"); const cross_chain_trade_type_1 = require("../../../models/cross-chain-trade-type"); const bridge_type_1 = require("../../common/models/bridge-type"); const tron_cross_chain_trade_1 = require("../../common/tron-cross-chain-trade/tron-cross-chain-trade"); class TronBridgersCrossChainTrade extends tron_cross_chain_trade_1.TronCrossChainTrade { get fromContractAddress() { return bridgers_contract_addresses_1.bridgersContractAddresses.TRON; } get methodName() { return ''; } constructor(params) { const { crossChainTrade, providerAddress, routePath } = params; super(providerAddress, routePath, false); this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.BRIDGERS; this.isAggregator = false; this.onChainSubtype = { from: undefined, to: undefined }; this.bridgeType = bridge_type_1.BRIDGE_TYPE.BRIDGERS; this.from = crossChainTrade.from; this.to = crossChainTrade.to; this.toTokenAmountMin = crossChainTrade.toTokenAmountMin; this.feeInfo = crossChainTrade.feeInfo; this.priceImpact = this.from.calculatePriceImpactPercent(this.to); this.slippage = crossChainTrade.slippage; } async getContractParams(_options) { throw new Error('Not implemeted'); } getTradeAmountRatio(fromUsd) { return fromUsd.dividedBy(this.to.tokenAmount); } getTradeInfo() { return { estimatedGas: null, feeInfo: this.feeInfo, priceImpact: this.priceImpact ?? null, slippage: this.slippage * 100, routePath: this.routePath }; } async getTransactionConfigAndAmount(receiverAddress) { const fromBlockchain = this.from.blockchain; const toBlockchain = this.to.blockchain; const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(this.from, this.feeInfo.rubicProxy?.platformFee?.percent); const fromTokenAddress = (0, token_native_address_proxy_1.createTokenNativeAddressProxy)(fromWithoutFee, bridgers_native_address_1.bridgersNativeAddress, false).address; const toTokenAddress = (0, token_native_address_proxy_1.createTokenNativeAddressProxy)(this.to, bridgers_native_address_1.bridgersNativeAddress, this.to.blockchain !== blockchain_name_1.BLOCKCHAIN_NAME.TRON).address; const quoteRequest = { fromTokenAddress, toTokenAddress, fromTokenAmount: fromWithoutFee.stringWeiAmount, fromTokenChain: to_bridgers_blockchain_1.toBridgersBlockchain[fromBlockchain], toTokenChain: to_bridgers_blockchain_1.toBridgersBlockchain[toBlockchain], sourceFlag: 'rubic' }; const quoteResponse = await injector_1.Injector.httpClient.post('https://sswap.swft.pro/api/sswap/quote', quoteRequest); const amountMinWei = web3_pure_1.Web3Pure.toWei(fromWithoutFee.tokenAmount .minus(quoteResponse.data.txData.serviceFee) .multipliedBy(quoteResponse.data.txData.instantRate) .minus(quoteResponse.data.txData.chainFee), this.to.decimals); const fromAddress = this.walletAddress; const swapRequest = { fromTokenAddress, toTokenAddress, fromAddress, toAddress: receiverAddress, fromTokenChain: to_bridgers_blockchain_1.toBridgersBlockchain[fromBlockchain], toTokenChain: to_bridgers_blockchain_1.toBridgersBlockchain[toBlockchain], fromTokenAmount: fromWithoutFee.stringWeiAmount, amountOutMin: amountMinWei, equipmentNo: fromAddress.slice(0, 32), sourceFlag: 'rubic', slippage: this.slippage.toString() }; const swapData = await injector_1.Injector.httpClient.post('https://sswap.swft.pro/api/sswap/swap', swapRequest); if (swapData.resCode === 1146) { throw new not_supported_region_1.NotSupportedRegionError(); } if (!swapData.data?.txData) { throw new errors_1.NotSupportedTokensError(); } const config = swapData.data?.txData; return { amount: amountMinWei, config: { signature: config.functionName, arguments: config.parameter, to: config.to, feeLimit: config?.options?.feeLimit, callValue: config?.options?.callValue } }; } } exports.TronBridgersCrossChainTrade = TronBridgersCrossChainTrade; //# sourceMappingURL=tron-bridgers-cross-chain-trade.js.map