Skip to content

Commit 56e4589

Browse files
committed
calculateBuyAmounts fix and add cache to wallet provider
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
1 parent ce9ca84 commit 56e4589

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,11 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
222222

223223
console.log("recommendationsManager", rec);
224224

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

228227
// buy, dont buy, sell, dont sell
229228

230-
const buyAmounts = await tokenProvider.getBuyAmounts();
229+
const buyAmounts = await tokenProvider.calculateBuyAmounts();
231230

232231
let buyAmount = buyAmounts[rec.conviction.toLowerCase().trim()];
233232
if (!buyAmount) {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export class TrustScoreManager {
6666
this.tokenProvider = tokenProvider;
6767
this.trustScoreDb = trustScoreDb;
6868
this.connection = new Connection(runtime.getSetting("RPC_URL"));
69-
this.baseMint = new PublicKey(runtime.getSetting("BASE_MINT") || "So11111111111111111111111111111111111111112");
69+
this.baseMint = new PublicKey(
70+
runtime.getSetting("BASE_MINT") ||
71+
"So11111111111111111111111111111111111111112"
72+
);
7073
this.backend = runtime.getSetting("BACKEND_URL");
7174
this.backendToken = runtime.getSetting("BACKEND_TOKEN");
7275
}

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

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IAgentRuntime, Memory, Provider, State } from "@ai16z/eliza";
22
import { Connection, PublicKey } from "@solana/web3.js";
33
import BigNumber from "bignumber.js";
4-
4+
import NodeCache from "node-cache";
55
// Provider configuration
66
const PROVIDER_CONFIG = {
77
BIRDEYE_API: "https://public-api.birdeye.so",
@@ -49,10 +49,14 @@ interface Prices {
4949
}
5050

5151
export class WalletProvider {
52+
private cache: NodeCache;
53+
5254
constructor(
5355
private connection: Connection,
5456
private walletPublicKey: PublicKey
55-
) {}
57+
) {
58+
this.cache = new NodeCache({ stdTTL: 300 }); // Cache TTL set to 5 minutes
59+
}
5660

5761
private async fetchWithRetry(
5862
runtime,
@@ -103,6 +107,15 @@ export class WalletProvider {
103107

104108
async fetchPortfolioValue(runtime): Promise<WalletPortfolio> {
105109
try {
110+
const cacheKey = `portfolio-${this.walletPublicKey.toBase58()}`;
111+
const cachedValue = this.cache.get<WalletPortfolio>(cacheKey);
112+
113+
if (cachedValue) {
114+
console.log("Cache hit for fetchPortfolioValue");
115+
return cachedValue;
116+
}
117+
console.log("Cache miss for fetchPortfolioValue");
118+
106119
const walletData = await this.fetchWithRetry(
107120
runtime,
108121
`${PROVIDER_CONFIG.BIRDEYE_API}/v1/wallet/token_list?wallet=${this.walletPublicKey.toBase58()}`
@@ -130,8 +143,7 @@ export class WalletProvider {
130143
}));
131144

132145
const totalSol = totalUsd.div(solPriceInUSD);
133-
134-
return {
146+
const portfolio = {
135147
totalUsd: totalUsd.toString(),
136148
totalSol: totalSol.toFixed(6),
137149
items: items.sort((a, b) =>
@@ -140,6 +152,8 @@ export class WalletProvider {
140152
.toNumber()
141153
),
142154
};
155+
this.cache.set(cacheKey, portfolio);
156+
return portfolio;
143157
} catch (error) {
144158
console.error("Error fetching portfolio:", error);
145159
throw error;
@@ -148,6 +162,15 @@ export class WalletProvider {
148162

149163
async fetchPrices(runtime): Promise<Prices> {
150164
try {
165+
const cacheKey = "prices";
166+
const cachedValue = this.cache.get<Prices>(cacheKey);
167+
168+
if (cachedValue) {
169+
console.log("Cache hit for fetchPrices");
170+
return cachedValue;
171+
}
172+
console.log("Cache miss for fetchPrices");
173+
151174
const { SOL, BTC, ETH } = PROVIDER_CONFIG.TOKEN_ADDRESSES;
152175
const tokens = [SOL, BTC, ETH];
153176
const prices: Prices = {
@@ -181,6 +204,7 @@ export class WalletProvider {
181204
}
182205
}
183206

207+
this.cache.set(cacheKey, prices);
184208
return prices;
185209
} catch (error) {
186210
console.error("Error fetching prices:", error);

0 commit comments

Comments
 (0)