rubic-sdk
Version:
Simplify dApp creation
100 lines • 4.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SymbiosisCcrTonTrade = void 0;
const errors_1 = require("../../../../../../common/errors");
const symbiosis_api_service_1 = require("../../../../../common/providers/symbiosis/services/symbiosis-api-service");
const cross_chain_trade_type_1 = require("../../../models/cross-chain-trade-type");
const bridge_type_1 = require("../../common/models/bridge-type");
const ton_cross_chain_trade_1 = require("../../common/ton-cross-chain-trade/ton-cross-chain-trade");
const symbiosis_utils_1 = require("../symbiosis-utils");
/**
* Calculated Symbiosis cross-chain trade.
*/
class SymbiosisCcrTonTrade extends ton_cross_chain_trade_1.TonCrossChainTrade {
get fromContractAddress() {
throw new Error('Not implemented');
}
get methodName() {
throw new Error('Not implemented');
}
constructor(crossChainTrade, providerAddress, routePath, useProxy) {
super(providerAddress, routePath, useProxy);
this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.SYMBIOSIS;
this.isAggregator = false;
this.bridgeType = bridge_type_1.BRIDGE_TYPE.SYMBIOSIS;
this.from = crossChainTrade.from;
this.to = crossChainTrade.to;
this.swappingParams = crossChainTrade.swapParams;
this.gasData = crossChainTrade.gasData;
this.priceImpact = crossChainTrade.priceImpact;
this.toTokenAmountMin = this.to.tokenAmount.multipliedBy(1 - crossChainTrade.slippage);
this.feeInfo = crossChainTrade.feeInfo;
this.slippage = crossChainTrade.slippage;
this.transitAmount = crossChainTrade.transitAmount;
this.onChainSubtype = symbiosis_utils_1.SymbiosisUtils.getSubtype(crossChainTrade.tradeType, crossChainTrade.to.blockchain);
this.promotions = crossChainTrade?.promotions || [];
}
async getContractParams(_options) {
throw new Error('Not implemented');
}
getTradeAmountRatio(fromUsd) {
return fromUsd.dividedBy(this.to.tokenAmount);
}
getTradeInfo() {
return {
estimatedGas: null,
feeInfo: this.feeInfo,
priceImpact: this.priceImpact ?? null,
slippage: this.slippage * 100,
routePath: this.routePath
};
}
async getTransactionConfigAndAmount(receiverAddress) {
const walletAddress = this.walletAddress;
const params = {
...this.swappingParams,
from: walletAddress,
to: receiverAddress || walletAddress,
revertableAddress: symbiosis_utils_1.SymbiosisUtils.getRevertableAddress(receiverAddress, walletAddress, this.to.blockchain)
};
const tradeData = await symbiosis_api_service_1.SymbiosisApiService.getCrossChainSwapTx(params);
const tx = tradeData.tx;
const swapMessage = tx.messages[0];
if (!swapMessage || tx.messages.length > 1) {
throw new Error('Wrong config');
}
const config = {
payload: swapMessage.payload,
amount: swapMessage.amount,
address: swapMessage.address
};
return { config, amount: tradeData.tokenAmountOut.amount };
}
async swap(options = {}) {
if (!options?.testMode) {
await this.checkTradeErrors();
}
await this.checkReceiverAddress(options.receiverAddress, true);
const config = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress || this.walletAddress);
const { onConfirm } = options;
let transactionHash;
const onTransactionHash = (hash) => {
if (onConfirm) {
onConfirm(hash);
}
transactionHash = hash;
};
try {
await this.web3Private.sendTransaction({ messages: [config], onTransactionHash });
return transactionHash;
}
catch (err) {
if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) {
return transactionHash;
}
throw err;
}
}
}
exports.SymbiosisCcrTonTrade = SymbiosisCcrTonTrade;
//# sourceMappingURL=symbiosis-ccr-ton-trade.js.map