UNPKG

rubic-sdk-next

Version:
136 lines 5.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TonOnChainTrade = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const errors_1 = require("../../../../../../common/errors"); const native_tokens_1 = require("../../../../../../common/tokens/constants/native-tokens"); const blockchain_name_1 = require("../../../../../../core/blockchain/models/blockchain-name"); const web3_pure_1 = require("../../../../../../core/blockchain/web3-pure/web3-pure"); const injector_1 = require("../../../../../../core/injector/injector"); const check_unsupported_receiver_address_1 = require("../../../../../common/utils/check-unsupported-receiver-address"); const on_chain_trade_1 = require("../on-chain-trade"); class TonOnChainTrade extends on_chain_trade_1.OnChainTrade { get spenderAddress() { throw new errors_1.RubicSdkError('No spender address!'); } get web3Public() { return injector_1.Injector.web3PublicService.getWeb3Public(blockchain_name_1.BLOCKCHAIN_NAME.TON); } get web3Private() { return injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(blockchain_name_1.BLOCKCHAIN_NAME.TON); } constructor(tradeStruct, providerAddress) { super(providerAddress); this.feeInfo = {}; this.path = []; this.skipAmountCheck = false; this.apiQuote = null; this.apiResponse = null; this.from = tradeStruct.from; this.to = tradeStruct.to; this.slippageTolerance = tradeStruct.slippageTolerance; this.gasFeeInfo = tradeStruct.gasFeeInfo; this.routingPath = tradeStruct.routingPath; this.additionalInfo = { isMultistep: this.routingPath.length > 1, isChangedSlippage: tradeStruct.isChangedSlippage }; this.apiQuote = tradeStruct?.apiQuote || null; this.apiResponse = tradeStruct?.apiResponse || null; } async needApprove() { return false; } async approve() { throw new errors_1.RubicSdkError('Method not implemented!'); } async encodeApprove() { throw new Error('Method is not supported'); } async encode(options) { await this.checkFromAddress(options.fromAddress, true); await this.checkReceiverAddress(options.receiverAddress); return this.setTransactionConfig(options); } async swap(options) { await this.checkWalletState(options?.testMode); const fromAddress = this.walletAddress; const transactionConfig = await this.encode({ ...options, fromAddress }); const { onConfirm } = options; let transactionHash; const onTransactionHash = (hash) => { if (onConfirm) { onConfirm(hash); } transactionHash = hash; }; try { await this.web3Private.sendTransaction({ messages: transactionConfig.tonMessages, onTransactionHash }); return transactionHash; } catch (err) { if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) { return transactionHash; } throw err; } } async setTransactionConfig(options) { const { config, amount } = await this.getTransactionConfigAndAmount(options.receiverAddress); if (!options.skipAmountCheck) { this.checkAmountChange(amount, this.to.stringWeiAmount); } return config; } async getTransactionConfigAndAmount(receiverAddress) { if (!this.apiResponse || !this.apiQuote) { throw new Error('Failed to load api response'); } const swapRequestParams = { ...this.apiQuote, fromAddress: this.walletAddress, receiver: receiverAddress || this.walletAddress, id: this.apiResponse.id }; const swapData = await this.fetchSwapData(swapRequestParams); const toAmount = swapData.estimate.destinationWeiAmount; return { config: swapData.transaction, amount: toAmount }; } async checkNativeBalance() { const balanceWei = await this.web3Public.getBalance(this.walletAddress); const requiredBalanceNonWei = this.gasFeeInfo?.totalGas ? web3_pure_1.Web3Pure.fromWei(this.gasFeeInfo.totalGas, native_tokens_1.nativeTokensList.TON.decimals) : new bignumber_js_1.default(0); if (balanceWei.lt(requiredBalanceNonWei)) { throw new errors_1.InsufficientFundsError(native_tokens_1.nativeTokensList[blockchain_name_1.BLOCKCHAIN_NAME.TON].symbol); } } async makePreSwapChecks(options) { (0, check_unsupported_receiver_address_1.checkUnsupportedReceiverAddress)(options.receiverAddress, this.walletAddress); await this.checkFromAddress(options.fromAddress); await this.checkNativeBalance(); await this.checkBalance(); if (!options.skipAmountCheck) { this.skipAmountCheck = true; const toWeiAmount = await this.calculateOutputAmount(options); this.checkAmountChange(toWeiAmount, this.to.stringWeiAmount); } } getTradeInfo() { return { estimatedGas: null, feeInfo: this.feeInfo, priceImpact: this.priceImpact ?? null, slippage: this.slippageTolerance * 100, routePath: this.routingPath }; } } exports.TonOnChainTrade = TonOnChainTrade; //# sourceMappingURL=ton-on-chain-trade.js.map