Skip to content

Commit

Permalink
refactoring for new block table clickhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Lefebvre committed Oct 19, 2023
1 parent 6efa7e8 commit a48b27e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_HOST=http://127.0.0.1:8123
DB_NAME=clickhouse_sink
DB_NAME=default
DB_USERNAME=default
DB_PASSWORD=
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 24 additions & 19 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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`;
}
Expand All @@ -103,25 +105,26 @@ 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;
`;
}
//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);
Expand All @@ -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`;
}

Expand Down
12 changes: 10 additions & 2 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const ContractResponseSchema = z.object({
example: '6',
})
,
chain: z.string().openapi({
example: 'eth',
})
});
export type ContractResponseSchema = z.infer<typeof ContractResponseSchema>;

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<typeof BalanceResponseSchema>;

0 comments on commit a48b27e

Please sign in to comment.