UNPKG

tronsave-sdk

Version:

TypeScript SDK for Tronsave API to manage TRON blockchain resources

209 lines (207 loc) 7.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TronsaveSDK = void 0; /** * Main SDK class for interacting with the Tronsave API */ class TronsaveSDK { /** * Initialize the SDK with configuration * @param config SDK configuration options */ constructor(config) { this.baseUrl = 'https://api.tronsave.io'; if (config === null || config === void 0 ? void 0 : config.network) { this.baseUrl = config.network === "testnet" ? "https://api-dev.tronsave.io" : "https://api.tronsave.io"; } this.apiKey = config === null || config === void 0 ? void 0 : config.apiKey; } /** * Helper method to perform fetch with timeout and error handling */ async Fetch(url, options) { // Set headers, including API key if provided const headers = { "Content-Type": "application/json", ...(this.apiKey ? { apikey: this.apiKey } : {}), ...(options.headers || {}), }; try { const response = await fetch(url, { ...options, headers, }); if (!response.ok) { let errorBody = {}; try { errorBody = await response.json(); } catch (error) { throw error; } throw new Error(errorBody.message); } return (await response.json()).data; } catch (error) { throw error; } } /** * Buy resource with standard parameters * @param orderInfo Buy resource order info - {@link OrderInfoParams} * @param options Optional: Options for the buy resource - {@link OrderOptionParams} * @returns information about the order - {@link BuyResourceResponse} * @example * const { orderId } = await tronsaveSDK.buyResource( { receiver: "TRx...Jhd", resourceType: "ENERGY", unitPrice: 'MEDIUM', durationSec: 259200, resourceAmount: 32000, }, { allowPartialFill: true, maxPriceAccepted: 100, } ); */ async buyResource(orderInfo, options, isSmartMatching) { return this.Fetch(`${this.baseUrl}/v2/buy-resource`, { method: "POST", body: JSON.stringify({ ...orderInfo, options, isSmartMatching }), }); } /** * Estimate buy resource with standard parameters * @param orderInfo Buy resource order info - {@link OrderInfoParams} * @param options Optional: Options for the buy resource - {@link OrderOptionParams} * @returns estimated data - {@link EstimateBuyResourceResponse} * * @example * const { unitPrice, durationSec, estimateTrx, availableResource } = await tronsaveSDK.estimateBuyResource( { receiver: "TRx...Jhd", resourceType: "ENERGY", unitPrice: 'MEDIUM', durationSec: 259200, resourceAmount: 32000, }, { allowPartialFill: true, maxPriceAccepted: 100, } ); */ async estimateBuyResource(orderInfo, options) { return this.Fetch(`${this.baseUrl}/v2/estimate-buy-resource`, { method: "POST", body: JSON.stringify({ ...orderInfo, options }), }); } /** * Extend request with standard parameters * @param extendRequestData - {@link ExtendRequestParams} * @returns information about the order - {@link ExtendRequestResponse} * * @example * const { orderId } = await tronsaveSDK.extendRequest( { receiver: "TRx...Jhd", extendData: [{ delegator: "TRx...Jfv", extendTo: 1715222400, isExtend: true, extraAmount: 0, }] } ); */ async extendRequest(extendRequestData) { return this.Fetch(`${this.baseUrl}/v2/extend-request`, { method: "POST", body: JSON.stringify(extendRequestData), }); } /** * Get extendable delegates * @param request - {@link GetExtendableDelegatesParams} * @returns information about the extendable delegates - {@link GetExtendableDelegatesResponse} * * @example * const extendableDelegates = await tronsaveSDK.getExtendableDelegates({ receiver: "TRx...Jhd", extendTo: 1715222400, resourceType: "ENERGY", maxPriceAccepted: 100, }); * */ async getExtendableDelegates(request) { return this.Fetch(`${this.baseUrl}/v2/get-extendable-delegates`, { method: "POST", body: JSON.stringify(request), }); } /** * Get user info by Api key * @returns Information about the internal account - {@link GetUserInfoResponse} * * @example * const userInfo = await tronsaveSDK.getUserInfo(); */ async getUserInfo() { return this.Fetch(`${this.baseUrl}/v2/user-info`, { method: "GET", }); } /** * Get order details by Api key * @param orderId Order id * @returns Order details - {@link OrderResponse} * * @example * const order = await tronsaveSDK.getOrder("67ce9ea9dede...8fa2e058"); */ async getOrder(orderId) { return this.Fetch(`${this.baseUrl}/v2/order/${orderId}`, { method: "GET", }); } /** * Get orders history * @param page - Page number. Default is 0, * @param limit - Limit number. Default is 10, Maximum is 50 * @returns Orders - {@link OrdersResponse} * * @example * const orders = await tronsaveSDK.getOrders(1, 10); */ async getOrders(page, limit) { const params = new URLSearchParams({ page: (page === null || page === void 0 ? void 0 : page.toString()) || "0", pageSize: (limit === null || limit === void 0 ? void 0 : limit.toString()) || "10" }).toString(); return this.Fetch(`${this.baseUrl}/v2/orders?${params}`, { method: "GET", }); } /** * Get order book * @param query Optional: query parameters - {@link OrderBookQuery} * @returns Order book data - {@link OrderBookResponse} * * @example * const orderBook = await getOrderBook({ * resourceType: 'ENERGY', * address: 'TRx...Jhd', * minDelegateAmount: 32000, * durationSec: 900 * }); */ async getOrderBook(query) { const params = new URLSearchParams(query).toString(); return this.Fetch(`${this.baseUrl}/v2/order-book?${params}`, { method: "GET", }); } } exports.TronsaveSDK = TronsaveSDK;