From 378765fa0e14d809625b5da317843e9acafaf1a7 Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Tue, 21 Nov 2023 14:02:22 -0500 Subject: [PATCH] delete transaction id from balance --- src/fetch/GET.ts | 2 -- src/fetch/approvals.ts | 20 -------------- src/fetch/openapi.ts | 33 +++-------------------- src/queries.spec.ts | 35 +------------------------ src/queries.ts | 59 +----------------------------------------- 5 files changed, 5 insertions(+), 144 deletions(-) delete mode 100644 src/fetch/approvals.ts diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index 7021df6..8778ab6 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -11,7 +11,6 @@ import { logger } from "../logger.js"; import swaggerHtml from "../../swagger/index.html" import swaggerFavicon from "../../swagger/favicon.png" import transfers from "./transfers.js"; -import approvals from "./approvals.js"; export default async function (req: Request) { const { pathname } = new URL(req.url); @@ -26,7 +25,6 @@ export default async function (req: Request) { if (pathname === "/balance") return balance(req); if (pathname === "/contract") return contract(req); if (pathname === "/transfers") return transfers(req); - if (pathname === "/approvals") return approvals(req); if (pathname === "/holders") return holders(req); logger.warn(`Not found: ${pathname}`); prometheus.request_error.inc({ pathname, status: 404 }); diff --git a/src/fetch/approvals.ts b/src/fetch/approvals.ts deleted file mode 100644 index 5b90fdc..0000000 --- a/src/fetch/approvals.ts +++ /dev/null @@ -1,20 +0,0 @@ -// from: https://github.com/pinax-network/substreams-clock-api/blob/main/src/fetch/block.ts -import { makeQuery } from "../clickhouse/makeQuery.js"; -import { logger } from "../logger.js"; -import { getApprovals } from "../queries.js"; -import * as prometheus from "../prometheus.js"; -import { toJSON } from "./utils.js"; - -export default async function (req: Request) { - try { - const { searchParams } = new URL(req.url); - logger.info({ searchParams: Object.fromEntries(Array.from(searchParams)) }); - const query = await getApprovals(searchParams); - const response = await makeQuery(query) - return toJSON(response.data); - } catch (e: any) { - logger.error(e); - prometheus.request_error.inc({ pathname: "/approvals", status: 400 }); - return new Response(e.message, { status: 400 }); - } -} \ No newline at end of file diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index 3cfd90d..1021bd2 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -6,7 +6,7 @@ import { config } from "../config.js"; import { registry } from "../prometheus.js"; import { supportedChainsQuery } from "./chains.js"; import { makeQuery } from "../clickhouse/makeQuery.js"; -import { getApprovals, getBalanceChanges, getContracts, getHolders, getTotalSupply, getTransfers } from "../queries.js"; +import { getBalanceChanges, getContracts, getHolders, getTotalSupply, getTransfers } from "../queries.js"; const TAGS = { MONITORING: "Monitoring", HEALTH: "Health", @@ -23,10 +23,7 @@ const supply_example = (await makeQuery(await getTotalSupply(new URLSearchParams 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; const holders_example = (await makeQuery(await getHolders(new URLSearchParams({ limit: "5" }), true))).data; -// const transfers_example = (await makeQuery(await getTransfers(new URLSearchParams({ limit: "5" }), true))).data; -// const approvals_example = (await makeQuery(await getApprovals(new URLSearchParams({ limit: "5" }), true))).data; -const transfers_example = {}; -const approvals_example = {}; +const transfers_example = (await makeQuery(await getTransfers(new URLSearchParams({ limit: "5" }), true))).data; const timestampSchema: SchemaObject = { anyOf: [ @@ -180,7 +177,6 @@ export default new OpenApiBuilder() parameterChain, parameterString("owner"), parameterString("contract"), - parameterString("transaction_id"), ...timestampFilter, ...blockFilter, parameterLimit, @@ -212,30 +208,7 @@ export default new OpenApiBuilder() 400: { description: "Bad request" }, }, }, - }) - // .addPath("/approvals", { - // get: { - // tags: [TAGS.USAGE], - // summary: "ERC20 Approvals", - // parameters: [ - // parameterChain, - // parameterString("contract"), - // parameterString("owner"), - // parameterString("spender"), - // parameterString("transaction_id"), - // ...amountFilter, - // ...timestampFilter, - // ...blockFilter, - // parameterLimit, - // parameterOffset, - // ], - // responses: { - // 200: { description: "Array of supply", content: { "application/json": { example: approvals_example, schema: { type: "array" } } } }, - // 400: { description: "Bad request" }, - // }, - // }, - // }) - .addPath("/holders", { + }).addPath("/holders", { get: { tags: [TAGS.USAGE], summary: "ERC20 holders", diff --git a/src/queries.spec.ts b/src/queries.spec.ts index d4a06b6..61c0b65 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -214,7 +214,7 @@ test("getBalanceChanges with options", () => { }); expect(formatSQL(getBalanceChanges(parameters))).toContain( formatSQL( - `WHERE(chain == '${chain}' AND owner == '${address}' AND balance_changes.transaction_id == '${transaction_id}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp})` + `WHERE(chain == '${chain}' AND owner == '${address}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp})` ) ); }); @@ -252,39 +252,6 @@ test("getTransfers", () => { ); }); -// Test Approvals - -test("getApprovals", () => { - const parameters = new URLSearchParams({ chain, contract: address, owner: address, sender: address, transaction_id }); - expect(formatSQL(getApprovals(parameters))).toContain( - formatSQL(`SELECT - address as contract, - owner, - spender, - value as amount, - transaction as transaction_id, - block_number, - timestamp, - chain`) - ); - expect(formatSQL(getApprovals(parameters))).toContain( - formatSQL(`FROM Approvals`) - ); - - - expect(formatSQL(getApprovals(parameters))).toContain( - formatSQL(`WHERE(Approvals.chain == '${chain}' AND Approvals.address == '${address}' AND Approvals.owner == '${address}' AND Approvals.sender == '${address}' AND Approvals.transaction == '${transaction_id}')`) - ); - - expect(formatSQL(getApprovals(parameters))).toContain( - formatSQL(`ORDER BY block_number DESC`) - ); - - expect(formatSQL(getApprovals(parameters))).toContain( - formatSQL(`LIMIT 100`) - ); -}); - // Test getHolders test("getHolders", () => { const parameters = new URLSearchParams({ chain, contract: address }); diff --git a/src/queries.ts b/src/queries.ts index 7a31049..384886f 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -132,7 +132,7 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole const chain = searchParams.get("chain"); const contract = getAddress(searchParams, "contract", false)?.toLowerCase(); const owner = getAddress(searchParams, "owner", false)?.toLowerCase(); - const transaction_id = searchParams.get("transaction_id")?.toLowerCase(); + // SQL Query const table = 'balance_changes' const contractTable = 'Contracts'; @@ -163,7 +163,6 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole 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); @@ -282,62 +281,6 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) { return query; } - -export function getApprovals(searchParams: URLSearchParams, example?: boolean) { - // Params - const contract = getAddress(searchParams, "contract", false)?.toLowerCase(); - const owner = getAddress(searchParams, "owner", false)?.toLowerCase(); - const sender = getAddress(searchParams, "sender", false)?.toLowerCase(); - const chain = searchParams.get("chain"); - const transaction_id = searchParams.get("transaction_id")?.toLowerCase(); - const amount = searchParams.get("amount"); - // Query - const table = 'Approvals' - - let query = `SELECT - address as contract, - owner, - spender, - value as amount, - transaction as transaction_id, - block_number, - timestamp, - chain - FROM ${table} `; - - if (!example) { - // WHERE statements - const where = []; - - // equals - if (chain) where.push(`${table}.chain == '${chain}'`); - if (contract) where.push(`${table}.address == '${contract}'`); - if (owner) where.push(`${table}.owner == '${owner}'`); - if (sender) where.push(`${table}.sender == '${sender}'`); - if (transaction_id) where.push(`${table}.transaction == '${transaction_id}'`); - - //add amount filter - addAmountFilter(searchParams, where); - // timestamp and block filter - addTimestampBlockFilter(searchParams, where); - - // 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} ` - - } - const limit = parseLimit(searchParams.get("limit"), 100); - query += ` LIMIT ${limit} ` - const offset = searchParams.get("offset"); - if (offset) query += ` OFFSET ${offset} ` - return query; -} - - - export function getChain() { return `SELECT DISTINCT chain FROM module_hashes`; } \ No newline at end of file