rubic-sdk
Version:
Simplify dApp creation
126 lines • 5.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LifiSolanaCrossChainTrade = void 0;
const errors_1 = require("../../../../../../common/errors");
const errors_2 = require("../../../../../../common/utils/errors");
const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure");
const get_solana_fee_1 = require("../../../../../common/utils/get-solana-fee");
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 solana_cross_chain_trade_1 = require("../../common/solana-cross-chain-trade/solana-cross-chain-trade");
const lifi_api_service_1 = require("../services/lifi-api-service");
class LifiSolanaCrossChainTrade extends solana_cross_chain_trade_1.SolanaCrossChainTrade {
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.providerGateway = this.route.steps[0].estimate.approvalAddress;
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 swapDirect(options = {}) {
this.checkWalletConnected();
await this.checkAllowanceAndApprove(options);
let transactionHash;
try {
const { data } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress);
const { onConfirm } = options;
const onTransactionHash = (hash) => {
if (onConfirm) {
onConfirm(hash);
}
transactionHash = hash;
};
await this.web3Private.sendTransaction({
data,
onTransactionHash
}, false);
return transactionHash;
}
catch (err) {
if (err?.error?.errorId === 'ERROR_LOW_GIVE_AMOUNT') {
throw new errors_1.TooLowAmountError();
}
if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) {
return transactionHash;
}
throw (0, errors_2.parseError)(err);
}
}
async getContractParams(_options, _skipAmountChangeCheck = false) {
throw new Error('Solana contracts is not implemented yet');
}
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 ('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);
}
}
exports.LifiSolanaCrossChainTrade = LifiSolanaCrossChainTrade;
//# sourceMappingURL=lifi-solana-cross-chain-trade.js.map