Skip to content

Commit

Permalink
Dynamic OpenApi example
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Lefebvre committed Oct 31, 2023
1 parent 441b380 commit d771fb7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
18 changes: 5 additions & 13 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { OpenApiBuilder, SchemaObject, ExampleObject, ParameterObject } from "op
import { config } from "../config.js";
import { registry } from "../prometheus.js";
import { supportedChainsQuery } from "./chains.js";

import { makeQuery } from "../clickhouse/makeQuery.js";
import { getBalanceChanges, getContracts, getTotalSupply } from "../queries.js";
const TAGS = {
MONITORING: "Monitoring",
HEALTH: "Health",
Expand All @@ -15,20 +16,11 @@ const TAGS = {

const arrayFilter = ["greater_or_equals_by_timestamp", "greater_by_timestamp", "less_or_equals_by_timestamp", "less_by_timestamp"];



const chains = await supportedChainsQuery();
// const supply_example = (await makeQuery(await getTotalSupply( new URLSearchParams({limit: "1"})))).data;
// const contract_example = (await makeQuery(await getContracts( new URLSearchParams({limit: "1"})))).data;
// const balance_example = (await makeQuery(await getBalanceChanges( new URLSearchParams({limit: "1"})))).data;

// TO-DO: make dynamic examples
const supply_example = {};
const contract_example = {};
const balance_example = {};
const supply_example = (await makeQuery(await getTotalSupply(new URLSearchParams({ limit: "2" }), true))).data;
const contract_example = (await makeQuery(await getContracts(new URLSearchParams({ limit: "2" }), true))).data;
const balance_example = (await makeQuery(await getBalanceChanges(new URLSearchParams({ limit: "2" }), true))).data;

// TO-DO: apply timestamp filters to docs
// https://github.com/pinax-network/substreams-erc20-api/issues/4
const timestampSchema: SchemaObject = {
anyOf: [
{ type: "number" },
Expand Down
98 changes: 54 additions & 44 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function addTimestampBlockFilter(searchParams: URLSearchParams, where: an
}
}

export function getTotalSupply(searchParams: URLSearchParams) {
export function getTotalSupply(searchParams: URLSearchParams, example?: boolean) {
// Params
const address = getAddress(searchParams, "address", false);
const chain = searchParams.get("chain");
Expand All @@ -41,36 +41,40 @@ export function getTotalSupply(searchParams: URLSearchParams) {
timestamp
FROM ${table} `;


// JOIN block table
query += ` JOIN blocks ON blocks.block_id = ${table}.block_id`;
query += ` LEFT JOIN Contracts ON ${contractTable}.address = ${table}.address`;
// WHERE statements
const where = [];
if (!example) {
// WHERE statements
const where = [];

// equals
if (chain) where.push(`${table}.chain == '${chain}'`);
if (address) where.push(`${table}.address == '${address}'`);

// equals
if (chain) where.push(`${table}.chain == '${chain}'`);
if (address) where.push(`${table}.address == '${address}'`);
// timestamp and block filter
addTimestampBlockFilter(searchParams, where);

// timestamp and block filter
addTimestampBlockFilter(searchParams, where);
if (symbol) where.push(`symbol == '${symbol}'`);
if (name) where.push(`name == '${name}'`);

if (symbol) where.push(`symbol == '${symbol}'`);
if (name) where.push(`name == '${name}'`);

// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;

// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;
// Sort and Limit
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `

// Sort and Limit
}
const limit = parseLimit(searchParams.get("limit"));
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `
query += ` LIMIT ${limit} `

return query;
}

export function getContracts(searchParams: URLSearchParams) {
export function getContracts(searchParams: URLSearchParams, example?: boolean) {
// Params
const chain = searchParams.get("chain");
const address = getAddress(searchParams, "address", false);
Expand All @@ -81,31 +85,34 @@ export function getContracts(searchParams: URLSearchParams) {
const table = 'Contracts'
let query = `SELECT * FROM ${table} `


// JOIN block table
query += ` JOIN blocks ON blocks.block_id = ${table}.block_id`;
if (!example) {
// WHERE statements
const where = [];
if (chain) where.push(`chain == '${chain}'`);
if (address) where.push(`address == '${address}'`);
if (symbol) where.push(`symbol == '${symbol}'`);
if (name) where.push(`name == '${name}'`);

// WHERE statements
const where = [];
if (chain) where.push(`chain == '${chain}'`);
if (address) where.push(`address == '${address}'`);
if (symbol) where.push(`symbol == '${symbol}'`);
if (name) where.push(`name == '${name}'`);
// timestamp and block filter
addTimestampBlockFilter(searchParams, where);

// timestamp and block filter
addTimestampBlockFilter(searchParams, where);
// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;

// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;
// Sort and Limit
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `

// Sort and Limit
}
const limit = parseLimit(searchParams.get("limit"));
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `
query += ` LIMIT ${limit} `
return query;
}

export function getBalanceChanges(searchParams: URLSearchParams) {
export function getBalanceChanges(searchParams: URLSearchParams, example?: boolean) {
const chain = searchParams.get("chain");
const contract = getAddress(searchParams, "contract", false);
const owner = getAddress(searchParams, "owner", false);
Expand All @@ -132,26 +139,29 @@ export function getBalanceChanges(searchParams: URLSearchParams) {
// JOIN block table
query += ` JOIN blocks ON blocks.block_id = ${table}.block_id`;
query += ` LEFT JOIN Contracts ON ${contractTable}.address = ${table}.contract`;
// WHERE statements
const where = [];

// equals
if (!example) {
// WHERE statements
const where = [];

// equals

if (chain) where.push(`chain == '${chain}'`);
if (owner) where.push(`owner == '${owner}'`);
if (contract) where.push(`contract == '${contract}'`);
if (transaction_id) where.push(`${table}.transaction_id == '${transaction_id}'`);
if (chain) where.push(`chain == '${chain}'`);
if (owner) where.push(`owner == '${owner}'`);
if (contract) where.push(`contract == '${contract}'`);
if (transaction_id) where.push(`${table}.transaction_id == '${transaction_id}'`);

// timestamp and block filter
addTimestampBlockFilter(searchParams, where);
// timestamp and block filter
addTimestampBlockFilter(searchParams, where);

// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;
// Join WHERE statements with AND
if (where.length) query += ` WHERE (${where.join(' AND ')})`;

// Sort and Limit
// Sort and Limit
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `
}
const limit = parseLimit(searchParams.get("limit"));
const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `
query += ` LIMIT ${limit} `
return query;
}
Expand Down

0 comments on commit d771fb7

Please sign in to comment.