Skip to content

Commit

Permalink
feat: add crons for heartbeat & member sync
Browse files Browse the repository at this point in the history
  • Loading branch information
peterferguson committed Apr 4, 2024
1 parent eb02fd1 commit ef8e84f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@elysiajs/cron": "^1.0.3",
"@safe-global/api-kit": "^2.2.0",
"@xmtp/grpc-api-client": "^0.2.4",
"@xmtp/xmtp-js": "^11.5.0",
Expand Down
22 changes: 14 additions & 8 deletions src/actions/add-members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export async function addMembers(
// ! xmtp doesn't return a list of failed members on creation
try {
const addedMembers = await bot.addMembers(groupId, members as string[]);
console.log(`Group ID is ${groupId} -> Added members ${JSON.stringify(addedMembers)}`);
console.log(
`Group ID is ${groupId} -> Added members ${JSON.stringify(addedMembers)}`,
);
} catch (e) {
// ! if a adding members fails we need to try each of them individually to see which of the members failed

Expand All @@ -50,23 +52,27 @@ export async function addMembers(
) {
console.log("member already in group", member);
throw new MemberAddFailure(member, "existing");
} else throw new MemberAddFailure(member, "pending");
}
throw new MemberAddFailure(member, "pending");
}),
),
);

for (const result of addPromises) {
if (result.status === "rejected" && result.reason instanceof MemberAddFailure) {
const { address, type } = result.reason;
if (type === "existing") existingMembers.push(address);
else pendingMembers.push(address);
if (
result.status === "rejected" &&
result.reason instanceof MemberAddFailure
) {
const { address, type } = result.reason;
if (type === "existing") existingMembers.push(address);
else pendingMembers.push(address);
}
}
}


const successfullyAddedMembers = members.filter(
(member) => ![pendingMembers, existingMembers].flat().includes(member as Address),
(member) =>
![pendingMembers, existingMembers].flat().includes(member as Address),
);

if (pendingMembers.length !== 0)
Expand Down
26 changes: 24 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { getOwnersSafes } from "./actions/get-owners-safes";
import { getGroupsByWalletAddresses } from "./actions/get-group-by-wallet-address";
import { addMembers } from "./actions/add-members";
import { removeMembers } from "./actions/remove-members";
import { db } from "./db";
import { sql } from "drizzle-orm";
import cron from "@elysiajs/cron";

/**
* This service is responsible for keeping xmtp group chat members in sync with the members of a safe.
Expand All @@ -34,6 +33,26 @@ import { sql } from "drizzle-orm";
*/

export default new Elysia()
.use(
cron({
name: "heartbeat",
pattern: "*/10 * * * * *", // every 10 seconds
run() {
console.log("Heartbeat");
},
}),
)
.use(
cron({
name: "sync-pending-members",
pattern: "* */5 * * * *", // every 5 minutes
async run() {
console.log("try sync pending members");
const pendingMembers = await syncPendingMembers();
return JSON.stringify(pendingMembers, null, 4);
},
}),
)
.get("/", () => "Onit XMTP bot 🤖")
.group("/:address", (app) => {
return app.get(
Expand Down Expand Up @@ -91,6 +110,9 @@ export default new Elysia()
return (await getGroup(groupId))?.wallets || [];
})
.post("/link-wallet", async ({ params: { groupId }, body }) => {
// TODO: check that each member is a member of the group
// TODO: if so then add the wallet to the group
// TODO: if not return an error
return "Not implemented";
});
})
Expand Down

0 comments on commit ef8e84f

Please sign in to comment.