rubic-sdk
Version:
Simplify dApp creation
111 lines • 5.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LifiBitcoinCrossChainTrade = void 0;
const errors_1 = require("../../../../../../common/errors");
const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure");
const injector_1 = require("../../../../../../core/injector/injector");
const get_solana_fee_1 = require("../../../../../common/utils/get-solana-fee");
const bitcoin_cross_chain_trade_1 = require("../../common/bitcoin-cross-chain-trade/bitcoin-cross-chain-trade");
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 lifi_api_service_1 = require("../services/lifi-api-service");
class LifiBitcoinCrossChainTrade extends bitcoin_cross_chain_trade_1.BitcoinCrossChainTrade {
get fromBlockchain() {
return this.from.blockchain;
}
get fromContractAddress() {
if (this.isProxyTrade) {
throw new Error('Solana contract is not implemented yet');
}
return rubic_proxy_contract_address_1.rubicProxyContractAddress[this.fromBlockchain].gateway;
}
get methodName() {
return 'startBridgeTokensViaGenericCrossChain';
}
get amountToCheck() {
return web3_pure_1.Web3Pure.toWei(this.toTokenAmountMin, this.to.decimals);
}
constructor(crossChainTrade, providerAddress, routePath, useProxy) {
super(providerAddress, routePath, useProxy);
/** @internal */
this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.LIFI;
this.isAggregator = false;
this.from = crossChainTrade.from;
this.to = crossChainTrade.to;
this.route = crossChainTrade.route;
this.memo = crossChainTrade.memo;
this.toTokenAmountMin = crossChainTrade.toTokenAmountMin;
this.feeInfo = crossChainTrade.feeInfo;
this.slippage = crossChainTrade.slippage;
this.priceImpact = crossChainTrade.priceImpact;
this.onChainSubtype = crossChainTrade.onChainSubtype;
this.bridgeType = crossChainTrade.bridgeType;
}
async getTransactionConfigAndAmount(receiverAddress) {
const firstStep = this.route.steps[0];
const step = {
...firstStep,
action: {
...firstStep.action,
fromAddress: this.walletAddress,
toAddress: receiverAddress || this.walletAddress
},
execution: {
status: 'NOT_STARTED',
process: [
{
message: 'Preparing transaction.',
startedAt: Date.now(),
status: 'STARTED',
type: 'CROSS_CHAIN'
}
]
}
};
try {
const rubicFee = (0, get_solana_fee_1.getSolanaFee)(this.from);
const swapResponse = await lifi_api_service_1.LifiApiService.getQuote(step.action.fromChainId, step.action.toChainId, step.action.fromToken.symbol, step.action.toToken.symbol, this.from.stringWeiAmount, step.action.fromAddress, step.action.toAddress, step.action.slippage, rubicFee ? rubicFee : undefined);
return {
config: swapResponse.transactionRequest,
amount: swapResponse.estimate.toAmountMin
};
}
catch (err) {
if (err.error?.message?.includes('None of the available routes could successfully generate a tx')) {
const enoughBalance = await this.checkEnoughBalance(step.estimate.gasCosts?.[0]);
if (!enoughBalance) {
throw new errors_1.InsufficientFundsGasPriceValueError();
}
}
if ('statusCode' in err && 'message' in err) {
throw new errors_1.RubicSdkError(err.message);
}
throw err;
}
}
getTradeInfo() {
return {
estimatedGas: this.estimatedGas,
feeInfo: this.feeInfo,
priceImpact: this.priceImpact || null,
slippage: this.slippage * 100,
routePath: this.routePath
};
}
getTradeAmountRatio(fromUsd) {
return fromUsd.dividedBy(this.to.tokenAmount);
}
async checkEnoughBalance(gasCosts) {
const web3Public = injector_1.Injector.web3PublicService.getWeb3Public(this.from.blockchain);
const userWeiBalance = await web3Public.getBalance(this.walletAddress);
if (gasCosts) {
const fromAmounWithGasCosts = this.from.weiAmount.plus(gasCosts.amount);
if (userWeiBalance.lte(fromAmounWithGasCosts)) {
return false;
}
}
return userWeiBalance.lte(this.from.weiAmount);
}
}
exports.LifiBitcoinCrossChainTrade = LifiBitcoinCrossChainTrade;
//# sourceMappingURL=lifi-bitcoin-cross-chain-trade.js.map