rubic-sdk
Version:
Simplify dApp creation
136 lines • 6.68 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenOceanProvider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../common/errors");
const on_chain_1 = require("../../../../../common/errors/on-chain");
const tokens_1 = require("../../../../../common/tokens");
const web3_pure_1 = require("../../../../../core/blockchain/web3-pure/web3-pure");
const injector_1 = require("../../../../../core/injector/injector");
const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee");
const on_chain_trade_type_1 = require("../common/models/on-chain-trade-type");
const on_chain_proxy_service_1 = require("../common/on-chain-proxy-service/on-chain-proxy-service");
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 get_open_ocean_api_url_1 = require("./constants/get-open-ocean-api-url");
const open_ocean_blockchain_1 = require("./constants/open-ocean-blockchain");
const open_ocean_on_chain_supported_blockchain_1 = require("./constants/open-ocean-on-chain-supported-blockchain");
const open_ocean_trade_1 = require("./open-ocean-trade");
class OpenOceanProvider {
constructor() {
this.onChainProxyService = new on_chain_proxy_service_1.OnChainProxyService();
}
async calculate(from, toToken, options) {
try {
this.checkIsSupportedBlockchain(from.blockchain);
await this.checkIsSupportedTokens(from, toToken);
const { fromWithoutFee, proxyFeeInfo } = await this.handleProxyContract(from, options);
const blockchain = from.blockchain;
const gasPrice = await injector_1.Injector.web3PublicService
.getWeb3Public(blockchain)
.getGasPrice();
const apiUrl = get_open_ocean_api_url_1.openOceanApiUrl.quote(open_ocean_blockchain_1.openOceanBlockchainName[blockchain]);
const quoteResponse = await injector_1.Injector.httpClient.get(apiUrl, {
params: {
chain: open_ocean_blockchain_1.openOceanBlockchainName[blockchain],
inTokenAddress: fromWithoutFee.address,
outTokenAddress: toToken.address,
amount: fromWithoutFee.tokenAmount.toString(),
slippage: options.slippageTolerance * 100,
gasPrice: web3_pure_1.Web3Pure.fromWei(gasPrice, tokens_1.nativeTokensList[from.blockchain].decimals)
.multipliedBy(10 ** 9)
.toFixed(0)
}
});
if ([500, 400].includes(quoteResponse.code)) {
return {
type: on_chain_trade_type_1.ON_CHAIN_TRADE_TYPE.OPEN_OCEAN,
error: new errors_1.RubicSdkError(quoteResponse.error)
};
}
const to = new tokens_1.PriceTokenAmount({
...toToken.asStruct,
weiAmount: new bignumber_js_1.default(quoteResponse.data.outAmount)
});
const toTokenWeiAmountMin = new bignumber_js_1.default(quoteResponse.data.outAmount).multipliedBy(1 - options.slippageTolerance);
const openOceanTradeStruct = {
from,
to,
gasFeeInfo: null,
slippageTolerance: options.slippageTolerance,
path: [from, to],
toTokenWeiAmountMin,
useProxy: options.useProxy,
proxyFeeInfo,
fromWithoutFee,
withDeflation: options.withDeflation
};
const gasFeeInfo = options.gasCalculation === 'calculate'
? await this.getGasFeeInfo(openOceanTradeStruct)
: null;
return new open_ocean_trade_1.OpenOceanTrade({
...openOceanTradeStruct,
gasFeeInfo
}, options.providerAddress);
}
catch (error) {
return {
type: on_chain_trade_type_1.ON_CHAIN_TRADE_TYPE.OPEN_OCEAN,
error
};
}
}
async checkContractState(fromBlockchain) {
const isPaused = await this.onChainProxyService.isContractPaused(fromBlockchain);
if (isPaused) {
throw new on_chain_1.OnChainIsUnavailableError();
}
}
async handleProxyContract(from, fullOptions) {
let fromWithoutFee;
let proxyFeeInfo;
if (fullOptions.useProxy) {
await this.checkContractState(from.blockchain);
proxyFeeInfo = await this.onChainProxyService.getFeeInfo(from, fullOptions.providerAddress);
fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(from, proxyFeeInfo.platformFee.percent);
}
else {
fromWithoutFee = from;
}
return {
fromWithoutFee,
proxyFeeInfo
};
}
async getGasFeeInfo(tradeStruct) {
try {
const gasPriceInfo = await (0, get_gas_price_info_1.getGasPriceInfo)(tradeStruct.from.blockchain);
const gasLimit = await open_ocean_trade_1.OpenOceanTrade.getGasLimit(tradeStruct);
return (0, get_gas_fee_info_1.getGasFeeInfo)(gasLimit, gasPriceInfo);
}
catch {
return null;
}
}
checkIsSupportedBlockchain(blockchain) {
if (!open_ocean_on_chain_supported_blockchain_1.openoceanOnChainSupportedBlockchains.some(item => item === blockchain)) {
throw new errors_1.RubicSdkError('Unsupported blockchain');
}
}
async checkIsSupportedTokens(from, to) {
const apiUrl = get_open_ocean_api_url_1.openOceanApiUrl.tokenList(open_ocean_blockchain_1.openOceanBlockchainName[from.blockchain]);
const tokenListResponse = await injector_1.Injector.httpClient.get(apiUrl);
const tokens = tokenListResponse?.data?.map(token => token.address.toLocaleLowerCase());
const isSupportedTokens = Boolean(tokens.length) &&
(from.isNative || tokens.includes(from.address.toLocaleLowerCase())) &&
(to.isNative || tokens.includes(to.address.toLocaleLowerCase()));
if (!isSupportedTokens) {
throw new errors_1.RubicSdkError('Unsupported token pair');
}
}
}
exports.OpenOceanProvider = OpenOceanProvider;
//# sourceMappingURL=open-ocean-provider.js.map
;