diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index f7350b8..eceb64a 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -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", @@ -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" }, diff --git a/src/queries.ts b/src/queries.ts index dc94aa4..8e82a2c 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -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"); @@ -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); @@ -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); @@ -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; }