create-message-kit 1.2.20 → 1.2.21

Sign up to get free protection for your applications and to get access to all the features.
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.20";
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-message-kit",
3
- "version": "1.2.20",
3
+ "version": "1.2.21",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -5,7 +5,6 @@ interface Participant {
5
5
  response: string;
6
6
  name: string;
7
7
  address: string;
8
- agent_address: string;
9
8
  }
10
9
  export interface TossData {
11
10
  group_id: string;
@@ -18,7 +17,6 @@ export interface TossData {
18
17
  end_time: string;
19
18
  description: string;
20
19
  participants: Participant[];
21
- toss_wallet_address: string;
22
20
  }
23
21
  export async function checkTossCorrect(
24
22
  context: XMTPContext,
@@ -64,8 +62,7 @@ export async function checkTossCorrect(
64
62
  return undefined;
65
63
  }
66
64
 
67
- const pool = tossData.amount * (tossData.participants?.length || 0);
68
- return { ...tossData, toss_id, pool };
65
+ return { ...tossData, toss_id };
69
66
  }
70
67
 
71
68
  export function extractTossId(message: string): string | null {
@@ -139,9 +139,13 @@ export async function handleTossCreation(context: XMTPContext) {
139
139
  if (params.description && params.options && !isNaN(Number(params.amount))) {
140
140
  const keys = await tossDBClient.keys("*");
141
141
  let tossId = keys.length + 1;
142
- const createdTossWallet = await walletService.createTempWallet(
143
- tossId.toString(),
142
+ const isCreated = await walletService.createWallet(
143
+ tossId + ":" + sender.address,
144
144
  );
145
+ if (!isCreated) {
146
+ await context.reply("Failed to create toss wallet");
147
+ return;
148
+ }
145
149
 
146
150
  let tossData: TossData = {
147
151
  toss_id: tossId.toString(),
@@ -158,20 +162,13 @@ export async function handleTossCreation(context: XMTPContext) {
158
162
  ? new Date(params.endTime).toLocaleString()
159
163
  : new Date(Date.now() + 24 * 60 * 60 * 1000).toLocaleString(),
160
164
  participants: [],
161
- toss_wallet_address: createdTossWallet?.address,
162
165
  };
163
- await tossDBClient.set(
164
- "toss:" + tossId.toString(),
165
- JSON.stringify(tossData),
166
- );
167
- console.log(tossData);
166
+ await tossDBClient.set("toss:" + tossId, JSON.stringify(tossData));
168
167
  if (tossId !== undefined) {
169
- let msg = generateTossMessage(tossData);
170
- console.log(msg);
171
- await context.send(msg);
168
+ await context.send(generateTossMessage(tossData));
172
169
  } else {
173
170
  await context.reply(
174
- `An error occurred while creating the toss. ${JSON.stringify(tossId)}`,
171
+ `An error occurred while creating the toss. ${tossId}`,
175
172
  );
176
173
  }
177
174
  }
@@ -183,7 +180,7 @@ export async function handleJoinToss(context: XMTPContext) {
183
180
  return;
184
181
  }
185
182
 
186
- const { toss_id, participants, amount } = tossData;
183
+ const { toss_id, participants, amount, admin_address } = tossData;
187
184
 
188
185
  const {
189
186
  message: {
@@ -192,7 +189,6 @@ export async function handleJoinToss(context: XMTPContext) {
192
189
  params: { response },
193
190
  },
194
191
  },
195
- group,
196
192
  walletService,
197
193
  } = context;
198
194
 
@@ -201,35 +197,16 @@ export async function handleJoinToss(context: XMTPContext) {
201
197
  await context.reply("You have already joined this toss.");
202
198
  return;
203
199
  }
204
-
205
- const tossWallet = await walletService.getTempWallet(toss_id);
206
- if (!tossWallet) {
207
- await context.reply("Toss wallet not found");
208
- return;
209
- }
200
+ //Create wallet for sender
201
+ await walletService.createWallet(sender.address);
210
202
  const balance = await walletService.checkBalance(sender.address);
211
-
212
- if (balance < amount) {
213
- await context.send("You need to fund your account. Check your DMs:");
214
- await walletService.requestFunds(context, amount);
215
- return;
216
- }
203
+ if (balance < amount) return walletService.requestFunds(amount);
217
204
 
218
205
  try {
219
- const senderWallet = await walletService.getUserWallet(sender.address);
220
- if (!senderWallet) {
221
- await context.reply("Sender wallet not found");
222
- return;
223
- }
224
- const transfer = await walletService.transfer(
225
- senderWallet,
226
- tossWallet,
227
- amount,
228
- );
229
- console.log("Transfer:", transfer.getTransactionHash());
206
+ let tempWalletID = toss_id + ":" + admin_address;
207
+ await walletService.transfer(sender.address, tempWalletID, amount);
230
208
  const participant = {
231
209
  address: sender.address,
232
- agent_address: senderWallet.address,
233
210
  response: response,
234
211
  name:
235
212
  (await context.getUserInfo(sender.address))?.preferredName ??
@@ -274,49 +251,30 @@ export async function handleEndToss(context: XMTPContext) {
274
251
  await context.reply("No participants for this toss.");
275
252
  return;
276
253
  } else if (admin_address.toLowerCase() !== sender.address.toLowerCase()) {
277
- await context.reply("Only the admin can end the toss.");
278
- return;
279
- } else if (
280
- !options
281
- .split(",")
282
- .map((o) => o.toLowerCase())
283
- .includes(option.toLowerCase())
284
- ) {
285
- await context.reply("Invalid option selected.");
254
+ await context.reply("Only the admin can cancel the toss.");
286
255
  return;
287
256
  }
288
- const { winners, losers } = await extractWinners(participants ?? [], option);
289
257
 
290
- if (winners.length === 0) {
291
- await context.reply("No winners for this toss.");
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
+ );
292
265
  return;
293
266
  }
294
267
 
268
+ //Winners
269
+
270
+ const { winners, losers } = await extractWinners(participants, option);
271
+
295
272
  const prize =
296
273
  (tossData.amount * (participants?.length ?? 0)) / (winners.length ?? 1);
297
274
 
298
275
  try {
299
276
  for (const winner of winners) {
300
- const tossWallet = await walletService.getTempWallet(toss_id);
301
-
302
- if (!tossWallet) {
303
- await context.reply("Toss wallet not found");
304
- return;
305
- }
306
- const winnerWallet = await walletService.getUserWallet(
307
- winner.address,
308
- winner.address,
309
- );
310
- if (!winnerWallet) {
311
- await context.reply("Winner wallet not found");
312
- return;
313
- }
314
- const transfer = await walletService.transfer(
315
- tossWallet,
316
- winnerWallet,
317
- prize,
318
- );
319
- console.log("Transfer:", transfer.getTransactionHash());
277
+ await walletService.transfer(tempWalletID, winner.address, prize);
320
278
  await tossDBClient.set(
321
279
  `toss:${toss_id}`,
322
280
  JSON.stringify({ ...tossData, status: "closed" }),
@@ -357,31 +315,26 @@ export async function handleCancelToss(context: XMTPContext) {
357
315
  return;
358
316
  }
359
317
 
360
- for (const participant of participants ?? []) {
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) {
361
328
  try {
362
- const tossWallet = await walletService.getTempWallet(toss_id);
363
-
364
- if (!tossWallet) {
365
- await context.reply("Toss wallet not found");
366
- return;
367
- }
368
-
369
- const participantWallet = await walletService.getUserWallet(
370
- 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,
371
334
  );
372
- if (!participantWallet) {
373
- await context.reply("Participant wallet not found");
374
- return;
375
- }
376
- const transfer = await walletService.transfer(
377
- tossWallet,
378
- participantWallet,
379
- amount,
335
+ await context.reply(
336
+ `Failed to send prize to ${participant.address} agent wallet`,
380
337
  );
381
- console.log("Transfer:", transfer.getTransactionHash());
382
- } catch (error) {
383
- console.error(`Failed to send prize to ${participant.address}:`, error);
384
- await context.reply(`Failed to send prize to ${participant.address}`);
385
338
  }
386
339
  }
387
340
 
@@ -424,20 +377,17 @@ export async function handleDM(context: XMTPContext) {
424
377
  if (skill === "help") {
425
378
  await context.send(DM_HELP_MESSAGE);
426
379
  } else if (skill === "create") {
427
- const walletExist = await walletService.getUserWallet(sender.address);
380
+ const walletExist = await walletService.getWallet(sender.address);
428
381
  if (walletExist) {
429
382
  await context.reply("You already have an agent wallet.");
430
383
  return;
431
384
  }
432
- const userWallet = await walletService.createUserWallet(sender.address);
433
- await context.reply(
434
- `Your agent wallet address is ${userWallet.address}\nBalance: $${await walletService.checkBalance(sender.address)}`,
435
- );
385
+ await walletService.createWallet(sender.address);
436
386
  } else if (skill === "balance") {
437
- const userWallet = await walletService.getUserWallet(sender.address);
387
+ const userWallet = await walletService.getWallet(sender.address);
438
388
 
439
389
  context.sendTo(
440
- `Your agent wallet address is ${userWallet?.address}\nBalance: $${await walletService.checkBalance(sender.address)}`,
390
+ `Your agent wallet for address is ${sender.address}\nBalance: $${await walletService.checkBalance(sender.address)}`,
441
391
  [sender.address],
442
392
  );
443
393
  } else if (skill === "fund") {
@@ -447,8 +397,7 @@ export async function handleDM(context: XMTPContext) {
447
397
  return;
448
398
  } else if (amount) {
449
399
  if (amount + balance <= 10) {
450
- await walletService.requestFunds(context, Number(amount));
451
- return;
400
+ return walletService.requestFunds(Number(amount));
452
401
  } else {
453
402
  await context.send("Wrong amount. Max 10 USDC.");
454
403
  return;
@@ -464,7 +413,7 @@ export async function handleDM(context: XMTPContext) {
464
413
  `Please specify the amount of USDC to prefund (1 to ${10 - balance}):`,
465
414
  options,
466
415
  );
467
- await walletService.requestFunds(context, Number(response));
416
+ return walletService.requestFunds(Number(response));
468
417
  } else if (skill === "withdraw") {
469
418
  const balance = await walletService.checkBalance(sender.address);
470
419
  if (balance === 0) {
@@ -478,6 +427,6 @@ export async function handleDM(context: XMTPContext) {
478
427
  `Please specify the amount of USDC to withdraw (1 to ${balance}):`,
479
428
  options,
480
429
  );
481
- await walletService.withdrawFunds(sender.address, Number(response));
430
+ await walletService.withdrawFunds(Number(response));
482
431
  }
483
432
  }