@@ -40,6 +40,7 @@ export interface TokenPerformance {
40
40
rapidDump : boolean ;
41
41
suspiciousVolume : boolean ;
42
42
validationTrust : number ;
43
+ balance : number ;
43
44
lastUpdated : Date ;
44
45
}
45
46
@@ -120,9 +121,20 @@ interface TokenPerformanceRow {
120
121
rapid_dump : number ;
121
122
suspicious_volume : number ;
122
123
validation_trust : number ;
124
+ balance : number ;
123
125
last_updated : string ;
124
126
}
125
127
128
+ interface Transaction {
129
+ tokenAddress : string ;
130
+ transactionHash : string ;
131
+ type : "buy" | "sell" ;
132
+ amount : number ;
133
+ price : number ;
134
+ isSimulation : boolean ;
135
+ timestamp : string ;
136
+ }
137
+
126
138
export class TrustScoreDatabase {
127
139
private db : Database ;
128
140
@@ -192,6 +204,7 @@ export class TrustScoreDatabase {
192
204
rapid_dump BOOLEAN DEFAULT FALSE,
193
205
suspicious_volume BOOLEAN DEFAULT FALSE,
194
206
validation_trust REAL DEFAULT 0,
207
+ balance REAL DEFAULT 0,
195
208
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
196
209
);
197
210
` ) ;
@@ -289,6 +302,20 @@ export class TrustScoreDatabase {
289
302
FOREIGN KEY (recommender_id) REFERENCES recommenders(id) ON DELETE CASCADE
290
303
);
291
304
` ) ;
305
+
306
+ // create transactions table
307
+ this . db . exec ( `
308
+ CREATE TABLE IF NOT EXISTS transactions (
309
+ token_address TEXT NOT NULL,
310
+ transaction_hash TEXT PRIMARY KEY,
311
+ type TEXT NOT NULL,
312
+ amount REAL NOT NULL,
313
+ price REAL NOT NULL,
314
+ timestamp TEXT NOT NULL,
315
+ is_simulation BOOLEAN DEFAULT FALSE,
316
+ FOREIGN KEY (token_address) REFERENCES token_performance(token_address) ON DELETE CASCADE
317
+ );
318
+ ` ) ;
292
319
}
293
320
294
321
/**
@@ -749,6 +776,25 @@ export class TrustScoreDatabase {
749
776
}
750
777
}
751
778
779
+ // update token balance
780
+
781
+ updateTokenBalance ( tokenAddress : string , balance : number ) : boolean {
782
+ const sql = `
783
+ UPDATE token_performance
784
+ SET balance = ?,
785
+ last_updated = CURRENT_TIMESTAMP
786
+ WHERE token_address = ?;
787
+ ` ;
788
+ try {
789
+ this . db . prepare ( sql ) . run ( balance , tokenAddress ) ;
790
+ console . log ( `Updated token balance for ${ tokenAddress } ` ) ;
791
+ return true ;
792
+ } catch ( error ) {
793
+ console . error ( "Error updating token balance:" , error ) ;
794
+ return false ;
795
+ }
796
+ }
797
+
752
798
/**
753
799
* Retrieves token performance metrics.
754
800
* @param tokenAddress Token's address
@@ -776,6 +822,7 @@ export class TrustScoreDatabase {
776
822
rapidDump : row . rapid_dump === 1 ,
777
823
suspiciousVolume : row . suspicious_volume === 1 ,
778
824
validationTrust : row . validation_trust ,
825
+ balance : row . balance ,
779
826
lastUpdated : new Date ( row . last_updated ) ,
780
827
} ;
781
828
}
@@ -1247,6 +1294,79 @@ export class TrustScoreDatabase {
1247
1294
} ;
1248
1295
}
1249
1296
1297
+ // ----- Transactions Methods -----
1298
+ /**
1299
+ * Adds a new transaction to the database.
1300
+ * @param transaction Transaction object
1301
+ * @returns boolean indicating success
1302
+ */
1303
+
1304
+ addTransaction ( transaction : Transaction ) : boolean {
1305
+ const sql = `
1306
+ INSERT INTO transactions (
1307
+ token_address,
1308
+ transaction_hash,
1309
+ type,
1310
+ amount,
1311
+ price,
1312
+ is_simulation,
1313
+ timestamp
1314
+ ) VALUES (?, ?, ?, ?, ?, ?);
1315
+ ` ;
1316
+ try {
1317
+ this . db
1318
+ . prepare ( sql )
1319
+ . run (
1320
+ transaction . tokenAddress ,
1321
+ transaction . transactionHash ,
1322
+ transaction . type ,
1323
+ transaction . amount ,
1324
+ transaction . price ,
1325
+ transaction . isSimulation ,
1326
+ transaction . timestamp
1327
+ ) ;
1328
+ return true ;
1329
+ } catch ( error ) {
1330
+ console . error ( "Error adding transaction:" , error ) ;
1331
+ return false ;
1332
+ }
1333
+ }
1334
+
1335
+ /**
1336
+ * Retrieves all transactions for a specific token.
1337
+ * @param tokenAddress Token's address
1338
+ * @returns Array of Transaction objects
1339
+ */
1340
+ getTransactionsByToken ( tokenAddress : string ) : Transaction [ ] {
1341
+ const sql = `SELECT * FROM transactions WHERE token_address = ? ORDER BY timestamp DESC;` ;
1342
+ const rows = this . db . prepare ( sql ) . all ( tokenAddress ) as Array < {
1343
+ token_address : string ;
1344
+ transaction_hash : string ;
1345
+ type : string ;
1346
+ amount : number ;
1347
+ price : number ;
1348
+ is_simulation : boolean ;
1349
+ timestamp : string ;
1350
+ } > ;
1351
+
1352
+ return rows . map ( ( row ) => {
1353
+ // Validate and cast 'type' to ensure it matches the expected union type
1354
+ if ( row . type !== "buy" && row . type !== "sell" ) {
1355
+ throw new Error ( `Unexpected transaction type: ${ row . type } ` ) ;
1356
+ }
1357
+
1358
+ return {
1359
+ tokenAddress : row . token_address ,
1360
+ transactionHash : row . transaction_hash ,
1361
+ type : row . type as "buy" | "sell" ,
1362
+ amount : row . amount ,
1363
+ price : row . price ,
1364
+ isSimulation : row . is_simulation ,
1365
+ timestamp : new Date ( row . timestamp ) . toISOString ( ) ,
1366
+ } ;
1367
+ } ) ;
1368
+ }
1369
+
1250
1370
/**
1251
1371
* Close the database connection gracefully.
1252
1372
*/
0 commit comments