Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/MarcoMandar/Eliza into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Nov 12, 2024
2 parents 9d9c30d + 77dab6e commit 616f1c4
Show file tree
Hide file tree
Showing 6 changed files with 1,052 additions and 670 deletions.
8 changes: 7 additions & 1 deletion packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ const startAgents = async () => {

function chat() {
const agentId = characters[0].name ?? "Agent";
rl.question("You: ", (input) => handleUserInput(input, agentId));
rl.question("You: ", async (input) => {
await handleUserInput(input, agentId);
if (input.toLowerCase() !== "exit") {
chat(); // Loop back to ask another question
}
});
}

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

async function handleUserInput(input, agentId) {
console.log("handleUserInput", input, agentId);
if (input.toLowerCase() === "exit") {
rl.close();
return;
Expand Down
1 change: 1 addition & 0 deletions packages/client-auto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@ai16z/eliza": "workspace:*",
"@ai16z/plugin-image-generation": "workspace:*",
"@ai16z/plugin-solana": "workspace:*",
"@types/body-parser": "1.19.5",
"@types/cors": "2.8.17",
"@types/express": "5.0.0",
Expand Down
56 changes: 46 additions & 10 deletions packages/client-auto/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
import {
Client,
IAgentRuntime
} from "@ai16z/eliza/src/types.ts";

import { Client, IAgentRuntime } from "@ai16z/eliza/src/types.ts";
import { TrustScoreManager } from "@ai16z/plugin-solana/src/providers/trustScoreProvider.ts";
import { TokenProvider } from "@ai16z/plugin-solana/src/providers/token.ts";
import { WalletProvider } from "@ai16z/plugin-solana/src/providers/wallet.ts";
import { TrustScoreDatabase } from "@ai16z/plugin-solana/src/adapters/trustScoreDatabase.ts";
import { Connection, PublicKey } from "@solana/web3.js";

export class AutoClient {
interval: NodeJS.Timeout;
runtime: IAgentRuntime;
trustScoreProvider: TrustScoreManager;
walletProvider: WalletProvider;

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

const trustScoreDb = new TrustScoreDatabase(runtime.databaseAdapter.db);
this.trustScoreProvider = new TrustScoreManager(null, trustScoreDb);
this.walletProvider = new WalletProvider(
new Connection(runtime.getSetting("RPC_URL")),
new PublicKey(runtime.getSetting("WALLET_PUBLIC_KEY"))
);

// start a loop that runs every x seconds
this.interval = setInterval(
async () => {
await this.makeTrades();
},
60 * 60 * 1000
); // 1 hour in milliseconds
}

makeTrades() {
async makeTrades() {
console.log("Running auto loop");

// malibu todos
const startDate = new Date(new Date().getTime() - 60 * 60 * 1000);
const endDate = new Date();
// get recommendations from the last hour (or whatever time period we want) in order desc by trust score
const recommendations =
await this.trustScoreProvider.getRecommendations(
startDate,
endDate
);
// get high trust recommendations
const highTrustRecommendations = recommendations.filter(
(r) => r.averageTrustScore > 0.7
);

// get information for all tokens which were recommended
const tokenInfos = highTrustRecommendations.map(
async (highTrustRecommendation) => {
const tokenProvider = new TokenProvider(
highTrustRecommendation.tokenAddress,
this.walletProvider
);
const tokenInfo = await tokenProvider.getProcessedTokenData();
const shouldTrade = await tokenProvider.shouldTradeToken();
return { tokenInfo, shouldTrade };
}
);

// get any additional information we might need
// make sure we're looking at the right tokens and data

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-solana/src/evaluators/trust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
}

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

if (!shouldTrade) {
console.warn(
Expand Down
111 changes: 111 additions & 0 deletions packages/plugin-solana/src/providers/trustScoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RecommenderMetrics,
TokenPerformance,
TradePerformance,
TokenRecommendation,
} from "../adapters/trustScoreDatabase.ts";
import settings from "@ai16z/eliza/src/settings.ts";
import {
Expand All @@ -33,6 +34,26 @@ interface sellDetails {
sell_amount: number;
sell_recommender_id: string | null;
}
interface RecommendationGroup {
recommendation: any;
trustScore: number;
}

interface RecommenderData {
recommenderId: string;
trustScore: number;
riskScore: number;
consistencyScore: number;
recommenderMetrics: RecommenderMetrics;
}

interface TokenRecommendationSummary {
tokenAddress: string;
averageTrustScore: number;
averageRiskScore: number;
averageConsistencyScore: number;
recommenders: RecommenderData[];
}
export class TrustScoreManager {
private tokenProvider: TokenProvider;
private trustScoreDb: TrustScoreDatabase;
Expand Down Expand Up @@ -436,6 +457,96 @@ export class TrustScoreManager {
);
return sellDetailsData;
}

// get all recommendations
async getRecommendations(
startDate: Date,
endDate: Date
): Promise<Array<TokenRecommendationSummary>> {
const recommendations = this.trustScoreDb.getRecommendationsByDateRange(
startDate,
endDate
);

// Group recommendations by tokenAddress
const groupedRecommendations = recommendations.reduce(
(acc, recommendation) => {
const { tokenAddress } = recommendation;
if (!acc[tokenAddress]) acc[tokenAddress] = [];
acc[tokenAddress].push(recommendation);
return acc;
},
{} as Record<string, Array<TokenRecommendation>>
);

const result = Object.keys(groupedRecommendations).map(
(tokenAddress) => {
const tokenRecommendations =
groupedRecommendations[tokenAddress];

// Initialize variables to compute averages
let totalTrustScore = 0;
let totalRiskScore = 0;
let totalConsistencyScore = 0;
const recommenderData = [];

tokenRecommendations.forEach((recommendation) => {
const tokenPerformance =
this.trustScoreDb.getTokenPerformance(
recommendation.tokenAddress
);
const recommenderMetrics =
this.trustScoreDb.getRecommenderMetrics(
recommendation.recommenderId
);

const trustScore = this.calculateTrustScore(
tokenPerformance,
recommenderMetrics
);
const consistencyScore = this.calculateConsistencyScore(
tokenPerformance,
recommenderMetrics
);
const riskScore = this.calculateRiskScore(tokenPerformance);

// Accumulate scores for averaging
totalTrustScore += trustScore;
totalRiskScore += riskScore;
totalConsistencyScore += consistencyScore;

recommenderData.push({
recommenderId: recommendation.recommenderId,
trustScore,
riskScore,
consistencyScore,
recommenderMetrics,
});
});

// Calculate averages for this token
const averageTrustScore =
totalTrustScore / tokenRecommendations.length;
const averageRiskScore =
totalRiskScore / tokenRecommendations.length;
const averageConsistencyScore =
totalConsistencyScore / tokenRecommendations.length;

return {
tokenAddress,
averageTrustScore,
averageRiskScore,
averageConsistencyScore,
recommenders: recommenderData,
};
}
);

// Sort recommendations by the highest average trust score
result.sort((a, b) => b.averageTrustScore - a.averageTrustScore);

return result;
}
}

export const trustScoreProvider: Provider = {
Expand Down
Loading

0 comments on commit 616f1c4

Please sign in to comment.