Skip to content

Commit

Permalink
chore: update error returned on create group
Browse files Browse the repository at this point in the history
chore: update NODE_ENV in dev
  • Loading branch information
peterferguson committed Apr 8, 2024
1 parent 9399553 commit a7511a8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 126 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
# override default entrypoint allows us to do `bun install` before serving
entrypoint: []
# execute bun install before we start the dev server in watch mode
command: "/bin/sh -c 'bun install && bun run --watch src/index.ts'"
command: "/bin/sh -c 'bun install && NODE_ENV=development bun run --watch src/index.ts'"
# expose the right ports
ports: ["8080:8080"]
# setup a host mounted volume to sync changes to the container
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "index.ts",
"type": "module",
"scripts": {
"dev": "bun --watch src/index.ts",
"dev": "NODE_ENV=development bun --watch src/index.ts",
"build": "bun build src/index.ts",
"start": "NODE_ENV=production bun src/index.ts",
"test": "bun test",
Expand Down Expand Up @@ -34,4 +34,4 @@
"viem": "^2.9.5",
"zod": "^3.22.4"
}
}
}
16 changes: 10 additions & 6 deletions src/actions/create-xmtp-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { bot } from "../lib/xmtp/client";
import { getDeployments } from "./get-deployments";
import { getGroupByWalletAddress } from "./get-group-by-wallet-address";
import { addMembers } from "./add-members";
import type { CliError } from "../lib/xmtp/cli";

/**
* TODO: ?
Expand Down Expand Up @@ -74,15 +75,18 @@ export async function createXmtpGroup(
if (groupId) await db.insert(schema.groups).values({ id: groupId });
} catch (e) {
console.log("failed to create group", groupId);
console.error("error code ->", (e as any).exitCode);
console.error("error ->", (e as any).stderr.toString());
} finally {
if (!groupId) {
console.log("Failed to create group");
return { groupId: undefined };
if (e instanceof Error) console.error(e.message);
if (e && typeof e === "object" && "exitCode" in e && "stderr" in e) {
console.error("error code ->", (e as CliError).exitCode);
console.error("error ->", (e as CliError).stderr.toString());
}
}

if (!groupId) {
console.log("Failed to create group");
return { groupId: undefined };
}

let deployments: Awaited<ReturnType<typeof getDeployments>> | undefined =
undefined;

Expand Down
150 changes: 73 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Elysia, t } from "elysia";
import {
createXmtpGroup,
createXmtpGroupValidator,
} from "./actions/create-xmtp-group";
import { getGroup } from "./actions/get-group";
import { syncPendingMembers } from "./actions/sync-pending-members";
import { AddressLiteral } from "./lib/validators";
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 { cron, Patterns } from "@elysiajs/cron";
import { db } from "./db";
import { sql } from "drizzle-orm";
import { Elysia, t } from 'elysia'
import { createXmtpGroup, createXmtpGroupValidator } from './actions/create-xmtp-group'
import { getGroup } from './actions/get-group'
import { syncPendingMembers } from './actions/sync-pending-members'
import { AddressLiteral } from './lib/validators'
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 { cron, Patterns } from '@elysiajs/cron'
import { db } from './db'
import { sql } from 'drizzle-orm'
import { bot } from './lib/xmtp/client'

/**
* This service is responsible for keeping xmtp group chat members in sync with the members of a safe.
Expand All @@ -29,6 +27,7 @@ import { sql } from "drizzle-orm";
* another call will have to be made to the service once the account is deployed.
*
* TODO: Add a method to link a deployed counterfactual account to the group chat
* TODO: Add the ability for a member to remove themselves from a group chat
* ! only run the next two methods if the group chat is a **deployed** safe
* TODO: Add a job to periodically check for new members in a safe and add them to the group chat
* TODO: Add a job to periodically check for removed members in a safe and remove them from the group chat
Expand All @@ -37,7 +36,7 @@ import { sql } from "drizzle-orm";
export default new Elysia()
.use(
cron({
name: "heartbeat",
name: 'heartbeat',
pattern: Patterns.EVERY_10_SECONDS,
run() {
console.log(
Expand All @@ -46,111 +45,108 @@ export default new Elysia()
sql`SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();`,
)[0] / 1024
} KB`,
);
)
},
}),
)
.use(
cron({
name: "sync-pending-members",
name: 'sync-pending-members',
pattern: Patterns.EVERY_5_MINUTES,
async run() {
console.log("try sync pending members");
await syncPendingMembers().catch((e) => console.error(e));
console.log('try sync pending members')
await syncPendingMembers().catch((e) => console.error(e))
},
}),
)
.get("/", () => "Onit XMTP bot 🤖")
.group(
"/wallet/:address",
{ params: t.Object({ address: AddressLiteral }) },
(app) => {
return app.get("/", async ({ params: { address } }) => {
console.log("getting groups by address", address);
// - get the addresses safes
const safes = await getOwnersSafes(address);
.get('/', async () => {
if (process.env.NODE_ENV === 'development') {
console.log('groups', await bot.listGroups())
await bot.send('5eb5b1fa27adc585a75cdedd6a1d4d5d', 'Hello').catch(console.error)
}

console.log("safes ->", safes);
return 'Onit XMTP bot 🤖'
})
.group('/wallet/:address', { params: t.Object({ address: AddressLiteral }) }, (app) => {
return app.get('/', async ({ params: { address } }) => {
console.log('getting groups by address', address)
// - get the addresses safes
const safes = await getOwnersSafes(address)

// - check for groups with the safe address
return (await getGroupsByWalletAddresses(safes)) || [];
});
},
)
.group("/group/:groupId", (app) => {
console.log('safes ->', safes)

// - check for groups with the safe address
return (await getGroupsByWalletAddresses(safes)) || []
})
})
.group('/group/:groupId', (app) => {
return app
.get("/", async ({ params: { groupId } }) => {
if (!groupId) return "Invalid group id";
return await getGroup(groupId);
.get('/', async ({ params: { groupId } }) => {
if (!groupId) return 'Invalid group id'
return await getGroup(groupId)
})
.get("/members", async ({ params: { groupId } }) => {
if (!groupId) return "Invalid group id";
return (await getGroup(groupId))?.pendingMembers || [];
.get('/members', async ({ params: { groupId } }) => {
if (!groupId) return 'Invalid group id'
return (await getGroup(groupId))?.pendingMembers || []
})
.post(
"/members",
'/members',
async ({ params: { groupId }, body: { members, type } }) => {
const group = await getGroup(groupId);
if (!groupId || !group) return "Invalid group id";
const group = await getGroup(groupId)
if (!groupId || !group) return 'Invalid group id'
// - we only enable adding and removing members if a wallet is not already attached to the group
if (group.wallets.length)
return "Members on group chat with wallets are managed by who is a signer on each of the wallet";
return 'Members on group chat with wallets are managed by who is a signer on each of the wallet'

switch (type) {
case "add":
return addMembers(groupId, members);
case "remove":
return removeMembers(groupId, members);
case 'add':
return addMembers(groupId, members)
case 'remove':
return removeMembers(groupId, members)
default:
return "Invalid type";
return 'Invalid type'
}
},
{
body: t.Object({
members: t.Array(AddressLiteral),
type: t.Union([t.Literal("add"), t.Literal("remove")]),
type: t.Union([t.Literal('add'), t.Literal('remove')]),
}),
},
)
.get("/wallets", async ({ params: { groupId } }) => {
if (!groupId) return "Invalid group id";
return (await getGroup(groupId))?.wallets || [];
.get('/wallets', async ({ params: { groupId } }) => {
if (!groupId) return 'Invalid group id'
return (await getGroup(groupId))?.wallets || []
})
.post("/link-wallet", async ({ params: { groupId }, body }) => {
.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";
});
return 'Not implemented'
})
})
.group("/bot", (app) => {
.group('/bot', (app) => {
return app
.get("/sync-pending-members", async () => {
const pendingMembers = await syncPendingMembers();
return JSON.stringify(pendingMembers, null, 4);
.get('/sync-pending-members', async () => {
const pendingMembers = await syncPendingMembers()
return JSON.stringify(pendingMembers, null, 4)
})
.post(
"/create",
'/create',
async ({ body }) => {
const result = await createXmtpGroup(body);
const result = await createXmtpGroup(body)

const { groupId, members, pendingMembers, deployments } = result;
const { groupId, members, pendingMembers, deployments } = result

console.log(
"Created group",
groupId,
members,
pendingMembers,
deployments,
);
console.log('Created group', groupId, members, pendingMembers, deployments)

if (!groupId) return "Failed to create group";
if (!groupId) return 'Failed to create group'

return result;
return result
},
{ body: createXmtpGroupValidator },
);
)
})
.listen(8080, ({ hostname, port }) => {
console.log(`🦊 Elysia is running at http://${hostname}:${port}`);
});
console.log(`🦊 Elysia is running at http://${hostname}:${port}`)
})
Loading

0 comments on commit a7511a8

Please sign in to comment.