Skip to content

Commit 1d5d74a

Browse files
authored
Merge pull request elizaOS#2017 from mgavrila/fix/amg/multiversx_plugin
fix: fix multiversx-plugin
2 parents b0bfa47 + 56cf7a0 commit 1d5d74a

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

packages/plugin-multiversx/src/actions/createToken.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@elizaos/core";
1414
import { WalletProvider } from "../providers/wallet";
1515
import { validateMultiversxConfig } from "../enviroment";
16-
16+
import { createTokenSchema } from "../utils/schemas";
1717
export interface CreateTokenContent extends Content {
1818
tokenName: string;
1919
tokenTicker: string;
@@ -23,10 +23,10 @@ export interface CreateTokenContent extends Content {
2323

2424
function isCreateTokenContent(
2525
runtime: IAgentRuntime,
26-
content: any
27-
): content is CreateTokenContent {
26+
content: CreateTokenContent
27+
) {
2828
console.log("Content for create token", content);
29-
return content.tokenName && content.tokenTicker && content.amount;
29+
return content.tokenName && content.tokenName && content.tokenName;
3030
}
3131

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

93+
const payload = content.object as CreateTokenContent;
94+
9295
// Validate transfer content
93-
if (!isCreateTokenContent(runtime, content)) {
94-
console.error("Invalid content for TRANSFER_TOKEN action.");
96+
if (!isCreateTokenContent(runtime, payload)) {
97+
console.error("Invalid content for CREATE_TOKEN action.");
9598
if (callback) {
9699
callback({
97100
text: "Unable to process transfer request. Invalid content provided.",
@@ -108,10 +111,10 @@ export default {
108111
const walletProvider = new WalletProvider(privateKey, network);
109112

110113
await walletProvider.createESDT({
111-
tokenName: content.tokenName,
112-
amount: content.amount,
113-
decimals: Number(content.decimals) || 18,
114-
tokenTicker: content.tokenTicker,
114+
tokenName: payload.tokenName,
115+
amount: payload.amount,
116+
decimals: Number(payload.decimals) || 18,
117+
tokenTicker: payload.tokenTicker,
115118
});
116119
return true;
117120
} catch (error) {

packages/plugin-multiversx/src/actions/transfer.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ import {
1313
} from "@elizaos/core";
1414
import { WalletProvider } from "../providers/wallet";
1515
import { validateMultiversxConfig } from "../enviroment";
16-
16+
import { transferSchema } from "../utils/schemas";
1717
export interface TransferContent extends Content {
1818
tokenAddress: string;
1919
amount: string;
2020
tokenIdentifier?: string;
2121
}
2222

23-
function isTransferContent(
24-
_runtime: IAgentRuntime,
25-
content: any
26-
): content is TransferContent {
23+
function isTransferContent(_runtime: IAgentRuntime, content: TransferContent) {
2724
console.log("Content for transfer", content);
2825
return (
2926
typeof content.tokenAddress === "string" &&
@@ -93,10 +90,13 @@ export default {
9390
runtime,
9491
context: transferContext,
9592
modelClass: ModelClass.SMALL,
93+
schema: transferSchema,
9694
});
9795

96+
const payload = content.object as TransferContent;
97+
9898
// Validate transfer content
99-
if (!isTransferContent(runtime, content)) {
99+
if (!isTransferContent(runtime, payload)) {
100100
console.error("Invalid content for TRANSFER_TOKEN action.");
101101
if (callback) {
102102
callback({
@@ -114,20 +114,20 @@ export default {
114114
const walletProvider = new WalletProvider(privateKey, network);
115115

116116
if (
117-
content.tokenIdentifier &&
118-
content.tokenIdentifier.toLowerCase() !== "egld"
117+
payload.tokenIdentifier &&
118+
payload.tokenIdentifier.toLowerCase() !== "egld"
119119
) {
120120
await walletProvider.sendESDT({
121-
receiverAddress: content.tokenAddress,
122-
amount: content.amount,
123-
identifier: content.tokenIdentifier,
121+
receiverAddress: payload.tokenAddress,
122+
amount: payload.amount,
123+
identifier: payload.tokenIdentifier,
124124
});
125125
return true;
126126
}
127127

128128
await walletProvider.sendEGLD({
129-
receiverAddress: content.tokenAddress,
130-
amount: content.amount,
129+
receiverAddress: payload.tokenAddress,
130+
amount: payload.amount,
131131
});
132132
return true;
133133
} catch (error) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { z } from "zod";
2+
3+
export const createTokenSchema = z.object({
4+
tokenName: z.string().min(1, { message: "Token name is required." }),
5+
tokenTicker: z.string().min(1, { message: "Token ticker is required." }),
6+
amount: z
7+
.number()
8+
.positive({ message: "Amount must be a positive number." }),
9+
decimals: z
10+
.number()
11+
.int()
12+
.min(0, { message: "Decimals must be at least 0" })
13+
.max(18, { message: "Decimals must be at most 18" })
14+
.nullable()
15+
.optional(),
16+
});
17+
18+
export const transferSchema = z.object({
19+
tokenAddress: z.string().min(1, { message: "Token address is required." }),
20+
amount: z.string().min(1, { message: "Amount is required." }),
21+
tokenIdentifier: z
22+
.string()
23+
.transform((val) => (val === "null" ? null : val))
24+
.nullable()
25+
.optional(),
26+
});

0 commit comments

Comments
 (0)