Skip to content

Commit

Permalink
Change default limit to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 29, 2024
1 parent c7799e1 commit fcc925a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const timestampExamplesArrayFilter = ["greater_or_equals_by_timestamp", "greater
const blockExamplesArrayFilter = ["greater_or_equals_by_block", "greater_by_block", "less_or_equals_by_block", "less_by_block"];
const amountExamplesArrayFilter = ["amount_greater_or_equals", "amount_greater", "amount_less_or_equals", "amount_less"];

const supply_example = await makeQuery(getTotalSupply(new URLSearchParams({ limit: "2" }), true)).then(res => addMetadata(res, 2, 1));
const supply_example = await makeQuery(getTotalSupply(new URLSearchParams({ limit: "1" }), true)).then(res => addMetadata(res, 1, 1));
const balance_example = await makeQuery(getBalanceChanges(new URLSearchParams({ limit: "2" }), true)).then(res => addMetadata(res, 2, 1));
const transfers_example = await makeQuery(getTransfers(new URLSearchParams({ limit: "5" }), true)).then(res => addMetadata(res, 5, 1));

Expand Down
13 changes: 6 additions & 7 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function getTotalSupply(searchParams: URLSearchParams, example?: boolean)
query += ` ORDER BY block_number ${sort_by ?? DEFAULT_SORT_BY} `;
}

const limit = parseLimit(searchParams.get("limit"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
Expand Down Expand Up @@ -115,7 +115,7 @@ export function getBalanceChanges(searchParams: URLSearchParams, example?: boole
//if (contract && !account) query += `GROUP BY (contract, account) ORDER BY timestamp DESC`;
}

const limit = parseLimit(searchParams.get("limit"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
Expand All @@ -132,10 +132,9 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) {

let query = "SELECT * FROM ";

if (searchParams.get("greater_or_equals_by_block") && searchParams.get("less_or_equals_by_block")) query += "transfers_block_num"
else if (from && !to) query += "transfers_from"
if (from && !to) query += "transfers_from"
else if (!from && to) query += "transfers_to"
else query += "transfer_events"
else query += "transfers_block_num"

if (!example) {
// WHERE statements
Expand All @@ -151,10 +150,10 @@ export function getTransfers(searchParams: URLSearchParams, example?: boolean) {

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

query += ` ORDER BY timestamp DESC`;
query += ` ORDER BY block_num DESC`;
}

const limit = parseLimit(searchParams.get("limit"), config.maxLimit);
const limit = parseLimit(searchParams.get("limit"));
if (limit) query += ` LIMIT ${limit}`;

const page = parsePage(searchParams.get("page"));
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export function parseBlockId(block_id?: string | null) {
}

export function parseLimit(limit?: string | null | number, defaultLimit?: number) {
let value = 0; // default 0 (no limit)
let value = 1; // default 1
if (defaultLimit)
value = defaultLimit;
if (limit) {
if (typeof limit === "string") value = parseInt(limit);
if (typeof limit === "number") value = limit;
}
// limit must be between 0 (no limit) and maxLimit
if (value < 0) value = 0;
// limit must be between 1 and maxLimit
if (value <= 0) value = 1;
if (value > config.maxLimit) value = config.maxLimit;
return value;
}
Expand Down

0 comments on commit fcc925a

Please sign in to comment.