create-message-kit 1.2.19 → 1.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -1
- package/package.json +1 -1
- package/templates/coinbase-agent/src/skills/drip.ts +1 -1
- package/templates/coinbase-agent/src/skills/mint.ts +1 -1
- package/templates/coinbase-agent/src/skills/pay.ts +1 -1
- package/templates/coinbase-agent/src/skills/swap.ts +2 -2
- package/templates/ens/src/skills/weather.ts +1 -0
- package/templates/faucet/src/skills/faucet.ts +1 -1
- package/templates/toss/.env.example +0 -1
- package/templates/toss/src/index.ts +2 -3
- package/templates/toss/src/plugins/helpers.ts +8 -19
- package/templates/toss/src/plugins/redis.ts +1 -64
- package/templates/toss/src/skills/toss.ts +76 -133
package/index.js
CHANGED
@@ -7,7 +7,7 @@ import { default as fs } from "fs-extra";
|
|
7
7
|
import { isCancel } from "@clack/prompts";
|
8
8
|
import { detect } from "detect-package-manager";
|
9
9
|
import pc from "picocolors";
|
10
|
-
const defVersion = "1.2.
|
10
|
+
const defVersion = "1.2.21";
|
11
11
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
12
12
|
|
13
13
|
// Read package.json to get the version
|
package/package.json
CHANGED
@@ -4,7 +4,7 @@ import { baseUrl } from "../index.js";
|
|
4
4
|
|
5
5
|
export const registerSkill: Skill[] = [
|
6
6
|
{
|
7
|
-
skill: "
|
7
|
+
skill: "mint",
|
8
8
|
examples: [
|
9
9
|
"/mint 0x73a333cb82862d4f66f0154229755b184fb4f5b0 1",
|
10
10
|
"/mint https://zora.co/collect/base/0x123456789/1...",
|
@@ -3,7 +3,7 @@ import type { Skill } from "@xmtp/message-kit";
|
|
3
3
|
|
4
4
|
export const registerSkill: Skill[] = [
|
5
5
|
{
|
6
|
-
skill: "
|
6
|
+
skill: "pay",
|
7
7
|
examples: ["/pay 10 vitalik.eth"],
|
8
8
|
description:
|
9
9
|
"Send a specified amount of a cryptocurrency to a destination address.",
|
@@ -4,7 +4,7 @@ import { baseUrl } from "../index.js";
|
|
4
4
|
|
5
5
|
export const registerSkill: Skill[] = [
|
6
6
|
{
|
7
|
-
skill: "
|
7
|
+
skill: "swap",
|
8
8
|
examples: ["/swap 10 usdc eth", "/swap 1 dai usdc"],
|
9
9
|
handler: handler,
|
10
10
|
description: "Exchange one type of cryptocurrency for another.",
|
@@ -39,7 +39,7 @@ export async function handler(context: XMTPContext) {
|
|
39
39
|
|
40
40
|
if (!amount || !token_from || !token_to) {
|
41
41
|
context.reply(
|
42
|
-
"Missing required parameters. Please provide amount, token_from, and token_to."
|
42
|
+
"Missing required parameters. Please provide amount, token_from, and token_to.",
|
43
43
|
);
|
44
44
|
return;
|
45
45
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -2,6 +2,5 @@ KEY= # the private key of the wallet
|
|
2
2
|
TEST_ENCRYPTION_KEY= # a different private key for encryption
|
3
3
|
OPENAI_API_KEY= # the API key for OpenAI
|
4
4
|
REDIS_CONNECTION_STRING= # the connection to the toss database in Redis
|
5
|
-
COINBASE_API_REDIS_URL= # the API key for OpenAI
|
6
5
|
COINBASE_API_KEY_NAME= # the API key name for Coinbase
|
7
6
|
COINBASE_API_KEY_PRIVATE_KEY= # the private key for Coinbase
|
@@ -9,8 +9,7 @@ import {
|
|
9
9
|
import { systemPrompt } from "./prompt.js";
|
10
10
|
import { toss } from "./skills/toss.js";
|
11
11
|
import fs from "fs";
|
12
|
-
|
13
|
-
const walletServiceDB = await getWalletService();
|
12
|
+
|
14
13
|
export const agent: Agent = {
|
15
14
|
name: "Toss Bot",
|
16
15
|
tag: "@toss",
|
@@ -29,5 +28,5 @@ run(
|
|
29
28
|
fs.writeFileSync("example_prompt.md", prompt);
|
30
29
|
await agentReply(context, prompt);
|
31
30
|
},
|
32
|
-
{ agent
|
31
|
+
{ agent },
|
33
32
|
);
|
@@ -1,12 +1,10 @@
|
|
1
1
|
import { XMTPContext } from "@xmtp/message-kit";
|
2
2
|
import { getRedisClient } from "./redis.js";
|
3
3
|
|
4
|
-
const tossDBClient = await getRedisClient();
|
5
4
|
interface Participant {
|
6
5
|
response: string;
|
7
6
|
name: string;
|
8
7
|
address: string;
|
9
|
-
agent_address: string;
|
10
8
|
}
|
11
9
|
export interface TossData {
|
12
10
|
group_id: string;
|
@@ -18,9 +16,7 @@ export interface TossData {
|
|
18
16
|
created_at: string;
|
19
17
|
end_time: string;
|
20
18
|
description: string;
|
21
|
-
|
22
|
-
participants?: Participant[];
|
23
|
-
toss_wallet_address: string;
|
19
|
+
participants: Participant[];
|
24
20
|
}
|
25
21
|
export async function checkTossCorrect(
|
26
22
|
context: XMTPContext,
|
@@ -29,9 +25,9 @@ export async function checkTossCorrect(
|
|
29
25
|
message: {
|
30
26
|
content: { previousMsg },
|
31
27
|
},
|
32
|
-
walletService,
|
33
28
|
group,
|
34
29
|
} = context;
|
30
|
+
|
35
31
|
if (!group) {
|
36
32
|
await context.reply("This command can only be used in a group.");
|
37
33
|
return undefined;
|
@@ -47,15 +43,14 @@ export async function checkTossCorrect(
|
|
47
43
|
);
|
48
44
|
return undefined;
|
49
45
|
}
|
50
|
-
|
51
|
-
const tossDataString = await tossDBClient.get(`toss:${
|
52
|
-
|
53
|
-
let tossData = hexString ? walletService.decrypt(hexString) : null;
|
46
|
+
const tossDBClient = await getRedisClient();
|
47
|
+
const tossDataString = await tossDBClient.get(`toss:${toss_id}`);
|
48
|
+
let tossData = tossDataString ? JSON.parse(tossDataString) : null;
|
54
49
|
|
55
50
|
if (typeof tossData === "string") {
|
56
51
|
tossData = JSON.parse(tossData) as TossData;
|
57
52
|
}
|
58
|
-
|
53
|
+
|
59
54
|
if (!tossData) {
|
60
55
|
await context.reply("Toss not found");
|
61
56
|
return undefined;
|
@@ -66,14 +61,8 @@ export async function checkTossCorrect(
|
|
66
61
|
await context.reply("This toss is not in this group.");
|
67
62
|
return undefined;
|
68
63
|
}
|
69
|
-
|
70
|
-
|
71
|
-
tossData.participants = tossData.encrypted_participants?.map((p: string) =>
|
72
|
-
walletService.decrypt(p),
|
73
|
-
);
|
74
|
-
}
|
75
|
-
const pool = tossData.amount * (tossData?.participants?.length || 0);
|
76
|
-
return { ...tossData, toss_id, pool };
|
64
|
+
|
65
|
+
return { ...tossData, toss_id };
|
77
66
|
}
|
78
67
|
|
79
68
|
export function extractTossId(message: string): string | null {
|
@@ -1,18 +1,7 @@
|
|
1
1
|
import { createClient } from "@redis/client";
|
2
|
-
import
|
3
|
-
|
4
|
-
let redisClient: RedisClientType | null = null;
|
2
|
+
import { RedisClientType } from "@redis/client";
|
5
3
|
|
6
4
|
export const getRedisClient = async () => {
|
7
|
-
if (redisClient?.isOpen) {
|
8
|
-
return redisClient;
|
9
|
-
}
|
10
|
-
|
11
|
-
if (!process.env.REDIS_CONNECTION_STRING) {
|
12
|
-
throw new Error(
|
13
|
-
"REDIS_CONNECTION_STRING not found in environment variables",
|
14
|
-
);
|
15
|
-
}
|
16
5
|
const client = createClient({
|
17
6
|
url: process.env.REDIS_CONNECTION_STRING,
|
18
7
|
});
|
@@ -22,57 +11,5 @@ export const getRedisClient = async () => {
|
|
22
11
|
});
|
23
12
|
|
24
13
|
await client.connect();
|
25
|
-
redisClient = client as RedisClientType;
|
26
|
-
return client as RedisClientType;
|
27
|
-
};
|
28
|
-
|
29
|
-
let walletService: RedisClientType | null = null;
|
30
|
-
|
31
|
-
export const getWalletService = async (): Promise<RedisClientType> => {
|
32
|
-
if (walletService?.isOpen) {
|
33
|
-
return walletService;
|
34
|
-
}
|
35
|
-
|
36
|
-
if (!process.env.COINBASE_API_REDIS_URL) {
|
37
|
-
throw new Error(
|
38
|
-
"COINBASE_API_REDIS_URL not found in environment variables",
|
39
|
-
);
|
40
|
-
}
|
41
|
-
|
42
|
-
const client = createClient({
|
43
|
-
url: process.env.COINBASE_API_REDIS_URL,
|
44
|
-
});
|
45
|
-
|
46
|
-
client.on("error", (error: Error) => {
|
47
|
-
console.error("Toss wallet Redis client error:", error);
|
48
|
-
});
|
49
|
-
|
50
|
-
client.on("connect", () => {
|
51
|
-
console.log("Connected to Toss Wallet Redis");
|
52
|
-
});
|
53
|
-
await client.connect();
|
54
|
-
walletService = client as RedisClientType;
|
55
14
|
return client as RedisClientType;
|
56
15
|
};
|
57
|
-
|
58
|
-
export async function updateField(
|
59
|
-
client: RedisClientType,
|
60
|
-
key: string,
|
61
|
-
updateObject: any,
|
62
|
-
) {
|
63
|
-
// Check if the key exists
|
64
|
-
const data = await client.get(key);
|
65
|
-
|
66
|
-
let updatedData;
|
67
|
-
if (data) {
|
68
|
-
// If the key exists, parse it and merge the updates
|
69
|
-
const parsedData = JSON.parse(data);
|
70
|
-
updatedData = { ...parsedData, ...updateObject };
|
71
|
-
} else {
|
72
|
-
// If the key doesn't exist, use the updateObject as the initial value
|
73
|
-
updatedData = updateObject;
|
74
|
-
}
|
75
|
-
|
76
|
-
// Save the updated or new data back to Redis
|
77
|
-
await client.set(key, JSON.stringify(updatedData));
|
78
|
-
}
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import { Skill, XMTPContext, getUserInfo } from "@xmtp/message-kit";
|
2
|
-
import { TimeoutError } from "@coinbase/coinbase-sdk";
|
3
2
|
import { getRedisClient } from "../plugins/redis.js";
|
4
3
|
import {
|
5
4
|
checkTossCorrect,
|
@@ -11,11 +10,9 @@ import {
|
|
11
10
|
DM_HELP_MESSAGE,
|
12
11
|
} from "../plugins/helpers.js";
|
13
12
|
|
14
|
-
const tossDBClient = await getRedisClient();
|
15
|
-
|
16
13
|
export const toss: Skill[] = [
|
17
14
|
{
|
18
|
-
skill: "
|
15
|
+
skill: "end",
|
19
16
|
description: "End a toss.",
|
20
17
|
handler: handleEndToss,
|
21
18
|
examples: ["/end yes", "/end no"],
|
@@ -26,14 +23,13 @@ export const toss: Skill[] = [
|
|
26
23
|
},
|
27
24
|
},
|
28
25
|
{
|
29
|
-
skill: "
|
26
|
+
skill: "create",
|
30
27
|
description: "Create an agent wallet.",
|
31
28
|
handler: handleDM,
|
32
29
|
examples: ["/create"],
|
33
|
-
params: {},
|
34
30
|
},
|
35
31
|
{
|
36
|
-
skill: "
|
32
|
+
skill: "fund",
|
37
33
|
description: "Fund your account.",
|
38
34
|
handler: handleDM,
|
39
35
|
examples: ["/fund 10"],
|
@@ -44,7 +40,7 @@ export const toss: Skill[] = [
|
|
44
40
|
},
|
45
41
|
},
|
46
42
|
{
|
47
|
-
skill: "
|
43
|
+
skill: "withdraw",
|
48
44
|
description: "Withdraw funds from your account.",
|
49
45
|
handler: handleDM,
|
50
46
|
examples: ["/withdraw 10"],
|
@@ -55,28 +51,25 @@ export const toss: Skill[] = [
|
|
55
51
|
},
|
56
52
|
},
|
57
53
|
{
|
58
|
-
skill: "
|
54
|
+
skill: "help",
|
59
55
|
description: "Get help with tossing.",
|
60
56
|
handler: handleDM,
|
61
57
|
examples: ["/help"],
|
62
|
-
params: {},
|
63
58
|
},
|
64
59
|
{
|
65
|
-
skill: "
|
60
|
+
skill: "cancel",
|
66
61
|
description: "Cancel a toss.",
|
67
62
|
handler: handleCancelToss,
|
68
63
|
examples: ["/cancel"],
|
69
|
-
params: {},
|
70
64
|
},
|
71
65
|
{
|
72
|
-
skill: "
|
66
|
+
skill: "balance",
|
73
67
|
description: "Check your balance.",
|
74
68
|
handler: handleDM,
|
75
69
|
examples: ["/balance"],
|
76
|
-
params: {},
|
77
70
|
},
|
78
71
|
{
|
79
|
-
skill: "
|
72
|
+
skill: "join",
|
80
73
|
description: "Join a toss.",
|
81
74
|
params: {
|
82
75
|
response: {
|
@@ -87,15 +80,13 @@ export const toss: Skill[] = [
|
|
87
80
|
examples: ["/join yes", "/join no"],
|
88
81
|
},
|
89
82
|
{
|
90
|
-
skill: "
|
83
|
+
skill: "status",
|
91
84
|
description: "Check the status of the toss.",
|
92
85
|
handler: handleTossStatus,
|
93
86
|
examples: ["/status"],
|
94
|
-
params: {},
|
95
87
|
},
|
96
88
|
{
|
97
|
-
skill:
|
98
|
-
"/toss [description] [options (separated by comma)] [amount] [judge(optional)] [endTime(optional)]",
|
89
|
+
skill: "toss",
|
99
90
|
description:
|
100
91
|
"Create a toss with a description, options, amount and judge(optional).",
|
101
92
|
handler: handleTossCreation,
|
@@ -120,9 +111,11 @@ export const toss: Skill[] = [
|
|
120
111
|
},
|
121
112
|
judge: {
|
122
113
|
type: "username",
|
114
|
+
optional: true,
|
123
115
|
},
|
124
116
|
endTime: {
|
125
117
|
type: "quoted",
|
118
|
+
optional: true,
|
126
119
|
},
|
127
120
|
},
|
128
121
|
},
|
@@ -142,12 +135,17 @@ export async function handleTossCreation(context: XMTPContext) {
|
|
142
135
|
return;
|
143
136
|
}
|
144
137
|
|
138
|
+
const tossDBClient = await getRedisClient();
|
145
139
|
if (params.description && params.options && !isNaN(Number(params.amount))) {
|
146
140
|
const keys = await tossDBClient.keys("*");
|
147
141
|
let tossId = keys.length + 1;
|
148
|
-
const
|
149
|
-
tossId.
|
142
|
+
const isCreated = await walletService.createWallet(
|
143
|
+
tossId + ":" + sender.address,
|
150
144
|
);
|
145
|
+
if (!isCreated) {
|
146
|
+
await context.reply("Failed to create toss wallet");
|
147
|
+
return;
|
148
|
+
}
|
151
149
|
|
152
150
|
let tossData: TossData = {
|
153
151
|
toss_id: tossId.toString(),
|
@@ -163,20 +161,14 @@ export async function handleTossCreation(context: XMTPContext) {
|
|
163
161
|
end_time: params.endTime
|
164
162
|
? new Date(params.endTime).toLocaleString()
|
165
163
|
: new Date(Date.now() + 24 * 60 * 60 * 1000).toLocaleString(),
|
166
|
-
encrypted_participants: [],
|
167
164
|
participants: [],
|
168
|
-
toss_wallet_address: createdTossWallet?.address,
|
169
165
|
};
|
170
|
-
await tossDBClient.set(
|
171
|
-
"toss:" + walletService.encrypt(tossId.toString()),
|
172
|
-
walletService.encrypt(tossData),
|
173
|
-
);
|
174
|
-
|
166
|
+
await tossDBClient.set("toss:" + tossId, JSON.stringify(tossData));
|
175
167
|
if (tossId !== undefined) {
|
176
168
|
await context.send(generateTossMessage(tossData));
|
177
169
|
} else {
|
178
170
|
await context.reply(
|
179
|
-
`An error occurred while creating the toss. ${
|
171
|
+
`An error occurred while creating the toss. ${tossId}`,
|
180
172
|
);
|
181
173
|
}
|
182
174
|
}
|
@@ -188,7 +180,7 @@ export async function handleJoinToss(context: XMTPContext) {
|
|
188
180
|
return;
|
189
181
|
}
|
190
182
|
|
191
|
-
const { toss_id, participants,
|
183
|
+
const { toss_id, participants, amount, admin_address } = tossData;
|
192
184
|
|
193
185
|
const {
|
194
186
|
message: {
|
@@ -197,55 +189,34 @@ export async function handleJoinToss(context: XMTPContext) {
|
|
197
189
|
params: { response },
|
198
190
|
},
|
199
191
|
},
|
200
|
-
group,
|
201
192
|
walletService,
|
202
193
|
} = context;
|
203
194
|
|
195
|
+
const tossDBClient = await getRedisClient();
|
204
196
|
if (participants?.some((p) => p.address === sender.address)) {
|
205
197
|
await context.reply("You have already joined this toss.");
|
206
198
|
return;
|
207
199
|
}
|
208
|
-
|
209
|
-
|
210
|
-
if (!tossWallet) {
|
211
|
-
await context.reply("Toss wallet not found");
|
212
|
-
return;
|
213
|
-
}
|
200
|
+
//Create wallet for sender
|
201
|
+
await walletService.createWallet(sender.address);
|
214
202
|
const balance = await walletService.checkBalance(sender.address);
|
215
|
-
|
216
|
-
if (balance < amount) {
|
217
|
-
await context.send("You need to fund your account. Check your DMs:");
|
218
|
-
await walletService.requestFunds(context, amount);
|
219
|
-
return;
|
220
|
-
}
|
203
|
+
if (balance < amount) return walletService.requestFunds(amount);
|
221
204
|
|
222
205
|
try {
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
return;
|
227
|
-
}
|
228
|
-
const transfer = await walletService.transfer(
|
229
|
-
senderWallet,
|
230
|
-
tossWallet,
|
231
|
-
amount,
|
232
|
-
);
|
233
|
-
console.log("Transfer:", transfer.getTransactionHash());
|
234
|
-
const encryptedParticipant = walletService.encrypt({
|
206
|
+
let tempWalletID = toss_id + ":" + admin_address;
|
207
|
+
await walletService.transfer(sender.address, tempWalletID, amount);
|
208
|
+
const participant = {
|
235
209
|
address: sender.address,
|
236
|
-
agent_address: senderWallet.address,
|
237
210
|
response: response,
|
238
211
|
name:
|
239
212
|
(await context.getUserInfo(sender.address))?.preferredName ??
|
240
213
|
sender.address,
|
241
|
-
}
|
242
|
-
|
214
|
+
};
|
215
|
+
participants.push(participant);
|
243
216
|
|
244
217
|
await tossDBClient.set(
|
245
|
-
`toss:${
|
246
|
-
|
247
|
-
JSON.stringify({ ...tossData, encrypted_participants }),
|
248
|
-
),
|
218
|
+
`toss:${toss_id}`,
|
219
|
+
JSON.stringify({ ...tossData, participants }),
|
249
220
|
);
|
250
221
|
|
251
222
|
await context.reply("Successfully joined the toss!");
|
@@ -275,58 +246,38 @@ export async function handleEndToss(context: XMTPContext) {
|
|
275
246
|
walletService,
|
276
247
|
} = context;
|
277
248
|
|
249
|
+
const tossDBClient = await getRedisClient();
|
278
250
|
if (participants?.length === 0) {
|
279
251
|
await context.reply("No participants for this toss.");
|
280
252
|
return;
|
281
253
|
} else if (admin_address.toLowerCase() !== sender.address.toLowerCase()) {
|
282
|
-
await context.reply("Only the admin can
|
283
|
-
return;
|
284
|
-
} else if (
|
285
|
-
!options
|
286
|
-
.split(",")
|
287
|
-
.map((o) => o.toLowerCase())
|
288
|
-
.includes(option.toLowerCase())
|
289
|
-
) {
|
290
|
-
await context.reply("Invalid option selected.");
|
254
|
+
await context.reply("Only the admin can cancel the toss.");
|
291
255
|
return;
|
292
256
|
}
|
293
|
-
const { winners, losers } = await extractWinners(participants ?? [], option);
|
294
257
|
|
295
|
-
|
296
|
-
|
258
|
+
let tempWalletID = toss_id + ":" + admin_address;
|
259
|
+
const balance = await walletService.checkBalance(tempWalletID);
|
260
|
+
const fundsNeeded = tossData.amount * participants?.length;
|
261
|
+
if (balance < fundsNeeded) {
|
262
|
+
await context.reply(
|
263
|
+
`Toss wallet does not have enough funds ${fundsNeeded}, has ${balance}`,
|
264
|
+
);
|
297
265
|
return;
|
298
266
|
}
|
299
267
|
|
268
|
+
//Winners
|
269
|
+
|
270
|
+
const { winners, losers } = await extractWinners(participants, option);
|
271
|
+
|
300
272
|
const prize =
|
301
273
|
(tossData.amount * (participants?.length ?? 0)) / (winners.length ?? 1);
|
302
274
|
|
303
275
|
try {
|
304
276
|
for (const winner of winners) {
|
305
|
-
|
306
|
-
|
307
|
-
if (!tossWallet) {
|
308
|
-
await context.reply("Toss wallet not found");
|
309
|
-
return;
|
310
|
-
}
|
311
|
-
const winnerWallet = await walletService.getUserWallet(
|
312
|
-
winner.address,
|
313
|
-
winner.address,
|
314
|
-
);
|
315
|
-
if (!winnerWallet) {
|
316
|
-
await context.reply("Winner wallet not found");
|
317
|
-
return;
|
318
|
-
}
|
319
|
-
const transfer = await walletService.transfer(
|
320
|
-
tossWallet,
|
321
|
-
winnerWallet,
|
322
|
-
prize,
|
323
|
-
);
|
324
|
-
console.log("Transfer:", transfer.getTransactionHash());
|
277
|
+
await walletService.transfer(tempWalletID, winner.address, prize);
|
325
278
|
await tossDBClient.set(
|
326
|
-
`toss:${
|
327
|
-
|
328
|
-
JSON.stringify({ ...tossData, status: "closed" }),
|
329
|
-
),
|
279
|
+
`toss:${toss_id}`,
|
280
|
+
JSON.stringify({ ...tossData, status: "closed" }),
|
330
281
|
);
|
331
282
|
}
|
332
283
|
// Clean up
|
@@ -355,6 +306,7 @@ export async function handleCancelToss(context: XMTPContext) {
|
|
355
306
|
walletService,
|
356
307
|
} = context;
|
357
308
|
|
309
|
+
const tossDBClient = await getRedisClient();
|
358
310
|
if (participants?.length === 0) {
|
359
311
|
await context.reply("No participants for this toss.");
|
360
312
|
return;
|
@@ -363,31 +315,26 @@ export async function handleCancelToss(context: XMTPContext) {
|
|
363
315
|
return;
|
364
316
|
}
|
365
317
|
|
366
|
-
|
318
|
+
let tempWalletID = toss_id + ":" + admin_address;
|
319
|
+
const balance = await walletService.checkBalance(tempWalletID);
|
320
|
+
const fundsNeeded = tossData.amount * participants?.length;
|
321
|
+
if (balance < fundsNeeded) {
|
322
|
+
await context.reply(
|
323
|
+
`Toss wallet does not have enough funds ${fundsNeeded}, has ${balance}`,
|
324
|
+
);
|
325
|
+
return;
|
326
|
+
}
|
327
|
+
for (const participant of participants) {
|
367
328
|
try {
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
}
|
374
|
-
|
375
|
-
const participantWallet = await walletService.getUserWallet(
|
376
|
-
participant.address,
|
329
|
+
await walletService.transfer(tempWalletID, participant.address, amount);
|
330
|
+
} catch (error) {
|
331
|
+
console.error(
|
332
|
+
`Failed to send prize to ${participant.address} agent wallet:`,
|
333
|
+
error,
|
377
334
|
);
|
378
|
-
|
379
|
-
|
380
|
-
return;
|
381
|
-
}
|
382
|
-
const transfer = await walletService.transfer(
|
383
|
-
tossWallet,
|
384
|
-
participantWallet,
|
385
|
-
amount,
|
335
|
+
await context.reply(
|
336
|
+
`Failed to send prize to ${participant.address} agent wallet`,
|
386
337
|
);
|
387
|
-
console.log("Transfer:", transfer.getTransactionHash());
|
388
|
-
} catch (error) {
|
389
|
-
console.error(`Failed to send prize to ${participant.address}:`, error);
|
390
|
-
await context.reply(`Failed to send prize to ${participant.address}`);
|
391
338
|
}
|
392
339
|
}
|
393
340
|
|
@@ -395,8 +342,8 @@ export async function handleCancelToss(context: XMTPContext) {
|
|
395
342
|
//await walletService.deleteTempWallet(tossWalletRedis, tossId.toString());
|
396
343
|
|
397
344
|
await tossDBClient.set(
|
398
|
-
`toss:${
|
399
|
-
|
345
|
+
`toss:${toss_id}`,
|
346
|
+
JSON.stringify({ ...tossData, status: "cancelled" }),
|
400
347
|
);
|
401
348
|
|
402
349
|
await context.reply(
|
@@ -430,20 +377,17 @@ export async function handleDM(context: XMTPContext) {
|
|
430
377
|
if (skill === "help") {
|
431
378
|
await context.send(DM_HELP_MESSAGE);
|
432
379
|
} else if (skill === "create") {
|
433
|
-
const walletExist = await walletService.
|
380
|
+
const walletExist = await walletService.getWallet(sender.address);
|
434
381
|
if (walletExist) {
|
435
382
|
await context.reply("You already have an agent wallet.");
|
436
383
|
return;
|
437
384
|
}
|
438
|
-
|
439
|
-
await context.reply(
|
440
|
-
`Your agent wallet address is ${userWallet.address}\nBalance: $${await walletService.checkBalance(sender.address)}`,
|
441
|
-
);
|
385
|
+
await walletService.createWallet(sender.address);
|
442
386
|
} else if (skill === "balance") {
|
443
|
-
const userWallet = await walletService.
|
387
|
+
const userWallet = await walletService.getWallet(sender.address);
|
444
388
|
|
445
389
|
context.sendTo(
|
446
|
-
`Your agent wallet address is ${
|
390
|
+
`Your agent wallet for address is ${sender.address}\nBalance: $${await walletService.checkBalance(sender.address)}`,
|
447
391
|
[sender.address],
|
448
392
|
);
|
449
393
|
} else if (skill === "fund") {
|
@@ -453,8 +397,7 @@ export async function handleDM(context: XMTPContext) {
|
|
453
397
|
return;
|
454
398
|
} else if (amount) {
|
455
399
|
if (amount + balance <= 10) {
|
456
|
-
|
457
|
-
return;
|
400
|
+
return walletService.requestFunds(Number(amount));
|
458
401
|
} else {
|
459
402
|
await context.send("Wrong amount. Max 10 USDC.");
|
460
403
|
return;
|
@@ -470,7 +413,7 @@ export async function handleDM(context: XMTPContext) {
|
|
470
413
|
`Please specify the amount of USDC to prefund (1 to ${10 - balance}):`,
|
471
414
|
options,
|
472
415
|
);
|
473
|
-
|
416
|
+
return walletService.requestFunds(Number(response));
|
474
417
|
} else if (skill === "withdraw") {
|
475
418
|
const balance = await walletService.checkBalance(sender.address);
|
476
419
|
if (balance === 0) {
|
@@ -484,6 +427,6 @@ export async function handleDM(context: XMTPContext) {
|
|
484
427
|
`Please specify the amount of USDC to withdraw (1 to ${balance}):`,
|
485
428
|
options,
|
486
429
|
);
|
487
|
-
await walletService.withdrawFunds(
|
430
|
+
await walletService.withdrawFunds(Number(response));
|
488
431
|
}
|
489
432
|
}
|