-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmtp.ts
54 lines (40 loc) · 1.64 KB
/
xmtp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cli from "cli";
import { Wallet } from "ethers";
import sample from "lodash/sample";
import { Client } from "@xmtp/xmtp-js";
import { MESSAGES_FILENAME, RECIPIENTS_FILENAME } from "./constants";
import { loadFromFile } from "./utils";
const messages = await loadFromFile(MESSAGES_FILENAME);
const recipients = await loadFromFile(RECIPIENTS_FILENAME);
export async function activateAccount(key: string) {
const wallet = new Wallet(key);
const { address } = wallet;
cli.spinner(`Активирую адрес ${address}`);
const isActivated = await Client.canMessage(address, { env: "production" });
if (isActivated) {
cli.spinner(`Адрес ${address} был активирован ранее`, true);
return;
}
await Client.create(wallet, { env: "production" });
cli.spinner(`Адрес ${address} активирован`, true);
}
export async function sendMessage(key: string) {
const wallet = new Wallet(key);
const { address } = wallet;
console.log(`Адрес: ${address}`);
const recipient = sample(recipients.filter((item) => item !== address));
if (!recipient) {
console.log("Нет подходящего получателя");
return;
}
const message = sample(messages);
if (!message) {
console.log("Нет подходящего сообщения");
return;
}
const xmtp = await Client.create(wallet, { env: "production" });
const conversation = await xmtp.conversations.newConversation(recipient);
cli.spinner("Отправляю сообщение");
await conversation.send(message);
cli.spinner(`Отправлено сообщение ${message} -> ${recipient}`, true);
}