Skip to content

Commit 616f1c4

Browse files
committed
Merge branch 'main' of https://github.com/MarcoMandar/Eliza into HEAD
2 parents 9d9c30d + 77dab6e commit 616f1c4

File tree

6 files changed

+1052
-670
lines changed

6 files changed

+1052
-670
lines changed

packages/agent/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,12 @@ const startAgents = async () => {
280280

281281
function chat() {
282282
const agentId = characters[0].name ?? "Agent";
283-
rl.question("You: ", (input) => handleUserInput(input, agentId));
283+
rl.question("You: ", async (input) => {
284+
await handleUserInput(input, agentId);
285+
if (input.toLowerCase() !== "exit") {
286+
chat(); // Loop back to ask another question
287+
}
288+
});
284289
}
285290

286291
console.log("Chat started. Type 'exit' to quit.");
@@ -298,6 +303,7 @@ const rl = readline.createInterface({
298303
});
299304

300305
async function handleUserInput(input, agentId) {
306+
console.log("handleUserInput", input, agentId);
301307
if (input.toLowerCase() === "exit") {
302308
rl.close();
303309
return;

packages/client-auto/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"@ai16z/eliza": "workspace:*",
99
"@ai16z/plugin-image-generation": "workspace:*",
10+
"@ai16z/plugin-solana": "workspace:*",
1011
"@types/body-parser": "1.19.5",
1112
"@types/cors": "2.8.17",
1213
"@types/express": "5.0.0",

packages/client-auto/src/index.ts

+46-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,65 @@
1-
import {
2-
Client,
3-
IAgentRuntime
4-
} from "@ai16z/eliza/src/types.ts";
5-
1+
import { Client, IAgentRuntime } from "@ai16z/eliza/src/types.ts";
2+
import { TrustScoreManager } from "@ai16z/plugin-solana/src/providers/trustScoreProvider.ts";
3+
import { TokenProvider } from "@ai16z/plugin-solana/src/providers/token.ts";
4+
import { WalletProvider } from "@ai16z/plugin-solana/src/providers/wallet.ts";
5+
import { TrustScoreDatabase } from "@ai16z/plugin-solana/src/adapters/trustScoreDatabase.ts";
6+
import { Connection, PublicKey } from "@solana/web3.js";
67

78
export class AutoClient {
89
interval: NodeJS.Timeout;
910
runtime: IAgentRuntime;
11+
trustScoreProvider: TrustScoreManager;
12+
walletProvider: WalletProvider;
1013

1114
constructor(runtime: IAgentRuntime) {
1215
this.runtime = runtime;
13-
// start a loop that runs every x seconds
14-
this.interval = setInterval(() => {
15-
this.makeTrades();
16-
}, 60 * 60 * 1000); // 1 hour in milliseconds
1716

17+
const trustScoreDb = new TrustScoreDatabase(runtime.databaseAdapter.db);
18+
this.trustScoreProvider = new TrustScoreManager(null, trustScoreDb);
19+
this.walletProvider = new WalletProvider(
20+
new Connection(runtime.getSetting("RPC_URL")),
21+
new PublicKey(runtime.getSetting("WALLET_PUBLIC_KEY"))
22+
);
23+
24+
// start a loop that runs every x seconds
25+
this.interval = setInterval(
26+
async () => {
27+
await this.makeTrades();
28+
},
29+
60 * 60 * 1000
30+
); // 1 hour in milliseconds
1831
}
1932

20-
makeTrades() {
33+
async makeTrades() {
2134
console.log("Running auto loop");
2235

2336
// malibu todos
37+
const startDate = new Date(new Date().getTime() - 60 * 60 * 1000);
38+
const endDate = new Date();
39+
// get recommendations from the last hour (or whatever time period we want) in order desc by trust score
40+
const recommendations =
41+
await this.trustScoreProvider.getRecommendations(
42+
startDate,
43+
endDate
44+
);
2445
// get high trust recommendations
46+
const highTrustRecommendations = recommendations.filter(
47+
(r) => r.averageTrustScore > 0.7
48+
);
2549

2650
// get information for all tokens which were recommended
51+
const tokenInfos = highTrustRecommendations.map(
52+
async (highTrustRecommendation) => {
53+
const tokenProvider = new TokenProvider(
54+
highTrustRecommendation.tokenAddress,
55+
this.walletProvider
56+
);
57+
const tokenInfo = await tokenProvider.getProcessedTokenData();
58+
const shouldTrade = await tokenProvider.shouldTradeToken();
59+
return { tokenInfo, shouldTrade };
60+
}
61+
);
62+
2763
// get any additional information we might need
2864
// make sure we're looking at the right tokens and data
2965

packages/plugin-solana/src/evaluators/trust.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
233233
}
234234

235235
// TODO: is this is a buy, sell, dont buy, or dont sell?
236-
const shouldTrade = await this.tokenProvider.shouldTrade();
236+
const shouldTrade = await this.tokenProvider.shouldTradeToken();
237237

238238
if (!shouldTrade) {
239239
console.warn(

packages/plugin-solana/src/providers/trustScoreProvider.ts

+111
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
RecommenderMetrics,
1616
TokenPerformance,
1717
TradePerformance,
18+
TokenRecommendation,
1819
} from "../adapters/trustScoreDatabase.ts";
1920
import settings from "@ai16z/eliza/src/settings.ts";
2021
import {
@@ -33,6 +34,26 @@ interface sellDetails {
3334
sell_amount: number;
3435
sell_recommender_id: string | null;
3536
}
37+
interface RecommendationGroup {
38+
recommendation: any;
39+
trustScore: number;
40+
}
41+
42+
interface RecommenderData {
43+
recommenderId: string;
44+
trustScore: number;
45+
riskScore: number;
46+
consistencyScore: number;
47+
recommenderMetrics: RecommenderMetrics;
48+
}
49+
50+
interface TokenRecommendationSummary {
51+
tokenAddress: string;
52+
averageTrustScore: number;
53+
averageRiskScore: number;
54+
averageConsistencyScore: number;
55+
recommenders: RecommenderData[];
56+
}
3657
export class TrustScoreManager {
3758
private tokenProvider: TokenProvider;
3859
private trustScoreDb: TrustScoreDatabase;
@@ -436,6 +457,96 @@ export class TrustScoreManager {
436457
);
437458
return sellDetailsData;
438459
}
460+
461+
// get all recommendations
462+
async getRecommendations(
463+
startDate: Date,
464+
endDate: Date
465+
): Promise<Array<TokenRecommendationSummary>> {
466+
const recommendations = this.trustScoreDb.getRecommendationsByDateRange(
467+
startDate,
468+
endDate
469+
);
470+
471+
// Group recommendations by tokenAddress
472+
const groupedRecommendations = recommendations.reduce(
473+
(acc, recommendation) => {
474+
const { tokenAddress } = recommendation;
475+
if (!acc[tokenAddress]) acc[tokenAddress] = [];
476+
acc[tokenAddress].push(recommendation);
477+
return acc;
478+
},
479+
{} as Record<string, Array<TokenRecommendation>>
480+
);
481+
482+
const result = Object.keys(groupedRecommendations).map(
483+
(tokenAddress) => {
484+
const tokenRecommendations =
485+
groupedRecommendations[tokenAddress];
486+
487+
// Initialize variables to compute averages
488+
let totalTrustScore = 0;
489+
let totalRiskScore = 0;
490+
let totalConsistencyScore = 0;
491+
const recommenderData = [];
492+
493+
tokenRecommendations.forEach((recommendation) => {
494+
const tokenPerformance =
495+
this.trustScoreDb.getTokenPerformance(
496+
recommendation.tokenAddress
497+
);
498+
const recommenderMetrics =
499+
this.trustScoreDb.getRecommenderMetrics(
500+
recommendation.recommenderId
501+
);
502+
503+
const trustScore = this.calculateTrustScore(
504+
tokenPerformance,
505+
recommenderMetrics
506+
);
507+
const consistencyScore = this.calculateConsistencyScore(
508+
tokenPerformance,
509+
recommenderMetrics
510+
);
511+
const riskScore = this.calculateRiskScore(tokenPerformance);
512+
513+
// Accumulate scores for averaging
514+
totalTrustScore += trustScore;
515+
totalRiskScore += riskScore;
516+
totalConsistencyScore += consistencyScore;
517+
518+
recommenderData.push({
519+
recommenderId: recommendation.recommenderId,
520+
trustScore,
521+
riskScore,
522+
consistencyScore,
523+
recommenderMetrics,
524+
});
525+
});
526+
527+
// Calculate averages for this token
528+
const averageTrustScore =
529+
totalTrustScore / tokenRecommendations.length;
530+
const averageRiskScore =
531+
totalRiskScore / tokenRecommendations.length;
532+
const averageConsistencyScore =
533+
totalConsistencyScore / tokenRecommendations.length;
534+
535+
return {
536+
tokenAddress,
537+
averageTrustScore,
538+
averageRiskScore,
539+
averageConsistencyScore,
540+
recommenders: recommenderData,
541+
};
542+
}
543+
);
544+
545+
// Sort recommendations by the highest average trust score
546+
result.sort((a, b) => b.averageTrustScore - a.averageTrustScore);
547+
548+
return result;
549+
}
439550
}
440551

441552
export const trustScoreProvider: Provider = {

0 commit comments

Comments
 (0)