diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index f975dbe..d7101f8 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -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", @@ -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)", @@ -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", diff --git a/src/queries.spec.ts b/src/queries.spec.ts index 60a16a0..802b010 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -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`);*/ }); \ No newline at end of file diff --git a/src/queries.ts b/src/queries.ts index 7a65328..60a7ef7 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -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 = [ @@ -49,6 +55,7 @@ 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}'`); @@ -56,6 +63,7 @@ export function getSale(searchParams: URLSearchParams) { 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 ')})`;