rubic-sdk-next
Version:
Simplify dApp creation
178 lines • 6.91 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.RubicApiUtils = void 0;
const core_1 = require("@cryptorubic/core");
const core_2 = require("@ton/core");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const tokens_1 = require("../../../common/tokens");
const native_tokens_1 = require("../../../common/tokens/constants/native-tokens");
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 rubic_api_parser_1 = require("./rubic-api-parser");
class RubicApiUtils {
static getTradeParams(quote, response, providerType) {
const isFailedResponse = !response;
return isFailedResponse
? RubicApiUtils.getEmptyTradeParams(quote, providerType)
: RubicApiUtils.getFullTradeParams(quote, response);
}
static async getEmptyTradeParams(quote, providerType) {
const [fromToken, toToken] = await Promise.all([
tokens_1.PriceTokenAmount.createToken({
address: quote.srcTokenAddress,
blockchain: quote.srcTokenBlockchain,
tokenAmount: new bignumber_js_1.default(quote.srcTokenAmount)
}),
tokens_1.PriceTokenAmount.createToken({
address: quote.dstTokenAddress,
blockchain: quote.dstTokenBlockchain,
tokenAmount: new bignumber_js_1.default(0)
})
]);
const swapType = fromToken.blockchain === toToken.blockchain ? 'on-chain' : 'cross-chain';
const routePath = [
{
path: [fromToken, toToken],
provider: providerType,
type: swapType
}
];
const tradeParams = {
from: fromToken,
to: toToken,
apiQuote: quote,
routePath,
feeInfo: {},
apiResponse: this.getEmptyResponse(fromToken, toToken, providerType)
};
return tradeParams;
}
static async getFullTradeParams(quote, response) {
const { fromToken, toToken } = RubicApiUtils.getFromToTokens(response.tokens, quote.srcTokenAmount, response.estimate.destinationTokenAmount);
const feeInfo = rubic_api_parser_1.RubicApiParser.parseFeeInfoDto(response.fees);
const routePath = rubic_api_parser_1.RubicApiParser.parseRoutingDto(response.routing);
const tradeParams = {
from: fromToken,
to: toToken,
feeInfo,
routePath,
apiQuote: quote,
apiResponse: response
};
return tradeParams;
}
static getFromToTokens(tokens, fromAmount, toAmount) {
const fromTokenAddress = RubicApiUtils.parseTokenAddress(tokens.from);
const toTokenAddress = RubicApiUtils.parseTokenAddress(tokens.to);
const fromToken = new tokens_1.PriceTokenAmount({
...tokens.from,
address: fromTokenAddress,
price: new bignumber_js_1.default(tokens.from.price || NaN),
tokenAmount: new bignumber_js_1.default(fromAmount)
});
const toToken = new tokens_1.PriceTokenAmount({
...tokens.to,
address: toTokenAddress,
price: new bignumber_js_1.default(tokens.to.price || NaN),
tokenAmount: new bignumber_js_1.default(toAmount)
});
return { fromToken, toToken };
}
static parseTokenAddress(token) {
const chainType = blockchains_info_1.BlockchainsInfo.getChainType(token.blockchain);
const isNativeToken = web3_pure_1.Web3Pure[chainType]?.isNativeAddress(token.address);
if (isNativeToken) {
return token.address;
}
if (chainType === chain_type_1.CHAIN_TYPE.TON) {
return core_2.Address.parseRaw(token.address).toString();
}
return token.address;
}
static getEmptyResponse(fromToken, toToken, providerType) {
const swapType = fromToken.blockchain === toToken.blockchain ? 'on-chain' : 'cross-chain';
const srcChainId = core_1.blockchainId[fromToken.blockchain];
const dstChainId = core_1.blockchainId[toToken.blockchain];
const nativeToken = native_tokens_1.nativeTokensList[fromToken.blockchain];
const from = {
...fromToken,
blockchainId: srcChainId
};
const to = {
...toToken,
blockchainId: dstChainId
};
const emptyResponse = {
providerType: providerType,
swapType,
useRubicContract: false,
transaction: {},
id: '0',
warnings: [],
routing: [
{
path: [
{
...from,
amount: '0'
},
{
...to,
amount: '0'
}
],
provider: providerType,
type: swapType
}
],
estimate: {
destinationTokenAmount: '0',
destinationTokenMinAmount: '0',
destinationWeiAmount: '0',
destinationWeiMinAmount: '0',
slippage: 0,
priceImpact: 0,
durationInMinutes: 0
},
tokens: {
from,
to
},
fees: {
gasTokenFees: {
nativeToken: {
...nativeToken,
blockchainId: srcChainId
},
protocol: {
fixedAmount: '0',
fixedUsdAmount: 0,
fixedWeiAmount: '0'
},
provider: {
fixedAmount: '0',
fixedUsdAmount: 0,
fixedWeiAmount: '0'
},
gas: {
gasPrice: '0',
gasLimit: '0',
totalWeiAmount: '0',
totalUsdAmount: 0
}
},
percentFees: {
percent: 0,
token: from
}
}
};
return emptyResponse;
}
}
exports.RubicApiUtils = RubicApiUtils;
//# sourceMappingURL=rubic-api-utils.js.map