Skip to content

Commit

Permalink
Added launchpad notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisassad committed Dec 3, 2024
1 parent 16641bd commit 9ed98fe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/core/src/integration/events.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Comment,
FarcasterAction,
FarcasterCast,
LaunchpadTrade,
PG_INT,
Reaction,
SubscriptionPreference,
Expand All @@ -11,7 +12,6 @@ import { z } from 'zod';
import {
CommunityStakeTrade,
LaunchpadTokenCreated,
LaunchpadTrade,
NamespaceDeployed,
} from './chain-event.schemas';
import { EventMetadata } from './util.schemas';
Expand Down Expand Up @@ -41,6 +41,9 @@ export const CommentCreated = Comment.omit({ search: true }).extend({
export const CommentUpvoted = Reaction.omit({ thread_id: true }).extend({
comment_id: PG_INT,
});

export const TradeEvent = LaunchpadTrade;

export const GroupCreated = z.object({
groupId: z.string(),
userId: z.string(),
Expand Down
8 changes: 8 additions & 0 deletions libs/core/src/integration/notifications.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ETHERS_BIG_NUMBER } from '@hicommonwealth/schemas';
import { z } from 'zod';
import * as events from './events.schemas';

Expand Down Expand Up @@ -157,3 +158,10 @@ export const WebhookNotification = z.object({
object_url: z.string(),
object_summary: z.string(),
});

export const TradeEventNotification = z.object({
community_id: z.string().describe('The community associated with the token'),
symbol: z.string().describe('The token address'),
is_buy: z.boolean().describe('If the trade was a buy or sell'),
floating_supply: ETHERS_BIG_NUMBER.describe('The remaining supply'),
});
6 changes: 6 additions & 0 deletions libs/core/src/ports/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CommentCreatedNotification,
CommunityStakeNotification,
SnapshotProposalCreatedNotification,
TradeEventNotification,
UpvoteNotification,
UserMentionedNotification,
WebhookNotification,
Expand Down Expand Up @@ -293,6 +294,7 @@ export enum WorkflowKeys {
EmailRecap = 'email-recap',
EmailDigest = 'email-digest',
Webhooks = 'webhooks',
TradeEvent = 'trade-event',
}

export enum KnockChannelIds {
Expand Down Expand Up @@ -346,6 +348,10 @@ export type NotificationsProviderTriggerOptions =
data: z.infer<typeof UpvoteNotification>;
key: WorkflowKeys.NewUpvotes;
}
| {
data: z.infer<typeof TradeEventNotification>;
key: WorkflowKeys.TradeEvent;
}
))
| WebhookProviderOptions;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
EventHandler,
notificationsProvider,
WorkflowKeys,
} from '@hicommonwealth/core';
import { models } from '@hicommonwealth/model';
import { QueryTypes } from 'sequelize';
import z from 'zod';

const output = z.boolean();

export const processTradeEvent: EventHandler<
'TradeEvent',
typeof output
> = async ({ payload }) => {
const provider = notificationsProvider();

const results = await models.sequelize.query<{
user_id: number;
community_id: string;
symbol: string;
}>(
`
SELECT U.user_id, C.community_id, T.symbol from "Users" U
JOIN "Addresses" A ON U.user_id = A.user_id
JOIN "Communities" C ON C.id = A.community_id
JOIN "Tokens" T ON T.namespace = C.namespace
WHERE :token_address = T.token_address;
`,
{
replacements: { token_address: payload.token_address },
type: QueryTypes.SELECT,
},
);

await provider.triggerWorkflow({
key: WorkflowKeys.TradeEvent,
users: results.map((u) => ({ id: String(u.user_id) })),
data: {
community_id: results[0].community_id,
symbol: results[0].symbol,
is_buy: payload.is_buy,
},
});

// if (payload.floating_supply)
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { processCommentUpvoted } from './eventHandlers/commentUpvoted';
import { processSnapshotProposalCreated } from './eventHandlers/snapshotProposalCreated';
import { processThreadCreated } from './eventHandlers/threadCreated';
import { processThreadUpvoted } from './eventHandlers/threadUpvoted';
import { processTradeEvent } from './eventHandlers/tradeEvent';
import { processUserMentioned } from './eventHandlers/userMentioned';

const notificationInputs = {
Expand All @@ -15,6 +16,7 @@ const notificationInputs = {
UserMentioned: events.UserMentioned,
ThreadUpvoted: events.ThreadUpvoted,
CommentUpvoted: events.CommentUpvoted,
TradeEvent: events.TradeEvent,
};

export function NotificationsPolicy(): Policy<typeof notificationInputs> {
Expand Down Expand Up @@ -49,6 +51,10 @@ export function NotificationsPolicy(): Policy<typeof notificationInputs> {
CommentUpvoted: async (event) => {
await processCommentUpvoted(event);
},
// eslint-disable-next-line @typescript-eslint/require-await
TradeEvent: async (event) => {
await processTradeEvent(event);
},
},
};
}

0 comments on commit 9ed98fe

Please sign in to comment.