Skip to content

Commit

Permalink
fix: plugin-quai (#2892)
Browse files Browse the repository at this point in the history
* fixed all logical and typing issue

* feat: use elizaLogger

---------

Co-authored-by: Shakker Nerd <[email protected]>
  • Loading branch information
AIFlowML and shakkernerd authored Jan 28, 2025
1 parent 8ba956f commit b35c2f0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
61 changes: 27 additions & 34 deletions packages/plugin-quai/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type Action,
composeContext,
generateObject,
elizaLogger,
} from "@elizaos/core";
import {
getQuaiAccount,
Expand Down Expand Up @@ -46,7 +47,7 @@ export default {
"PAY_ON_QUAI",
],
// eslint-disable-next-line
validate: async (runtime: IAgentRuntime, message: Memory) => {
validate: async (runtime: IAgentRuntime, _message: Memory) => {
return validateSettings(runtime);
},
description:
Expand All @@ -58,18 +59,16 @@ export default {
_options: { [key: string]: unknown },
callback?: HandlerCallback
): Promise<boolean> => {
console.log("Starting TRANSFER_TOKEN handler...");
elizaLogger.log("Starting TRANSFER_TOKEN handler...");

// Initialize or update state
if (!state) {
state = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
}
const currentState = !state
? await runtime.composeState(message)
: await runtime.updateRecentMessageState(state);

// Compose transfer context
const transferContext = composeContext({
state,
state: currentState,
template: transferTemplate,
});

Expand All @@ -80,11 +79,11 @@ export default {
modelClass: ModelClass.MEDIUM,
});

console.log("Transfer content:", content);
elizaLogger.debug("Transfer content:", content);

// Validate transfer content
if (!isTransferContent(content)) {
console.error("Invalid content for TRANSFER_TOKEN action.");
elizaLogger.error("Invalid content for TRANSFER_TOKEN action.");
if (callback) {
callback({
text: "Not enough information to transfer tokens. Please respond with token address, recipient, and amount.",
Expand All @@ -96,44 +95,38 @@ export default {

try {
const account = getQuaiAccount(runtime);
const amount = formatUnits(content.amount, "wei");

var txObj: TransactionRequest = {};
if (content.tokenAddress) {
// TODO: transfer QRC20s
} else {
txObj = {
to: content.recipient,
const amount = formatUnits(content.amount, "wei");

// Declare transaction object at function scope
const txObj: TransactionRequest = content.tokenAddress
? {} // TODO: transfer QRC20s
: {
to: content.recipient,
value: amount,
from: account.address,
};

console.log(
"Transferring",
amount,
"QUAI",
"to",
content.recipient
);
}
elizaLogger.log(
"Transferring",
amount,
"QUAI",
"to",
content.recipient
);

const tx = await account.sendTransaction(txObj)
const tx = await account.sendTransaction(txObj);

console.log(
"Transfer completed successfully! tx: " + tx.hash
);
elizaLogger.success(`Transfer completed successfully! tx: ${tx.hash}`);
if (callback) {
callback({
text:
"Transfer completed successfully! tx: " +
tx.hash,
text: `Transfer completed successfully! tx: ${tx.hash}`,
content: {},
});
}

return true;
} catch (error) {
console.error("Error during token transfer:", error);
elizaLogger.error("Error during token transfer:", error);
if (callback) {
callback({
text: `Error transferring tokens: ${error.message}`,
Expand Down
55 changes: 45 additions & 10 deletions packages/plugin-quai/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,62 @@ export interface TransferContent extends Content {
}

export function isTransferContent(
content: any
content: unknown
): content is TransferContent {
if (!content || typeof content !== 'object') {
return false;
}

const contentObj = content as { tokenAddress?: unknown; recipient?: unknown; amount?: unknown };

// Validate types
const validTypes =
(content.tokenAddress === null || typeof content.tokenAddress === "string") &&
typeof content.recipient === "string" &&
(typeof content.amount === "string" ||
typeof content.amount === "number");
(contentObj.tokenAddress === null || typeof contentObj.tokenAddress === "string") &&
typeof contentObj.recipient === "string" &&
(typeof contentObj.amount === "string" ||
typeof contentObj.amount === "number");
if (!validTypes) {
return false;
}

// Validate addresses (20-bytes with 0x prefix)
const recipient = contentObj.recipient as string;
const tokenAddress = contentObj.tokenAddress as string | null;

const validRecipient =
content.recipient.startsWith("0x") &&
content.recipient.length === 42;
recipient.startsWith("0x") &&
recipient.length === 42;

// If tokenAddress is provided, validate it
const validTokenAddress = content.tokenAddress === null ||
(content.tokenAddress.startsWith("0x") &&
content.tokenAddress.length === 42);
const validTokenAddress = tokenAddress === null ||
(tokenAddress.startsWith("0x") &&
tokenAddress.length === 42);

return validRecipient && validTokenAddress;
}

// export function isTransferContent(
// content: any
// ): content is TransferContent {
// // Validate types
// const validTypes =
// (content.tokenAddress === null || typeof content.tokenAddress === "string") &&
// typeof content.recipient === "string" &&
// (typeof content.amount === "string" ||
// typeof content.amount === "number");
// if (!validTypes) {
// return false;
// }

// // Validate addresses (20-bytes with 0x prefix)
// const validRecipient =
// content.recipient.startsWith("0x") &&
// content.recipient.length === 42;

// // If tokenAddress is provided, validate it
// const validTokenAddress = content.tokenAddress === null ||
// (content.tokenAddress.startsWith("0x") &&
// content.tokenAddress.length === 42);

// return validRecipient && validTokenAddress;

0 comments on commit b35c2f0

Please sign in to comment.