Skip to content

Commit

Permalink
delete transaction id from balance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Lefebvre committed Nov 21, 2023
1 parent 59b9e31 commit 378765f
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 144 deletions.
2 changes: 0 additions & 2 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 });
Expand Down
20 changes: 0 additions & 20 deletions src/fetch/approvals.ts

This file was deleted.

33 changes: 3 additions & 30 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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: [
Expand Down Expand Up @@ -180,7 +177,6 @@ export default new OpenApiBuilder()
parameterChain,
parameterString("owner"),
parameterString("contract"),
parameterString("transaction_id"),
...timestampFilter,
...blockFilter,
parameterLimit,
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 1 addition & 34 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`
)
);
});
Expand Down Expand Up @@ -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 });
Expand Down
59 changes: 1 addition & 58 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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`;
}

0 comments on commit 378765f

Please sign in to comment.