Skip to content

Commit

Permalink
Revert "Merge commit '1c2225c6d001e78dffddd03093bd83d760773385' into …
Browse files Browse the repository at this point in the history
…main"

This reverts commit 11c2194, reversing
changes made to 94575c8.
  • Loading branch information
Matlefebvre1234 committed Jul 29, 2024
1 parent 11c2194 commit d3740dc
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 578 deletions.
40 changes: 15 additions & 25 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import client from './src/clickhouse/client.js';
import openapi from "./tsp-output/@typespec/openapi3/openapi.json";

import { Hono } from "hono";
import { z } from "zod";
import { paths } from './src/types/zod.gen.js';
import { ZodBigInt, ZodBoolean, ZodDate, ZodDefault, ZodNumber, ZodOptional, ZodTypeAny, ZodUndefined, ZodUnion, z } from "zod";
import { EndpointByMethod } from './src/types/zod.gen.js';
import { APP_VERSION } from "./src/config.js";
import { logger } from './src/logger.js';
import * as prometheus from './src/prometheus.js';
import { makeUsageQuery } from "./src/usage.js";
import { APIErrorResponse } from "./src/utils.js";

import { fixEndpointParametersCoercion } from "./src/types/api.js";

import type { Context } from "hono";
import type { EndpointParameters, EndpointReturnTypes, UsageEndpoints, ValidQueryParams } from "./src/types/api.js";
import type { EndpointParameters, EndpointReturnTypes, UsageEndpoints } from "./src/types/api.js";

function ERC20TokenAPI() {
const app = new Hono();
Expand Down Expand Up @@ -63,41 +63,31 @@ function ERC20TokenAPI() {
async (_) => new Response(await prometheus.registry.metrics(), { headers: { "Content-Type": prometheus.registry.contentType } })
);

// Call once
fixEndpointParametersCoercion();

const createUsageEndpoint = (endpoint: UsageEndpoints) => app.get(
// Hono using different syntax than OpenAPI for path parameters
// `/{path_param}` (OpenAPI) VS `/:path_param` (Hono)
endpoint.replace(/{([^}]+)}/g, ":$1"),
async (ctx: Context) => {
const result = EndpointByMethod["get"][endpoint].parameters.safeParse({
query: ctx.req.query(),
path: ctx.req.param()
}) as z.SafeParseSuccess<EndpointParameters<typeof endpoint>>;

let resultQuery;
let resultPath;

console.log(ctx.req.param())
if (paths[endpoint]["get"]["parameters"]["path"] != undefined) {

resultPath = paths[endpoint]["get"]["parameters"]["path"].safeParse(ctx.req.param()) as z.SafeParseSuccess<EndpointParameters<typeof endpoint>["path"]>;
}

if (paths[endpoint]["get"]["parameters"]["query"] != undefined) {

resultQuery = paths[endpoint]["get"]["parameters"]["query"].safeParse(ctx.req.query()) as z.SafeParseSuccess<EndpointParameters<typeof endpoint>["query"]>;
}


if ((resultPath == undefined || resultPath.success) && (resultQuery == undefined || resultQuery.success)) {
console.log("Success")
if (result.success) {
return makeUsageQuery(
ctx,
endpoint,
{
...resultQuery?.data,
...result.data.query,
// Path parameters may not always be present
...resultPath?.data
} as ValidQueryParams
...("path" in result.data ? result.data.path : {})
}
);
} else {
return APIErrorResponse(ctx, 400, "bad_query_input", resultPath?.error || resultQuery?.error);
return APIErrorResponse(ctx, 400, "bad_query_input", result.error);
}
}
);
Expand Down
113 changes: 56 additions & 57 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
{
"name": "erc20-token-api",
"description": "Get informations about ERC20 tokens",
"version": "0.0.1",
"homepage": "https://github.com/pinax-network/erc20-token-api",
"license": "MIT",
"authors": [
{
"name": "Etienne Donneger",
"email": "[email protected]",
"url": "https://github.com/0237h"
},
{
"name": "Denis Carriere",
"email": "[email protected]",
"url": "https://github.com/DenisCarriere/"
},
{
"name": "Mathieu Lefebvre",
"email": "[email protected]",
"url": "https://github.com/Matlefebvre1234/"
}
],
"dependencies": {
"@clickhouse/client-web": "latest",
"@kubb/cli": "^2.23.2",
"@kubb/core": "^2.23.2",
"commander": "latest",
"dotenv": "latest",
"ethers": "^6.12.1",
"hono": "latest",
"prom-client": "latest",
"tslog": "latest",
"typed-openapi": "latest",
"zod": "latest"
"name": "erc20-token-api",
"description": "Get informations about ERC20 tokens",
"version": "0.0.1",
"homepage": "https://github.com/pinax-network/erc20-token-api",
"license": "MIT",
"authors": [
{
"name": "Etienne Donneger",
"email": "[email protected]",
"url": "https://github.com/0237h"
},
"private": true,
"scripts": {
"build": "export APP_VERSION=$(git rev-parse --short HEAD) && bun build --compile index.ts --outfile erc20-token-api",
"clean": "bun i --force",
"dev": "export APP_VERSION=$(git rev-parse --short HEAD) && bun --watch index.ts",
"lint": "export APP_VERSION=$(git rev-parse --short HEAD) && bun run tsc --noEmit --skipLibCheck --pretty",
"start": "export APP_VERSION=$(git rev-parse --short HEAD) && bun index.ts",
"test": "bun test --coverage",
"types": "bun run tsp compile ./src/typespec && bun run typed-openapi ./tsp-output/@typespec/openapi3/openapi.json -o ./src/types/zod.gen.ts -r zod",
"types:check": "bun run tsp compile ./src/typespec --no-emit --pretty --warn-as-error",
"types:format": "bun run tsp format src/typespec/**/*.tsp",
"types:watch": "bun run tsp compile ./src/typespec --watch --pretty --warn-as-error",
"generate:": "bun run kubb --config ./kubb.config.ts"
{
"name": "Denis Carriere",
"email": "[email protected]",
"url": "https://github.com/DenisCarriere/"
},
"type": "module",
"devDependencies": {
"@typespec/compiler": "latest",
"@typespec/openapi3": "latest",
"@typespec/protobuf": "latest",
"bun-types": "latest",
"typescript": "latest"
},
"prettier": {
"tabWidth": 4
{
"name": "Mathieu Lefebvre",
"email": "[email protected]",
"url": "https://github.com/Matlefebvre1234/"
}
],
"dependencies": {
"@clickhouse/client-web": "latest",
"@kubb/cli": "^2.23.2",
"@kubb/core": "^2.23.2",
"commander": "latest",
"dotenv": "latest",
"ethers": "^6.12.1",
"hono": "latest",
"prom-client": "latest",
"tslog": "latest",
"typed-openapi": "latest",
"zod": "latest"
},
"private": true,
"scripts": {
"build": "export APP_VERSION=$(git rev-parse --short HEAD) && bun build --compile index.ts --outfile erc20-token-api",
"clean": "bun i --force",
"dev": "export APP_VERSION=$(git rev-parse --short HEAD) && bun --watch index.ts",
"lint": "export APP_VERSION=$(git rev-parse --short HEAD) && bun run tsc --noEmit --skipLibCheck --pretty",
"start": "export APP_VERSION=$(git rev-parse --short HEAD) && bun index.ts",
"test": "bun test --coverage",
"types": "bun run tsp compile ./src/typespec && bun run typed-openapi ./tsp-output/@typespec/openapi3/openapi.json -o ./src/types/zod.gen.ts -r zod",
"types:check": "bun run tsp compile ./src/typespec --no-emit --pretty --warn-as-error",
"types:format": "bun run tsp format src/typespec/**/*.tsp",
"types:watch": "bun run tsp compile ./src/typespec --watch --pretty --warn-as-error"
},
"type": "module",
"devDependencies": {
"@typespec/compiler": "latest",
"@typespec/openapi3": "latest",
"@typespec/protobuf": "latest",
"bun-types": "latest",
"typescript": "latest"
},
"prettier": {
"tabWidth": 4
}
}
11 changes: 5 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import pkg from "../package.json";

export const DEFAULT_PORT = "8080";
export const DEFAULT_HOSTNAME = "localhost";
export const DEFAULT_HOST = "http://ch-node44d.mar.eosn.io:8123";
export const DATABASE_SUFFIX = "token_v1"; // API will use {chain}_{DATABASE_SUFFIX} as the database name
export const DEFAULT_USERNAME = "default";
export const DEFAULT_PASSWORD = "rtlbYofewjyuW2kD";
export const DEFAULT_HOST = "http://localhost:8123";
export const DEFAULT_DATABASE = "eth_erc20_token";
export const DEFAULT_USERNAME = "testing";
export const DEFAULT_PASSWORD = "secret";
export const DEFAULT_MAX_LIMIT = 10000;
export const DEFAULT_VERBOSE = true;
export const DEFAULT_SORT_BY = "DESC";
Expand All @@ -28,12 +28,11 @@ const opts = program
.addOption(new Option("-p, --port <number>", "HTTP port on which to attach the API").env("PORT").default(DEFAULT_PORT))
.addOption(new Option("--hostname <string>", "Server listen on HTTP hostname").env("HOSTNAME").default(DEFAULT_HOSTNAME))
.addOption(new Option("--host <string>", "Database HTTP hostname").env("HOST").default(DEFAULT_HOST))
.addOption(new Option("--database <string>", "The database suffix to use inside ClickHouse for {chain}_{database}").env("DATABASE").default(`eth_${DATABASE_SUFFIX}`))
.addOption(new Option("--database <string>", "The database to use inside ClickHouse").env("DATABASE").default(DEFAULT_DATABASE))
.addOption(new Option("--username <string>", "Database user").env("USERNAME").default(DEFAULT_USERNAME))
.addOption(new Option("--password <string>", "Password associated with the specified username").env("PASSWORD").default(DEFAULT_PASSWORD))
.addOption(new Option("--max-limit <number>", "Maximum LIMIT queries").env("MAX_LIMIT").default(DEFAULT_MAX_LIMIT))
.addOption(new Option("-v, --verbose <boolean>", "Enable verbose logging").choices(["true", "false"]).env("VERBOSE").default(DEFAULT_VERBOSE))

.parse()
.opts();

Expand Down
43 changes: 22 additions & 21 deletions src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DATABASE_SUFFIX, DEFAULT_SORT_BY } from "./config.js";
import type { UsageEndpoints, ValidUserParams } from "./types/api.js";

import { DEFAULT_SORT_BY } from "./config.js";
import { getAddress, parseLimit, parseTimestamp, formatTxid } from "./utils.js";
import type { EndpointReturnTypes, UsageEndpoints, UsageResponse, ValidUserParams } from "./types/api.js";
import { Contract } from "ethers";
import { SupportedChains } from "./types/zod.gen.js";

export function addBlockFilter(q: any, additional_query_params: any, where: any[]) {

Expand Down Expand Up @@ -43,28 +45,27 @@ export function getChains() {

// Use a for loop to iterate over each item
for (const chain of supportedChain) {
queries.push(`SELECT '${chain}' as chain, MIN(block_num) as block_num FROM ${chain}_${DATABASE_SUFFIX}.cursors`)
queries.push(`SELECT '${chain}' as chain, MIN(block_num) as block_num FROM ${chain}_erc20_token.cursors`)
}

let query = queries.join(' UNION ALL ');
return query;
}



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

if (endpoint === "/{chain}/supply") {
const q = query_param as ValidUserParams<typeof endpoint>;

let contract = q.contract
let contract = q.contract;

let additional_query_params = {};


// Query
const table = `${q.chain}_${DATABASE_SUFFIX}.mv_supply_contract`
const contractTable = `${q.chain}_${DATABASE_SUFFIX}.contracts`;
const table = `${q.chain}_erc20_token.mv_supply_contract`
const contractTable = `${q.chain}_erc20_token.contracts`;
let query = `SELECT
${table}.contract,
${table}.supply as supply,
Expand Down Expand Up @@ -122,7 +123,7 @@ export function getContracts(endpoint: UsageEndpoints, query_param: any, example
let name = q.name;

// Query
const table = `${q.chain}_${DATABASE_SUFFIX}.contracts`
const table = `${q.chain}_erc20_token.contracts`
let query = `SELECT
${table}.contract,
${table}.name,
Expand Down Expand Up @@ -168,8 +169,8 @@ function getBalanceChanges_latest(q: any) {
let contract = q.contract;
let account = q.account;

let table = `${q.chain}_${DATABASE_SUFFIX}.account_balances`
const contractTable = `${q.chain}_${DATABASE_SUFFIX}.contracts`;
let table = `${q.chain}_erc20_token.account_balances`
const contractTable = `${q.chain}_erc20_token.contracts`;
let query = `SELECT
${table}.account,
${table}.contract,
Expand Down Expand Up @@ -224,10 +225,10 @@ function getBalanceChanges_historical(q: any) {
let additional_query_params = {};

let table;
const contractTable = `${q.chain}_${DATABASE_SUFFIX}.contracts`;
const contractTable = `${q.chain}_erc20_token.contracts`;
// SQL Query
if (contract) table = `${q.chain}_${DATABASE_SUFFIX}.balance_changes_contract_historical_mv`;
else table = `${q.chain}_${DATABASE_SUFFIX}.balance_changes_account_historical_mv`
if (contract) table = `${q.chain}_erc20_token.balance_changes_contract_historical_mv`;
else table = `${q.chain}_erc20_token.balance_changes_account_historical_mv`

let query = `SELECT
${table}.owner,
Expand Down Expand Up @@ -322,7 +323,7 @@ export function getBalanceChanges(endpoint: UsageEndpoints, query_param: any) {
function getHolder_latest(q: any) {
let contract = q.contract;
// SQL Query
const table = `${q.chain}_${DATABASE_SUFFIX}.token_holders_mv`
const table = `${q.chain}_erc20_token.token_holders_mv`
let query = `SELECT
account,
amount,
Expand Down Expand Up @@ -353,7 +354,7 @@ function getHolder_historical(q: any) {
let contract = q.contract;
let additional_query_params = {};
// SQL Query
const table = `${q.chain}_${DATABASE_SUFFIX}.balance_changes_contract_historical_mv`
const table = `${q.chain}_erc20_token.balance_changes_contract_historical_mv`
let query = `SELECT
owner as account,
new_balance AS amount,
Expand Down Expand Up @@ -431,10 +432,10 @@ export function getTransfers(endpoint: UsageEndpoints, query_param: any) {
let additional_query_params = {};

// SQL Query
let table = `${q.chain}_${DATABASE_SUFFIX}.transfers`
let mvFromTable = `${q.chain}_${DATABASE_SUFFIX}.transfers_from_historical_mv`
let mvToTable = `${q.chain}_${DATABASE_SUFFIX}.transfers_to_historical_mv`
let mvContractTable = `${q.chain}_${DATABASE_SUFFIX}.transfers_contract_historical_mv`
let table = `${q.chain}_erc20_token.transfers`
let mvFromTable = `${q.chain}_erc20_token.transfers_from_historical_mv`
let mvToTable = `${q.chain}_erc20_token.transfers_to_historical_mv`
let mvContractTable = `${q.chain}_erc20_token.transfers_contract_historical_mv`

let query = `SELECT
contract,
Expand Down Expand Up @@ -490,7 +491,7 @@ export function getTransfer(endpoint: UsageEndpoints, query_param: any) {
const transaction_id = q.trx_id;

// SQL Query
let table = `${q.chain}_${DATABASE_SUFFIX}.transfers`
let table = `${q.chain}_erc20_token.transfers`

let query = `SELECT
contract,
Expand Down
Loading

0 comments on commit d3740dc

Please sign in to comment.