Skip to content

Commit

Permalink
name refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Matlefebvre1234 committed Jun 20, 2024
1 parent c4f1a08 commit 2f234e7
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 185 deletions.
13 changes: 6 additions & 7 deletions clickhouse schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,19 @@ POPULATE
AS SELECT * FROM supply;



CREATE TABLE IF NOT EXISTS transfers (
"id" String,
contract FixedString(40),
`from` String,
`to` String,
value String,
transaction String,
tx_id String,
block_num UInt32(),
timestamp DateTime64(3, 'UTC'),
)
ENGINE = MergeTree PRIMARY KEY ("id")
ORDER BY (id,block_num,timestamp);
ENGINE = MergeTree PRIMARY KEY ("tx_id")
ORDER BY (tx_id,block_num,timestamp);

-- Indexes for block_number
ALTER TABLE transfers ADD INDEX transfers_block_number_index block_num TYPE minmax;

-- MV for contract --
CREATE MATERIALIZED VIEW transfers_contract_historical_mv
Expand All @@ -148,4 +146,5 @@ CREATE MATERIALIZED VIEW transfers_to_historical_mv
ENGINE = MergeTree()
ORDER BY (`to`, contract)
POPULATE
AS SELECT * FROM transfers;
AS SELECT * FROM transfers;

4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ function ERC20TokenAPI() {
createUsageEndpoint("/holders");
createUsageEndpoint("/supply"); // TODO: Same as `balance``
createUsageEndpoint("/transfers"); // TODO: Redefine `block_range` params
createUsageEndpoint("/transfers/{transaction_id}");
createUsageEndpoint("/contract");
createUsageEndpoint("/transfers/{tx_id}");
createUsageEndpoint("/tokens");
app.notFound((ctx: Context) => APIErrorResponse(ctx, 404, "route_not_found", `Path not found: ${ctx.req.method} ${ctx.req.path}`));

return app;
Expand Down
60 changes: 37 additions & 23 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export function getTotalSupply(endpoint: UsageEndpoints, query_param: any, examp
${table}.block_num,
${contractTable}.name as name,
${contractTable}.symbol as symbol,
${contractTable}.decimals as decimals,
${contractTable}.decimals as precision,
toUnixTimestamp(${table}.timestamp)*1000 as timestamp
FROM ${table} `;


// JOIN Contracts table
query += ` LEFT JOIN Contracts ON ${contractTable}.contract = ${table}.contract`;
query += ` LEFT JOIN contracts ON ${contractTable}.contract = ${table}.contract`;
if (!example) {
// WHERE statements
const where = [];
Expand Down Expand Up @@ -85,7 +85,7 @@ export function getTotalSupply(endpoint: UsageEndpoints, query_param: any, examp


export function getContracts(endpoint: UsageEndpoints, query_param: any, example?: boolean) {
if (endpoint === "/contract") {
if (endpoint === "/tokens") {
const q = query_param as ValidUserParams<typeof endpoint>;

// Params
Expand All @@ -104,7 +104,7 @@ export function getContracts(endpoint: UsageEndpoints, query_param: any, example
${table}.contract,
${table}.name,
${table}.symbol,
${table}.decimals,
${table}.decimals as precision,
${table}.block_num,
toDateTime(toUnixTimestamp(${table}.timestamp) * 1000) as timestamp
FROM ${table} `
Expand Down Expand Up @@ -150,27 +150,27 @@ export function getBalanceChanges(endpoint: UsageEndpoints, query_param: any, ex
if (q.account) owner = getAddress(q.account, "account", false)?.toLowerCase();

let table;
const contractTable = 'contracts';
// SQL Query
if (contract) table = 'balance_changes_contract_historical_mv';
else table = "balance_changes_account_historical_mv"

let query = `SELECT
owner as account,
contract,
new_balance AS balance,
toDateTime(toUnixTimestamp(timestamp)*1000) AS timestamp,
block_num `
${table}.owner,
${table}.contract,
${table}.new_balance AS balance,
toDateTime(toUnixTimestamp(${table}.timestamp)*1000) AS timestamp,
${table}.block_num `
query += ` FROM ${table}`


//Join for latest block between block range selected
const blockfilter: any = [];
let blockfilterQuery = "";
addBlockFilter(q, blockfilter);
if (blockfilter.length) blockfilterQuery += ` WHERE(${blockfilter.join(' AND ')})`;
let joinSelectQuery = "";

if (contract) joinSelectQuery = `SELECT owner, MAX(block_num) FROM (SELECT owner, block_num , contract FROM ${table} ${blockfilterQuery})`;
if (contract) joinSelectQuery = `SELECT owner, MAX(block_num) as block_num FROM (SELECT owner, block_num , contract FROM ${table} ${blockfilterQuery})`;
else joinSelectQuery = `SELECT contract, owner, MAX(block_num) as block_num FROM (SELECT owner, block_num , contract FROM ${table} ${blockfilterQuery})`;
const joinWhereQuery: any = [];
//add where filter to joinQuery
Expand All @@ -185,13 +185,12 @@ export function getBalanceChanges(endpoint: UsageEndpoints, query_param: any, ex
if (contract) query += ` JOIN (${joinSelectQuery}) as latest ON ${table}.owner = latest.owner AND ${table}.block_num = latest.block_num`
else query += ` JOIN (${joinSelectQuery}) as latest ON ${table}.owner = latest.owner AND ${table}.block_num = latest.block_num AND ${table}.contract = latest.contract`


if (!example) {
// WHERE statements
const where = [];

// equals
where.push(`account == '${owner}'`)
where.push(`owner == '${owner}'`)
where.push(`balance != '0'`);
if (contract) where.push(`contract == '${contract}'`);

Expand All @@ -208,7 +207,23 @@ export function getBalanceChanges(endpoint: UsageEndpoints, query_param: any, ex
query += ` LIMIT ${limit} `
const offset = q.page;
if (offset) query += ` OFFSET ${offset} `
return query;


// add Join contract

let Allquery = `SELECT
query.owner as account,
query.contract,
query.balance,
${contractTable}.name as name,
${contractTable}.symbol as symbol,
${contractTable}.decimals as precision,
query.timestamp,
query.block_num,
FROM (${query}) as query JOIN ${contractTable} ON query.contract = ${contractTable}.contract`

return Allquery;
}
else {
return ""
Expand Down Expand Up @@ -240,7 +255,7 @@ export function getHolders(endpoint: UsageEndpoints, query_param: any, example?:
addBlockFilter(q, blockfilter);
if (blockfilter.length) blockfilterQuery += ` WHERE(${blockfilter.join(' AND ')})`;

let joinSelectQuery = `SELECT account, MAX(block_num) FROM (SELECT account, block_num ,contract FROM ${table} ${blockfilterQuery})`;
let joinSelectQuery = `SELECT account, MAX(block_num) as block_num FROM (SELECT account, block_num ,contract FROM ${table} ${blockfilterQuery})`;
const joinWhereQuery: any = [];

//add where filter to joinQuery
Expand Down Expand Up @@ -305,7 +320,7 @@ export function getTransfers(endpoint: UsageEndpoints, query_param: any, example
from,
to,
value as amount,
transaction as transaction_id,
tx_id,
block_num,
toDateTime(toUnixTimestamp(timestamp)*1000) as timestamp`

Expand Down Expand Up @@ -348,16 +363,17 @@ export function getTransfers(endpoint: UsageEndpoints, query_param: any, example

export function getTransfer(endpoint: UsageEndpoints, query_param: any, example?: boolean) {


if (endpoint === "/transfers/{transaction_id}") {
console.log("///////////////////////////////////////////////////////", endpoint)
console.log("enpoint", endpoint)
if (endpoint === "/transfers/{tx_id}") {
const q = query_param as ValidUserParams<typeof endpoint>;

let contract;
let from;
let to;

// const chain = searchParams.get("chain");
const transaction_id = formatTxid(q.transaction_id);
const transaction_id = formatTxid(q.tx_id);


// SQL Query
Expand All @@ -368,7 +384,7 @@ export function getTransfer(endpoint: UsageEndpoints, query_param: any, example?
from,
to,
value as amount,
transaction as transaction_id,
tx_id,
block_num,
toDateTime(toUnixTimestamp(timestamp)*1000) as timestamp`

Expand All @@ -379,12 +395,10 @@ export function getTransfer(endpoint: UsageEndpoints, query_param: any, example?
const where = [];

// equals
if (transaction_id) where.push(`transaction == '${transaction_id}'`);
if (transaction_id) where.push(`tx_id == '${transaction_id}'`);

// Join WHERE statements with AND
if (where.length) query += ` WHERE(${where.join(' AND ')})`;
//add ORDER BY and GROUP BY
query += ` ORDER BY timestamp DESC`
}
return query;
}
Expand Down
56 changes: 28 additions & 28 deletions src/types/zod.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export const BalanceChange = z.object({
change_type: z.number(),
block_num: z.number(),
timestamp: z.number(),
transaction_id: z.string(),
tx_id: z.string(),
});

export type Contract = z.infer<typeof Contract>;
export const Contract = z.object({
address: z.string(),
contract: z.string(),
name: z.string(),
symbol: z.string(),
decimals: z.number(),
Expand Down Expand Up @@ -81,21 +81,21 @@ export const ResponseMetadata = z.object({

export type Supply = z.infer<typeof Supply>;
export const Supply = z.object({
address: z.string(),
contract: z.string(),
supply: z.string(),
block_num: z.number(),
timestamp: z.number(),
});

export type Transfer = z.infer<typeof Transfer>;
export const Transfer = z.object({
address: z.string(),
contract: z.string(),
from: z.string(),
to: z.string(),
value: z.string(),
block_num: z.number(),
timestamp: z.number(),
transaction_id: z.string(),
tx_id: z.string(),
});

export type TypeSpec_OpenAPI_Contact = z.infer<typeof TypeSpec_OpenAPI_Contact>;
Expand Down Expand Up @@ -130,25 +130,6 @@ export const get_Usage_balance = {
}),
};

export type get_Usage_contract = typeof get_Usage_contract;
export const get_Usage_contract = {
method: z.literal("GET"),
path: z.literal("/contract"),
parameters: z.object({
query: z.object({
contract: z.string().optional(),
symbol: z.string().optional(),
name: z.string().optional(),
limit: z.number().optional(),
page: z.number().optional(),
}),
}),
response: z.object({
data: TypeSpec_OpenAPI_Contact,
meta: ResponseMetadata,
}),
};

export type get_Usage_head = typeof get_Usage_head;
export const get_Usage_head = {
method: z.literal("GET"),
Expand Down Expand Up @@ -231,6 +212,25 @@ export const get_Usage_supply = {
}),
};

export type get_Usage_tokens = typeof get_Usage_tokens;
export const get_Usage_tokens = {
method: z.literal("GET"),
path: z.literal("/tokens"),
parameters: z.object({
query: z.object({
contract: z.string().optional(),
symbol: z.string().optional(),
name: z.string().optional(),
limit: z.number().optional(),
page: z.number().optional(),
}),
}),
response: z.object({
data: TypeSpec_OpenAPI_Contact,
meta: ResponseMetadata,
}),
};

export type get_Usage_transfers = typeof get_Usage_transfers;
export const get_Usage_transfers = {
method: z.literal("GET"),
Expand All @@ -254,14 +254,14 @@ export const get_Usage_transfers = {
export type get_Usage_transfer = typeof get_Usage_transfer;
export const get_Usage_transfer = {
method: z.literal("GET"),
path: z.literal("/transfers/{transaction_id}"),
path: z.literal("/transfers/{tx_id}"),
parameters: z.object({
query: z.object({
limit: z.number().optional(),
page: z.number().optional(),
}),
path: z.object({
transaction_id: z.string(),
tx_id: z.string(),
}),
}),
response: z.object({
Expand All @@ -282,15 +282,15 @@ export const get_Docs_version = {
export const EndpointByMethod = {
get: {
"/balance": get_Usage_balance,
"/contract": get_Usage_contract,
"/head": get_Usage_head,
"/health": get_Monitoring_health,
"/holders": get_Usage_holders,
"/metrics": get_Monitoring_metrics,
"/openapi": get_Docs_openapi,
"/supply": get_Usage_supply,
"/tokens": get_Usage_tokens,
"/transfers": get_Usage_transfers,
"/transfers/{transaction_id}": get_Usage_transfer,
"/transfers/{tx_id}": get_Usage_transfer,
"/version": get_Docs_version,
},
};
Expand Down
8 changes: 4 additions & 4 deletions src/typespec/models.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace Models {

model CommonERC20<TimestampType> {
...BlockInfo<TimestampType>;
transaction_id: string;
tx_id: string;
}

model Transfer<TimestampType> {
address: string;
contract: string;
from: string;
to: string;
value: string;
Expand All @@ -32,13 +32,13 @@ namespace Models {
}

model Supply<TimestampType> {
address: string;
contract: string;
supply: string;
...BlockInfo<TimestampType>;
}

model Contract<TimestampType>{
address: string;
contract: string;
name: string;
symbol: string;
decimals: int64;
Expand Down
Loading

0 comments on commit 2f234e7

Please sign in to comment.