Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/simulation sell types #642

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-solana/src/actions/swapDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const executeSwapForDAO: Action = {

try {
const connection = new Connection(
"https://api.mainnet-beta.solana.com" // better if we use a better rpc
runtime.getSetting("RPC_URL") as string
);
const authority = Keypair.fromSecretKey(
Uint8Array.from(
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 @@ -147,7 +147,7 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
for (const rec of filteredRecommendations) {
// create the wallet provider and token provider
const walletProvider = new WalletProvider(
new Connection("https://api.mainnet-beta.solana.com"),
new Connection(runtime.getSetting("RPC_URL")),
new PublicKey(runtime.getSetting("WALLET_PUBLIC_KEY"))
);
const tokenProvider = new TokenProvider(
Expand Down
28 changes: 19 additions & 9 deletions packages/plugin-solana/src/providers/simulationSellingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
TrustScoreDatabase,
TokenPerformance,
// TradePerformance,
// TokenRecommendation,
ProcessedTokenData,
TokenRecommendation,
} from "@ai16z/plugin-trustdb";
import { Connection, PublicKey } from "@solana/web3.js";
// Assuming TokenProvider and IAgentRuntime are available
Expand All @@ -12,6 +11,7 @@ import { TokenProvider } from "./token.ts";
import { IAgentRuntime } from "@ai16z/eliza";
import { WalletProvider } from "./wallet.ts";
import * as amqp from "amqplib";
import { ProcessedTokenData } from "../types/token.ts";

interface SellDetails {
sell_amount: number;
Expand Down Expand Up @@ -130,7 +130,7 @@ export class SimulationSellingService {

try {
console.log(
`Executing sell for token ${tokenPerformance.tokenSymbol}: ${amountToSell}`
`Executing sell for token ${tokenPerformance.symbol}: ${amountToSell}`
);

// Update the sell details
Expand All @@ -148,7 +148,7 @@ export class SimulationSellingService {
// Update sell details in the database
const sellDetailsData = await this.updateSellDetails(
tokenAddress,
tokenPerformance.recommenderId,
sell_recommender_id,
sellTimeStamp,
sellDetails,
true, // isSimulation
Expand Down Expand Up @@ -206,15 +206,21 @@ export class SimulationSellingService {
);
// const shouldTrade = await tokenProvider.shouldTradeToken();
// if (shouldTrade) {
const tokenRecommendations: TokenRecommendation[] =
this.trustScoreDb.getRecommendationsByToken(
tokenPerformance.tokenAddress
);
const tokenRecommendation: TokenRecommendation =
tokenRecommendations[0];
const balance = tokenPerformance.balance;
const sell_recommender_id = tokenPerformance.recommenderId;
const sell_recommender_id = tokenRecommendation.recommenderId;
const tokenAddress = tokenPerformance.tokenAddress;
const process = await this.startProcessInTheSonarBackend(
tokenAddress,
balance,
true,
sell_recommender_id,
tokenPerformance.initial_mc
tokenPerformance.initialMarketCap
);
if (process) {
this.runningProcesses.add(tokenAddress);
Expand All @@ -223,7 +229,10 @@ export class SimulationSellingService {
});
}

public processTokenPerformance(tokenAddress: string) {
public processTokenPerformance(
tokenAddress: string,
recommenderId: string
) {
try {
const runningProcesses = this.runningProcesses;
// check if token is already being processed
Expand All @@ -233,20 +242,21 @@ export class SimulationSellingService {
}
const tokenPerformance =
this.trustScoreDb.getTokenPerformance(tokenAddress);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const tokenProvider = new TokenProvider(
tokenPerformance.tokenAddress,
this.walletProvider,
this.runtime.cacheManager
);
const balance = tokenPerformance.balance;
const sell_recommender_id = tokenPerformance.recommenderId;
const sell_recommender_id = recommenderId;
const process = this.startProcessInTheSonarBackend(
tokenAddress,
balance,
true,
sell_recommender_id,
tokenPerformance.initial_mc
tokenPerformance.initialMarketCap
);
if (process) {
this.runningProcesses.add(tokenAddress);
Expand Down
10 changes: 7 additions & 3 deletions packages/plugin-solana/src/providers/trustScoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class TrustScoreManager {
this.backendToken = runtime.getSetting("BACKEND_TOKEN");
this.simulationSellingService = new SimulationSellingService(
runtime,
this.tokenProvider
this.trustScoreDb
);
}

Expand Down Expand Up @@ -416,6 +416,7 @@ export class TrustScoreManager {

this.trustScoreDb.upsertTokenPerformance({
tokenAddress: tokenAddress,
symbol: processedData.tokenCodex.symbol,
priceChange24h: processedData.tradeData.price_change_24h_percent,
volumeChange24h: processedData.tradeData.volume_24h,
trade_24h_change: processedData.tradeData.trade_24h_change_percent,
Expand Down Expand Up @@ -453,7 +454,10 @@ export class TrustScoreManager {
};
this.trustScoreDb.addTransaction(transaction);
}
this.simulationSellingService.processTokenPerformance(tokenAddress);
this.simulationSellingService.processTokenPerformance(
tokenAddress,
recommenderId
);
// api call to update trade performance
this.createTradeInBe(tokenAddress, recommenderId, username, data);
return creationData;
Expand Down Expand Up @@ -531,7 +535,7 @@ export class TrustScoreManager {
const processedData: ProcessedTokenData =
await this.tokenProvider.getProcessedTokenData();
const wallet = new WalletProvider(
new Connection("https://api.mainnet-beta.solana.com"),
this.connection,
new PublicKey(Wallet!)
);
const prices = await wallet.fetchPrices(runtime);
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-trustdb/src/adapters/trustScoreDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RecommenderMetrics {

export interface TokenPerformance {
tokenAddress: string;
symbol: string;
priceChange24h: number;
volumeChange24h: number;
trade_24h_change: number;
Expand Down Expand Up @@ -109,6 +110,7 @@ interface RecommenderMetricsRow {

interface TokenPerformanceRow {
token_address: string;
symbol: string;
price_change_24h: number;
volume_change_24h: number;
trade_24h_change: number;
Expand Down Expand Up @@ -193,6 +195,7 @@ export class TrustScoreDatabase {
this.db.exec(`
CREATE TABLE IF NOT EXISTS token_performance (
token_address TEXT PRIMARY KEY,
symbol TEXT,
price_change_24h REAL,
volume_change_24h REAL,
trade_24h_change REAL,
Expand Down Expand Up @@ -818,6 +821,7 @@ export class TrustScoreDatabase {

return {
tokenAddress: row.token_address,
symbol: row.symbol,
priceChange24h: row.price_change_24h,
volumeChange24h: row.volume_change_24h,
trade_24h_change: row.trade_24h_change,
Expand Down Expand Up @@ -852,6 +856,7 @@ export class TrustScoreDatabase {

return rows.map((row) => ({
tokenAddress: row.token_address,
symbol: row.symbol,
priceChange24h: row.price_change_24h,
volumeChange24h: row.volume_change_24h,
trade_24h_change: row.trade_24h_change,
Expand Down
Loading
Loading