forked from zmzimpl/auto-friend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-message.js
273 lines (250 loc) · 8.36 KB
/
auto-message.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import axios from "axios";
import { readFileSync } from "fs";
import { getUserInfo, getDir, logIntro, sleep, decrypt, chalk } from "./utils";
import WebSocket from "ws";
import { createPublicClient, formatEther, webSocket } from "viem";
import { base } from "viem/chains";
import pkg from "lodash";
import readlineSync from "readline-sync";
import consoleStamp from "console-stamp";
const { throttle } = pkg;
const wallets = JSON.parse(readFileSync(getDir("wallets.json"), "utf8"));
const websocketClient = createPublicClient({
chain: base,
transport: webSocket(
"wss://base-mainnet.blastapi.io/fe9c30fc-3bc5-4064-91e2-6ab5887f8f4d"
),
});
const abi = JSON.parse(readFileSync(getDir("abi.json"), "utf-8"));
const contractAddress = "0xcf205808ed36593aa40a44f10c7f7c2f67d4a4d4";
const autoMessage = async (wallet) => {
let holdings = [];
const wss = `wss://prod-api.kosetto.com/?authorization=${wallet.authorization}`;
let socket;
let pingInterval;
let unwatch;
const sendPing = () => {
// 检查连接是否打开
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ action: "ping" }));
}
};
const connect = () => {
socket = new WebSocket(wss);
socket.on("open", () => {
console.log("Connected to the WebSocket server");
// 当连接打开时,每3秒发送一次ping
pingInterval = setInterval(sendPing, 3000);
monitor();
});
socket.on("message", (data) => {
const message = data.toString();
console.log("Message from server:", JSON.parse(message));
});
socket.on("error", (error) => {
console.error("WebSocket Error:", error);
});
socket.on("close", (code, reason) => {
console.log(`Connection closed with code ${code}: ${reason}`);
// 清除ping的定时器,因为连接已关闭
clearInterval(pingInterval);
setTimeout(() => {
console.log("Trying to reconnect...");
connect();
}, 5000); // 5秒后尝试重连
});
};
const getHoldings = async () => {
try {
const res = await axios({
method: "get",
url: `https://prod-api.kosetto.com/portfolio/${wallet.address}`,
headers: {
Host: "prod-api.kosetto.com",
"sec-ch-ua":
'"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
accept: "application/json",
"sec-ch-ua-platform": '"Android"',
"sec-ch-ua-mobile": "?1",
authorization: wallet.authorization,
"user-agent":
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36",
"content-type": "application/json",
origin: "https://www.friend.tech",
"sec-fetch-site": "cross-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
referer: "https://www.friend.tech/",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"if-none-match": wallet.ifNoneMatch,
timeout: 3000,
},
});
if (res.data?.holdings?.length) {
holdings = res.data?.holdings;
} else {
holdings = [];
}
} catch (error) {
console.log("refreshHoldings error", error.message);
await sleep(3);
await getHoldings();
}
};
const fetchGlobalActivity = async () => {
const { data: globalActivities } = await axios.get(
"https://prod-api.kosetto.com/global-activity",
{
headers: {
accept: "application/json",
authorization:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZGRyZXNzIjoiMHg2MzRiNWIwZDk0MGY2YTRjNDhkNWU2MTgwYTQ3ZWJiNTQzYTIzZjQ2IiwiaWF0IjoxNjk0MzMwNzgzLCJleHAiOjE2OTY5MjI3ODN9.j1Zq2G46fwTl356O5wnOTn2ZQc-F3pvECpp1YApJDl4",
"content-type": "application/json",
"sec-ch-ua":
'"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
Referer: "https://www.friend.tech/",
"Referrer-Policy": "strict-origin-when-cross-origin",
},
}
);
// console.log(globalActivities.events);
};
const fetchProfile = async (subject) => {
console.log(chalk.gray("fetch user profile..."));
try {
const res = await axios.get(
`https://prod-api.kosetto.com/users/${subject}`,
{
timeout: 3000,
}
);
if (res.data?.id) {
const username = res.data?.twitterUsername;
if (!username) {
console.log("no twitter username, skip");
return {};
}
const userInfo = await getUserInfo(username);
console.log(
chalk.blue(`${username} followers ${userInfo?.followers_count}`)
);
return {
subject: subject,
username,
followers: userInfo?.followers_count,
};
} else {
return {};
}
} catch (error) {
console.log(chalk.red("fetchProfile err", error.message));
return {};
}
};
const monitor = async () => {
if (unwatch) {
await unwatch();
}
unwatch = websocketClient.watchContractEvent({
address: contractAddress,
abi: abi,
eventName: "Trade",
onLogs: throttle(async (logs) => {
const lowPriceLogs = logs.filter((log) => {
return parseFloat(formatEther(log.args.ethAmount)) < 0.01;
});
for (let index = 0; index < lowPriceLogs.length; index++) {
const log = lowPriceLogs[index];
const profile = await fetchProfile(log.args.subject);
if (!profile.followers) continue;
if (profile.followers > 20000) {
// 向你自己的聊天室发送内容
sendMessage({
...profile,
price: parseFloat(formatEther(log.args.ethAmount)),
roomOwner: wallet.user_id,
chatRoomId: wallet.address,
});
// 下面这个代码会向你所有持有的 key 的聊天室发送内容
// await getHoldings();
// if (holdings.length) {
// for (let index = 0; index < holdings.length; index++) {
// const holding = holdings[index];
// sendMessage(
// {
// ...profile,
// price: parseFloat(formatEther(log.args.ethAmount)),
// roomOwner: holding.username,
// },
// holding.chatRoomId
// );
// await sleep(2);
// }
// }
}
}
// const profile = await fetchProfile(log.args.subject);
}, 2000),
// 每 3 秒执行一次,因为频率太高,获取推特的关注人数方法会有问题() => checkIfBuy(logs)
});
};
function generateHexId(length = 10) {
let result = "";
const characters = "0123456789abcdef";
for (let i = 0; i < length; i++) {
result += characters.charAt(
Math.floor(Math.random() * characters.length)
);
}
return result;
}
const sendMessage = (profile, chatRoomId) => {
const msg = {
action: "sendMessage",
text: `${
profile.roomOwner !== wallet.user_id
? "Hi, " + profile.roomOwner
: "New Event"
}
Low Price Twitter Active:
Twitter Name: @${profile.username}
Followers: ${profile.followers}
Price: ${profile.price} ETH
`,
imagePaths: [],
chatRoomId: chatRoomId,
clientMessageId: generateHexId(),
};
socket.send(JSON.stringify(msg));
};
const execute = async () => {
connect();
// await getHoldings();
};
execute();
};
logIntro();
consoleStamp(console, {
format: ":date(yyyy/mm/dd HH:MM:ss)",
});
const password1 = readlineSync.question("Password1: ", {
hideEchoBack: true, // The typed text on screen is hidden by `*` (default).
});
const password2 = readlineSync.question("Password2: ", {
hideEchoBack: true, // The typed text on screen is hidden by `*` (default).
});
process.env.pw1 = password1;
process.env.pw2 = password2;
if (password1 && password2) {
for (let index = 0; index < wallets.length; index++) {
const wallet = wallets[index];
process.env.useTwitterAPI = wallet.useTwitterAPI;
autoMessage({
...wallet,
pk: decrypt(wallet.pk, password1, password2),
authorization: decrypt(wallet.authorization, password1, password2),
});
}
}