aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
125 lines (124 loc) • 4.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Prices = void 0;
const caller_1 = require("../utils/caller");
/**
* The `Prices` class provides methods for fetching price information for various
* coins on the Sui network, including single-coin or multi-coin queries.
*/
class Prices extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates a new `Prices` instance for retrieving coin price data from
* Aftermath's backend or other data sources.
*
* @param config - Optional configuration, including network and access token.
*/
constructor(config) {
super(config, "price-info");
}
// =========================================================================
// Prices
// =========================================================================
/**
* Retrieves detailed price information (including current price and 24h change)
* for a single coin.
*
* @param inputs - Contains the `coin` type (e.g., "0x2::sui::SUI").
* @returns A promise resolving to a `CoinPriceInfo` object.
*
* @example
* ```typescript
*
* const afSdk = new Aftermath("MAINNET");
* await afSdk.init(); // initialize provider
*
* const prices = afSdk.Prices();
*
* const suiPriceInfo = await prices.getCoinPriceInfo({
* coin: "0x2::sui::SUI"
* });
* console.log(suiPriceInfo.price, suiPriceInfo.priceChange24HoursPercentage);
* ```
*/
getCoinPriceInfo(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const coinsToPriceInfo = yield this.getCoinsToPriceInfo({
coins: [inputs.coin],
});
return Object.values(coinsToPriceInfo)[0];
});
}
/**
* Retrieves detailed price information for multiple coins simultaneously,
* returning a record keyed by `CoinType`.
*
* @param inputs - An object containing an array of `coins`.
* @returns A promise resolving to a `CoinsToPriceInfo` mapping each coin type to its price info.
*
* @example
* ```typescript
* const prices = new Prices();
* const info = await prices.getCoinsToPriceInfo({
* coins: ["0x2::sui::SUI", "0x<some_other_coin>"]
* });
* console.log(info);
* ```
*/
getCoinsToPriceInfo(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("", inputs);
});
}
/**
* Fetches only the current price in USD for a single coin.
*
* @param inputs - Contains the `coin` type.
* @returns A promise resolving to a `number` representing the price in USD.
*
* @example
* ```typescript
* const prices = new Prices();
* const suiPrice = await prices.getCoinPrice({ coin: "0x2::sui::SUI" });
* console.log("SUI price in USD:", suiPrice);
* ```
*/
getCoinPrice(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const priceInfo = yield this.getCoinPriceInfo(inputs);
return priceInfo.price;
});
}
/**
* Fetches current prices in USD for multiple coins, returning a record keyed by `CoinType`.
*
* @param inputs - Contains an array of `coins`.
* @returns A promise resolving to a `CoinsToPrice` object mapping coin types to their prices in USD.
*
* @example
* ```typescript
* const prices = new Prices();
* const multiPrices = await prices.getCoinsToPrice({ coins: ["0x2::sui::SUI", "0x<other>"] });
* console.log(multiPrices["0x2::sui::SUI"]); // e.g. 1.23
* ```
*/
getCoinsToPrice(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const coinsToPriceInfo = yield this.getCoinsToPriceInfo(inputs);
const coinsToPrice = Object.entries(coinsToPriceInfo).reduce((acc, [coinType, info]) => (Object.assign(Object.assign({}, acc), { [coinType]: info.price })), {});
return coinsToPrice;
});
}
}
exports.Prices = Prices;