Skip to content

Commit 93ec4de

Browse files
authored
Merge pull request #59 from GeneralMagicio/farcaster-bot-new-changes
Farcaster bot new changes
2 parents d61b45e + 8147a29 commit 93ec4de

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/cronJobs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { schedule } from 'node-cron';
2-
import { sendCastsFor12Hours } from './neynar/utils';
2+
import { sendDailyCasts } from './neynar/utils';
33

4-
const sendCastsCronJobTime = '21 21 0,12 * * *'; // at 00:21 and 12:21 every day
4+
const sendCastsCronJobTime = '21 1 17 * * *'; // at 17:01 every day
55

66
export const initializeCronJobs = () => {
77
sendCastsCronJob();
@@ -10,7 +10,7 @@ export const initializeCronJobs = () => {
1010
const sendCastsCronJob = () => {
1111
schedule(sendCastsCronJobTime, async () => {
1212
try {
13-
await sendCastsFor12Hours();
13+
await sendDailyCasts();
1414
} catch (e) {
1515
console.error('sendCastsCronJob error', e);
1616
}

src/neynar/utils.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const getDelegations = async (start: number, end?: number) => {
1919

2020
await prisma.$connect();
2121

22-
const delegations = await prisma.collectionDelegation.findMany({
22+
const newDelegations = await prisma.collectionDelegation.findMany({
2323
where: {
2424
platform: 'FARCASTER',
2525
createdAt: {
@@ -29,10 +29,30 @@ const getDelegations = async (start: number, end?: number) => {
2929
},
3030
});
3131

32+
const uniqueFids = Array.from(
33+
new Set(newDelegations.map((delegation) => delegation.target)),
34+
);
35+
36+
console.log('Today new delegations FIDs: ', uniqueFids);
37+
38+
if (uniqueFids.length === 0) {
39+
await prisma.$disconnect();
40+
return [];
41+
}
42+
43+
const targetsDelegations = await prisma.collectionDelegation.findMany({
44+
where: {
45+
platform: 'FARCASTER',
46+
target: {
47+
in: uniqueFids,
48+
},
49+
},
50+
});
51+
3252
const result: { fid: number; username: string; totalDelegates: number }[] =
3353
[];
3454

35-
for (const delegation of delegations) {
55+
for (const delegation of targetsDelegations) {
3656
const index = result.findIndex(
3757
(el) => el.fid === Number(delegation.target),
3858
);
@@ -50,34 +70,22 @@ const getDelegations = async (start: number, end?: number) => {
5070
}
5171
}
5272

73+
console.log('Today casts to be sent: ', result);
74+
5375
await prisma.$disconnect();
5476

5577
return result;
5678
};
5779

58-
export const sendCastsFor12Hours = async () => {
80+
export const sendDailyCasts = async () => {
5981
const currentTimestamp = new Date();
60-
// Get the timestamp for 00:00 of the current day
61-
const midnightTimestamp = new Date(currentTimestamp);
62-
midnightTimestamp.setHours(0, 0, 0, 0); // set to 00:00:00
63-
// Get the timestamp for 12:00 of the current day
64-
const noonTimestamp = new Date(currentTimestamp);
65-
noonTimestamp.setHours(12, 0, 0, 0); // set to 12:00:00
66-
// Check if the current time is past 00:00 and 12:00
67-
if (currentTimestamp < midnightTimestamp) {
68-
// If current time is earlier than today's midnight, subtract 1 day for midnight timestamp
69-
midnightTimestamp.setDate(midnightTimestamp.getDate() - 1);
70-
}
71-
if (currentTimestamp < noonTimestamp) {
72-
// If current time is earlier than today's noon, subtract 1 day for noon timestamp
73-
noonTimestamp.setDate(noonTimestamp.getDate() - 1);
74-
}
75-
let startTimestamp = midnightTimestamp.getTime();
76-
let endTimestamp = noonTimestamp.getTime();
77-
if (endTimestamp < startTimestamp) {
78-
[startTimestamp, endTimestamp] = [endTimestamp, startTimestamp];
79-
}
80-
const delegations = await getDelegations(startTimestamp, endTimestamp);
82+
// Get the timestamp for 17:00 of the current day
83+
const endTimestamp = new Date(currentTimestamp);
84+
endTimestamp.setHours(17, 0, 0, 0); // set to 00:00:00
85+
const delegations = await getDelegations(
86+
endTimestamp.getTime() - 24 * 60 * 60 * 1000, // Get the timestamp for 17:00 of the previous day
87+
endTimestamp.getTime(),
88+
);
8189
if (!delegations || delegations.length === 0) return;
8290
for (const delegation of delegations) {
8391
await sendDelegationCast(delegation);
@@ -91,17 +99,27 @@ const sendDelegationCast = async (props: {
9199
totalDelegates: number;
92100
}) => {
93101
const { username, totalDelegates } = props || {};
102+
const oneDelegate = totalDelegates === 1;
94103
if (!farcasterSignerUUID) {
95104
throw new Error(
96105
'Make sure you set FARCASTER_SIGNER_UUID in your .env file',
97106
);
98107
}
99108
await neynarClient.publishCast(
100109
farcasterSignerUUID,
101-
`Hey @${username} 👋
110+
`@${username} 👋
102111
103-
${totalDelegates} people have delegated to you in the last 12 hours 🥳
112+
🗳️ ${totalDelegates} ${
113+
oneDelegate ? 'person has' : 'people have'
114+
} delegated to you in @Pairwise's Liquid Democracy experiment!
104115
105-
They want you to vote on their behalf in the @Optimism Retro Funding 6 Round. Go to https://app.pairwise.vote/ and rank the projects!`,
116+
🤝 Delegate this voting power to the @Farcaster users you trust to judge the impact of the governance projects in @Optimism's Retro Funding 6 round or vote yourself! 🫡
117+
118+
👇
119+
https://app.pairwise.vote
120+
`,
121+
);
122+
console.log(
123+
`Cast successfully sent to @${username} for ${totalDelegates} delegations`,
106124
);
107125
};

0 commit comments

Comments
 (0)