teleton 0.2.0 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -397,6 +397,14 @@ Contributions are welcome.
397
397
 
398
398
  ---
399
399
 
400
+ ## Contributors
401
+
402
+ <a href="https://github.com/TONresistor/teleton-agent/graphs/contributors">
403
+ <img src="https://contrib.rocks/image?repo=TONresistor/teleton-agent" />
404
+ </a>
405
+
406
+ ---
407
+
400
408
  ## License
401
409
 
402
410
  MIT License - See [LICENSE](LICENSE) for details.
@@ -0,0 +1,133 @@
1
+ import {
2
+ TELETON_ROOT
3
+ } from "./chunk-EYWNOHMJ.js";
4
+
5
+ // src/market/scraper-db.ts
6
+ import Database from "better-sqlite3";
7
+ import { join } from "path";
8
+ var DB_PATH = join(TELETON_ROOT, "gifts.db");
9
+ function initScraperDb() {
10
+ const db = new Database(DB_PATH);
11
+ db.pragma("journal_mode = WAL");
12
+ db.exec(`
13
+ -- Gift collections (Plush Pepes, Heart Lockets, etc.)
14
+ CREATE TABLE IF NOT EXISTS gift_collections (
15
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
16
+ address TEXT UNIQUE NOT NULL,
17
+ name TEXT NOT NULL,
18
+ floor_ton REAL,
19
+ floor_usd REAL,
20
+ volume_7d REAL,
21
+ listed_count INTEGER,
22
+ owners INTEGER,
23
+ supply INTEGER,
24
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
25
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
26
+ );
27
+
28
+ -- Models per collection (Cozy Galaxy, Milano, etc.)
29
+ CREATE TABLE IF NOT EXISTS gift_models (
30
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
31
+ collection_id INTEGER NOT NULL,
32
+ name TEXT NOT NULL,
33
+ floor_ton REAL,
34
+ rarity_percent REAL,
35
+ count INTEGER,
36
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
37
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
38
+ FOREIGN KEY (collection_id) REFERENCES gift_collections(id),
39
+ UNIQUE(collection_id, name)
40
+ );
41
+
42
+ -- Price history (for trends)
43
+ CREATE TABLE IF NOT EXISTS price_history (
44
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
45
+ collection_id INTEGER,
46
+ model_id INTEGER,
47
+ floor_ton REAL NOT NULL,
48
+ floor_usd REAL,
49
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
50
+ FOREIGN KEY (collection_id) REFERENCES gift_collections(id),
51
+ FOREIGN KEY (model_id) REFERENCES gift_models(id)
52
+ );
53
+
54
+ -- Indexes for frequent queries
55
+ CREATE INDEX IF NOT EXISTS idx_price_history_collection ON price_history(collection_id, timestamp);
56
+ CREATE INDEX IF NOT EXISTS idx_price_history_model ON price_history(model_id, timestamp);
57
+ CREATE INDEX IF NOT EXISTS idx_models_collection ON gift_models(collection_id);
58
+ `);
59
+ return db;
60
+ }
61
+ function upsertCollection(db, collection) {
62
+ const stmt = db.prepare(`
63
+ INSERT INTO gift_collections (address, name, floor_ton, floor_usd, volume_7d, updated_at)
64
+ VALUES (@address, @name, @floor_ton, @floor_usd, @volume_7d, CURRENT_TIMESTAMP)
65
+ ON CONFLICT(address) DO UPDATE SET
66
+ name = @name,
67
+ floor_ton = @floor_ton,
68
+ floor_usd = @floor_usd,
69
+ volume_7d = @volume_7d,
70
+ updated_at = CURRENT_TIMESTAMP
71
+ RETURNING id
72
+ `);
73
+ const result = stmt.get({
74
+ address: collection.address,
75
+ name: collection.name,
76
+ floor_ton: collection.floorTON || null,
77
+ floor_usd: collection.floorUSD || null,
78
+ volume_7d: collection.volume7d || null
79
+ });
80
+ return result.id;
81
+ }
82
+ function getCollectionId(db, address) {
83
+ const row = db.prepare(`SELECT id FROM gift_collections WHERE address = ?`).get(address);
84
+ return row?.id ?? null;
85
+ }
86
+ function upsertModel(db, collectionId, model) {
87
+ const stmt = db.prepare(`
88
+ INSERT INTO gift_models (collection_id, name, floor_ton, rarity_percent, count, updated_at)
89
+ VALUES (@collection_id, @name, @floor_ton, @rarity_percent, @count, CURRENT_TIMESTAMP)
90
+ ON CONFLICT(collection_id, name) DO UPDATE SET
91
+ floor_ton = @floor_ton,
92
+ rarity_percent = @rarity_percent,
93
+ count = @count,
94
+ updated_at = CURRENT_TIMESTAMP
95
+ RETURNING id
96
+ `);
97
+ const result = stmt.get({
98
+ collection_id: collectionId,
99
+ name: model.name,
100
+ floor_ton: model.floor || null,
101
+ rarity_percent: model.pct ? parseFloat(model.pct) : null,
102
+ count: model.count || null
103
+ });
104
+ return result.id;
105
+ }
106
+ function addPriceHistory(db, collectionId, modelId, floorTon, floorUsd = null) {
107
+ const stmt = db.prepare(`
108
+ INSERT INTO price_history (collection_id, model_id, floor_ton, floor_usd)
109
+ VALUES (?, ?, ?, ?)
110
+ `);
111
+ stmt.run(collectionId, modelId, floorTon, floorUsd);
112
+ }
113
+ function getScraperStats(db) {
114
+ const collections = db.prepare("SELECT COUNT(*) as count FROM gift_collections").get();
115
+ const models = db.prepare("SELECT COUNT(*) as count FROM gift_models").get();
116
+ const history = db.prepare("SELECT COUNT(*) as count FROM price_history").get();
117
+ const lastUpdate = db.prepare("SELECT MAX(updated_at) as last FROM gift_collections").get();
118
+ return {
119
+ collections: collections.count,
120
+ models: models.count,
121
+ historyEntries: history.count,
122
+ lastUpdate: lastUpdate.last
123
+ };
124
+ }
125
+
126
+ export {
127
+ initScraperDb,
128
+ upsertCollection,
129
+ getCollectionId,
130
+ upsertModel,
131
+ addPriceHistory,
132
+ getScraperStats
133
+ };