Skip to content

Commit

Permalink
fix WHERE space & remove empty spaces
Browse files Browse the repository at this point in the history
ref: #9
  • Loading branch information
DenisCarriere committed Oct 26, 2023
1 parent 61c2a38 commit 79c42f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
42 changes: 19 additions & 23 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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, ''))
});

Expand Down
8 changes: 4 additions & 4 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit 79c42f6

Please sign in to comment.