Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…c20-api into main
  • Loading branch information
Mathieu Lefebvre committed Oct 25, 2023
2 parents d81c349 + 54b1851 commit 92ce5c8
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 57 deletions.
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// from: https://github.com/pinax-network/substreams-clock-api/blob/main/index.ts
import { config } from "./src/config";
import { logger } from "./src/logger";
import GET from "./src/fetch/GET";
import { config } from "./src/config.js";
import { logger } from "./src/logger.js";
import GET from "./src/fetch/GET.js";
import * as prometheus from "./src/prometheus.js";

if (config.verbose) logger.enable();
Expand Down
6 changes: 3 additions & 3 deletions src/clickhouse/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// from: https://github.com/pinax-network/substreams-clock-api/blob/main/src/clickhouse/createClient.ts
import { createClient } from "@clickhouse/client-web";
import { ping } from "./ping";
import { APP_NAME, config } from "../config";
import { ping } from "./ping.js";
import { APP_NAME, config } from "../config.js";

const client = createClient({
...config,
clickhouse_settings: {
allow_experimental_object_type: 1,
readonly: "1"
readonly: "1",
},
application: APP_NAME,
})
Expand Down
6 changes: 3 additions & 3 deletions src/clickhouse/makeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// from: https://github.com/pinax-network/substreams-clock-api/blob/main/src/clickhouse/makeQuery.ts
import { logger } from "../logger";
import * as prometheus from "../prometheus";
import client from "./createClient";
import { logger } from "../logger.js";
import * as prometheus from "../prometheus.js";
import client from "./createClient.js";

export interface Meta {
name: string,
Expand Down
2 changes: 1 addition & 1 deletion src/clickhouse/ping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// from: https://github.com/pinax-network/substreams-clock-api/blob/main/src/clickhouse/ping.ts
import { PingResult } from "@clickhouse/client-web";
import client from "./createClient";
import client from "./createClient.js";

// Does not work with Bun's implementation of Node streams.
export async function ping(): Promise<PingResult> {
Expand Down
6 changes: 4 additions & 2 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import contract from "./contract.js";
import supply from "./supply.js";
import * as prometheus from "../prometheus.js";
import { logger } from "../logger.js";
import swaggerHtml from "../../swagger/index.html"
import swaggerFavicon from "../../swagger/favicon.png"

export default async function (req: Request) {
const { pathname} = new URL(req.url);
prometheus.request.inc({pathname});
if ( pathname === "/" ) return new Response(Bun.file("./swagger/index.html"));
if ( pathname === "/favicon.png" ) return new Response(Bun.file("./swagger/favicon.png"));
if ( pathname === "/" ) return new Response(Bun.file(swaggerHtml));
if ( pathname === "/favicon.png" ) return new Response(Bun.file(swaggerFavicon));
if ( pathname === "/health" ) return health(req);
if ( pathname === "/metrics" ) return new Response(await registry.metrics(), {headers: {"Content-Type": registry.contentType}});
if ( pathname === "/openapi" ) return new Response(openapi, {headers: {"Content-Type": "application/json"}});
Expand Down
8 changes: 4 additions & 4 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import pkg from "../../package.json" assert { type: "json" };

import { OpenApiBuilder, SchemaObject, ExampleObject, ParameterObject } from "openapi3-ts/oas31";
import { config } from "../config";
import { getBalanceChanges, getContracts, getTotalSupply } from "../queries";
import { registry } from "../prometheus";
import { makeQuery } from "../clickhouse/makeQuery";
import { config } from "../config.js";
import { getBalanceChanges, getContracts, getTotalSupply } from "../queries.js";
import { registry } from "../prometheus.js";
import { makeQuery } from "../clickhouse/makeQuery.js";
import { supportedChainsQuery } from "./chains.js";

const TAGS = {
Expand Down
8 changes: 4 additions & 4 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// from: https://github.com/pinax-network/substreams-clock-api/blob/main/src/queries.spec.ts

import { expect, test } from "bun:test";
import { getContracts, getChain, getTotalSupply, getBalanceChanges } from "./queries";
import { getContracts, getChain, getTotalSupply, getBalanceChanges } from "./queries.js";

const chain = "eth";
const address = 'dac17f958d2ee523a2206206994597c13d831ec7'

test("getContracts", () => {
expect(getContracts(new URLSearchParams({ chain, address })))
.toBe(`SELECT * FROM Contracts JOIN block ON block.block_id = Contracts.block_id WHERE (chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1`);
.toBe(`SELECT * FROM Contracts JOIN blocks ON blocks.block_id = Contracts.block_id WHERE (chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1`);
});

test("getTotalSupply", () => {
expect(getTotalSupply(new URLSearchParams({ chain, address })))
.toBe(`SELECT * FROM TotalSupply JOIN block ON block.block_id = TotalSupply.block_id WHERE (chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1`);
.toBe(`SELECT * FROM TotalSupply JOIN blocks ON blocks.block_id = TotalSupply.block_id WHERE (chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1`);
});

test("getBalanceChanges", () => {
expect(getBalanceChanges(new URLSearchParams({ chain, owner: address })))
.toBe(`SELECT * FROM balance_changes JOIN block ON block.block_id = balance_changes.block_id WHERE (chain == '${chain}' AND owner == '${address}') ORDER BY block_number DESC LIMIT 1`);
.toBe(`SELECT * FROM balance_changes JOIN blocks ON blocks.block_id = balance_changes.block_id WHERE (chain == '${chain}' AND owner == '${address}') ORDER BY block_number DESC LIMIT 1`);
});

test("getChain", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_SORT_BY } from "./config";
import { getAddress, parseLimit, parseTimestamp } from "./utils";
import { DEFAULT_SORT_BY } from "./config.js";
import { getAddress, parseLimit } from "./utils.js";

export function getTotalSupply(searchParams: URLSearchParams) {
// Params
Expand Down
14 changes: 14 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module "*.png" {
const content: string;
export default content;
}

declare module "*.html" {
const content: string;
export default content;
}

declare module "*.sql" {
const content: string;
export default content;
}
2 changes: 1 addition & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { formatAddress, checkValidAddress, getAddress } from "./utils";
import { formatAddress, checkValidAddress, getAddress } from "./utils.js";

const address = "0xdac17f958d2ee523a2206206994597c13d831ec7";

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import { config } from "./config";
import { config } from "./config.js";

export function getAddress(searchParams: URLSearchParams, key: string, required: boolean = false) {
const address = formatAddress(searchParams.get(key));
Expand Down
46 changes: 13 additions & 33 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
{
"compilerOptions": {
// add Bun type definitions
"types": ["bun-types"],

// enable latest features
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",

// if TS 5.x+
"moduleResolution": "bundler",
"noEmit": true,
"allowImportingTsExtensions": true,
"moduleDetection": "force",
// if TS 4.x or earlier
// "moduleResolution": "nodenext",

"jsx": "react-jsx", // support JSX
"allowJs": true, // allow importing `.js` from `.ts`

// best practices
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist/",
"strict": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strictNullChecks": true,
"alwaysStrict": true,
"skipLibCheck": true,
"composite": true,
"downlevelIteration": true,
"allowSyntheticDefaultImports": true
},
"include": [
"src/**/*.ts",
"package.json"
],
"exclude": [
"node_modules",
"src/tests/*.spec.ts"
]
"types": ["bun-types"]
}
}

0 comments on commit 92ce5c8

Please sign in to comment.