From a48b27e55d39f888e3520573b51d8841d40bfa8a Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Thu, 19 Oct 2023 12:41:32 -0400 Subject: [PATCH] refactoring for new block table clickhouse --- .env | 2 +- src/config.ts | 2 +- src/queries.ts | 43 ++++++++++++++++++++++++------------------- src/schemas.ts | 12 ++++++++++-- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/.env b/.env index 5ea1c32..186a8c4 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ DB_HOST=http://127.0.0.1:8123 -DB_NAME=clickhouse_sink +DB_NAME=default DB_USERNAME=default DB_PASSWORD= diff --git a/src/config.ts b/src/config.ts index ff180e5..ccfdf24 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,7 @@ import pkg from "../package.json"; export const DEFAULT_PORT = "8080"; export const DEFAULT_HOSTNAME = "localhost"; export const DEFAULT_DB_HOST = "http://localhost:8123"; -export const DEFAULT_DB_NAME = "clickhouse_sink"; +export const DEFAULT_DB_NAME = "default"; export const DEFAULT_DB_USERNAME = "default"; export const DEFAULT_DB_PASSWORD = ""; export const DEFAULT_MAX_ELEMENTS_QUERIES = 10; diff --git a/src/queries.ts b/src/queries.ts index 41d0948..87b31bb 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -27,13 +27,14 @@ export async function getTotalSupply( if (ethers.isAddress(address)) { let sqlquery: string = ""; if (block) { - sqlquery = `SELECT * + sqlquery = `SELECT address,supply, block_number AS block,chain FROM TotalSupply - WHERE address = '${address}' AND CAST(block AS INT) >= ${block} - ORDER BY block + JOIN block ON block.block_id = TotalSupply.block_id + WHERE address = '${address}' AND block_number >= ${block} + ORDER BY block_number LIMIT 1`; } else { - sqlquery = `SELECT * FROM TotalSupply WHERE address = '${address}' ORDER BY block DESC LIMIT 1`; + sqlquery = `SELECT address,supply, chain FROM TotalSupply JOIN block ON block.block_id = TotalSupply.block_id WHERE address = '${address}' ORDER BY block_number DESC LIMIT 1`; } const resultSet = await client.query({ query: sqlquery, @@ -54,7 +55,7 @@ export async function getContract(address: string | undefined) { if (address) { address = formatAddress(address); if (ethers.isAddress(address)) { - let sqlquery: string = `SELECT * FROM Contracts WHERE address = '${address}'`; + let sqlquery: string = `SELECT address,name,symbol,decimals,chain FROM Contracts WHERE address = '${address}'`; const resultSet = await client.query({ query: sqlquery, format: "JSONEachRow", @@ -87,12 +88,13 @@ export async function getBalance( SELECT contract, new_balance AS balance, - block_num, - ROW_NUMBER() OVER (PARTITION BY contract ORDER BY block_num DESC) AS rn + chain, + ROW_NUMBER() OVER (PARTITION BY contract ORDER BY block_number DESC) AS rn FROM balance_changes + JOIN block ON block.block_id = balance_changes.block_id WHERE owner = '${wallet}' ) - SELECT contract, balance, block_num + SELECT contract, balance,chain FROM RankedBalances WHERE rn = 1`; } @@ -103,12 +105,14 @@ export async function getBalance( SELECT contract, new_balance AS balance, - block_num, - ROW_NUMBER() OVER (PARTITION BY contract ORDER BY block_num) AS rn + block_number, + chain, + ROW_NUMBER() OVER (PARTITION BY contract ORDER BY block_number) AS rn FROM balance_changes - WHERE owner = '${wallet}' AND CAST(block_num AS INT) >= ${block} + JOIN block ON block.block_id = balance_changes.block_id + WHERE owner = '${wallet}' AND block_number >= ${block} ) - SELECT contract, balance, block_num + SELECT contract, balance, block_number,chain FROM RankedBalances WHERE rn = 1; `; @@ -116,12 +120,11 @@ export async function getBalance( //GET balance of a specific contract for a wallet LATEST BLOCK else if (!block && address) { if (ethers.isAddress(address)) { - sqlquery = `SELECT contract, - new_balance AS balance, - block_num, + sqlquery = `SELECT contract, new_balance AS balance,chain FROM balance_changes + JOIN block ON block.block_id = balance_changes.block_id WHERE owner = '${wallet}' AND contract = '${address}' - ORDER BY block_num DESC + ORDER BY block_number DESC LIMIT 1`; console.log(sqlquery); @@ -132,10 +135,12 @@ export async function getBalance( } else if (block && address) { sqlquery = `SELECT contract, new_balance AS balance, - block_num, + block_number AS block, + chain FROM balance_changes - WHERE owner = '${wallet}' AND contract = '${address}' AND CAST(block_num AS INT) >= ${block} - ORDER BY block_num + JOIN block ON block.block_id = balance_changes.block_id + WHERE owner = '${wallet}' AND contract = '${address}' AND block_number >= ${block} + ORDER BY block_number LIMIT 1`; } diff --git a/src/schemas.ts b/src/schemas.ts index 0ce1122..cc5118e 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -36,6 +36,9 @@ export const ContractResponseSchema = z.object({ example: '6', }) , + chain: z.string().openapi({ + example: 'eth', + }) }); export type ContractResponseSchema = z.infer; @@ -84,9 +87,9 @@ export const SupplyResponseSchema = z.object({ example: 1000000, }), - timestamp: z.string().or(z.number()) + chain: z.string() .openapi({ - example: '1697483144', + example: 'eth', }) , contract: ContractResponseSchema.optional() @@ -138,6 +141,11 @@ export const BalanceResponseSchema = z.object({ block: z.number().or(z.string()).openapi({ example: 1009707, }), + chain: z.string() + .openapi({ + example: 'eth', + }) + , }); export type BalanceResponseSchema = z.infer;