Skip to content

Commit 80296c8

Browse files
committed
add token performance and simulation token performance
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
1 parent 6671265 commit 80296c8

File tree

2 files changed

+434
-1
lines changed

2 files changed

+434
-1
lines changed

src/adapters/trustScoreDatabase.ts

+299
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ export interface RecommenderMetricsHistory {
6464
recordedAt: Date;
6565
}
6666

67+
export interface TradePerformance {
68+
token_address: string;
69+
recommender_id: string;
70+
buy_price: number;
71+
sell_price: number;
72+
buy_timeStamp: string;
73+
sell_timeStamp: string;
74+
buy_amount: number;
75+
sell_amount: number;
76+
buy_sol: number;
77+
received_sol: number;
78+
buy_value_usd: number;
79+
sell_value_usd: number;
80+
profit_usd: number;
81+
profit_percent: number;
82+
buy_market_cap: number;
83+
sell_market_cap: number;
84+
market_cap_change: number;
85+
buy_liquidity: number;
86+
sell_liquidity: number;
87+
liquidity_change: number;
88+
last_updated: string;
89+
rapidDump: boolean;
90+
}
91+
6792
interface RecommenderMetricsRow {
6893
recommender_id: string;
6994
overall_trust_score: number;
@@ -191,6 +216,67 @@ export class TrustScoreDatabase {
191216
FOREIGN KEY (recommender_id) REFERENCES recommenders(id) ON DELETE CASCADE
192217
);
193218
`);
219+
220+
// ----- Create TradePerformance Tables -----
221+
this.db.exec(`
222+
CREATE TABLE IF NOT EXISTS trade (
223+
token_address TEXT NOT NULL,
224+
recommender_id TEXT NOT NULL,
225+
buy_price REAL NOT NULL,
226+
sell_price REAL,
227+
buy_timeStamp TEXT NOT NULL,
228+
sell_timeStamp TEXT,
229+
buy_amount REAL NOT NULL,
230+
sell_amount REAL,
231+
buy_sol REAL NOT NULL,
232+
received_sol REAL,
233+
buy_value_usd REAL NOT NULL,
234+
sell_value_usd REAL,
235+
profit_usd REAL,
236+
profit_percent REAL,
237+
buy_market_cap REAL NOT NULL,
238+
sell_market_cap REAL,
239+
market_cap_change REAL,
240+
buy_liquidity REAL NOT NULL,
241+
sell_liquidity REAL,
242+
liquidity_change REAL,
243+
last_updated TEXT DEFAULT (datetime('now')),
244+
rapidDump BOOLEAN DEFAULT FALSE,
245+
PRIMARY KEY (token_address, recommender_id, buy_timeStamp),
246+
FOREIGN KEY (token_address) REFERENCES token_performance(token_address) ON DELETE CASCADE,
247+
FOREIGN KEY (recommender_id) REFERENCES recommenders(id) ON DELETE CASCADE
248+
);
249+
`);
250+
// create trade simulation table
251+
this.db.exec(`
252+
CREATE TABLE IF NOT EXISTS simulation_trade (
253+
token_address TEXT NOT NULL,
254+
recommender_id TEXT NOT NULL,
255+
buy_price REAL NOT NULL,
256+
sell_price REAL,
257+
buy_timeStamp TEXT NOT NULL,
258+
sell_timeStamp TEXT,
259+
buy_amount REAL NOT NULL,
260+
sell_amount REAL,
261+
buy_sol REAL NOT NULL,
262+
received_sol REAL,
263+
buy_value_usd REAL NOT NULL,
264+
sell_value_usd REAL,
265+
profit_usd REAL,
266+
profit_percent REAL,
267+
buy_market_cap REAL NOT NULL,
268+
sell_market_cap REAL,
269+
market_cap_change REAL,
270+
buy_liquidity REAL NOT NULL,
271+
sell_liquidity REAL,
272+
liquidity_change REAL,
273+
last_updated TEXT DEFAULT (datetime('now')),
274+
rapidDump BOOLEAN DEFAULT FALSE,
275+
PRIMARY KEY (token_address, recommender_id, buy_timeStamp),
276+
FOREIGN KEY (token_address) REFERENCES token_performance(token_address) ON DELETE CASCADE,
277+
FOREIGN KEY (recommender_id) REFERENCES recommenders(id) ON DELETE CASCADE
278+
);
279+
`);
194280
}
195281

196282
/**
@@ -652,6 +738,219 @@ export class TrustScoreDatabase {
652738
}));
653739
}
654740

741+
/**
742+
* Inserts a new trade performance into the specified table.
743+
* @param trade The TradePerformance object containing trade details.
744+
* @param isSimulation Whether the trade is a simulation. If true, inserts into simulation_trade; otherwise, into trade.
745+
* @returns boolean indicating success.
746+
*/
747+
addTradePerformance(trade: TradePerformance, isSimulation: boolean): boolean {
748+
const tableName = isSimulation ? "simulation_trade" : "trade";
749+
const sql = `
750+
INSERT INTO ${tableName} (
751+
token_address,
752+
recommender_id,
753+
buy_price,
754+
sell_price,
755+
buy_timeStamp,
756+
sell_timeStamp,
757+
buy_amount,
758+
sell_amount,
759+
buy_sol,
760+
received_sol,
761+
buy_value_usd,
762+
sell_value_usd,
763+
profit_usd,
764+
profit_percent,
765+
buy_market_cap,
766+
sell_market_cap,
767+
market_cap_change,
768+
buy_liquidity,
769+
sell_liquidity,
770+
liquidity_change,
771+
last_updated,
772+
rapidDump
773+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
774+
`;
775+
try {
776+
this.db
777+
.prepare(sql)
778+
.run(
779+
trade.token_address,
780+
trade.recommender_id,
781+
trade.buy_price,
782+
trade.sell_price || null,
783+
trade.buy_timeStamp,
784+
trade.sell_timeStamp || null,
785+
trade.buy_amount,
786+
trade.sell_amount || null,
787+
trade.buy_sol,
788+
trade.received_sol || null,
789+
trade.buy_value_usd,
790+
trade.sell_value_usd || null,
791+
trade.profit_usd || null,
792+
trade.profit_percent || null,
793+
trade.buy_market_cap,
794+
trade.sell_market_cap || null,
795+
trade.market_cap_change || null,
796+
trade.buy_liquidity,
797+
trade.sell_liquidity || null,
798+
trade.liquidity_change || null,
799+
trade.last_updated || new Date().toISOString(),
800+
trade.rapidDump ? 1 : 0
801+
);
802+
console.log(`Inserted trade into ${tableName}:`, trade);
803+
return true;
804+
} catch (error) {
805+
console.error(`Error inserting trade into ${tableName}:`, error);
806+
return false;
807+
}
808+
}
809+
810+
/**
811+
* Updates an existing trade with sell details.
812+
* @param tokenAddress The address of the token.
813+
* @param recommenderId The UUID of the recommender.
814+
* @param buyTimeStamp The timestamp when the buy occurred.
815+
* @param sellDetails An object containing sell-related details.
816+
* @param isSimulation Whether the trade is a simulation. If true, updates in simulation_trade; otherwise, in trade.
817+
* @returns boolean indicating success.
818+
*/
819+
820+
updateTradePerformanceOnSell(
821+
tokenAddress: string,
822+
recommenderId: string,
823+
buyTimeStamp: string,
824+
sellDetails: {
825+
sell_price: number;
826+
sell_timeStamp: string;
827+
sell_amount: number;
828+
received_sol: number;
829+
sell_value_usd: number;
830+
profit_usd: number;
831+
profit_percent: number;
832+
sell_market_cap: number;
833+
market_cap_change: number;
834+
sell_liquidity: number;
835+
liquidity_change: number;
836+
rapidDump: boolean;
837+
},
838+
isSimulation: boolean
839+
): boolean {
840+
const tableName = isSimulation ? "simulation_trade" : "trade";
841+
const sql = `
842+
UPDATE ${tableName}
843+
SET
844+
sell_price = ?,
845+
sell_timeStamp = ?,
846+
sell_amount = ?,
847+
received_sol = ?,
848+
sell_value_usd = ?,
849+
profit_usd = ?,
850+
profit_percent = ?,
851+
sell_market_cap = ?,
852+
market_cap_change = ?,
853+
sell_liquidity = ?,
854+
liquidity_change = ?,
855+
rapidDump = ?
856+
WHERE
857+
token_address = ?
858+
AND recommender_id = ?
859+
AND buy_timeStamp = ?;
860+
`;
861+
try {
862+
const result = this.db
863+
.prepare(sql)
864+
.run(
865+
sellDetails.sell_price,
866+
sellDetails.sell_timeStamp,
867+
sellDetails.sell_amount,
868+
sellDetails.received_sol,
869+
sellDetails.sell_value_usd,
870+
sellDetails.profit_usd,
871+
sellDetails.profit_percent,
872+
sellDetails.sell_market_cap,
873+
sellDetails.market_cap_change,
874+
sellDetails.sell_liquidity,
875+
sellDetails.liquidity_change,
876+
sellDetails.rapidDump ? 1 : 0,
877+
tokenAddress,
878+
recommenderId,
879+
buyTimeStamp
880+
);
881+
882+
if (result.changes === 0) {
883+
console.warn(
884+
`No trade found to update in ${tableName} for token: ${tokenAddress}, recommender: ${recommenderId}, buyTimeStamp: ${buyTimeStamp}`
885+
);
886+
return false;
887+
}
888+
889+
console.log(`Updated trade in ${tableName}:`, {
890+
token_address: tokenAddress,
891+
recommender_id: recommenderId,
892+
buy_timeStamp: buyTimeStamp,
893+
...sellDetails,
894+
});
895+
return true;
896+
} catch (error) {
897+
console.error(`Error updating trade in ${tableName}:`, error);
898+
return false;
899+
}
900+
}
901+
902+
//getTradePerformance
903+
904+
/**
905+
* Retrieves trade performance metrics.
906+
* @param tokenAddress Token's address
907+
* @param recommenderId Recommender's UUID
908+
* @param buyTimeStamp Timestamp when the buy occurred
909+
* @param isSimulation Whether the trade is a simulation. If true, retrieves from simulation_trade; otherwise, from trade.
910+
* @returns TradePerformance object or null
911+
*/
912+
913+
getTradePerformance(
914+
tokenAddress: string,
915+
recommenderId: string,
916+
buyTimeStamp: string,
917+
isSimulation: boolean
918+
): TradePerformance | null {
919+
const tableName = isSimulation ? "simulation_trade" : "trade";
920+
const sql = `SELECT * FROM ${tableName} WHERE token_address = ? AND recommender_id = ? AND buy_timeStamp = ?;`;
921+
const row = this.db
922+
.prepare(sql)
923+
.get(tokenAddress, recommenderId, buyTimeStamp) as
924+
| TradePerformance
925+
| undefined;
926+
if (!row) return null;
927+
928+
return {
929+
token_address: row.token_address,
930+
recommender_id: row.recommender_id,
931+
buy_price: row.buy_price,
932+
sell_price: row.sell_price,
933+
buy_timeStamp: row.buy_timeStamp,
934+
sell_timeStamp: row.sell_timeStamp,
935+
buy_amount: row.buy_amount,
936+
sell_amount: row.sell_amount,
937+
buy_sol: row.buy_sol,
938+
received_sol: row.received_sol,
939+
buy_value_usd: row.buy_value_usd,
940+
sell_value_usd: row.sell_value_usd,
941+
profit_usd: row.profit_usd,
942+
profit_percent: row.profit_percent,
943+
buy_market_cap: row.buy_market_cap,
944+
sell_market_cap: row.sell_market_cap,
945+
market_cap_change: row.market_cap_change,
946+
buy_liquidity: row.buy_liquidity,
947+
sell_liquidity: row.sell_liquidity,
948+
liquidity_change: row.liquidity_change,
949+
last_updated: row.last_updated,
950+
rapidDump: row.rapidDump,
951+
};
952+
}
953+
655954
/**
656955
* Close the database connection gracefully.
657956
*/

0 commit comments

Comments
 (0)