Skip to content

Commit

Permalink
Update parameters name in query to match DB columns (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h authored May 3, 2024
1 parent 697aa78 commit 5a21d12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { APP_VERSION } from "../config.js";
export default async function (req: Request) {
const { pathname } = new URL(req.url);
prometheus.request.inc({ pathname });

// Landing page
if (pathname === "/") return new Response(Bun.file(swaggerHtml));
if (pathname === "/favicon.png") return new Response(Bun.file(swaggerFavicon));
Expand Down
14 changes: 7 additions & 7 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ test("addTimestampBlockFilter", () => {
});
addTimestampBlockFilter(searchParams, where);

expect(where).toContain("block_number >= 123");
expect(where).toContain("block_number <= 123");
expect(where).toContain("block_num >= 123");
expect(where).toContain("block_num <= 123");
expect(where).toContain("toUnixTimestamp(timestamp) >= 1697587200");
expect(where).toContain("toUnixTimestamp(timestamp) <= 1697587100");
});
Expand All @@ -57,13 +57,13 @@ test("getTotalSupply", () => {
const parameters = new URLSearchParams({ contract });
const query = formatSQL(getTotalSupply(parameters));

expect(query).toContain(formatSQL('SELECT *, updated_at_block_num AS block_number, updated_at_timestamp AS timestamp FROM token_supplies'));
expect(query).toContain(formatSQL('SELECT *, updated_at_block_num AS block_num, updated_at_timestamp AS timestamp FROM token_supplies'));
expect(query).toContain(
formatSQL(
`WHERE(contract == '${contract}')`
)
);
expect(query).toContain(formatSQL(`ORDER BY block_number DESC`));
expect(query).toContain(formatSQL(`ORDER BY block_num DESC`));
expect(query).toContain(formatSQL(`LIMIT 1`));
});

Expand Down Expand Up @@ -91,13 +91,13 @@ test("getBalanceChange", () => {
const parameters = new URLSearchParams({ account, contract });
const query = formatSQL(getBalanceChanges(parameters));

expect(query).toContain(formatSQL(`SELECT *, updated_at_block_num AS block_number, updated_at_timestamp AS timestamp FROM account_balances`));
expect(query).toContain(formatSQL(`SELECT *, updated_at_block_num AS block_num, updated_at_timestamp AS timestamp FROM account_balances`));
expect(query).toContain(
formatSQL(
`WHERE(account == '${account}' AND contract == '${contract}')`
)
);
expect(query).toContain(formatSQL(`ORDER BY timestamp DESC`));
expect(query).toContain(formatSQL(`ORDER BY block_num DESC`));
expect(query).toContain(formatSQL(`LIMIT 1`));
});

Expand Down Expand Up @@ -129,7 +129,7 @@ test("getTransfers", () => {
expect(query).toContain(
formatSQL(
`WHERE(contract == '${contract}'
AND from == '${account}' AND to == '${account}' AND transaction == '${transaction_id}')`
AND from == '${account}' AND to == '${account}' AND trx_id == '${transaction_id}')`
)
);
expect(query).toContain(formatSQL(`ORDER BY block_num DESC`));
Expand Down
13 changes: 7 additions & 6 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function addTimestampBlockFilter(searchParams: URLSearchParams, where: an
const block_number = searchParams.get(`${key}_by_block`);
const timestamp = parseTimestamp(searchParams.get(`${key}_by_timestamp`));

if (block_number) where.push(`block_number ${operator} ${block_number}`);
if (block_number) where.push(`block_num ${operator} ${block_number}`);
if (timestamp) where.push(`toUnixTimestamp(timestamp) ${operator} ${timestamp}`);
}
}
Expand All @@ -62,7 +62,7 @@ export function getTotalSupply(searchParams: URLSearchParams, example?: boolean)
const symbol = searchParams.get("symbol");
const issuer = searchParams.get("issuer");

let query = 'SELECT *, updated_at_block_num AS block_number, updated_at_timestamp AS timestamp FROM token_supplies';
let query = 'SELECT *, updated_at_block_num AS block_num, updated_at_timestamp AS timestamp FROM token_supplies';

if (!example) {
// WHERE statements
Expand All @@ -79,7 +79,7 @@ export function getTotalSupply(searchParams: URLSearchParams, example?: boolean)
if (where.length) query += ` FINAL WHERE(${where.join(' AND ')})`;

const sort_by = searchParams.get("sort_by");
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `;
query += ` ORDER BY block_num ${sort_by ?? DEFAULT_SORT_BY} `;
}

const limit = parseLimit(searchParams.get("limit"));
Expand All @@ -95,7 +95,7 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole
const contract = searchParams.get("contract");
const account = searchParams.get("account");

let query = 'SELECT *, updated_at_block_num AS block_number, updated_at_timestamp AS timestamp FROM account_balances';
let query = 'SELECT *, updated_at_block_num AS block_num, updated_at_timestamp AS timestamp FROM account_balances';

if (!example) {
// WHERE statements
Expand All @@ -110,7 +110,7 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole

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

if (contract && account) query += ` ORDER BY timestamp DESC`;
if (contract && account) query += ` ORDER BY block_num DESC`;
//if (!contract && account) query += `GROUP BY (contract, account) ORDER BY timestamp DESC`;
//if (contract && !account) query += `GROUP BY (contract, account) ORDER BY timestamp DESC`;
}
Expand All @@ -124,6 +124,7 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole
return query;
}

// TODO: Investigate why transaction_id and timestamp (alone) request search in billions rows
export function getTransfers(searchParams: URLSearchParams, example?: boolean) {
const contract = searchParams.get("contract");
const from = searchParams.get("from");
Expand All @@ -143,7 +144,7 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) {
if (contract) where.push(`contract == '${contract}'`);
if (from) where.push(`from == '${from}'`);
if (to) where.push(`to == '${to}'`);
if (transaction_id) where.push(`transaction == '${transaction_id}'`);
if (transaction_id) where.push(`trx_id == '${transaction_id}'`);

addAmountFilter(searchParams, where);
addTimestampBlockFilter(searchParams, where);
Expand Down

0 comments on commit 5a21d12

Please sign in to comment.