rubic-sdk-next
Version:
Simplify dApp creation
82 lines • 3.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TonCrossChainTrade = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../../common/errors");
const blockchain_name_1 = require("../../../../../../core/blockchain/models/blockchain-name");
const injector_1 = require("../../../../../../core/injector/injector");
const cross_chain_trade_1 = require("../cross-chain-trade");
class TonCrossChainTrade extends cross_chain_trade_1.CrossChainTrade {
/**
* Gas fee info in source blockchain.
*/
get fromWeb3Public() {
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);
}
async encode(options) {
await this.checkFromAddress(options.fromAddress, true);
await this.checkReceiverAddress(options.receiverAddress, true);
return this.setTransactionConfig(options?.skipAmountCheck || false, options?.useCacheData || false, options.testMode, options?.receiverAddress || this.walletAddress);
}
async swap(options = {}) {
if (!options?.testMode) {
await this.checkTradeErrors();
}
await this.checkReceiverAddress(options.receiverAddress, true);
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 getTransactionConfigAndAmount(testMode, receiverAddress) {
const swapRequestParams = {
...this.apiQuote,
fromAddress: this.walletAddress,
receiver: receiverAddress,
id: this.apiResponse.id,
enableChecks: !testMode
};
const swapData = await this.fetchSwapData(swapRequestParams);
this._uniqueInfo = swapData.uniqueInfo ?? {};
const toAmount = swapData.estimate.destinationWeiAmount;
return { config: swapData.transaction, amount: toAmount };
}
getUsdPrice(providerFeeToken) {
let feeSum = new bignumber_js_1.default(0);
const providerFee = this.feeInfo.provider?.cryptoFee;
if (providerFee) {
feeSum = feeSum.plus(providerFee.amount.multipliedBy(providerFeeToken || providerFee.token.price));
}
return this.to.price.multipliedBy(this.to.tokenAmount).minus(feeSum);
}
authWallet() {
throw new errors_1.RubicSdkError('Method not implemented.');
}
}
exports.TonCrossChainTrade = TonCrossChainTrade;
//# sourceMappingURL=ton-cross-chain-trade.js.map