Skip to content

Commit

Permalink
Merge pull request elizaOS#2017 from mgavrila/fix/amg/multiversx_plugin
Browse files Browse the repository at this point in the history
fix: fix multiversx-plugin
  • Loading branch information
wtfsayo authored Jan 9, 2025
2 parents b0bfa47 + 56cf7a0 commit 1d5d74a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
23 changes: 13 additions & 10 deletions packages/plugin-multiversx/src/actions/createToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@elizaos/core";
import { WalletProvider } from "../providers/wallet";
import { validateMultiversxConfig } from "../enviroment";

import { createTokenSchema } from "../utils/schemas";
export interface CreateTokenContent extends Content {
tokenName: string;
tokenTicker: string;
Expand All @@ -23,10 +23,10 @@ export interface CreateTokenContent extends Content {

function isCreateTokenContent(
runtime: IAgentRuntime,
content: any
): content is CreateTokenContent {
content: CreateTokenContent
) {
console.log("Content for create token", content);
return content.tokenName && content.tokenTicker && content.amount;
return content.tokenName && content.tokenName && content.tokenName;
}

const createTokenTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
Expand Down Expand Up @@ -87,11 +87,14 @@ export default {
runtime,
context: transferContext,
modelClass: ModelClass.SMALL,
schema: createTokenSchema,
});

const payload = content.object as CreateTokenContent;

// Validate transfer content
if (!isCreateTokenContent(runtime, content)) {
console.error("Invalid content for TRANSFER_TOKEN action.");
if (!isCreateTokenContent(runtime, payload)) {
console.error("Invalid content for CREATE_TOKEN action.");
if (callback) {
callback({
text: "Unable to process transfer request. Invalid content provided.",
Expand All @@ -108,10 +111,10 @@ export default {
const walletProvider = new WalletProvider(privateKey, network);

await walletProvider.createESDT({
tokenName: content.tokenName,
amount: content.amount,
decimals: Number(content.decimals) || 18,
tokenTicker: content.tokenTicker,
tokenName: payload.tokenName,
amount: payload.amount,
decimals: Number(payload.decimals) || 18,
tokenTicker: payload.tokenTicker,
});
return true;
} catch (error) {
Expand Down
26 changes: 13 additions & 13 deletions packages/plugin-multiversx/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import {
} from "@elizaos/core";
import { WalletProvider } from "../providers/wallet";
import { validateMultiversxConfig } from "../enviroment";

import { transferSchema } from "../utils/schemas";
export interface TransferContent extends Content {
tokenAddress: string;
amount: string;
tokenIdentifier?: string;
}

function isTransferContent(
_runtime: IAgentRuntime,
content: any
): content is TransferContent {
function isTransferContent(_runtime: IAgentRuntime, content: TransferContent) {
console.log("Content for transfer", content);
return (
typeof content.tokenAddress === "string" &&
Expand Down Expand Up @@ -93,10 +90,13 @@ export default {
runtime,
context: transferContext,
modelClass: ModelClass.SMALL,
schema: transferSchema,
});

const payload = content.object as TransferContent;

// Validate transfer content
if (!isTransferContent(runtime, content)) {
if (!isTransferContent(runtime, payload)) {
console.error("Invalid content for TRANSFER_TOKEN action.");
if (callback) {
callback({
Expand All @@ -114,20 +114,20 @@ export default {
const walletProvider = new WalletProvider(privateKey, network);

if (
content.tokenIdentifier &&
content.tokenIdentifier.toLowerCase() !== "egld"
payload.tokenIdentifier &&
payload.tokenIdentifier.toLowerCase() !== "egld"
) {
await walletProvider.sendESDT({
receiverAddress: content.tokenAddress,
amount: content.amount,
identifier: content.tokenIdentifier,
receiverAddress: payload.tokenAddress,
amount: payload.amount,
identifier: payload.tokenIdentifier,
});
return true;
}

await walletProvider.sendEGLD({
receiverAddress: content.tokenAddress,
amount: content.amount,
receiverAddress: payload.tokenAddress,
amount: payload.amount,
});
return true;
} catch (error) {
Expand Down
26 changes: 26 additions & 0 deletions packages/plugin-multiversx/src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { z } from "zod";

export const createTokenSchema = z.object({
tokenName: z.string().min(1, { message: "Token name is required." }),
tokenTicker: z.string().min(1, { message: "Token ticker is required." }),
amount: z
.number()
.positive({ message: "Amount must be a positive number." }),
decimals: z
.number()
.int()
.min(0, { message: "Decimals must be at least 0" })
.max(18, { message: "Decimals must be at most 18" })
.nullable()
.optional(),
});

export const transferSchema = z.object({
tokenAddress: z.string().min(1, { message: "Token address is required." }),
amount: z.string().min(1, { message: "Amount is required." }),
tokenIdentifier: z
.string()
.transform((val) => (val === "null" ? null : val))
.nullable()
.optional(),
});

0 comments on commit 1d5d74a

Please sign in to comment.