rubic-sdk
Version:
Simplify dApp creation
139 lines • 7.5 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouterEvmCrossChainTrade = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../../common/errors");
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 router_api_service_1 = require("../../../../../common/providers/router/services/router-api-service");
const get_from_without_fee_1 = require("../../../../../common/utils/get-from-without-fee");
const router_cross_chain_util_service_1 = require("../utils/router-cross-chain-util-service");
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 RouterEvmCrossChainTrade 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.fromBlockchain].gateway
: this.routerQuoteConfig.allowanceTo;
}
get methodName() {
return 'startBridgeTokensViaGenericCrossChain';
}
constructor(params) {
const { providerAddress, routePath, useProxy, crossChainTrade } = params;
super(providerAddress, routePath, useProxy);
this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.ROUTER;
this.onChainSubtype = { from: undefined, to: undefined };
this.bridgeType = bridge_type_1.BRIDGE_TYPE.ROUTER;
this.isAggregator = false;
this.from = crossChainTrade.from;
this.to = crossChainTrade.to;
this.feeInfo = crossChainTrade.feeInfo;
this.gasData = crossChainTrade.gasData;
this.priceImpact = crossChainTrade.priceImpact;
this.routerQuoteConfig = crossChainTrade.routerQuoteConfig;
this.slippage = crossChainTrade.slippage;
this.toTokenAmountMin = this.to.tokenAmount.multipliedBy(1 - this.slippage);
}
async getContractParams(options, skipAmountChangeCheck) {
const { data, value: providerValue, to, gasPrice } = await this.setTransactionConfig(skipAmountChangeCheck || false, options?.useCacheData || false, options?.receiverAddress);
try {
const isEvmDestination = blockchains_info_1.BlockchainsInfo.isEvmBlockchainName(this.to.blockchain);
const receivingAsset = isEvmDestination ? this.to.address : evm_web3_pure_1.EvmWeb3Pure.EMPTY_ADDRESS;
const toBlockchain = this.to.blockchain;
let receiverAddress = '';
if (!isEvmDestination && options.receiverAddress) {
receiverAddress = await router_cross_chain_util_service_1.RouterCrossChainUtilService.checkAndConvertAddress(toBlockchain, options?.receiverAddress);
}
else {
receiverAddress = options?.receiverAddress || this.walletAddress;
}
const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData({
...options,
receiverAddress
}, {
walletAddress: this.walletAddress,
fromTokenAmount: this.from,
toTokenAmount: this.to,
srcChainTrade: null,
providerAddress: this.providerAddress,
type: `native:${this.type}`,
fromAddress: this.walletAddress,
toAddress: receivingAsset
});
const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(this.from, this.feeInfo?.rubicProxy?.platformFee?.percent);
const extraNativeFee = this.from.isNative
? new bignumber_js_1.default(providerValue).minus(fromWithoutFee.stringWeiAmount).toFixed()
: new bignumber_js_1.default(providerValue).toFixed();
const providerData = await proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getGenericProviderData(to, data, this.fromBlockchain, to, 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, {
gasPrice: gasPrice
});
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
};
}
catch (err) {
console.log(err?.message);
throw err;
}
}
async getTransactionConfigAndAmount(receiverAddress) {
const toBlockchain = this.to.blockchain;
const toAddress = await router_cross_chain_util_service_1.RouterCrossChainUtilService.checkAndConvertAddress(toBlockchain, receiverAddress || this.walletAddress, this.to.address);
const { txn, destination } = await router_api_service_1.RouterApiService.getSwapTx({
...this.routerQuoteConfig,
senderAddress: this.walletAddress,
receiverAddress: toAddress,
refundAddress: this.walletAddress
});
if (!txn) {
throw new errors_1.RubicSdkError();
}
const config = {
data: txn.data,
value: txn.value,
to: txn.to,
gasPrice: txn.gasPrice
};
return { config, amount: destination.tokenAmount };
}
getTradeInfo() {
return {
estimatedGas: this.estimatedGas,
feeInfo: this.feeInfo,
priceImpact: this.priceImpact,
slippage: this.slippage * 100,
routePath: this.routePath
};
}
}
exports.RouterEvmCrossChainTrade = RouterEvmCrossChainTrade;
/**
* FROM - 100
* FROM+PERCENT - 103
* FROM-PERCENT - 97
*
* FROM-PERCENT - > api -> FROM-PERCENT+EXTRA
* FROM-PERCENT+EXTRA - FROM >< 0
*/
//# sourceMappingURL=router-evm-cross-chain-trade.js.map