Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launchpad Notifications #10137

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/adapters/src/rabbitmq/configuration/rascalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function getAllRascalConfigs(
RascalRoutingKeys.NotificationsProviderUserMentioned,
RascalRoutingKeys.NotificationsProviderCommentUpvoted,
RascalRoutingKeys.NotificationsProviderThreadUpvoted,
RascalRoutingKeys.NotificationsProviderTradeEvent,
],
},
[RascalBindings.NotificationsSettings]: {
Expand Down
1 change: 1 addition & 0 deletions libs/adapters/src/rabbitmq/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export enum RascalRoutingKeys {
NotificationsProviderChainEventCreated = EventNames.ChainEventCreated,
NotificationsProviderSnapshotProposalCreated = EventNames.SnapshotProposalCreated,
NotificationsProviderUserMentioned = EventNames.UserMentioned,
NotificationsProviderTradeEvent = EventNames.TradeEvent,

NotificationsSettingsPreferencesUpdated = EventNames.SubscriptionPreferencesUpdated,

Expand Down
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
7 changes: 7 additions & 0 deletions libs/core/src/integration/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export enum EventNames {
FarcasterVoteCreated = 'FarcasterVoteCreated',
ContestRolloverTimerTicked = 'ContestRolloverTimerTicked',

// Launchpad
TradeEvent = 'TradeEvent',

// Preferences
SubscriptionPreferencesUpdated = 'SubscriptionPreferencesUpdated',

Expand Down Expand Up @@ -148,4 +151,8 @@ export type EventPairs =
| {
event_name: EventNames.SignUpFlowCompleted;
event_payload: z.infer<typeof events.SignUpFlowCompleted>;
}
| {
event_name: EventNames.TradeEvent;
event_payload: z.infer<typeof events.TradeEvent>;
};
10 changes: 10 additions & 0 deletions libs/core/src/integration/notifications.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,13 @@ 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 symbol'),
is_buy: z.boolean().describe('If the trade was a buy or sell'),
});

export const CapReachedNotification = z.object({
symbol: z.string().describe('The token symbol'),
});
1 change: 1 addition & 0 deletions libs/core/src/integration/outbox.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const outboxEvents = {
[E.ThreadCreated]: P.ThreadCreated,
[E.ThreadUpvoted]: P.ThreadUpvoted,
[E.UserMentioned]: P.UserMentioned,
[E.TradeEvent]: P.TradeEvent,
} as const;

export const Outbox = z.union(
Expand Down
12 changes: 12 additions & 0 deletions libs/core/src/ports/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
} from '../framework';
import { EventNames, Events } from '../integration/events';
import {
CapReachedNotification,
ChainProposalsNotification,
CommentCreatedNotification,
CommunityStakeNotification,
SnapshotProposalCreatedNotification,
TradeEventNotification,
UpvoteNotification,
UserMentionedNotification,
WebhookNotification,
Expand Down Expand Up @@ -293,6 +295,8 @@ export enum WorkflowKeys {
EmailRecap = 'email-recap',
EmailDigest = 'email-digest',
Webhooks = 'webhooks',
TradeEvent = 'trade-event',
CapReached = 'cap-reached',
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { events, LaunchpadTrade, logger } from '@hicommonwealth/core';
import {
EventNames,
events,
LaunchpadTrade,
logger,
} from '@hicommonwealth/core';
import { commonProtocol as cp } from '@hicommonwealth/evm-protocols';
import { commonProtocol, models } from '@hicommonwealth/model';
import { commonProtocol, emitEvent, models } from '@hicommonwealth/model';
import { BigNumber } from 'ethers';
import Web3 from 'web3';
import { z } from 'zod';
Expand Down Expand Up @@ -44,7 +49,7 @@ export async function handleLaunchpadTrade(
const web3 = new Web3(chainNode.private_url! || chainNode.url!);
const block = await web3.eth.getBlock(event.rawLog.blockHash);

await models.LaunchpadTrade.findOrCreate({
const [trade] = await models.LaunchpadTrade.findOrCreate({
where: {
eth_chain_id: chainNode.eth_chain_id!,
transaction_hash: event.rawLog.transactionHash,
Expand Down Expand Up @@ -99,5 +104,14 @@ export async function handleLaunchpadTrade(

token.liquidity_transferred = true;
await token.save();

await emitEvent(models.Outbox, [
{
event_name: EventNames.TradeEvent,
event_payload: {
...trade,
},
},
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 < BigInt(10000)) {
await provider.triggerWorkflow({
key: WorkflowKeys.CapReached,
users: results.map((u) => ({ id: String(u.user_id) })),
data: {
symbol: results[0].symbol,
},
});
}
};
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);
},
},
};
}