rubic-sdk
Version:
Simplify dApp creation
112 lines • 5.95 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EddyBridgeProvider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../common/errors");
const tokens_1 = require("../../../../../common/tokens");
const blockchain_name_1 = require("../../../../../core/blockchain/models/blockchain-name");
const fake_wallet_address_1 = require("../../../../common/constants/fake-wallet-address");
const check_unsupported_receiver_address_1 = require("../../../../common/utils/check-unsupported-receiver-address");
const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee");
const calculate_rates_1 = require("./utils/calculate-rates");
const cross_chain_trade_type_1 = require("../../models/cross-chain-trade-type");
const cross_chain_provider_1 = require("../common/cross-chain-provider");
const proxy_cross_chain_evm_trade_1 = require("../common/proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade");
const eddy_bridge_supported_chains_1 = require("./constants/eddy-bridge-supported-chains");
const swap_limits_1 = require("./constants/swap-limits");
const eddy_bridge_trade_1 = require("./eddy-bridge-trade");
const eddy_bridge_api_service_1 = require("./services/eddy-bridge-api-service");
const eddy_bridge_contract_service_1 = require("./services/eddy-bridge-contract-service");
const eddy_bridge_routing_directions_1 = require("./utils/eddy-bridge-routing-directions");
class EddyBridgeProvider extends cross_chain_provider_1.CrossChainProvider {
constructor() {
super(...arguments);
this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.EDDY_BRIDGE;
}
isSupportedBlockchain(fromBlockchain) {
return eddy_bridge_supported_chains_1.eddyBridgeSupportedChains.some(chain => chain === fromBlockchain);
}
async calculate(from, toToken, options) {
const fromBlockchain = from.blockchain;
const useProxy = false;
const walletAddress = this.getWalletAddress(fromBlockchain) || fake_wallet_address_1.FAKE_WALLET_ADDRESS;
try {
(0, check_unsupported_receiver_address_1.checkUnsupportedReceiverAddress)(options?.receiverAddress, options?.fromAddress || walletAddress);
const routingDirection = (0, eddy_bridge_routing_directions_1.eddyRoutingDirection)(from, toToken);
const feeInfo = await this.getFeeInfo(fromBlockchain, options.providerAddress, from, useProxy);
const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(from, feeInfo.rubicProxy?.platformFee?.percent);
const [eddySlippage, outputAmount] = await Promise.all([
eddy_bridge_contract_service_1.EddyBridgeContractService.getEddySlipage(),
(0, calculate_rates_1.calculateRates)(fromWithoutFee, toToken, options.slippageTolerance)
]);
const to = await tokens_1.PriceTokenAmount.createToken({
...toToken.asStruct,
weiAmount: new bignumber_js_1.default(outputAmount)
});
const trade = new eddy_bridge_trade_1.EddyBridgeTrade({
crossChainTrade: {
feeInfo,
from: fromWithoutFee,
gasData: await this.getGasData(from),
to,
priceImpact: from.calculatePriceImpactPercent(to),
slippage: eddySlippage + options.slippageTolerance,
routingDirection,
quoteOptions: options
},
providerAddress: options.providerAddress,
routePath: await this.getRoutePath(from, to),
useProxy
});
return this.getCalculationResult(fromWithoutFee, trade);
}
catch (err) {
const rubicSdkError = cross_chain_provider_1.CrossChainProvider.parseError(err);
return {
trade: null,
error: rubicSdkError,
tradeType: this.type
};
}
}
async getCalculationResult(fromWithoutFee, trade) {
const limits = swap_limits_1.EDDY_BRIDGE_LIMITS.find(info => fromWithoutFee.isEqualTo(info));
if (!limits) {
throw new errors_1.NotSupportedTokensError();
}
let hasEnoughCapacity = true;
if (fromWithoutFee.blockchain !== blockchain_name_1.BLOCKCHAIN_NAME.ZETACHAIN) {
try {
const maxAmountWei = await eddy_bridge_api_service_1.EddyBridgeApiService.getWeiTokenLimitInForeignChain(fromWithoutFee);
hasEnoughCapacity = fromWithoutFee.weiAmount.lte(maxAmountWei);
}
catch { }
}
if (fromWithoutFee.tokenAmount.lt(limits.min)) {
return {
trade,
error: new errors_1.MinAmountError(limits.min, fromWithoutFee.symbol),
tradeType: this.type
};
}
if (fromWithoutFee.tokenAmount.gt(limits.max) || !hasEnoughCapacity) {
return {
trade,
error: new errors_1.MaxAmountError(limits.max, fromWithoutFee.symbol),
tradeType: this.type
};
}
return { trade, tradeType: this.type };
}
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.EddyBridgeProvider = EddyBridgeProvider;
//# sourceMappingURL=eddy-bridge-provider.js.map