@acryl/data-service-client-js 中文文档教程

发布于 5年前 浏览 11 更新于 3年前

JS Library for Acryl data services

Usage

const DataServiceClient = require('@acryl/data-service-client-js').default;

// Initialization
const client = new DataServiceClient({
  rootUrl: 'http://api.acrylplatform.com/v0',
  fetch: req => window.fetch(req).then(res => res.text()), // fetch must return string
  parse: str => JSON.parse(str),
});

// Fetching
(async () => {
  const { data } = await client.getAssets('ACRYL'); // data: Asset {}
})();

Methods

Response format

{ data, ...meta }

All methods do GET requests, until query is under 2k symbols, then automatically switch to POST

  • getAssets:
await client.getAssets('ACRYL'); // One { data: Asset }
await client.getAssets('ACRYL', '68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2'); // Or many { data: Asset[] }
await client.getAssetsByTicker('ACRYL');  // Many { data: Asset[] }
await client.getAssetsByTicker('*');  // Many { data: Asset[] } - all assets
  • getPairs
const getPairs = client.getPairs('MATCHER_ID'); // set up DEX matcher
await getPairs([
  'ACRYL/68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2',
  'ACRYL/4vN61TPpk5qKidGt5RYgd1To4nzxsoDyAL5buBk37hwF'
]); // get pairs
  • getExchangeTxs:
await client.getExchangeTxs('8rEwYY4wQ4bkEkk95EiyeQnvnonX6TAnU6eiBAbVSADk'); // By id { data: Tx }
await client.getExchangeTxs({
  timeStart?: string | Date | number;
  timeEnd?: string | Date | number;
  matcher?: string;
  sender?: string;
  amountAsset?: string | Asset;
  priceAsset?: string | Asset;
  limit?: number;
  sort?: 'asc'|'desc';
}); // With filters { data: Tx[] }
await client.getExchangeTxs(); // Top 100 with default filters (timeStart = timestamp of first entry in db, timeEnd = now)
  • aliases:
const alias1 = await client.aliases.getById('@askerych');
/*
  { data: {
      address: '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M',
      alias: '@askerych'
    }
  }
  */
const alias2 = await client.aliases.getByAddress(
  '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M'
);
/*
  { data: [{
      address: '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M',
      alias: '@askerych'
    }]
  }
  */
  • getCandles:
await client.getCandles(amountAsset: string, priceAsset: string, {
  timeStart: string | Date | number;
  timeEnd?: string | Date | number;
  interval: string; // interval pattern is xy, where x - number, y - one of m (minutes), h (hours), d (days), M (Month)
});
await client.getCandles('ACRYL', '68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2', {
  timeStart: '2018-12-01',
  timeEnd: '2018-12-31',
  interval: '1d'
});

Pagination

type Response<T> = {
  data: T;
  fetchMore?: TFunction;
};
const response1 = await client.getExchangeTxs({ limit: 1, sort: 'asc' });
const data1 = response1.data;
// Process data

const response2 = await res1.fetchMore(2); // 2 more

Custom init params

您可以为库设置自定义获取器、解析器和转换器。

type TLibOptions = {
  rootUrl: string;
  fetch?: TFunction;
  parse?: TParser;
  transform?: TFunction;
};

路径是 fetch -> 解析 -> 转换

Fetch must return string!

获取接受(url,选项) 默认获取是 fetch(...).then(res => res.text())

默认解析是 JSON.parse.bind(JSON) ,如果你想添加对大数字的支持,你可以使用 json-bigint 的解析。

默认情况下,转换函数具有下一个签名(这取决于您的解析函数):

({
  __type,
  data,
  ...rest
}) => any

基本上您可以打开 __type 并进行转换。

JS Library for Acryl data services

Usage

const DataServiceClient = require('@acryl/data-service-client-js').default;

// Initialization
const client = new DataServiceClient({
  rootUrl: 'http://api.acrylplatform.com/v0',
  fetch: req => window.fetch(req).then(res => res.text()), // fetch must return string
  parse: str => JSON.parse(str),
});

// Fetching
(async () => {
  const { data } = await client.getAssets('ACRYL'); // data: Asset {}
})();

Methods

Response format

{ data, ...meta }

All methods do GET requests, until query is under 2k symbols, then automatically switch to POST

  • getAssets:
await client.getAssets('ACRYL'); // One { data: Asset }
await client.getAssets('ACRYL', '68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2'); // Or many { data: Asset[] }
await client.getAssetsByTicker('ACRYL');  // Many { data: Asset[] }
await client.getAssetsByTicker('*');  // Many { data: Asset[] } - all assets
  • getPairs
const getPairs = client.getPairs('MATCHER_ID'); // set up DEX matcher
await getPairs([
  'ACRYL/68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2',
  'ACRYL/4vN61TPpk5qKidGt5RYgd1To4nzxsoDyAL5buBk37hwF'
]); // get pairs
  • getExchangeTxs:
await client.getExchangeTxs('8rEwYY4wQ4bkEkk95EiyeQnvnonX6TAnU6eiBAbVSADk'); // By id { data: Tx }
await client.getExchangeTxs({
  timeStart?: string | Date | number;
  timeEnd?: string | Date | number;
  matcher?: string;
  sender?: string;
  amountAsset?: string | Asset;
  priceAsset?: string | Asset;
  limit?: number;
  sort?: 'asc'|'desc';
}); // With filters { data: Tx[] }
await client.getExchangeTxs(); // Top 100 with default filters (timeStart = timestamp of first entry in db, timeEnd = now)
  • aliases:
const alias1 = await client.aliases.getById('@askerych');
/*
  { data: {
      address: '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M',
      alias: '@askerych'
    }
  }
  */
const alias2 = await client.aliases.getByAddress(
  '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M'
);
/*
  { data: [{
      address: '3P5uMgn1xvrm7g3sbUVAGLtetkNUa1AHn2M',
      alias: '@askerych'
    }]
  }
  */
  • getCandles:
await client.getCandles(amountAsset: string, priceAsset: string, {
  timeStart: string | Date | number;
  timeEnd?: string | Date | number;
  interval: string; // interval pattern is xy, where x - number, y - one of m (minutes), h (hours), d (days), M (Month)
});
await client.getCandles('ACRYL', '68X83bfvSSQauL81fcfqA3Vne9s4y2BRAG36DGRcv4V2', {
  timeStart: '2018-12-01',
  timeEnd: '2018-12-31',
  interval: '1d'
});

Pagination

type Response<T> = {
  data: T;
  fetchMore?: TFunction;
};
const response1 = await client.getExchangeTxs({ limit: 1, sort: 'asc' });
const data1 = response1.data;
// Process data

const response2 = await res1.fetchMore(2); // 2 more

Custom init params

You can set custom fetcher, parser and transformer for library.

type TLibOptions = {
  rootUrl: string;
  fetch?: TFunction;
  parse?: TParser;
  transform?: TFunction;
};

The path is fetch -> parse -> transform

Fetch must return string!

Fetch accepts (url, options) Fetch by default is fetch(...).then(res => res.text())

Parse by default is JSON.parse.bind(JSON) , you can use json-bigint's parse if you want to add support for bignumbers.

Transform function has next signature by default (it depends on your parse function):

({
  __type,
  data,
  ...rest
}) => any

Basically you can switch on __type and do transformations.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文