rubic-sdk
Version:
Simplify dApp creation
155 lines • 7.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MesonCrossChainProvider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../common/errors");
const tokens_1 = require("../../../../../common/tokens");
const blockchain_1 = require("../../../../../common/utils/blockchain");
const blockchain_name_1 = require("../../../../../core/blockchain/models/blockchain-name");
const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee");
const meson_cross_chain_factory_1 = require("./meson-cross-chain-factory");
const meson_cross_chain_utils_1 = require("./services/meson-cross-chain-utils");
const cross_chain_provider_1 = require("../common/cross-chain-provider");
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");
const meson_cross_chain_supported_chains_1 = require("./constants/meson-cross-chain-supported-chains");
const meson_cross_chain_api_service_1 = require("./services/meson-cross-chain-api-service");
class MesonCrossChainProvider extends cross_chain_provider_1.CrossChainProvider {
constructor() {
super(...arguments);
this.type = bridge_type_1.BRIDGE_TYPE.MESON;
}
isSupportedBlockchain(blockchain) {
return meson_cross_chain_supported_chains_1.mesonCrossChainSupportedChains.some(supportedBlockchain => supportedBlockchain === blockchain);
}
async calculate(from, toToken, options) {
const fromBlockchain = from.blockchain;
let useProxy = options?.useProxy?.[this.type] ?? true;
if (fromBlockchain === blockchain_name_1.BLOCKCHAIN_NAME.TRON) {
useProxy = false;
}
try {
if (!useProxy) {
throw new errors_1.RubicSdkError('Meson only supports proxy swaps!');
}
const fromWith6Decimals = this.getFromWith6Decimals(from);
const { max, min, mesonFee, sourceAssetString, targetAssetString } = await this.fetchTradeInfo(fromWith6Decimals, toToken);
const feeInfo = await this.getFeeInfo(fromBlockchain, options.providerAddress, from, useProxy);
const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(fromWith6Decimals, feeInfo.rubicProxy?.platformFee?.percent);
const toAmount = fromWithoutFee.tokenAmount.minus(mesonFee);
const to = new tokens_1.PriceTokenAmount({
...toToken.asStruct,
tokenAmount: toAmount
});
const trade = meson_cross_chain_factory_1.MesonCrossChainFactory.createTrade({
crossChainTrade: {
feeInfo,
from: fromWith6Decimals,
gasData: await this.getGasData(from),
to,
priceImpact: from.calculatePriceImpactPercent(to),
sourceAssetString,
targetAssetString
},
providerAddress: options.providerAddress,
routePath: await this.getRoutePath(from, to),
useProxy
});
if (fromWithoutFee.tokenAmount.lt(min)) {
return {
trade,
error: new errors_1.MinAmountError(min, from.symbol),
tradeType: this.type
};
}
if (fromWithoutFee.tokenAmount.gt(max)) {
return {
trade,
error: new errors_1.MaxAmountError(max, from.symbol),
tradeType: this.type
};
}
return { trade, tradeType: this.type };
}
catch (err) {
const rubicSdkError = cross_chain_provider_1.CrossChainProvider.parseError(err);
return {
trade: null,
error: rubicSdkError,
tradeType: this.type
};
}
}
async fetchTradeInfo(sourceToken, targetToken) {
const mesonChains = await meson_cross_chain_api_service_1.MesonCcrApiService.fetchChainsLimits();
const sourceTokenInfo = this.getApiTokenInfo(sourceToken, mesonChains);
const targetTokenInfo = this.getApiTokenInfo(targetToken, mesonChains);
const mesonChainsSymbols = this.getTxChainsSymbols(sourceToken, targetToken, mesonChains);
const sourceAssetString = `${mesonChainsSymbols[0]}:${sourceTokenInfo.id}`;
const targetAssetString = `${mesonChainsSymbols[1]}:${targetTokenInfo.id}`;
const mesonFee = await meson_cross_chain_api_service_1.MesonCcrApiService.fetchMesonFee(sourceAssetString, targetAssetString, sourceToken.tokenAmount.toFixed());
const min = Math.max(Number(sourceTokenInfo.min), Number(targetTokenInfo.min));
const max = Math.min(Number(sourceTokenInfo.max), Number(targetTokenInfo.max));
return {
mesonFee,
sourceAssetString,
targetAssetString,
min: new bignumber_js_1.default(min),
max: new bignumber_js_1.default(max)
};
}
getApiTokenInfo(token, apiChains) {
const searchebleId = meson_cross_chain_utils_1.MesonCrossChainUtils.getSearchebleId(token.blockchain);
const foundChain = apiChains.find(chain => (0, blockchain_1.compareAddresses)(chain.chainId, searchebleId));
if (!foundChain) {
throw new errors_1.NotSupportedBlockchain();
}
const foundToken = token.isNative
? foundChain.tokens.find(apiToken => !Object.hasOwn(apiToken, 'addr'))
: foundChain.tokens.find(apiToken => apiToken?.addr ? (0, blockchain_1.compareAddresses)(apiToken.addr, token.address) : false);
if (!foundToken) {
throw new errors_1.NotSupportedTokensError();
}
return foundToken;
}
getTxChainsSymbols(sourceToken, targetToken, apiChains) {
const sourceChainIdHex = meson_cross_chain_utils_1.MesonCrossChainUtils.getSearchebleId(sourceToken.blockchain);
const targetChainIdHex = meson_cross_chain_utils_1.MesonCrossChainUtils.getSearchebleId(targetToken.blockchain);
const ids = ['', ''];
for (const chain of apiChains) {
if (ids[0] && ids[1])
break;
if ((0, blockchain_1.compareAddresses)(chain.chainId, sourceChainIdHex))
ids[0] = chain.id;
if ((0, blockchain_1.compareAddresses)(chain.chainId, targetChainIdHex))
ids[1] = chain.id;
}
return ids;
}
/**
* Meson inputs only value with 6 or less decimals in swap request
*/
getFromWith6Decimals(from) {
const stringAmount = from.tokenAmount.toFixed();
const [, decimals] = stringAmount.split('.');
if (!decimals || decimals.length <= 6) {
return from;
}
const amount = from.tokenAmount.decimalPlaces(6, bignumber_js_1.default.ROUND_DOWN);
return new tokens_1.PriceTokenAmount({
...from.asStruct,
tokenAmount: amount
});
}
async getRoutePath(fromToken, toToken) {
return [{ type: 'cross-chain', provider: this.type, path: [fromToken, toToken] }];
}
async getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy) {
return proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getFeeInfo(fromBlockchain, providerAddress, percentFeeToken, useProxy);
}
}
exports.MesonCrossChainProvider = MesonCrossChainProvider;
//# sourceMappingURL=meson-cross-chain-provider.js.map