Skip to content

Commit

Permalink
Merge pull request #1745 from sinecose/console-to-elizaLogger
Browse files Browse the repository at this point in the history
feat(plugin-near): replace console.log to eliza logger
  • Loading branch information
monilpat authored Jan 3, 2025
2 parents 7cb04dc + 48fbcab commit cddc9ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
17 changes: 9 additions & 8 deletions packages/plugin-near/src/actions/swap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ActionExample,
HandlerCallback,
elizaLogger,
IAgentRuntime,
Memory,
ModelClass,
Expand Down Expand Up @@ -34,7 +35,7 @@ async function checkStorageBalance(
});
return balance !== null && balance.total !== "0";
} catch (error) {
console.log(`Error checking storage balance: ${error}`);
elizaLogger.log(`Error checking storage balance: ${error}`);
return false;
}
}
Expand Down Expand Up @@ -142,7 +143,7 @@ async function swapToken(

return transactions;
} catch (error) {
console.error("Error in swapToken:", error);
elizaLogger.error("Error in swapToken:", error);
throw error;
}
}
Expand Down Expand Up @@ -186,8 +187,8 @@ export const executeSwap: Action = {
"TRADE_TOKENS_NEAR",
"EXCHANGE_TOKENS_NEAR",
],
validate: async (runtime: IAgentRuntime, message: Memory) => {
console.log("Message:", message);
validate: async (_runtime: IAgentRuntime, message: Memory) => {
elizaLogger.log("Message:", message);
return true;
},
description: "Perform a token swap using Ref Finance.",
Expand Down Expand Up @@ -221,14 +222,14 @@ export const executeSwap: Action = {
modelClass: ModelClass.LARGE,
});

console.log("Response:", response);
elizaLogger.log("Response:", response);

if (
!response.inputTokenId ||
!response.outputTokenId ||
!response.amount
) {
console.log("Missing required parameters, skipping swap");
elizaLogger.log("Missing required parameters, skipping swap");
const responseMsg = {
text: "I need the input token ID, output token ID, and amount to perform the swap",
};
Expand Down Expand Up @@ -290,7 +291,7 @@ export const executeSwap: Action = {
}
}

console.log("Swap completed successfully!");
elizaLogger.log("Swap completed successfully!");
const txHashes = results.map((r) => r.transaction.hash).join(", ");

const responseMsg = {
Expand All @@ -300,7 +301,7 @@ export const executeSwap: Action = {
callback?.(responseMsg);
return true;
} catch (error) {
console.error("Error during token swap:", error);
elizaLogger.error("Error during token swap:", error);
const responseMsg = {
text: `Error during swap: ${error instanceof Error ? error.message : String(error)}`,
};
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-near/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Memory,
ModelClass,
State,
elizaLogger,
type Action,
composeContext,
generateObject,
Expand Down Expand Up @@ -132,7 +133,7 @@ export const executeTransfer: Action = {

// Validate transfer content
if (!isTransferContent(runtime, content)) {
console.error("Invalid content for TRANSFER_NEAR action.");
elizaLogger.error("Invalid content for TRANSFER_NEAR action.");
if (callback) {
callback({
text: "Unable to process transfer request. Invalid content provided.",
Expand Down Expand Up @@ -163,7 +164,7 @@ export const executeTransfer: Action = {

return true;
} catch (error) {
console.error("Error during NEAR transfer:", error);
elizaLogger.error("Error during NEAR transfer:", error);
if (callback) {
callback({
text: `Error transferring NEAR: ${error}`,
Expand Down
22 changes: 14 additions & 8 deletions packages/plugin-near/src/providers/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IAgentRuntime, Memory, Provider, State } from "@elizaos/core";
import {
IAgentRuntime,
Memory,
Provider,
State,
elizaLogger,
} from "@elizaos/core";
import { KeyPair, keyStores, connect, Account, utils } from "near-api-js";
import BigNumber from "bignumber.js";
import { KeyPairString } from "near-api-js/lib/utils";
Expand Down Expand Up @@ -51,7 +57,7 @@ export class WalletProvider implements Provider {
try {
return await this.getFormattedPortfolio(runtime);
} catch (error) {
console.error("Error in wallet provider:", error);
elizaLogger.error("Error in wallet provider:", error);
return null;
}
}
Expand Down Expand Up @@ -102,7 +108,7 @@ export class WalletProvider implements Provider {
}
return await response.json();
} catch (error) {
console.error(`Attempt ${i + 1} failed:`, error);
elizaLogger.error(`Attempt ${i + 1} failed:`, error);
lastError = error as Error;
if (i < PROVIDER_CONFIG.MAX_RETRIES - 1) {
await new Promise((resolve) =>
Expand All @@ -125,7 +131,7 @@ export class WalletProvider implements Provider {
const cachedValue = this.cache.get<WalletPortfolio>(cacheKey);

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

Expand Down Expand Up @@ -160,7 +166,7 @@ export class WalletProvider implements Provider {
this.cache.set(cacheKey, portfolio);
return portfolio;
} catch (error) {
console.error("Error fetching portfolio:", error);
elizaLogger.error("Error fetching portfolio:", error);
throw error;
}
}
Expand All @@ -181,7 +187,7 @@ export class WalletProvider implements Provider {
this.cache.set(cacheKey, price);
return price;
} catch (error) {
console.error("Error fetching NEAR price:", error);
elizaLogger.error("Error fetching NEAR price:", error);
return 0;
}
}
Expand Down Expand Up @@ -214,7 +220,7 @@ export class WalletProvider implements Provider {
const portfolio = await this.fetchPortfolioValue(runtime);
return this.formatPortfolio(runtime, portfolio);
} catch (error) {
console.error("Error generating portfolio report:", error);
elizaLogger.error("Error generating portfolio report:", error);
return "Unable to fetch wallet information. Please try again later.";
}
}
Expand All @@ -234,7 +240,7 @@ const walletProvider: Provider = {
const provider = new WalletProvider(accountId);
return await provider.getFormattedPortfolio(runtime);
} catch (error) {
console.error("Error in wallet provider:", error);
elizaLogger.error("Error in wallet provider:", error);
return null;
}
},
Expand Down

0 comments on commit cddc9ee

Please sign in to comment.