Skip to content

Commit

Permalink
Added template_id filtering via a JOIN on Assets table
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Oct 31, 2023
1 parent a0f0e81 commit cbadab9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default new OpenApiBuilder()
get: {
tags: [TAGS.USAGE],
summary: "Get sales",
description: "Get sales by `collection_name`, `sale_id`, `timestamp`, `block_number`, `listing_price_amount`, `listing_price_symcode`, `trx_id` or `asset_id_in_asset_ids`",
description: "Get sales by `collection_name`, `sale_id`, `timestamp`, `block_number`, `template_id`, `listing_price_amount`, `listing_price_symcode`, `trx_id` or `asset_id_in_asset_ids`",
parameters: [
{
name: "collection_name",
Expand All @@ -75,6 +75,14 @@ export default new OpenApiBuilder()
required: false,
schema: { type: "number" },
},
{
name: 'timestamp',
in: 'query',
description: 'Filter by exact timestamp',
required: false,
schema: timestampSchema,
examples: timestampExamples,
},
{
name: "block_number",
description: "Filter by Block number (ex: 18399498)",
Expand All @@ -83,12 +91,11 @@ export default new OpenApiBuilder()
schema: { type: "number" },
},
{
name: 'timestamp',
in: 'query',
description: 'Filter by exact timestamp',
name: "template_id",
description: "Filter by Asset Template ID (ex: 10620)",
in: "query",
required: false,
schema: timestampSchema,
examples: timestampExamples,
schema: { type: "number" },
},
{
name: "listing_price_amount",
Expand Down
9 changes: 6 additions & 3 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ test("getSalesCount", () => {

test("getSale", () => {
expect(getSale(new URLSearchParams({collection_name})))
.toBe(`SELECT * FROM Sales JOIN blocks ON blocks.block_id = Sales.block_id WHERE (collection_name == '${collection_name}') ORDER BY sale_id DESC LIMIT 1`);

.toBe(`SELECT sale_id, trx_id, asset_ids, listing_price_amount, listing_price_precision, listing_price_symcode,
s.collection_name as collection_name, template_id, block_number, timestamp FROM (SELECT * FROM Sales ARRAY JOIN asset_ids) AS s
JOIN Assets AS a ON a.asset_id = s.asset_ids AND a.collection_name = s.collection_name
JOIN blocks ON blocks.block_id = s.block_id WHERE (collection_name == '${collection_name}') ORDER BY sale_id DESC LIMIT 1`);
/*
expect(getSale(new URLSearchParams({collection_name, greater_or_equals_by_listing_price_amount: '15', limit: '10'})))
.toBe(`SELECT * FROM Sales JOIN blocks ON blocks.block_id = Sales.block_id WHERE (listing_price_amount >= 15 AND collection_name == '${collection_name}') ORDER BY sale_id DESC LIMIT 10`);
expect(getSale(new URLSearchParams({asset_id_in_asset_ids: '2199024044581'})))
.toBe(`SELECT * FROM Sales JOIN blocks ON blocks.block_id = Sales.block_id WHERE (has(asset_ids, 2199024044581)) ORDER BY sale_id DESC LIMIT 1`);
.toBe(`SELECT * FROM Sales JOIN blocks ON blocks.block_id = Sales.block_id WHERE (has(asset_ids, 2199024044581)) ORDER BY sale_id DESC LIMIT 1`);*/
});
14 changes: 11 additions & 3 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ export interface Sale {

export function getSale(searchParams: URLSearchParams) {
// SQL Query
let query = `SELECT * FROM Sales`;
let query = `SELECT sale_id, trx_id, asset_ids, listing_price_amount, listing_price_precision, listing_price_symcode,
s.collection_name as collection_name, template_id, block_number, timestamp FROM`;

// explode asset_ids array (useful for bundle sales)
query += ` (SELECT * FROM Sales ARRAY JOIN asset_ids) AS s`;
// JOIN assets table where Assets.asset_id is in Sales.asset_ids
query += `\nJOIN Assets AS a ON a.asset_id = s.asset_ids AND a.collection_name = s.collection_name`;

// JOIN block table
const where = [];
query += ` JOIN blocks ON blocks.block_id = Sales.block_id`;
query += `\nJOIN blocks ON blocks.block_id = s.block_id`;

const where = [];
// Clickhouse Operators
// https://clickhouse.com/docs/en/sql-reference/operators
const operators = [
Expand Down Expand Up @@ -49,13 +55,15 @@ export function getSale(searchParams: URLSearchParams) {
const listing_price_amount = searchParams.get('listing_price_amount');
const listing_price_symcode = searchParams.get('listing_price_symcode');
const trx_id = searchParams.get('trx_id');
const template_id = searchParams.get('template_id');
if (collection_name) where.push(`collection_name == '${collection_name}'`);
if (sale_id) where.push(`sale_id == '${sale_id}'`);
if (block_number) where.push(`block_number == '${block_number}'`);
if (timestamp) where.push(`toUnixTimestamp(timestamp) == ${timestamp}`);
if (listing_price_amount) where.push(`listing_price_amount == ${listing_price_amount}`);
if (listing_price_symcode) where.push(`listing_price_symcode == '${listing_price_symcode}'`);
if (trx_id) where.push(`trx_id == '${trx_id}'`);
if (template_id) where.push(`template_id == '${template_id}'`);

// Join WHERE statements with AND
if ( where.length ) query += ` WHERE (${where.join(' AND ')})`;
Expand Down

0 comments on commit cbadab9

Please sign in to comment.