Git Product home page Git Product logo

redstone-api's Introduction

Redstone API

License Github activity Discord NPM

Twitter

Redstone API is a Javascript library for fetching trusted token pricing data from Redstone data ecosystem.

It is a Javascript wrapper for Redstone HTTP Api.

๐Ÿš€ Demo

Try it directly in CodeSandbox: demo link

โœ… Why Redstone API

โœ“ Secure

Redstone pricing data is secured on Arweave and protected by the provider's collateral. Learn more

โœ“ Easy to use

You don't need any API keys. Just install the npm package and add a single line of code. Quick start

โœ“ 100+ tokens

We support BTC, ETH, AR, EUR, and many other crypto and fiat currencies. All supported tokens

โœ“ TypeScript Support

Redstone API is fully written in Typescript and then compiled to JavaScript. Source code

๐Ÿ“– Documentation

This readme should provide you with all the information you need to start using redstone api. If you want to see the full documentation, visit api.docs.redstone.finance

๐Ÿ“ฆ Installation

Using npm

npm install redstone-api

Using yarn

yarn add redstone-api

๐Ÿค– Usage

Importing

// Using Node.js `require()`
const redstone = require('redstone-api');

// Using ES6 imports
import redstone from 'redstone-api';

Get the latest price for a single token

const price = await redstone.getPrice("AR");

console.log(price.value); // latest price value for AR token (in USD)
console.log(price.timestamp); // the exact timestamp of the price

๐Ÿ’ก Note: All the prices are denominated in USD. You can fetch price data for BTC, ETH, AR, EUR and any other of 100+ supported tokens.

Available symbols

You can use a symbols object to explore all available symbols right in the code.

import redstone from 'redstone-api';

const price = await redstone.getPrice("AR");

Price data format
{
  value: 123.23, // Number: Price value in USD
  timestamp: 1617146511173, // Number: Timestamp (ms) for price
  provider: "I-5rWUehEv-MjdK9gFw09RxfSLQX9DIHxG614Wf8qo0", // String: Provider arweave address
  permawebTx: "V8FUU0BG4kVOJwKWHzgkn1aEFm-eanhqqEXfPFY7pmI", // String: Arweave transaction id
  source: {"coingecko": 123,"sushiswap": 123.23,"uniswap": 123.35}, // Object: Prices from different sources
}

Fetch price using promises
// As async/await is only a syntactic sugar on Javascript
// Promises you can use them in a "standard" way
const price = redstone.getPrice("AR").then((price) => {
  console.log(price.value); // latest price value for AR token
});


Get the latest prices for several tokens

To fetch prices for several tokens use the getPrice method and pass an array with any subset of supported tokens.

const prices = await redstone.getPrice(["BTC", "ETH", "AR", "EUR"]);

console.log(prices); // Example output below
/*
{
  "BTC": {
    value: 58953.39,
    timestamp: 1617152802779,
    ...
  },
  "ETH": {
    value: 1856.75,
    timestamp: 1617152802779,
    ...
  },
  ...
}
*/


console.log(prices["BTC"].value); // latest price value for BTC
console.log(prices["ETH"].value); // latest price value for ETH
console.log(prices["AR"].value); // latest price value for AR

Get prices for all available tokens

To fetch the latest prices for all available tokens use the getAllPrices method.

const prices = await redstone.getAllPrices();

console.log(prices); // Example output below
/*
{
  "BTC": {...},
  "ETH": {...},
  ...
}
*/

console.log(prices["AR"].value); // latest price value for AR
console.log(prices["EUR"].value); // latest price value for EUR

Get the historical price for a single token

To get the historical price use the getHistoricalPrice method.

const price = await redstone.getHistoricalPrice("AR", {
  date: "2021-03-30T12:35:09", // Any convertable to date type
});

console.log(price.value); // AR price for specific time

๐Ÿ’ก Note: date argument must be convertable to Date type. You may pass date (e.g. new Date(2021-04-01)), timestamp (e.g. 1617709771289), or just string (e.g. 2021-04-01 or 2021-04-01T12:30:58).


Get the historical price for several tokens

To fetch the historical price for several tokens pass an array of symbols to getHistoricalPrice method.

const symbols = ["AR", "BTC", "UNI", "ETH", "EUR"];
const prices = await redstone.getHistoricalPrice(symbols, {
  date: "2021-03-30T12:35:09",
});

console.log(prices["BTC"].value); // BTC price for specific time

Get the historical prices in a time range

To fetch the historical prices in a time range specify token symbol as the first argument of the getHistoricalPrice method, and startDate, endDate and interval as fields of the second argument.

๐Ÿ’ก Note: currently Redstone API supports fetching historical prices in a time range only for a single token.

const prices = await redstone.getHistoricalPrice("AR", {
  startDate: "2021-03-29T12:35:09",
  endDate: "2021-03-30T12:35:09",
  interval: 3600 * 1000, // 1 hour
});

console.log(prices); // Example output below
/*
[
  {
    value: 28.8,
    timestamp: 1617016995624,
    ...
  },
  {
    value: 28.59,
    timestamp: 1617014111705,
    ...
  },
  ...
]
*/

๐Ÿ’ก Note: startDate and endDate argument must be convertable to Date type.

Get prices with pagination

To fetch prices with pagination specify token symbol as the first argument of the getHistoricalPrice method, and offset with limit as properties of the second argument.

๐Ÿ’ก Note: pagination is supported only for a single token.

const prices = await redstone.getHistoricalPrice("AR", {
  offset: 1000,
  limit: 100,
});

Verify signature

All prices saved in Redstone have a signature, thanks to which you always can verify if the price data has been submitted by the trusted provider.

To do so you can set verifySignature option to true in getPrice, getHistoricalPrice or getAllPrices methods. If signature is invalid - error will be thrown.

const price = await redstone.getPrice("AR", {
  verifySignature: true,
});
console.log(price.value);

Fluent Interface

Redstone implements a fluent interface to simplify query creation thanks to a human readable syntax. Learn more

redstone fluent interface example


Using a custom cache api url

Option 1. Using a setCacheApiUrl method

redstone.setCacheApiUrl("http://localhost:9000/prices");
redstone.getPrice("AR").then(console.log);

Option 2. Initialising a new redstone api instance with a cacheApiUrl param

const redstoneApi = new redstone.Api({
  cacheApiUrl: "http://localhost:9000/prices",
});
redstoneApi.getPrice("AR").then(console.log);

๐Ÿ’ก Note: To use a custom cache api url with the redstone fluent interface you should pass a cacheApiUrl as an argument of the exec method each time you make a query.

redstone.query().symbol("AR").latest().exec({
  cacheApiUrl: "http://localhost:9000/prices",
}).then(console.log);

Get prices from Arweave

By default, Redstone API fetches data from the Redstone cache layer. It works way faster than fetching directly from Arweave Blockchain. Even so, thanks to signature verification prices data is still trusted and secure.

We strongly recommend using the default fetching mechanism which leverages cache to speed up queries. But if you want to fetch data directly from Arweave you can do it by initialising a new Api client and setting useCache option to false.

const redstoneArweaveClient = new redstone.Api({ useCache: false });

const price = await redstoneArweaveClient.getPrice("AR");

console.log(price.value); // AR price value fetched directly from Arweave

๐Ÿš€ Examples

๐Ÿ’ฌ Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

๐Ÿ“œ License

This software is licensed under the MIT ยฉ Redstone

redstone-api's People

Contributors

hatskier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

redstone-api's Issues

Analyze price deviation of the avalanche-prod data service (for Jeremi)

We would like to analyze price deviation of some assets delivered by RedStone HTTP Api. This information is extremely important for RedStone oracles to evaluate risks and prevent price manipulation attacks.

Assets

The full list of assets to analyze is below:

  • "ETH"
  • "USDT"
  • "PNG"
  • "AVAX"
  • "XAVA"
  • "LINK"
  • "BTC"
  • "FRAX"
  • "YAK"
  • "QI"
  • "USDC"
  • "YYAV3SA1"
  • "sAVAX"
  • "SAV2"
  • "TJ_AVAX_USDC_LP"
  • "PNG_AVAX_USDC_LP"
  • "YY_TJ_AVAX_USDC_LP"
  • "MOO_TJ_AVAX_USDC_LP"

HTTP Api

Here is the link to the RedStone HTTP Api documentation: https://api.docs.redstone.finance/http-api/prices
You can use the redstone-avalanche-prod-1 provider.

Time intervals and time range

We would like to analyze historical price of each asset on the max available range (up to 1 year), breaking it on the following time intervals:

  • 5m
  • 10m
  • 30m
  • 1h
  • 1d

Expected result

We would like to get the chart with the historical deviations for each of the assets (and for each interval).
We would also like to calcualte max deviation (in percents) for each of the assets (and for each interval).

You can work on your own repository and you can use any tools and languages. In case of any questions please feel free to email me at [email protected], I'll be happy to help.

Fix signature verification

Previously our signature verification was based on arweave signatures. However, we don't store them in DB anymore. We should switch to evm signature verification.

Improve tests using mocks

Our current tests in redstone-api send real HTTP requests, which makes them dependent on the network and work more like integration tests. We should mock the HTTP requests to just test the redstone-api code (without testing the real api response).

Let's not remove the old tests. We can move them to the __tests__/real folder. The new tests (with mocks) can be put to the __tests__/mock folder.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.