-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
213 lines (200 loc) · 7.26 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { Client } from "@xmtp/xmtp-js";
import { GrpcApiClient } from "@xmtp/grpc-api-client";
import { Wallet } from "ethers";
const XMTP_RATE_LIMIT = 1000;
const XMTP_RATE_LIMIT_TIME = 60 * 1000; // 1 minute
const XMTP_RATE_LIMIT_TIME_INCREASE = XMTP_RATE_LIMIT_TIME * 5; // 5 minutes
const BROADCAST_AMOUNT = 10000;
const broadcastAddresses = new Array<string>(BROADCAST_AMOUNT).fill("");
const delay = (ms: number) =>
new Promise<void>((resolve) => setTimeout(resolve, ms));
const run = async () => {
const startTime = Date.now();
const wallet = Wallet.createRandom();
// Create the client with your wallet. This will connect to the XMTP development network by default
console.log("Creating client");
const client = await Client.create(wallet, {
apiClientFactory: GrpcApiClient.fromOptions,
});
const batches: string[][] = [];
let batch: string[] = [];
const canMessageAddresses = await client.canMessage(broadcastAddresses);
let errorCount = 0;
for (let i = 0; i < canMessageAddresses.length; i++) {
if (canMessageAddresses[i]) {
batch.push(broadcastAddresses[i]);
}
// Add a batch of 500 addresses to the batches array
// An introduction message is sent for new contacts, so each new message will actually be 2 messages in this case
// We want to send 1000 messages per minute, so we split the batches in half
// Additional optimization can be done to send messages to contacts that have already been introduced
if (batch.length === XMTP_RATE_LIMIT) {
batches.push(batch);
batch = [];
}
}
if (batch.length > 0) {
batches.push(batch);
}
let currentRateLimitWaitTime = XMTP_RATE_LIMIT_TIME;
for (let i = 0; i < batches.length; i++) {
let batchWaitTime = currentRateLimitWaitTime;
const batchResponse = await Promise.allSettled(
batches[i].map(async (address, index) => {
const conversation = await client.conversations.newConversation(
address
);
try {
await conversation.send("Hello from XMTP!");
} catch (err) {
errorCount++;
console.log(`Rate limited, waiting ${batchWaitTime}`);
await delay(batchWaitTime);
try {
await conversation.send("Hello from XMTP!");
} catch (err) {
errorCount++;
currentRateLimitWaitTime = XMTP_RATE_LIMIT_TIME_INCREASE;
batchWaitTime = currentRateLimitWaitTime;
console.log(`Rate limited more, waiting ${batchWaitTime}`);
await delay(batchWaitTime);
await conversation.send("Hello from XMTP!");
}
}
console.log(`Sent message for batch ${i} index ${index} to ${address}`);
})
);
for (let j = 0; j < batchResponse.length; j++) {
const element = batchResponse[j];
if (element.status === "rejected") {
errorCount++;
console.error(element.reason);
// Add error handling here
}
}
if (i !== batches.length - 1) {
// Wait between batches
console.log(`Waiting between batches ${i} and ${i + 1}`);
await delay(currentRateLimitWaitTime);
}
}
const endTime = Date.now();
console.log(`Total time: ${endTime - startTime}ms with ${errorCount} errors`);
};
const runBatches = async () => {
const startTime = Date.now();
const wallet = Wallet.createRandom();
// Create the client with your wallet. This will connect to the XMTP development network by default
console.log("Creating client");
const client = await Client.create(wallet, {
apiClientFactory: GrpcApiClient.fromOptions,
});
const batches: string[][] = [];
let batch: string[] = [];
const canMessageAddresses = await client.canMessage(broadcastAddresses);
let errorCount = 0;
for (let i = 0; i < canMessageAddresses.length; i++) {
if (canMessageAddresses[i]) {
batch.push(broadcastAddresses[i]);
}
// Add a batch of 500 addresses to the batches array
// An introduction message is sent for new contacts, so each new message will actually be 2 messages in this case
// We want to send 1000 messages per minute, so we split the batches in half
// Additional optimization can be done to send messages to contacts that have already been introduced
if (batch.length === XMTP_RATE_LIMIT / 2) {
batches.push(batch);
batch = [];
}
}
if (batch.length > 0) {
batches.push(batch);
}
for (let i = 0; i < batches.length; i++) {
const batch: string[] = [];
await Promise.all(
batches[i].map(async (address, index) => {
const conversation = await client.conversations.newConversation(
address
);
try {
await conversation.send("Hello from XMTP!");
console.log(
`Sent message for batch ${i} index ${index} to ${address}`
);
} catch (err) {
errorCount++;
console.error(err);
batch.push(address);
// Add error handling here
}
})
);
if (i !== batches.length - 1) {
// Wait between batches
console.log(`Waiting between batches ${i} and ${i + 1}`);
await delay(XMTP_RATE_LIMIT_TIME_INCREASE);
}
if (batch.length > 0) {
batches.push(batch);
}
}
const endTime = Date.now();
console.log(`Total time: ${endTime - startTime}ms with ${errorCount} errors`);
};
const runWait = async () => {
const startTime = Date.now();
const wallet = Wallet.createRandom();
// Create the client with your wallet. This will connect to the XMTP development network by default
console.log("Creating client");
const client = await Client.create(wallet, {
apiClientFactory: GrpcApiClient.fromOptions,
});
const canMessageAddresses = await client.canMessage(broadcastAddresses);
let errorCount = 0;
let currentWait = 0;
for (let i = 0; i < canMessageAddresses.length; i++) {
if (canMessageAddresses[i]) {
const conversation = await client.conversations.newConversation(
broadcastAddresses[i]
);
try {
await conversation.send("Hello from XMTP!");
} catch (err) {
errorCount++;
console.error(err);
if (currentWait === 0) {
currentWait = XMTP_RATE_LIMIT_TIME;
} else if (currentWait === XMTP_RATE_LIMIT_TIME) {
currentWait = XMTP_RATE_LIMIT_TIME_INCREASE;
}
await delay(currentWait);
try {
await conversation.send("Hello from XMTP!");
} catch (err) {
errorCount++;
console.error(err);
await delay(XMTP_RATE_LIMIT_TIME_INCREASE);
try {
await conversation.send("Hello from XMTP!");
} catch (err) {
errorCount++;
console.error(err);
}
}
}
console.log(`Sent message ${i} to ${broadcastAddresses[i]}`);
}
}
const endTime = Date.now();
// Total time awaiting each send: 3421425ms with 8 errors ~57 minutes
// Total time awaiting each send: 8232861ms with 24702 errors
console.log(
`Total time awaiting each send: ${
endTime - startTime
}ms with ${errorCount} errors`
);
};
runBatches();
// runWait: Total time awaiting each send: 3421425ms with 8 errors ~57 minutes
// runBatch: Total time awaiting each send: 3354697ms with 0 errors
// Run: Total time batch greedy: 8232861ms with 24702 errors