Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugin-conflux): output detailed invalid content #1602

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions packages/plugin-conflux/src/actions/confiPump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Memory,
State,
HandlerCallback,
elizaLogger,
} from "@elizaos/core";
import { generateObject, composeContext, ModelClass } from "@elizaos/core";
import {
Expand Down Expand Up @@ -38,7 +39,7 @@ async function ensureAllowance(
memeAddress: `0x${string}`,
amount: bigint
) {
console.log(
elizaLogger.log(
`Checking allowance: token: ${tokenAddress} meme: ${memeAddress} amount: ${amount}`
);

Expand All @@ -54,10 +55,10 @@ async function ensureAllowance(
args: [account.address, memeAddress],
});

console.log("allowance:", allowance);
elizaLogger.log("allowance:", allowance);

if (allowance < amount) {
console.log(
elizaLogger.log(
`allowance(${allowance}) is less than amount(${amount}), approving...`
);

Expand All @@ -73,11 +74,11 @@ async function ensureAllowance(
kzg: null,
});

console.log(`Approving hash: ${hash}`);
elizaLogger.log(`Approving hash: ${hash}`);
await publicClient.waitForTransactionReceipt({ hash });
console.log(`Approving success: ${hash}`);
elizaLogger.log(`Approving success: ${hash}`);
} else {
console.log(`No need to approve`);
elizaLogger.log(`No need to approve`);
}
}

Expand Down Expand Up @@ -213,9 +214,13 @@ export const confiPump: Action = {
switch (contentObject.action) {
case "CREATE_TOKEN":
if (!isPumpCreateContent(contentObject)) {
throw new Error("Invalid content");
elizaLogger.error(
"Invalid PumpCreateContent: ",
contentObject
);
throw new Error("Invalid PumpCreateContent");
}
console.log(
elizaLogger.log(
"creating: ",
contentObject.params.name,
contentObject.params.symbol,
Expand All @@ -235,13 +240,17 @@ export const confiPump: Action = {

case "BUY_TOKEN":
if (!isPumpBuyContent(contentObject)) {
throw new Error("Invalid content");
elizaLogger.error(
"Invalid PumpBuyContent: ",
contentObject
);
throw new Error("Invalid PumpBuyContent");
}
value = parseUnits(
contentObject.params.value.toString(),
18
);
console.log(
elizaLogger.log(
"buying: ",
contentObject.params.tokenAddress,
value
Expand All @@ -260,12 +269,16 @@ export const confiPump: Action = {

case "SELL_TOKEN":
if (!isPumpSellContent(contentObject)) {
throw new Error("Invalid content");
elizaLogger.error(
"Invalid PumpSellContent: ",
contentObject
);
throw new Error("Invalid PumpSellContent");
}
const tokenAddress = getAddress(
contentObject.params.tokenAddress as `0x${string}`
);
console.log(
elizaLogger.log(
"selling: ",
tokenAddress,
account.address,
Expand Down Expand Up @@ -312,7 +325,7 @@ export const confiPump: Action = {
value,
account,
});
console.log("simulate: ", simulate);
elizaLogger.log("simulate: ", simulate);

const hash = await walletClient.sendTransaction({
account,
Expand All @@ -332,7 +345,7 @@ export const confiPump: Action = {
});
}
} catch (error) {
console.error(`Error performing the action: ${error}`);
elizaLogger.error(`Error performing the action: ${error}`);
if (callback) {
callback({
text: `Failed to perform the action: ${content.object.action}: ${error}`,
Expand Down
18 changes: 3 additions & 15 deletions packages/plugin-conflux/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,13 @@ export function isPumpContent(object: any): object is PumpContent {
}

export function isPumpCreateContent(object: any): object is PumpCreateContent {
if (PumpCreateSchema.safeParse(object).success) {
return true;
}
console.error("Invalid content: ", object);
return false;
return PumpCreateSchema.safeParse(object).success;
}

export function isPumpBuyContent(object: any): object is PumpBuyContent {
if (PumpBuySchema.safeParse(object).success) {
return true;
}
console.error("Invalid content: ", object);
return false;
return PumpBuySchema.safeParse(object).success;
}

export function isPumpSellContent(object: any): object is PumpSellContent {
if (PumpSellSchema.safeParse(object).success) {
return true;
}
console.error("Invalid content: ", object);
return false;
return PumpSellSchema.safeParse(object).success;
}
Loading