Skip to content

Commit

Permalink
Merge pull request #69 from pinknetworkx/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fabian-emilius authored May 29, 2022
2 parents 993f1ba + 937ef9c commit 4c59784
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 128 deletions.
10 changes: 0 additions & 10 deletions definitions/materialized/atomicmarket_stats_markets.sql

This file was deleted.

11 changes: 0 additions & 11 deletions definitions/materialized/atomicmarket_stats_prices.sql

This file was deleted.

1 change: 0 additions & 1 deletion definitions/materialized/atomicmarket_template_prices.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ CREATE INDEX atomicmarket_template_prices_suggested_average ON atomicmarket_temp
CREATE INDEX atomicmarket_template_prices_min ON atomicmarket_template_prices USING btree ("min");
CREATE INDEX atomicmarket_template_prices_max ON atomicmarket_template_prices USING btree ("max");
CREATE INDEX atomicmarket_template_prices_sales ON atomicmarket_template_prices USING btree (sales);

374 changes: 374 additions & 0 deletions definitions/migrations/1.3.13/atomicmarket.sql

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions definitions/migrations/1.3.13/database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE dbinfo SET "value" = '1.3.13' WHERE name = 'version';
29 changes: 29 additions & 0 deletions definitions/tables/atomicmarket_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ CREATE TABLE atomicmarket_buyoffers_assets
CONSTRAINT atomicmarket_buyoffers_assets_pkey PRIMARY KEY (market_contract, buyoffer_id, assets_contract, asset_id)
);

CREATE TABLE atomicmarket_stats_markets (
listing_id bigint not null,
price bigint not null,
"time" bigint not null,
template_id bigint,
asset_id bigint,
market_contract varchar(12) not null,
listing_type text not null,
buyer varchar(12) not null,
seller varchar(12) not null,
maker_marketplace varchar(12) not null,
taker_marketplace varchar(12) not null,
assets_contract varchar(12) not null,
collection_name varchar(12),
schema_name varchar(12),
symbol varchar(12) not null,
CONSTRAINT atomicmarket_stats_markets_pkey PRIMARY KEY (market_contract, listing_type, listing_id)
);

-- Foreign Keys
ALTER TABLE ONLY atomicmarket_auctions
ADD CONSTRAINT atomicmarket_auctions_token_symbol_fkey FOREIGN KEY (market_contract, token_symbol)
Expand Down Expand Up @@ -278,3 +297,13 @@ CREATE INDEX atomicmarket_buyoffers_assets_asset_id ON atomicmarket_buyoffers_as
CREATE INDEX atomicmarket_sales_missing_mint ON atomicmarket_sales(assets_contract, sale_id, offer_id) WHERE template_mint IS NULL;
CREATE INDEX atomicmarket_buyoffers_missing_mint ON atomicmarket_buyoffers(assets_contract, buyoffer_id) WHERE template_mint IS NULL;
CREATE INDEX atomicmarket_auctions_missing_mint ON atomicmarket_auctions(assets_contract, auction_id) WHERE template_mint IS NULL;


CREATE INDEX atomicmarket_stats_markets_collection_name ON atomicmarket_stats_markets USING btree (collection_name);
CREATE INDEX atomicmarket_stats_markets_buyer ON atomicmarket_stats_markets USING btree (buyer);
CREATE INDEX atomicmarket_stats_markets_seller ON atomicmarket_stats_markets USING btree (seller);
CREATE INDEX atomicmarket_stats_markets_price ON atomicmarket_stats_markets USING btree (price);
CREATE INDEX atomicmarket_stats_markets_time ON atomicmarket_stats_markets USING btree ("time");
CREATE INDEX atomicmarket_stats_markets_asset_id ON atomicmarket_stats_markets USING btree ("asset_id");
CREATE INDEX atomicmarket_stats_markets_schema_name ON atomicmarket_stats_markets USING btree ("schema_name");
CREATE INDEX atomicmarket_stats_markets_template_id ON atomicmarket_stats_markets USING btree ("template_id");
29 changes: 0 additions & 29 deletions definitions/views/atomicmarket_stats_markets_master.sql

This file was deleted.

60 changes: 14 additions & 46 deletions definitions/views/atomicmarket_stats_prices_master.sql
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
CREATE OR REPLACE VIEW atomicmarket_stats_prices_master AS
SELECT *
FROM (
(
SELECT
sale.market_contract, 'sale' listing_type, sale.sale_id listing_id,
sale.assets_contract, sale.collection_name,
MIN(asset.schema_name) schema_name, MIN(asset.template_id) template_id, MIN(asset.asset_id) asset_id,
sale.settlement_symbol symbol, sale.final_price price, sale.updated_at_time "time"
FROM
atomicassets_assets asset, atomicassets_offers_assets offer_asset, atomicmarket_sales sale
WHERE
sale.assets_contract = offer_asset.contract AND sale.offer_id = offer_asset.offer_id AND
offer_asset.contract = asset.contract AND offer_asset.asset_id = asset.asset_id AND
sale.final_price IS NOT NULL AND sale.state = 3
GROUP BY sale.market_contract, sale.sale_id
HAVING COUNT(*) = 1
) UNION ALL (
SELECT
auction.market_contract, 'auction' listing_type, auction.auction_id listing_id,
auction.assets_contract, auction.collection_name,
MIN(asset.schema_name) schema_name, MIN(asset.template_id) template_id, MIN(asset.asset_id) asset_id,
auction.token_symbol symbol, auction.price, (auction.end_time * 1000) "time"
FROM
atomicassets_assets asset, atomicmarket_auctions_assets auction_asset, atomicmarket_auctions auction
WHERE
auction.assets_contract = auction_asset.assets_contract AND auction.auction_id = auction_asset.auction_id AND
auction_asset.assets_contract = asset.contract AND auction_asset.asset_id = asset.asset_id AND
auction.buyer IS NOT NULL AND auction.state = 1 AND auction.end_time < extract(epoch from now())
GROUP BY auction.market_contract, auction.auction_id
HAVING COUNT(*) = 1
) UNION ALL (
SELECT
buyoffer.market_contract, 'buyoffer' listing_type, buyoffer.buyoffer_id listing_id,
buyoffer.assets_contract, buyoffer.collection_name,
MIN(asset.schema_name) schema_name, MIN(asset.template_id) template_id, MIN(asset.asset_id) asset_id,
buyoffer.token_symbol symbol, buyoffer.price, buyoffer.updated_at_time "time"
FROM
atomicassets_assets asset, atomicmarket_buyoffers_assets buyoffer_asset, atomicmarket_buyoffers buyoffer
WHERE
buyoffer.assets_contract = buyoffer_asset.assets_contract AND buyoffer.buyoffer_id = buyoffer_asset.buyoffer_id AND
buyoffer_asset.assets_contract = asset.contract AND buyoffer_asset.asset_id = asset.asset_id AND
buyoffer.state = 3
GROUP BY buyoffer.market_contract, buyoffer.buyoffer_id
HAVING COUNT(*) = 1
)
) t1
SELECT
market_contract,
listing_type,
listing_id,
assets_contract,
collection_name,
schema_name,
template_id,
asset_id,
symbol,
price,
"time"
FROM atomicmarket_stats_markets
WHERE asset_id IS NOT NULL
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eosio-contract-api",
"version": "1.3.12",
"version": "1.3.13",
"description": "EOSIO Contract API",
"author": "pink.gg",
"license": "AGPL-3.0",
Expand Down
6 changes: 3 additions & 3 deletions src/api/namespaces/atomicmarket/handlers/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getPricesAction(params: RequestValues, ctx: AtomicMarketCo

const query = new QueryBuilder(
'SELECT price.*, token.token_precision, token.token_contract, asset.template_mint ' +
'FROM atomicmarket_stats_prices price, atomicassets_assets asset, atomicmarket_tokens token '
'FROM atomicmarket_stats_prices_master price, atomicassets_assets asset, atomicmarket_tokens token '
);

query.equal('price.market_contract', ctx.coreArgs.atomicmarket_account);
Expand Down Expand Up @@ -152,7 +152,7 @@ export async function getPricesSalesDaysAction(params: RequestValues, ctx: Atomi
AVG(price.price)::bigint average,
COUNT(*) sales, token.token_symbol, token.token_precision, token.token_contract,
(price.time / (3600 * 24 * 1000)) daytime
FROM atomicmarket_stats_prices price, atomicmarket_tokens token
FROM atomicmarket_stats_prices_master price, atomicmarket_tokens token
`);

query.equal('price.market_contract', ctx.coreArgs.atomicmarket_account);
Expand Down Expand Up @@ -330,4 +330,4 @@ function buildUserInventoryPriceResponse(result: IUserInventoryPricesQueryRespon
return {
collections: Object.values(response)
};
}
}
23 changes: 14 additions & 9 deletions src/api/namespaces/atomicmarket/handlers/stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ describe('AtomicMarket Stats API', () => {
await client.createFullSale({
final_price: 1,
listing_price: 1, listing_symbol: 'TOKEN1',
settlement_symbol: 'TOKEN1', state: SaleApiState.SOLD
settlement_symbol: 'TOKEN1', state: SaleApiState.SOLD,
taker_marketplace: 'X',
}, {template_id});

await client.createFullSale({
final_price: 1,
listing_price: 1,
listing_symbol: 'TOKEN1',
settlement_symbol: 'TOKEN1',
state: SaleApiState.SOLD
state: SaleApiState.SOLD,
taker_marketplace: 'X',
}, {template_id});

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({symbol: 'TOKEN1'}, context);

Expand All @@ -56,7 +58,7 @@ describe('AtomicMarket Stats API', () => {
// Not included
await client.createTemplate();

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({symbol: 'TOKEN1', template_id: template_id}, context);

Expand All @@ -79,7 +81,7 @@ describe('AtomicMarket Stats API', () => {
// Not included
await client.createTemplate();

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({symbol: 'TOKEN1', schema_name}, context);

Expand All @@ -102,7 +104,7 @@ describe('AtomicMarket Stats API', () => {
// Not included
await client.createTemplate();

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({symbol: 'TOKEN1', collection_name}, context);

Expand All @@ -124,7 +126,7 @@ describe('AtomicMarket Stats API', () => {
// Not included
await client.createTemplate();

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({symbol: 'TOKEN1', search: 'test'}, context);

Expand Down Expand Up @@ -155,6 +157,7 @@ describe('AtomicMarket Stats API', () => {
listing_price: 1, listing_symbol: 'TOKEN1',
settlement_symbol: 'TOKEN1', state: SaleApiState.SOLD,
updated_at_time: now,
taker_marketplace: 'X',
}, {template_id});


Expand All @@ -167,6 +170,7 @@ describe('AtomicMarket Stats API', () => {
settlement_symbol: 'TOKEN1',
state: SaleApiState.SOLD,
updated_at_time: now - 20,
taker_marketplace: 'X',
}, {template_id: template_id2});

// Not included
Expand All @@ -177,9 +181,10 @@ describe('AtomicMarket Stats API', () => {
settlement_symbol: 'TOKEN1',
state: SaleApiState.SOLD,
updated_at_time: now + 20,
taker_marketplace: 'X',
}, {template_id: template_id2});

await client.query('REFRESH MATERIALIZED VIEW "atomicmarket_stats_prices"');
await client.refreshStatsMarket();

const response = await getTemplateStatsAction({
symbol: 'TOKEN1',
Expand All @@ -204,4 +209,4 @@ describe('AtomicMarket Stats API', () => {
});

after(async () => await client.end());
});
});
8 changes: 4 additions & 4 deletions src/api/namespaces/atomicmarket/handlers/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export async function getSchemaStatsByCollectionV2Action(params: RequestValues,

const query = new QueryBuilder(
'SELECT template.schema_name, SUM(price.price) volume, COUNT(*) sales ' +
'FROM atomicmarket_stats_prices price, atomicassets_templates "template" '
'FROM atomicmarket_stats_prices_master price, atomicassets_templates "template" '
);

query.addCondition('price.assets_contract = template.contract AND price.template_id = template.template_id');
Expand Down Expand Up @@ -311,9 +311,9 @@ export async function getTemplateStatsAction(params: RequestValues, ctx: AtomicM
FROM atomicassets_templates "template"
LEFT JOIN (
SELECT assets_contract, template_id, SUM(price) "volume", COUNT(*) "sales"
FROM atomicmarket_stats_prices "asp"
WHERE
"asp".assets_contract = $1 AND "asp".market_contract = $2 AND
FROM atomicmarket_stats_prices_master "asp"
WHERE
"asp".assets_contract = $1 AND "asp".market_contract = $2 AND
"asp".symbol = $3 ${buildRangeCondition('"asp".time', args.after, args.before)}
GROUP BY "asp".assets_contract, "asp".template_id
) "stats" ON ("stats".template_id = "template".template_id AND "stats".assets_contract = "template".contract)
Expand Down
10 changes: 8 additions & 2 deletions src/api/namespaces/atomicmarket/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ export class AtomicMarketTestClient extends AtomicAssetsTestClient {
});
}

async refreshPrice(): Promise<Record<string, any>> {
return this.query('REFRESH MATERIALIZED VIEW atomicmarket_template_prices');
async refreshPrice(): Promise<void> {
await this.refreshStatsMarket();

await this.query('REFRESH MATERIALIZED VIEW atomicmarket_template_prices');
}

async refreshStatsMarket(): Promise<void> {
await this.query('SELECT update_atomicmarket_stats_market()');
}

async createBuyOffer(values: Record<string, any> = {}): Promise<Record<string, any>> {
Expand Down
5 changes: 4 additions & 1 deletion src/bin/init-test-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ async function main(): Promise<void> {
process.exit(0);
}

main().catch(logger.error);
main().catch(err => {
logger.error(err);
process.exit(1);
});
Loading

0 comments on commit 4c59784

Please sign in to comment.