Skip to content

Commit

Permalink
Merge pull request elizaOS#1602 from bendanzhentan/refactor-output-co…
Browse files Browse the repository at this point in the history
…nflux-errors

refactor(plugin-conflux): output detailed invalid content
  • Loading branch information
shakkernerd authored Dec 31, 2024
2 parents 0230d2f + 361140b commit a1e0f60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 98 deletions.
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;
}
73 changes: 4 additions & 69 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1e0f60

Please sign in to comment.