Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…c20-api into main
  • Loading branch information
Mathieu Lefebvre committed Oct 27, 2023
2 parents 64bffc5 + 84cc566 commit 04850d4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
72 changes: 40 additions & 32 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ 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, ''));
.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, ''));
.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 @@ -39,19 +47,19 @@ test("getTotalSupply", () => {
Contracts.name as name,
Contracts.symbol as symbol,
Contracts.decimals as decimals,
timestamp,
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
DESC LIMIT 1 `.replace(/\s+/g, ''));
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 @@ -61,18 +69,16 @@ test("getTotalSupply optional", () => {
Contracts.name as name,
Contracts.symbol as symbol,
Contracts.decimals as decimals,
timestamp,
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}' 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
DESC LIMIT ${limit} `.replace(/\s+/g, ''));
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 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 @@ -89,15 +95,15 @@ 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
WHERE(chain == '${chain}' AND owner == '${address}') ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, ''))
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 @@ -114,9 +120,11 @@ 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
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, ''))
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
10 changes: 5 additions & 5 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 All @@ -38,7 +38,7 @@ export function getTotalSupply(searchParams: URLSearchParams) {
${contractTable}.name as name,
${contractTable}.symbol as symbol,
${contractTable}.decimals as decimals,
timestamp,
timestamp
FROM ${table} `;

// JOIN block table
Expand All @@ -59,7 +59,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 @@ -95,7 +95,7 @@ export function getContracts(searchParams: URLSearchParams) {
addTimestampBlockFilter(searchParams, where);

// 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 @@ -146,7 +146,7 @@ export function getBalanceChanges(searchParams: URLSearchParams) {
addTimestampBlockFilter(searchParams, where);

// 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 04850d4

Please sign in to comment.