Skip to content

Commit

Permalink
calculateBuyAmounts fix and add cache to wallet provider
Browse files Browse the repository at this point in the history
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
  • Loading branch information
MarcoMandar committed Nov 16, 2024
1 parent ce9ca84 commit 56e4589
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/plugin-solana/src/evaluators/trust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,11 @@ async function handler(runtime: IAgentRuntime, message: Memory) {

console.log("recommendationsManager", rec);


// - from here we just need to make sure code is right

// buy, dont buy, sell, dont sell

const buyAmounts = await tokenProvider.getBuyAmounts();
const buyAmounts = await tokenProvider.calculateBuyAmounts();

let buyAmount = buyAmounts[rec.conviction.toLowerCase().trim()];
if (!buyAmount) {
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-solana/src/providers/trustScoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export class TrustScoreManager {
this.tokenProvider = tokenProvider;
this.trustScoreDb = trustScoreDb;
this.connection = new Connection(runtime.getSetting("RPC_URL"));
this.baseMint = new PublicKey(runtime.getSetting("BASE_MINT") || "So11111111111111111111111111111111111111112");
this.baseMint = new PublicKey(
runtime.getSetting("BASE_MINT") ||
"So11111111111111111111111111111111111111112"
);
this.backend = runtime.getSetting("BACKEND_URL");
this.backendToken = runtime.getSetting("BACKEND_TOKEN");
}
Expand Down
32 changes: 28 additions & 4 deletions packages/plugin-solana/src/providers/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IAgentRuntime, Memory, Provider, State } from "@ai16z/eliza";
import { Connection, PublicKey } from "@solana/web3.js";
import BigNumber from "bignumber.js";

import NodeCache from "node-cache";
// Provider configuration
const PROVIDER_CONFIG = {
BIRDEYE_API: "https://public-api.birdeye.so",
Expand Down Expand Up @@ -49,10 +49,14 @@ interface Prices {
}

export class WalletProvider {
private cache: NodeCache;

constructor(
private connection: Connection,
private walletPublicKey: PublicKey
) {}
) {
this.cache = new NodeCache({ stdTTL: 300 }); // Cache TTL set to 5 minutes
}

private async fetchWithRetry(
runtime,
Expand Down Expand Up @@ -103,6 +107,15 @@ export class WalletProvider {

async fetchPortfolioValue(runtime): Promise<WalletPortfolio> {
try {
const cacheKey = `portfolio-${this.walletPublicKey.toBase58()}`;
const cachedValue = this.cache.get<WalletPortfolio>(cacheKey);

if (cachedValue) {
console.log("Cache hit for fetchPortfolioValue");
return cachedValue;
}
console.log("Cache miss for fetchPortfolioValue");

const walletData = await this.fetchWithRetry(
runtime,
`${PROVIDER_CONFIG.BIRDEYE_API}/v1/wallet/token_list?wallet=${this.walletPublicKey.toBase58()}`
Expand Down Expand Up @@ -130,8 +143,7 @@ export class WalletProvider {
}));

const totalSol = totalUsd.div(solPriceInUSD);

return {
const portfolio = {
totalUsd: totalUsd.toString(),
totalSol: totalSol.toFixed(6),
items: items.sort((a, b) =>
Expand All @@ -140,6 +152,8 @@ export class WalletProvider {
.toNumber()
),
};
this.cache.set(cacheKey, portfolio);
return portfolio;
} catch (error) {
console.error("Error fetching portfolio:", error);
throw error;
Expand All @@ -148,6 +162,15 @@ export class WalletProvider {

async fetchPrices(runtime): Promise<Prices> {
try {
const cacheKey = "prices";
const cachedValue = this.cache.get<Prices>(cacheKey);

if (cachedValue) {
console.log("Cache hit for fetchPrices");
return cachedValue;
}
console.log("Cache miss for fetchPrices");

const { SOL, BTC, ETH } = PROVIDER_CONFIG.TOKEN_ADDRESSES;
const tokens = [SOL, BTC, ETH];
const prices: Prices = {
Expand Down Expand Up @@ -181,6 +204,7 @@ export class WalletProvider {
}
}

this.cache.set(cacheKey, prices);
return prices;
} catch (error) {
console.error("Error fetching prices:", error);
Expand Down

0 comments on commit 56e4589

Please sign in to comment.