Skip to content

Commit

Permalink
add support for multi chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Matlefebvre1234 committed Jun 28, 2024
1 parent c3ea890 commit fb8acc3
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 258 deletions.
14 changes: 7 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ function ERC20TokenAPI() {
}
);

createUsageEndpoint("/balance"); // TODO: Maybe separate `block_num`/`timestamp` queries with path parameters (additional response schemas)
createUsageEndpoint("/head");
createUsageEndpoint("/holders");
createUsageEndpoint("/supply"); // TODO: Same as `balance``
createUsageEndpoint("/transfers"); // TODO: Redefine `block_range` params
createUsageEndpoint("/transfers/{tx_id}");
createUsageEndpoint("/tokens");
createUsageEndpoint("/{chain}/balance"); // TODO: Maybe separate `block_num`/`timestamp` queries with path parameters (additional response schemas)
createUsageEndpoint("/chains");
createUsageEndpoint("/{chain}/holders");
createUsageEndpoint("/{chain}/supply"); // TODO: Same as `balance``
createUsageEndpoint("/{chain}/transfers"); // TODO: Redefine `block_range` params
createUsageEndpoint("/{chain}/transfers/{tx_id}");
createUsageEndpoint("/{chain}/tokens");
app.notFound((ctx: Context) => APIErrorResponse(ctx, 404, "route_not_found", `Path not found: ${ctx.req.method} ${ctx.req.path}`));

return app;
Expand Down
37 changes: 24 additions & 13 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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, where: any[]) {
if (q.block_num) where.push(`block_num <= ${q.block_num}`);
Expand All @@ -20,9 +21,26 @@ export function addBlockRangeFilter(q: any, where: any[]) {
}


export function getChains() {


//ADD more chains if needed
let supportedChain = ['eth'];
let queries = [];

// Use a for loop to iterate over each item
for (const chain of supportedChain) {
queries.push(`SELECT '${chain}' as chain, MAX(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 === "/supply") {
if (endpoint === "/{chain}/supply") {
const q = query_param as ValidUserParams<typeof endpoint>;

let address;
Expand Down Expand Up @@ -85,7 +103,7 @@ export function getTotalSupply(endpoint: UsageEndpoints, query_param: any, examp


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

// Params
Expand Down Expand Up @@ -280,7 +298,7 @@ function getBalanceChanges_historical(q: any) {
}
export function getBalanceChanges(endpoint: UsageEndpoints, query_param: any) {

if (endpoint === "/balance") {
if (endpoint === "/{chain}/balance") {
const q = query_param as ValidUserParams<typeof endpoint>;
let query;
if (q.block_num) query = getBalanceChanges_historical(q);
Expand Down Expand Up @@ -382,7 +400,7 @@ FROM ${table} `;
export function getHolders(endpoint: UsageEndpoints, query_param: any) {


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

let query;
Expand All @@ -398,7 +416,7 @@ export function getHolders(endpoint: UsageEndpoints, query_param: any) {
export function getTransfers(endpoint: UsageEndpoints, query_param: any) {


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

let contract;
Expand Down Expand Up @@ -463,10 +481,7 @@ export function getTransfers(endpoint: UsageEndpoints, query_param: any) {


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

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

let contract;
Expand Down Expand Up @@ -507,7 +522,3 @@ export function getTransfer(endpoint: UsageEndpoints, query_param: any) {
return ""
}
}

export function getChain() {
return `ETH`;
}
136 changes: 78 additions & 58 deletions src/types/zod.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const Supply = z.object({
timestamp: z.number(),
});

export type SupportedChains = z.infer<typeof SupportedChains>;
export const SupportedChains = z.literal("eth");

export type Transfer = z.infer<typeof Transfer>;
export const Transfer = z.object({
contract: z.string(),
Expand All @@ -111,29 +114,10 @@ export const Version = z.object({
commit: z.string(),
});

export type get_Usage_balance = typeof get_Usage_balance;
export const get_Usage_balance = {
method: z.literal("GET"),
path: z.literal("/balance"),
parameters: z.object({
query: z.object({
contract: z.union([z.string(), z.undefined()]),
account: z.string(),
block_num: z.union([z.number(), z.undefined()]),
limit: z.union([z.number(), z.undefined()]),
page: z.union([z.number(), z.undefined()]),
}),
}),
response: z.object({
data: z.array(BalanceChange),
meta: ResponseMetadata,
}),
};

export type get_Usage_head = typeof get_Usage_head;
export const get_Usage_head = {
export type get_Usage_chains = typeof get_Usage_chains;
export const get_Usage_chains = {
method: z.literal("GET"),
path: z.literal("/head"),
path: z.literal("/chains"),
parameters: z.object({
query: z.object({
limit: z.number().optional(),
Expand All @@ -143,6 +127,7 @@ export const get_Usage_head = {
response: z.object({
data: z.array(
z.object({
chain: SupportedChains,
block_num: z.number(),
}),
),
Expand All @@ -158,44 +143,77 @@ export const get_Monitoring_health = {
response: z.string(),
};

export type get_Monitoring_metrics = typeof get_Monitoring_metrics;
export const get_Monitoring_metrics = {
method: z.literal("GET"),
path: z.literal("/metrics"),
parameters: z.never(),
response: z.string(),
};

export type get_Docs_openapi = typeof get_Docs_openapi;
export const get_Docs_openapi = {
method: z.literal("GET"),
path: z.literal("/openapi"),
parameters: z.never(),
response: z.unknown(),
};

export type get_Docs_version = typeof get_Docs_version;
export const get_Docs_version = {
method: z.literal("GET"),
path: z.literal("/version"),
parameters: z.never(),
response: Version,
};

export type get_Usage_balance = typeof get_Usage_balance;
export const get_Usage_balance = {
method: z.literal("GET"),
path: z.literal("/{chain}/balance"),
parameters: z.object({
query: z.object({
contract: z.union([z.string(), z.undefined()]),
account: z.string(),
block_num: z.union([z.number(), z.undefined()]),
limit: z.union([z.number(), z.undefined()]),
page: z.union([z.number(), z.undefined()]),
}),
path: z.object({
chain: z.literal("eth"),
}),
}),
response: z.object({
data: z.array(BalanceChange),
meta: ResponseMetadata,
}),
};

export type get_Usage_holders = typeof get_Usage_holders;
export const get_Usage_holders = {
method: z.literal("GET"),
path: z.literal("/holders"),
path: z.literal("/{chain}/holders"),
parameters: z.object({
query: z.object({
contract: z.string(),
block_num: z.union([z.number(), z.undefined()]),
limit: z.union([z.number(), z.undefined()]),
page: z.union([z.number(), z.undefined()]),
}),
path: z.object({
chain: z.literal("eth"),
}),
}),
response: z.object({
data: z.array(Holder),
meta: ResponseMetadata,
}),
};

export type get_Monitoring_metrics = typeof get_Monitoring_metrics;
export const get_Monitoring_metrics = {
method: z.literal("GET"),
path: z.literal("/metrics"),
parameters: z.never(),
response: z.string(),
};

export type get_Docs_openapi = typeof get_Docs_openapi;
export const get_Docs_openapi = {
method: z.literal("GET"),
path: z.literal("/openapi"),
parameters: z.never(),
response: z.unknown(),
};

export type get_Usage_supply = typeof get_Usage_supply;
export const get_Usage_supply = {
method: z.literal("GET"),
path: z.literal("/supply"),
path: z.literal("/{chain}/supply"),
parameters: z.object({
query: z.object({
contract: z.string().optional(),
Expand All @@ -205,6 +223,9 @@ export const get_Usage_supply = {
limit: z.number().optional(),
page: z.number().optional(),
}),
path: z.object({
chain: z.literal("eth"),
}),
}),
response: z.object({
data: z.array(Supply),
Expand All @@ -215,7 +236,7 @@ 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"),
path: z.literal("/{chain}/tokens"),
parameters: z.object({
query: z.object({
contract: z.string().optional(),
Expand All @@ -224,6 +245,9 @@ export const get_Usage_tokens = {
limit: z.number().optional(),
page: z.number().optional(),
}),
path: z.object({
chain: z.literal("eth"),
}),
}),
response: z.object({
data: TypeSpec_OpenAPI_Contact,
Expand All @@ -234,7 +258,7 @@ export const get_Usage_tokens = {
export type get_Usage_transfers = typeof get_Usage_transfers;
export const get_Usage_transfers = {
method: z.literal("GET"),
path: z.literal("/transfers"),
path: z.literal("/{chain}/transfers"),
parameters: z.object({
query: z.object({
from: z.string().optional(),
Expand All @@ -244,6 +268,9 @@ export const get_Usage_transfers = {
limit: z.number().optional(),
page: z.number().optional(),
}),
path: z.object({
chain: z.literal("eth"),
}),
}),
response: z.object({
data: z.array(Transfer),
Expand All @@ -254,13 +281,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/{tx_id}"),
path: z.literal("/{chain}/transfers/{tx_id}"),
parameters: z.object({
query: z.object({
limit: z.number().optional(),
page: z.number().optional(),
}),
path: z.object({
chain: z.literal("eth"),
tx_id: z.string(),
}),
}),
Expand All @@ -270,28 +298,20 @@ export const get_Usage_transfer = {
}),
};

export type get_Docs_version = typeof get_Docs_version;
export const get_Docs_version = {
method: z.literal("GET"),
path: z.literal("/version"),
parameters: z.never(),
response: Version,
};

// <EndpointByMethod>
export const EndpointByMethod = {
get: {
"/balance": get_Usage_balance,
"/head": get_Usage_head,
"/chains": get_Usage_chains,
"/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/{tx_id}": get_Usage_transfer,
"/version": get_Docs_version,
"/{chain}/balance": get_Usage_balance,
"/{chain}/holders": get_Usage_holders,
"/{chain}/supply": get_Usage_supply,
"/{chain}/tokens": get_Usage_tokens,
"/{chain}/transfers": get_Usage_transfers,
"/{chain}/transfers/{tx_id}": get_Usage_transfer,
},
};
export type EndpointByMethod = typeof EndpointByMethod;
Expand Down
Loading

0 comments on commit fb8acc3

Please sign in to comment.