Prices

The Prices class provides methods to fetch price information for coins on the Sui network. It allows you to get the current price of a coin, as well as price information for multiple coins simultaneously.

Creating a Prices Instance

To use the Prices class, you need to create an instance of it. You can specify the Sui network ("MAINNET" or "TESTNET") when creating the instance.

const af = new Aftermath("MAINNET"); 
const prices = af.Prices();

Fetching Coin Price Information

Get Price Info for a Single Coin

Retrieve detailed price information for a single coin using the getCoinPriceInfo method.

const coinType = "0x..."; // Replace with the CoinType (SUI address)
const priceInfo = await prices.getCoinPriceInfo({ coin: coinType });

console.log(priceInfo);
/*
{
  price: 1.23, // Current price of the coin in USD
  priceChange24HoursPercentage: 5.67, // Percentage change in the last 24 hours
}
*/

Get Price Info for Multiple Coins

Fetch price information for multiple coins with the getCoinsToPriceInfo method.

const coinTypes = ["0x...", "0x..."]; // Replace with CoinTypes
const coinsToPriceInfo = await prices.getCoinsToPriceInfo({ coins: coinTypes });

console.log(coinsToPriceInfo);
/*
{
  "0x...": {
    price: 1.23,
    priceChange24HoursPercentage: 5.67,
  },
  "0x...": {
    price: 4.56,
    priceChange24HoursPercentage: -2.34,
  },
}
*/

Get Current Price for a Single Coin

If you only need the current price of a coin, use the getCoinPrice method.

const coinType = "0x..."; // Replace with the CoinType
const price = await prices.getCoinPrice({ coin: coinType });

console.log(`Current price: $${price}`);

Get Current Prices for Multiple Coins

To get the current prices for multiple coins, use the getCoinsToPrice method.

const coinTypes = ["0x...", "0x..."]; // Replace with CoinTypes
const coinsToPrice = await prices.getCoinsToPrice({ coins: coinTypes });

console.log(coinsToPrice);
/*
{
  "0x...": 1.23,
  "0x...": 4.56,
}
*/

Types

`CoinType`: A string representing the unique identifier of a coin (e.g., its SUI address).

`CoinPriceInfo`: An object containing price information for a coin. -

price: The current price of the coin in USD.

priceChange24HoursPercentage: The percentage change in price over the last 24 hours.

Example Usage

import { Aftermath } from "aftermath-ts-sdk";

(async () => {
  const af = new Aftermath("MAINNET"); 
  const prices = af.Prices();

  // Get price info for a single coin
  const coinType = "0x..."; // Replace with the CoinType
  const priceInfo = await prices.getCoinPriceInfo({ coin: coinType });
  console.log(`Price of coin ${coinType}: $${priceInfo.price}`);

  // Get prices for multiple coins
  const coinTypes = ["0x...", "0x..."]; // Replace with CoinTypes
  const coinsToPrice = await prices.getCoinsToPrice({ coins: coinTypes });
  for (const [coin, price] of Object.entries(coinsToPrice)) {
    console.log(`Price of coin ${coin}: $${price}`);
  }
})();

Last updated