rubic-sdk
Version:
Simplify dApp creation
146 lines • 9.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmBridgersCrossChainTrade = 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 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 ton_web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/typed-web3-pure/ton-web3-pure/ton-web3-pure");
const tron_web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/typed-web3-pure/tron-web3-pure/tron-web3-pure");
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 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");
class EvmBridgersCrossChainTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade {
get fromContractAddress() {
return this.isProxyTrade
? rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].gateway
: bridgers_contract_addresses_1.bridgersContractAddresses[this.from.blockchain];
}
get methodName() {
return 'startBridgeTokensViaGenericCrossChain';
}
constructor(params) {
const { crossChainTrade, providerAddress, routePath, useProxy } = params;
super(providerAddress, routePath, useProxy);
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.gasData = crossChainTrade.gasData;
this.priceImpact = this.from.calculatePriceImpactPercent(this.to);
this.slippage = crossChainTrade.slippage;
}
async getContractParams(options) {
const { data, value: providerValue, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress);
const isEvmDestination = blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain);
let receiverAddress = isEvmDestination
? options.receiverAddress || this.walletAddress
: options.receiverAddress;
let toAddress = this.to.address;
if (this.to.blockchain === blockchain_name_1.BLOCKCHAIN_NAME.TRON) {
receiverAddress = tron_web3_pure_1.TronWeb3Pure.addressToHex(receiverAddress);
toAddress = tron_web3_pure_1.TronWeb3Pure.addressToHex(toAddress);
}
if (this.to.blockchain === blockchain_name_1.BLOCKCHAIN_NAME.TON && !this.to.isNative) {
receiverAddress = ton_web3_pure_1.TonWeb3Pure.addressToHex(receiverAddress);
toAddress = ton_web3_pure_1.TonWeb3Pure.addressToHex(toAddress);
}
const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData({ ...options, receiverAddress }, {
walletAddress: this.walletAddress,
fromTokenAmount: this.from,
toTokenAmount: this.to,
toAddress,
srcChainTrade: null,
providerAddress: this.providerAddress,
type: `native:${this.type}`,
fromAddress: this.walletAddress
});
const providerData = await proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getGenericProviderData(to, data, this.from.blockchain, to, '0');
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) {
return fromUsd.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 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, true).address;
const useLowerCase = blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain);
const toTokenAddress = (0, token_native_address_proxy_1.createTokenNativeAddressProxy)(this.to, bridgers_native_address_1.bridgersNativeAddress, useLowerCase).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 { config, amount: amountMinWei };
}
}
exports.EvmBridgersCrossChainTrade = EvmBridgersCrossChainTrade;
//# sourceMappingURL=evm-bridgers-cross-chain-trade.js.map