From 79c42f62591943d4e517c4259da6c9516fb7614e Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Thu, 26 Oct 2023 18:38:44 -0400 Subject: [PATCH] fix WHERE space & remove empty spaces ref: https://github.com/pinax-network/substreams-erc20-api/issues/9 --- src/queries.spec.ts | 42 +++++++++++++++++++----------------------- src/queries.ts | 8 ++++---- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/queries.spec.ts b/src/queries.spec.ts index 0295e82..712834c 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -12,23 +12,23 @@ const greater_or_equals_by_timestamp = "1697587200"; const less_or_equals_by_timestamp = "1697587100"; const transaction_id = "ab3612eed62a184eed2ae86bcad766183019cf40f82e5316f4d7c4e61f4baa44" -//Test Contract +// Test Contract test("getContracts", () => { expect(getContracts(new URLSearchParams({ chain, address })).replace(/\s+/g, '')) .toBe(`SELECT * FROM Contracts JOIN blocks ON blocks.block_id = Contracts.block_id WHERE(chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, '')); }); -//Test Contract with optionals options +// Test Contract with optionals options test("getContracts Optional", () => { expect(getContracts(new URLSearchParams({ chain, address, symbol, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, name, limit })).replace(/\s+/g, '')) .toBe(`SELECT * FROM Contracts JOIN blocks ON blocks.block_id = Contracts.block_id WHERE(chain == '${chain}' AND address == '${address}' AND symbol == '${symbol}' AND name =='${name}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp}) ORDER BY block_number DESC LIMIT ${limit} `.replace(/\s+/g, '')); }); -//Test TotalSupply +// Test TotalSupply test("getTotalSupply", () => { expect(getTotalSupply(new URLSearchParams({ chain, address })).replace(/\s+/g, '')) - .toBe(`SELECT + .toBe(`SELECT TotalSupply.address as address, TotalSupply.supply as supply, TotalSupply.id as id, @@ -40,17 +40,17 @@ test("getTotalSupply", () => { Contracts.decimals as decimals, timestamp, FROM TotalSupply - JOIN blocks ON blocks.block_id = TotalSupply.block_id - LEFT JOIN Contracts ON Contracts.address = TotalSupply.address - WHERE(TotalSupply.chain == '${chain}' AND TotalSupply.address == '${address}') - ORDER BY block_number + JOIN blocks ON blocks.block_id = TotalSupply.block_id + LEFT JOIN Contracts ON Contracts.address = TotalSupply.address + WHERE(TotalSupply.chain == '${chain}' AND TotalSupply.address == '${address}') + ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, '')); }); -//Test TotalSupply +// Test TotalSupply test("getTotalSupply optional", () => { expect(getTotalSupply(new URLSearchParams({ chain, address, symbol, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, name, limit })).replace(/\s+/g, '')) - .toBe(`SELECT + .toBe(`SELECT TotalSupply.address as address, TotalSupply.supply as supply, TotalSupply.id as id, @@ -62,16 +62,14 @@ test("getTotalSupply optional", () => { Contracts.decimals as decimals, timestamp, FROM TotalSupply - JOIN blocks ON blocks.block_id = TotalSupply.block_id - LEFT JOIN Contracts ON Contracts.address = TotalSupply.address + JOIN blocks ON blocks.block_id = TotalSupply.block_id + LEFT JOIN Contracts ON Contracts.address = TotalSupply.address WHERE(TotalSupply.chain == '${chain}' AND TotalSupply.address == '${address}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp} AND symbol == '${symbol}' AND name == '${name}') - ORDER BY block_number + ORDER BY block_number DESC LIMIT ${limit} `.replace(/\s+/g, '')); }); - - -//Test BalanceChanges +// Test BalanceChanges test("getBalanceChanges", () => { expect(getBalanceChanges(new URLSearchParams({ chain, owner: address })).replace(/\s+/g, '')) .toBe(`SELECT balance_changes.contract as contract, @@ -88,15 +86,13 @@ test("getBalanceChanges", () => { block_number, timestamp FROM balance_changes - JOIN blocks ON blocks.block_id = balance_changes.block_id - LEFT JOIN Contracts ON Contracts.address = balance_changes.contract + JOIN blocks ON blocks.block_id = balance_changes.block_id + LEFT JOIN Contracts ON Contracts.address = balance_changes.contract WHERE(chain == '${chain}' AND owner == '${address}') ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, '')) }); - - -//Test BalanceChanges Optional +// Test BalanceChanges Optional test("getBalanceChanges Optional", () => { expect(getBalanceChanges(new URLSearchParams({ chain, owner: address, transaction_id, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, limit })).replace(/\s+/g, '')) .toBe(`SELECT balance_changes.contract as contract, @@ -113,8 +109,8 @@ test("getBalanceChanges Optional", () => { block_number, timestamp FROM balance_changes - JOIN blocks ON blocks.block_id = balance_changes.block_id - LEFT JOIN Contracts ON Contracts.address = balance_changes.contract + JOIN blocks ON blocks.block_id = balance_changes.block_id + LEFT JOIN Contracts ON Contracts.address = balance_changes.contract 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}) ORDER BY block_number DESC LIMIT ${limit} `.replace(/\s+/g, '')) }); diff --git a/src/queries.ts b/src/queries.ts index d7680a3..3bd9442 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -11,7 +11,7 @@ export function getTotalSupply(searchParams: URLSearchParams) { // Query const table = 'TotalSupply' const contractTable = 'Contracts'; - let query = `SELECT + let query = `SELECT ${table}.address as address, ${table}.supply as supply, ${table}.id as id, @@ -52,7 +52,7 @@ export function getTotalSupply(searchParams: URLSearchParams) { // Join WHERE statements with AND - if (where.length) query += ` WHERE(${where.join(' AND ')})`; + if (where.length) query += ` WHERE (${where.join(' AND ')})`; // Sort and Limit const limit = parseLimit(searchParams.get("limit")); @@ -98,7 +98,7 @@ export function getContracts(searchParams: URLSearchParams) { } // Join WHERE statements with AND - if (where.length) query += ` WHERE(${where.join(' AND ')})`; + if (where.length) query += ` WHERE (${where.join(' AND ')})`; // Sort and Limit const limit = parseLimit(searchParams.get("limit")); @@ -158,7 +158,7 @@ export function getBalanceChanges(searchParams: URLSearchParams) { } // Join WHERE statements with AND - if (where.length) query += ` WHERE(${where.join(' AND ')})`; + if (where.length) query += ` WHERE (${where.join(' AND ')})`; // Sort and Limit const limit = parseLimit(searchParams.get("limit"));