rubic-sdk
Version:
Simplify dApp creation
86 lines • 4.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SquidRouterOnChainProvider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const tokens_1 = require("../../../../../../common/tokens");
const blockchain_id_1 = require("../../../../../../core/blockchain/utils/blockchains-info/constants/blockchain-id");
const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure");
const squidrouter_api_service_1 = require("../../../../../common/providers/squidrouter/services/squidrouter-api-service");
const squidrouter_cross_chain_supported_blockchain_1 = require("../../../../../cross-chain/calculation-manager/providers/squidrouter-provider/constants/squidrouter-cross-chain-supported-blockchain");
const on_chain_trade_type_1 = require("../../common/models/on-chain-trade-type");
const aggregator_on_chain_provider_abstract_1 = require("../../common/on-chain-aggregator/aggregator-on-chain-provider-abstract");
const get_gas_fee_info_1 = require("../../common/utils/get-gas-fee-info");
const get_gas_price_info_1 = require("../../common/utils/get-gas-price-info");
const squidrouter_on_chain_trade_1 = require("./squidrouter-on-chain-trade");
class SquidRouterOnChainProvider extends aggregator_on_chain_provider_abstract_1.AggregatorOnChainProvider {
constructor() {
super(...arguments);
this.tradeType = on_chain_trade_type_1.ON_CHAIN_TRADE_TYPE.SQUIDROUTER;
this.nativeAddress = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
}
isSupportedBlockchain(blockchain) {
return squidrouter_cross_chain_supported_blockchain_1.squidrouterCrossChainSupportedBlockchains.some(item => item === blockchain);
}
async calculate(from, toToken, options) {
const fromBlockchain = from.blockchain;
const toBlockchain = toToken.blockchain;
try {
const { fromWithoutFee, proxyFeeInfo } = await this.handleProxyContract(from, options);
const fakeAddress = '0xe388Ed184958062a2ea29B7fD049ca21244AE02e';
const fromAddress = options?.fromAddress || fakeAddress;
const receiver = fromAddress;
const requestParams = {
fromAddress,
fromChain: blockchain_id_1.blockchainId[fromBlockchain].toString(),
fromToken: from.isNative ? this.nativeAddress : from.address,
fromAmount: fromWithoutFee.stringWeiAmount,
toChain: blockchain_id_1.blockchainId[toBlockchain].toString(),
toToken: toToken.isNative ? this.nativeAddress : toToken.address,
toAddress: receiver,
slippage: Number(options.slippageTolerance * 100)
};
const { route } = await squidrouter_api_service_1.SquidRouterApiService.getRoute(requestParams);
const providerGateway = route.transactionRequest.target;
const to = new tokens_1.PriceTokenAmount({
...toToken.asStruct,
tokenAmount: web3_pure_1.Web3Pure.fromWei(route.estimate.toAmount, toToken.decimals)
});
const routePath = this.getRoutePath(from, to);
const tradeStruct = {
from,
to,
path: routePath,
proxyFeeInfo,
transactionRequest: requestParams,
slippageTolerance: options.slippageTolerance,
useProxy: options.useProxy,
usedForCrossChain: options.usedForCrossChain,
withDeflation: options.withDeflation,
fromWithoutFee,
gasFeeInfo: await this.getGasFeeInfo(from, route)
};
return new squidrouter_on_chain_trade_1.SquidRouterOnChainTrade(tradeStruct, options.providerAddress, providerGateway);
}
catch (err) {
return {
type: this.tradeType,
error: err
};
}
}
async getGasFeeInfo(from, route) {
try {
const gasPriceInfo = await (0, get_gas_price_info_1.getGasPriceInfo)(from.blockchain);
const gasLimit = new bignumber_js_1.default(route.transactionRequest.gasLimit);
return (0, get_gas_fee_info_1.getGasFeeInfo)(gasPriceInfo, { gasLimit });
}
catch {
return null;
}
}
}
exports.SquidRouterOnChainProvider = SquidRouterOnChainProvider;
//# sourceMappingURL=squidrouter-on-chain-provider.js.map