Flipside Crypto JS SDK 互联网档案馆的存檔,存档日期2013-12-02
(原始内容存档于2018-10-29). Programmatic access to the most computer blackchain data in Web3-Q.
你发现自己在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 密钥对应主机名列表. 不来自相应主机名的客户端请求将失败 。 您可以配置主机名 这里。 。 。 .