Skip to content

Commit

Permalink
v1.0.0 (#19)
Browse files Browse the repository at this point in the history
* Rename variables for consistency and interoperability

- `blocknum` -> `block_number`
- `blockchain` -> `chain`

* 1.0.0
  • Loading branch information
0237h authored Oct 17, 2023
1 parent 43f8bcd commit 275b983
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bun-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
26 changes: 13 additions & 13 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,57 +42,57 @@ function parseBlockTimeQueryResponse(json: JSONObjectEachRow): BlocktimeQueryRes
[key: string]: Array<string>
}) => {
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<BlocktimeQueryResponsesSchema> {
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<BlocktimeQueryResponsesSchema> {
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<BlocktimeQueryResponsesSchema> {
export async function blocknumQuery(chain: string, timestamp: Date | Date[]): Promise<BlocktimeQueryResponsesSchema> {
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);

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,
});
*/
return { todo: 'Not Implemented', data: [[null]] };
}

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
}
2 changes: 1 addition & 1 deletion src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() }),
Expand Down

0 comments on commit 275b983

Please sign in to comment.