Skip to content

Commit

Permalink
fixed all logical and typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AIFlowML committed Jan 28, 2025
1 parent 504056d commit 4ce6bb4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 40 deletions.
52 changes: 22 additions & 30 deletions packages/plugin-quai/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 @@ -61,15 +61,13 @@ export default {
console.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 @@ -96,37 +94,31 @@ 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
);
}

const tx = await account.sendTransaction(txObj)

console.log(
"Transfer completed successfully! tx: " + tx.hash
"Transferring",
amount,
"QUAI",
"to",
content.recipient
);

const tx = await account.sendTransaction(txObj);

console.log(`Transfer completed successfully! tx: ${tx.hash}`);
if (callback) {
callback({
text:
"Transfer completed successfully! tx: " +
tx.hash,
text: `Transfer completed successfully! tx: ${tx.hash}`,
content: {},
});
}
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 4ce6bb4

Please sign in to comment.