Skip to content

Commit

Permalink
Refactor API for new database schemas
Browse files Browse the repository at this point in the history
* Removed `/chains` endpoint, will use different tables or databases
  for each chain
* Updated documentation
* Consistent code style across all files
* Added `amount` filter for all endpoints
* Added query parameters verification for `supply` endpoint
* Updated queries to use MV tables
  • Loading branch information
0237h committed Apr 16, 2024
1 parent 2d3e46a commit b7f7219
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 315 deletions.
50 changes: 31 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
## REST API

| Pathname | Description |
|-------------------------------------------|-----------------------|
| GET `/chains` | Available `chains`
| GET `/supply` | Antelope Tokens total supply
| GET `/balance` | Antelope Tokens balance changes
| GET `/transfers` | Antelope Tokens transfers
| GET `/health` | Health check
| GET `/metrics` | Prometheus metrics
| GET `/openapi` | [OpenAPI v3 JSON](https://spec.openapis.org/oas/v3.0.0)
| Method | Path | Description |
| :---: | --- | --- |
| GET <br>`text/html` | `/` | Swagger API playground |
| GET <br>`application/json` | `/supply` | Antelope Tokens total supply |
| GET <br>`application/json` | `/balance` | Antelope Tokens balance changes |
| GET <br>`application/json` | `/transfers` | Antelope Tokens transfers |
| GET <br>`text/plain` | `/health` | Performs health checks and checks if the database is accessible |
| GET <br>`text/plain` | `/metrics` | Prometheus metrics |
| GET <br>`application/json` | `/openapi` | OpenAPI specification |

## Requirements

- [ClickHouse](clickhouse.com/)
- [Substreams Sink ClickHouse](https://github.com/pinax-network/substreams-sink-clickhouse/)

## Quickstart
## Quick start

Install [Bun](https://bun.sh/)

```console
$ bun install
Expand All @@ -34,16 +36,29 @@ $ bun lint
$ bun test
```

## [`Bun` Binary Releases](https://github.com/pinax-network/substreams-sink-websockets/releases)

> [!NOTE]
> Currently not available
## [`Bun` Binary Releases](https://github.com/pinax-network/antelope-token-api/releases)

> Linux Only
> For Linux x86
```console
$ wget https://github.com/pinax-network/antelope-token-api/releases/download/v0.1.0/antelope-token-api
$ wget https://github.com/pinax-network/antelope-token-api/releases/download/v2.0.0/antelope-token-api
$ chmod +x ./antelope-token-api
$ ./antelope-token-api --help
Usage: antelope-token-api [options]

Token balances, supply and transfers from the Antelope blockchains

Options:
-V, --version output the version number
-p, --port <number> HTTP port on which to attach the API (default: "8080", env: PORT)
--hostname <string> Server listen on HTTP hostname (default: "localhost", env: HOSTNAME)
--host <string> Database HTTP hostname (default: "http://localhost:8123", env: HOST)
--database <string> The database to use inside ClickHouse (default: "default", env: DATABASE)
--username <string> Database user (default: "default", env: USERNAME)
--password <string> Password associated with the specified username (default: "", env: PASSWORD)
--max-limit <number> Maximum LIMIT queries (default: 10000, env: MAX_LIMIT)
-v, --verbose <boolean> Enable verbose logging (choices: "true", "false", default: false, env: VERBOSE)
-h, --help display help for command
```

## `.env` Environment variables
Expand All @@ -67,9 +82,6 @@ VERBOSE=true

## Docker environment

> [!NOTE]
> Currently not available
Pull from GitHub Container registry
```bash
docker pull ghcr.io/pinax-network/antelope-token-api:latest
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const app = Bun.serve({
port: config.port,
fetch(req: Request) {
if (req.method === "GET") return GET(req);
prometheus.request_error.inc({pathname: new URL(req.url).pathname, status: 400});
prometheus.request_error.inc({ pathname: new URL(req.url).pathname, status: 400 });
return new Response("Invalid request", { status: 400 });
}
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "antelope-token-api",
"description": "Transfers and account balances from the Antelope blockchains",
"version": "0.1.0",
"description": "Token balances, supply and transfers from the Antelope blockchains",
"version": "2.0.0",
"homepage": "https://github.com/pinax-network/antelope-token-api",
"license": "MIT",
"type": "module",
"authors": [
{
"name": "Etienne Donneger",
"email": "[email protected]",
"url": "https://github.com/Krow10"
"url": "https://github.com/0237h"
},
{
"name": "Denis Carriere",
Expand Down
2 changes: 0 additions & 2 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { registry } from "../prometheus.js";
import openapi from "./openapi.js";
import health from "./health.js";
import chains from "./chains.js";
import balance from "./balance.js";
import supply from "./supply.js";
import * as prometheus from "../prometheus.js";
Expand All @@ -19,7 +18,6 @@ export default async function (req: Request) {
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" } });
if (pathname === "/chains") return chains(req);
if (pathname === "/supply") return supply(req);
if (pathname === "/balance") return balance(req);
if (pathname === "/transfers") return transfers(req);
Expand Down
39 changes: 18 additions & 21 deletions src/fetch/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ import { getBalanceChanges } from "../queries.js";
import * as prometheus from "../prometheus.js";
import { toJSON } from "./utils.js";


function verifyParams(searchParams: URLSearchParams) {
const chain = searchParams.get("chain");
const account = searchParams.get("account");
const contract = searchParams.get("contract");
const account = searchParams.get("account");
const contract = searchParams.get("contract");

if (!chain) throw new Error("chain is required");
if (!account && !contract) throw new Error("account or contract is required");
if (!account && !contract) throw new Error("account or contract is required");
}

export default async function (req: Request) {
try {
const { searchParams } = new URL(req.url);
logger.info({ searchParams: Object.fromEntries(Array.from(searchParams)) });
verifyParams(searchParams);
const query = getBalanceChanges(searchParams);
const response = await makeQuery(query)
return toJSON(response.data);
} catch (e: any) {
logger.error(e);
prometheus.request_error.inc({ pathname: "/balance", status: 400 });
return new Response(e.message, { status: 400 });
}
try {
const { searchParams } = new URL(req.url);
logger.info({ searchParams: Object.fromEntries(Array.from(searchParams)) });

verifyParams(searchParams);
const query = getBalanceChanges(searchParams);
const response = await makeQuery(query)

return toJSON(response.data);
} catch (e: any) {
logger.error(e);
prometheus.request_error.inc({ pathname: "/balance", status: 400 });

return new Response(e.message, { status: 400 });
}
}
21 changes: 0 additions & 21 deletions src/fetch/chains.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/fetch/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { logger } from "../logger.js";
import * as prometheus from "../prometheus.js";

export default async function (_req: Request) {
try {
const response = await client.ping();
try {
const response = await client.ping();

if (response.success === false) throw new Error(response.error.message);
if (response.success === true ) return new Response("OK");
if (response.success === false) throw new Error(response.error.message);
if (response.success === true) return new Response("OK");

return new Response("Unknown response from ClickHouse");
} catch (e: any) {
logger.error(e);
prometheus.request_error.inc({ pathname: "/health", status: 500});
return new Response("Unknown response from ClickHouse");
} catch (e: any) {
logger.error(e);
prometheus.request_error.inc({ pathname: "/health", status: 500 });

return new Response(e.message, { status: 500 });
}
return new Response(e.message, { status: 500 });
}
}
Loading

0 comments on commit b7f7219

Please sign in to comment.