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 f8a2884 commit a8d0edb
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 572 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
}
}
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 a8d0edb

Please sign in to comment.