From 275b983930804a0009fc9a42c9dc1882788af5f9 Mon Sep 17 00:00:00 2001 From: Etienne Donneger <23462475+Krow10@users.noreply.github.com> Date: Tue, 17 Oct 2023 18:25:36 -0400 Subject: [PATCH] v1.0.0 (#19) * Rename variables for consistency and interoperability - `blocknum` -> `block_number` - `blockchain` -> `chain` * 1.0.0 --- .github/workflows/bun-test.yml | 2 +- package.json | 2 +- src/queries.ts | 26 +++++++++++++------------- src/schemas.ts | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/bun-test.yml b/.github/workflows/bun-test.yml index 82923ce..8395b70 100644 --- a/.github/workflows/bun-test.yml +++ b/.github/workflows/bun-test.yml @@ -27,7 +27,7 @@ jobs: - name: 'Insert mock data into Clickhouse DB for testing' run: | curl https://clickhouse.com/ | sh - echo "CREATE TABLE demo ( id UUID, timestamp DateTime, blocknum UInt64, blockchain Enum('EOS' = 0, 'ETH' = 1, 'WAX' = 2, 'UX' = 3) ) ENGINE = MergeTree ORDER BY tuple(id, blockchain); INSERT INTO demo (*) VALUES ('660e46fa-229b-4f96-8000-3a95580b73f5', '2020-06-21 05:06:23', '751069', 'ETH'), ('ed1528be-e0b7-4f6e-8014-32cda1118f04', '2022-07-12 09:07:23', '525619', 'WAX'), ('05c5e3f0-61b1-4d3e-8030-0eb13ea3cc73', '2021-03-27 05:03:17', '187507', 'WAX'), ('130cee83-0e51-41de-8032-4e491053b488', '2021-03-02 06:03:31', '821014', 'UX'), ('09e76a45-2a71-4cdd-8032-e6cc98a05e26', '2020-08-29 08:08:54', '617393', 'ETH');" > setup.sql + echo "CREATE TABLE demo ( id UUID, timestamp DateTime, block_number UInt64, chain Enum('EOS' = 0, 'ETH' = 1, 'WAX' = 2, 'UX' = 3) ) ENGINE = MergeTree ORDER BY tuple(id, chain); INSERT INTO demo (*) VALUES ('660e46fa-229b-4f96-8000-3a95580b73f5', '2020-06-21 05:06:23', '751069', 'ETH'), ('ed1528be-e0b7-4f6e-8014-32cda1118f04', '2022-07-12 09:07:23', '525619', 'WAX'), ('05c5e3f0-61b1-4d3e-8030-0eb13ea3cc73', '2021-03-27 05:03:17', '187507', 'WAX'), ('130cee83-0e51-41de-8032-4e491053b488', '2021-03-02 06:03:31', '821014', 'UX'), ('09e76a45-2a71-4cdd-8032-e6cc98a05e26', '2020-08-29 08:08:54', '617393', 'ETH');" > setup.sql ./clickhouse client --queries-file ./setup.sql - name: 'Run test' diff --git a/package.json b/package.json index 9745304..933e9f9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "substreams-clock-api", "description": "Timestamps <> Block numbers conversion for your favorite chains", - "version": "0.2.0", + "version": "1.0.0", "homepage": "https://github.com/pinax-network/substreams-clock-api", "license": "MIT", "authors": [ diff --git a/src/queries.ts b/src/queries.ts index 71131f1..1a0eeb2 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -42,23 +42,23 @@ function parseBlockTimeQueryResponse(json: JSONObjectEachRow): BlocktimeQueryRes [key: string]: Array }) => { return BlocktimeQueryResponseSchema.parse({ - blockchain: Object.values(r)[0][0], + chain: Object.values(r)[0][0], block_number: Object.values(r)[0][1], timestamp: Object.values(r)[0][2] }); })); } -export async function timestampQuery(blockchain: string, blocknum: number | number[]): Promise { - const query = `SELECT (blockchain, blocknum, timestamp) FROM ${config.name} WHERE (blockchain == '${blockchain}') AND (blocknum IN (${blocknum.toString()}))`; +export async function timestampQuery(chain: string, block_number: number | number[]): Promise { + const query = `SELECT (chain, block_number, timestamp) FROM ${config.name} WHERE (chain == '${chain}') AND (block_number IN (${block_number.toString()}))`; const json = await makeQuery(query); return parseBlockTimeQueryResponse(json); } -export async function blocknumQuery(blockchain: string, timestamp: Date | Date[]): Promise { +export async function blocknumQuery(chain: string, timestamp: Date | Date[]): Promise { timestamp = Array.isArray(timestamp) ? timestamp : [timestamp]; - const query = `SELECT (blockchain, blocknum, timestamp) FROM ${config.name} WHERE (blockchain == '${blockchain}') AND (timestamp IN (${ + const query = `SELECT (chain, block_number, timestamp) FROM ${config.name} WHERE (chain == '${chain}') AND (timestamp IN (${ timestamp.map((t) => '\'' + t.toISOString().replace('T', ' ').substring(0, 19) + '\'').toString() // Format dates to find them in DB (mock data) }))`; // TODO: Find closest instead of matching timestamp or another route ? const json = await makeQuery(query); @@ -66,22 +66,22 @@ export async function blocknumQuery(blockchain: string, timestamp: Date | Date[] return parseBlockTimeQueryResponse(json) } -export async function currentBlocknumQuery(blockchain: string) { - const query = `SELECT MAX(blocknum) AS current FROM ${config.name} GROUP BY blockchain HAVING (blockchain == '${blockchain}')`; +export async function currentBlocknumQuery(chain: string) { + const query = `SELECT MAX(block_number) AS current FROM ${config.name} GROUP BY chain HAVING (chain == '${chain}')`; const json = await makeQuery(query); return SingleBlocknumQueryResponseSchema.parse({ - chain: blockchain, + chain, block_number: Object.values(json as JSONObjectEachRow)[0].current, }); } -export async function finalBlocknumQuery(blockchain: string) { - /*const query = `SELECT MAX(blocknum) as final FROM ${config.name} GROUP BY blockchain HAVING (blockchain == '${blockchain}')`; +export async function finalBlocknumQuery(chain: string) { + /*const query = `SELECT MAX(block_number) as final FROM ${config.name} GROUP BY chain HAVING (chain == '${chain}')`; const json = await makeQuery(query); return SingleBlocknumQueryResponseSchema.parse({ - chain: blockchain, + chain block_number: Object.values(json as JSONObjectEachRow)[0].final, }); */ @@ -89,10 +89,10 @@ export async function finalBlocknumQuery(blockchain: string) { } export async function supportedChainsQuery() { - const query = `SELECT DISTINCT blockchain FROM ${config.name}`; + const query = `SELECT DISTINCT chain FROM ${config.name}`; // Required format for returning a const value in order to make z.enum() work in the schema definitions const json = await makeQuery(query, 'JSONColumns'); - return json.blockchain + return json.chain } diff --git a/src/schemas.ts b/src/schemas.ts index 0ee8f20..a453869 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -87,7 +87,7 @@ export const TimestampSchema = z.object({ // Represents a block number <> timestamp conversion output for `/{c}/timestamp` and `/{c}/blocknum` endpoints // It can either be a single output for each field or an array of outputs export const BlocktimeQueryResponseSchema = z.object({ - blockchain: z.enum(supportedChains).openapi({ example: 'EOS' }), + chain: z.enum(supportedChains).openapi({ example: 'EOS' }), block_number: z_blocknum.openapi({ example: 1337 }), timestamp: z.union([ z_timestamp.openapi({ example: new Date().toISOString() }),