@flipsidecrypto/sdk

中文文档
2.1.0发布于 2024/03/08MITTypeScriptCommonJS近 30 天下载 1.1万

官方翻页 Crypto SDK

中文使用文档
文档目录11 节
  1. · 安装 SDK
  2. 开始
  3. 细节
  4. 这个 Query 对象
  5. 这个 QueryResultSet 对象
  6. 倾斜
  7. 排序结果
  8. 过滤结果
  9. 了解MaxAgeMinutes(和缓存结果)
  10. 理解查询秒
  11. 客户端请求要求

Flipside Crypto JS SDK 互联网档案馆的存檔,存档日期2013-12-02

(原始内容存档于2018-10-29). Programmatic access to the most computer blackchain data in Web3-Q.

tests

你发现自己在Flipside Crypto JS/typescript sdk.

· 安装 SDK

yarn add @flipsidecrypto/sdk

或者使用 npm 时

npm install @flipsidecrypto/sdk

开始

import { Flipside, Query, QueryResultSet } from "@flipsidecrypto/sdk";

// Initialize `Flipside` with your API key
const flipside = new Flipside(
  "<YOUR_API_KEY>",
  "https://api-v2.flipsidecrypto.xyz"
);

// Parameters can be passed into SQL statements via simple & native string interpolation
const myAddress = "0x....";

// Create a query object for the `query.run` function to execute
const query: Query = {
  sql: `select nft_address, mint_price_eth, mint_price_usd from flipside_prod_db.ethereum_core.ez_nft_mints where nft_to_address = LOWER('${myAddress}')`,
  maxAgeMinutes: 30,
};

// Send the `Query` to Flipside's query engine and await the results
const result: QueryResultSet = await flipside.query.run(query);

// Iterate over the results
result.records.map((record) => {
  const nftAddress = record.nft_address
  const mintPriceEth = record.mint_price_eth
  const mintPriceUSD = = record.mint_price_usd
  console.log(`address ${nftAddress} minted at a price of ${mintPrice} ETH or $${mintPriceUSD} USD`);
});

细节

这个 Query 对象

这个 Query 对象包含您可以发送到查询引擎执行的 sql 和配置.

type Query = {
  // SQL query to execute
  sql: string;

  // The number of minutes you are willing to accept cached
  // result up to. If set to 30, if cached results exist within
  // the last 30 minutes the api will return them.
  maxAgeMinutes?: number;

  // An override on the query result cahce.
  // A value of false will re-execute the query and override
  // maxAgeMinutes
  cached?: boolean;

  // The number of minutes until your query run times out
  timeoutMinutes?: number;

  // The number of records to return, defaults to 100000
  pageSize?: number;

  // The page number to return, defaults to 1
  pageNumber?: number;

  // The owner of the data source (defaults to 'flipside')
  dataProvider?: string;

  // The data source to execute the query against (defaults to 'snowflake-default')
  dataSource?: string;
};

让我们创建一个查询,以获取所有 NFT 刻制的地址:

const yourAddress = "<your_ethereum_address>";

const query: Query = {
  sql: `select nft_address, mint_price_eth, mint_price_usd from flipside_prod_db.ethereum_core.ez_nft_mints where nft_to_address = LOWER('${myAddress}')`,
  maxAgeMinutes: 5,
  cached: true,
  timeoutMinutes: 15,
  pageNumber: 1,
  pageSize: 10,
};

现在让我们执行查询 并检索结果.

const result: QueryResultSet = await flipside.query.run(query);

考虑到 ttlMinutes 参数.

这个 QueryResultSet 对象

执行查询后,结果将存储在 QueryResultSet 对象.

interface QueryResultSet {
  // The server id of the query
  queryId: string | null;

  // The status of the query (`PENDING`, `FINISHED`, `ERROR`)
  status: QueryStatus | null;

  // The names of the columns in the result set
  columns: string[] | null;

  // The type of the columns in the result set
  columnTypes: string[] | null;

  // The results of the query
  rows: any[] | null;

  // Summary stats on the query run (i.e. the number of rows returned, the elapsed time, etc)
  runStats: QueryRunStats | null;

  // The results of the query transformed as an array of objects
  records: QueryResultRecord[] | null;

  // The page of results
  page: PageStats | null;

  // If the query failed, this will contain the error
  error:
    | ApiError
    | QueryRunRateLimitError
    | QueryRunTimeoutError
    | QueryRunExecutionError
    | ServerError
    | UserError
    | UnexpectedSDKError
    | null;
}

让我们看看上面的询问结果.

我们的查询选中 nft_address, (中文(简体) ), mint_price_eth,以及 mint_price_usd我们可以通过 records 参数。 我们查询中的列名称被指定为每个记录对象的密钥 .

result.records.map((record) => {
  const nftAddress = record.nft_address;
  const mintPriceEth = record.mint_price_eth;
  const mintPriceUSD = record.mint_price_usd;
  console.log(
    `address ${nftAddress} minted at a price of ${mintPriceEth} ETH or $${mintPriceUSD} USD`
  );
});

倾斜

要浏览结果,请使用 getQueryResults 方法.

// what page are we starting on?
let currentPageNumber = 1

// How many records do we want to return in the page?
let pageSize = 1000

// set total pages to 1 higher than the `currentPageNumber` until
// we receive the total pages from `getQueryResults` given the 
// provided `pageSize` (totalPages is dynamically determined by the API 
// based on the `pageSize` you provide)
let totalPages = 2

// we'll store all the page results in `allRows`
let allRows = []

while (currentPageNumber <= totalPages) {
  results = await flipside.query.getQueryResults({
    queryRunId: result.queryId,
    pageNumber: currentPageNumber,
    pageSize: pageSize
  })
  totalPages = results.page.totalPages
  allRows = [...allRows, ...results.records]
  currentPageNumber += 1
}

排序结果

让我们得到结果 按下降顺序排序 mint_price_usd。 。 。 .

results = await flipside.query.getQueryResults({
  queryRunId: result.queryId,
  pageNumber: 1,
  pageSize: 1000,
  sortBy: [
    {
      column: 'mint_price_usd',
      direction: 'desc'
    }
  ]
})

有效指示包括: desc 财务报告和审定财务报表 asc。也可以按多列排序。 您提供排序对象的顺序决定了哪个排序对象优先.

以下示例将首先按降序排序: mint_price_usd 然后按上升顺序排列 nft_address。 。 。 .

results = await flipside.query.getQueryResults({
  queryRunId: result.queryId,
  pageNumber: 1,
  pageSize: 1000,
  sortBy: [
    {
      column: 'mint_price_usd',
      direction: 'desc'
    },
    {
      column: 'nft_address',
      direction: 'asc'
    }
  ]
})

这里的参考文献是 SortBy 类型 :

interface SortBy {
  column: string;
  direction: "desc" | "asc";
}

过滤结果

现在,让我们过滤结果在哪里 mint_price_usd 大于 10

results = await flipside.query.getQueryResults({
  queryRunId: result.queryId,
  pageNumber: 1,
  pageSize: 1000,
  filters: [
    {
      gt: 10,
      column: 'mint_price_usd'
    }
  ]
})

滤镜可以用于:等同,不等同,大于或等同于,小于,小于或等同于,同入,不入. 所有过滤器都会在整个结果集上执行服务器侧.

以下是过滤器类型 :

interface Filter {
  column: string;
  eq?: string | number | null;
  neq?: string | number | null;
  gt?: number | null;
  gte?: number | null;
  lt?: number | null;
  lte?: number | null;
  like?: string | number | null;
  in?: any[] | null;
  notIn?: any[] | null;
}

了解MaxAgeMinutes(和缓存结果)

参数 maxAgeMinutes 可用于控制查询会重新执行还是返回缓存结果。 举个例子吧.

设定 maxAgeMinutes 改为30:

const query: Query = {
  sql: `select nft_address, mint_price_eth, mint_price_usd from flipside_prod_db.ethereum_core.ez_nft_mints where nft_to_address = LOWER('${myAddress}')`,
  maxAgeMinutes: 30
};

在幕后, Flipside API 会将 sql 文本展开并使用该散列来确定是否存在在过去30分钟内记录的结果 。 如果没有结果,或者存在的结果已经超过30分钟,查询会重新执行.

如果您想要强制缓存抓取并重新执行查询 。 你有两个选择,要么设定 maxAgeMinutes 改为0或通过 cache=false设置 cache 以有效设置虚假 maxAgeMinutes 改为 0.

const query: Query = {
  sql: `select nft_address, mint_price_eth, mint_price_usd from flipside_prod_db.ethereum_core.ez_nft_mints where nft_to_address = LOWER('${myAddress}')`,
  maxAgeMinutes: 0
};

// or:
const query: Query = {
  sql: `select nft_address, mint_price_eth, mint_price_usd from flipside_prod_db.ethereum_core.ez_nft_mints where nft_to_address = LOWER('${myAddress}')`,
  maxAgeMinutes: 30,
  cache: false
};

理解查询秒

您可以通过查看 runStats 对象 QueryResultSet。 。 。 .

const runStats = result.runStats

有一些数据返回:

type QueryRunStats = {
  startedAt: Date;
  endedAt: Date;
  elapsedSeconds: number;
  queryExecStartedAt: Date;
  queryExecEndedAt: Date;
  streamingStartedAt: Date;
  streamingEndedAt: Date;
  queuedSeconds: number;
  streamingSeconds: number;
  queryExecSeconds: number;
  bytes: number; // the number of bytes returned by the query
  recordCount: number;
};

您的账户仅借入 queryExecSeconds。这是您针对 Flipside 数据仓库执行的查询的计算秒数.

const execSeconds = runStats.queryExecSeconds

您只有在查询执行时才会被借记 。 所以,如果你设置 maxAgeMinutes 至大于 0 的值,且查询不会重新执行,那么您将只被收取执行的时间.

Flipside 对返回的字节/ 记录数不收费 .

客户端请求要求

所有 API 密钥对应主机名列表. 不来自相应主机名的客户端请求将失败 。 您可以配置主机名 这里。 。 。 .

版本与发布记录

@flipsidecrypto/sdk 共发布 7 个版本,首次发布于 2022/05/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2024/03/082 年前
2.1.0-beta.0beta2023/05/303 年前
2.0.02023/05/223 年前
1.1.12022/11/014 年前
1.1.02022/07/124 年前
1.0.02022/06/294 年前

关于 @flipsidecrypto/sdk 的常见问题

@flipsidecrypto/sdk 是什么?

官方翻页 Crypto SDK

如何安装 @flipsidecrypto/sdk?

在项目目录运行 npm install @flipsidecrypto/sdk,或使用 pnpm add / yarn add / bun add。

@flipsidecrypto/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flipsidecrypto/sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@flipsidecrypto/sdk 可以商用吗?

@flipsidecrypto/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flipsidecrypto/sdk 的下载量有多少?

根据 npm 官方下载统计,@flipsidecrypto/sdk 在 2026/10/15 – 2026/11/13 这 30 天内被下载 10,518 次,日均约 351 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flipsidecrypto/sdk 还在维护吗?

最新版本 2.1.0 发布于 2024/03/08(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。