rubic-sdk-next
Version:
Simplify dApp creation
158 lines • 6.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SuiOnChainTrade = void 0;
const transactions_1 = require("@mysten/sui/transactions");
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 errors_2 = require("../../../../../../common/utils/errors");
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 on_chain_trade_1 = require("../on-chain-trade");
class SuiOnChainTrade 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.SUI);
}
get web3Private() {
return injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(blockchain_name_1.BLOCKCHAIN_NAME.SUI);
}
constructor(tradeStruct, providerAddress) {
super(providerAddress);
this.lastTransactionConfig = null;
this.apiQuote = null;
this.apiResponse = null;
this.from = tradeStruct.from;
this.to = tradeStruct.to;
this.slippageTolerance = tradeStruct.slippageTolerance;
this.path = tradeStruct.path;
this.gasFeeInfo = tradeStruct.gasFeeInfo;
this.useProxy = tradeStruct.useProxy;
this.fromWithoutFee = tradeStruct.fromWithoutFee;
this.apiQuote = tradeStruct?.apiQuote || null;
this.apiResponse = tradeStruct?.apiResponse || null;
this.feeInfo = {
rubicProxy: {
...(tradeStruct.proxyFeeInfo?.fixedFeeToken && {
fixedFee: {
amount: tradeStruct.proxyFeeInfo?.fixedFeeToken.tokenAmount || new bignumber_js_1.default(0),
token: tradeStruct.proxyFeeInfo?.fixedFeeToken
}
}),
...(tradeStruct.proxyFeeInfo?.platformFee && {
platformFee: {
percent: tradeStruct.proxyFeeInfo?.platformFee.percent || 0,
token: tradeStruct.proxyFeeInfo?.platformFee.token
}
})
}
};
this.withDeflation = tradeStruct.withDeflation;
}
async approve(_options, _checkNeedApprove = true, _amount = 'infinity') {
throw new Error('Method is not supported');
}
async encodeApprove(_tokenAddress, _spenderAddress, _value, _options = {}) {
throw new Error('Method is not supported');
}
async checkAllowanceAndApprove() {
throw new Error('Method is not supported');
}
/**
* Calculates value for swap transaction.
* @param providerValue Value, returned from cross-chain provider.
*/
getSwapValue(providerValue) {
const nativeToken = native_tokens_1.nativeTokensList[this.from.blockchain];
const fixedFeeValue = web3_pure_1.Web3Pure.toWei(this.feeInfo.rubicProxy?.fixedFee?.amount || 0, nativeToken.decimals);
let fromValue;
if (this.from.isNative) {
if (providerValue) {
fromValue = new bignumber_js_1.default(providerValue).dividedBy(1 - (this.feeInfo.rubicProxy?.platformFee?.percent || 0) / 100);
}
else {
fromValue = this.from.weiAmount;
}
}
else {
fromValue = new bignumber_js_1.default(providerValue || 0);
}
return new bignumber_js_1.default(fromValue).plus(fixedFeeValue).toFixed(0, 0);
}
async swap(options = {}) {
await this.checkWalletState(options?.testMode);
const { onConfirm } = options;
let transactionHash;
const onTransactionHash = (hash) => {
if (onConfirm) {
onConfirm(hash);
}
transactionHash = hash;
};
const fromAddress = this.walletAddress;
const receiverAddress = options.receiverAddress || this.walletAddress;
const transactionConfig = await this.encode({
fromAddress,
receiverAddress,
...options
});
try {
const tx = transactionConfig.transaction;
await this.web3Private.sendTransaction({
transactionBlock: transactions_1.Transaction.from(tx),
onTransactionHash
});
return transactionHash;
}
catch (err) {
if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) {
return transactionHash;
}
throw (0, errors_2.parseError)(err);
}
}
async encode(options) {
await this.checkFromAddress(options.fromAddress, true);
await this.checkReceiverAddress(options.receiverAddress);
return this.setTransactionConfig(options);
}
async getTransactionConfigAndAmount(options) {
if (!this.apiResponse || !this.apiQuote) {
throw new Error('Failed to load api response');
}
const swapRequestData = {
...this.apiQuote,
fromAddress: this.walletAddress,
receiver: options?.receiverAddress || this.walletAddress,
id: this.apiResponse.id
};
const swapData = await this.fetchSwapData(swapRequestData);
const config = {
transaction: swapData.transaction.transaction
};
const amount = swapData.estimate.destinationWeiAmount;
return { tx: config, toAmount: amount };
}
async setTransactionConfig(options) {
if (this.lastTransactionConfig && options.useCacheData) {
return this.lastTransactionConfig;
}
const { tx, toAmount } = await this.getTransactionConfigAndAmount(options);
this.lastTransactionConfig = tx;
setTimeout(() => {
this.lastTransactionConfig = null;
}, 15000);
if (!options.skipAmountCheck) {
this.checkAmountChange(toAmount, this.to.stringWeiAmount);
}
return tx;
}
}
exports.SuiOnChainTrade = SuiOnChainTrade;
//# sourceMappingURL=sui-on-chain-trade.js.map