rubic-sdk
Version:
Simplify dApp creation
46 lines • 2.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TeleSwapUtilsService = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const chain_type_1 = require("../../../../../../core/blockchain/models/chain-type");
const blockchains_info_1 = require("../../../../../../core/blockchain/utils/blockchains-info/blockchains-info");
const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure");
const teleswap_network_tickers_1 = require("../constants/teleswap-network-tickers");
class TeleSwapUtilsService {
static async calculateOutputWeiAmount(fromToken, toToken, teleSwapSdk) {
const fromChainType = blockchains_info_1.BlockchainsInfo.getChainType(fromToken.blockchain);
if (fromChainType === chain_type_1.CHAIN_TYPE.EVM) {
return TeleSwapUtilsService.calculateBtcOutputWeiAmount(fromToken, teleSwapSdk);
}
return TeleSwapUtilsService.calculateEvmOutputWeiAmount(fromToken, toToken, teleSwapSdk);
}
static async calculateEvmOutputWeiAmount(fromToken, toToken, sdk) {
const toTokenAddress = TeleSwapUtilsService.getTokenAddress(toToken);
const estimation = (await sdk.wrapAndSwapEstimate(fromToken.tokenAmount.toFixed(), teleswap_network_tickers_1.teleSwapNetworkTickers[toToken.blockchain], toTokenAddress));
const toAmount = new bignumber_js_1.default('outputAmount' in estimation
? estimation.outputAmount
: web3_pure_1.Web3Pure.toWei(estimation.outputAmountBTC, toToken.decimals));
// const feeWeiAmount = toAmount.multipliedBy(TeleSwapUtilsService.bitcoinFeePercent);
return toAmount;
}
static async calculateBtcOutputWeiAmount(fromToken, sdk) {
const fromTokenAddress = TeleSwapUtilsService.getTokenAddress(fromToken);
const estimation = await sdk.swapAndUnwrapEstimate({
inputAmount: fromToken.stringWeiAmount,
...(fromTokenAddress && { inputToken: fromTokenAddress })
}, teleswap_network_tickers_1.teleSwapNetworkTickers[fromToken.blockchain]);
return new bignumber_js_1.default(web3_pure_1.Web3Pure.toWei(estimation.outputAmountBTC, 8));
}
static getTokenAddress(token) {
if (token.isNative) {
return null;
}
return token.address;
}
}
exports.TeleSwapUtilsService = TeleSwapUtilsService;
TeleSwapUtilsService.bitcoinFeePercent = 0.02;
//# sourceMappingURL=teleswap-utils-service.js.map