Skip to content

Commit

Permalink
Merge pull request #72 from show-karma/staging
Browse files Browse the repository at this point in the history
merge: production
  • Loading branch information
Arthh authored Jul 28, 2023
2 parents 7c43e55 + c4f0579 commit b7fad7d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "NODE_ENV=development TZ=UTC yarn ts-node src/main.ts",
"service:discord:message:update:consumer": "TZ=UTC NODE_ENV=development nodemon src/services/discord-message-consumer/main.ts",
"service:discord:channel:cleaner:consumer": "TZ=UTC NODE_ENV=development nodemon src/services/discord-channel-cleaner-consumer/main.ts",
"apecoin:role:update": "yarn ts-node src/services/apecoin/index.ts"
"apecoin:role:update": "yarn ts-node src/services/apecoin/job.ts"
},
"dependencies": {
"@discordjs/builders": "^0.15.0",
Expand Down
1 change: 1 addition & 0 deletions src/@types/discord-message-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface DiscordSQSMessage {
users?: string[];
discordId?: string;
publicAddress?: string;
daoName?: string;
daos: {
name: string;
guildId: string;
Expand Down
47 changes: 25 additions & 22 deletions src/services/apecoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import axios from 'axios';
Expand All @@ -22,7 +23,7 @@ async function fetchDaoData() {
}
}

async function fetchDelegates(daoName: string) {
async function fetchDelegates(daoName: string, publicAddress?: string) {
const delegateQuery = `
SELECT "t1"."discordHandle",
"u"."publicAddress",
Expand All @@ -36,9 +37,10 @@ async function fetchDelegates(daoName: string) {
WHERE "t1"."daoName" = $1
AND "t2"."period" = '1y'
AND "t1"."discordHandle" IS NOT NULL
${publicAddress ? `AND "u"."publicAddress" = $2` : ''}
`;
try {
const result = await pool.query(delegateQuery, [daoName]);
const result = await pool.query(delegateQuery, [daoName, publicAddress]);
return result?.rows;
} catch (err) {
console.error('Error fetching delegates:', err);
Expand All @@ -58,19 +60,23 @@ async function fetchDelegateTrustLevel(
}

async function delegateHasPermission(delegate, dao) {
let hasPermission =
delegate.trustLevel = !delegate.handle ? 'no forum link' : 'already have the criterea';

delegate.hasPermission =
delegate.offChainVotesPct >= 50 ||
(+delegate.balance >= 50000 && delegate.offChainVotesPct > 0);

if (!hasPermission && delegate.handle) {
const trustLevel = await fetchDelegateTrustLevel(delegate.handle, dao.forum);
hasPermission = hasPermission || trustLevel >= 2;
if (!delegate.hasPermission && delegate.handle) {
delegate.trustLevel = await fetchDelegateTrustLevel(delegate.handle, dao.forum);
delegate.hasPermission = delegate.hasPermission || delegate.trustLevel >= 2;
}

return hasPermission;
return delegate;
}

async function manageRoles(client: Client, guildId: string, handles: string[], action: string) {
async function manageRoles(client: Client, guildId: string, handles: any[], action: string) {
if (!handles.length) return;

const guild = client.guilds.cache.get(guildId);

if (!guild) throw new Error('Guild not found');
Expand All @@ -80,24 +86,26 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a
if (!role) throw new Error('Role not found');

for (const handle of handles) {
const member = await guild.members.fetch(handle).catch(console.error);
const member = await guild.members.fetch(handle.discordHandle).catch(console.error);

if (member) {
await member.roles[action](role).catch(console.error);
console.log(`Role: ${role.name} | action: ${action} | user: ${member.user.tag}`);
console.log(
`Role: ${role.name} | action: ${action} | user: ${member.user.tag} | address: ${handle.publicAddress} | offChain: ${handle.offChainVotesPct} | balance: ${handle.balance} | forumLevel: ${handle.trustLevel} | hasCriterea: ${handle.hasPermission}`
);
}
}
}

(async () => {
export async function discordRoleManager(daoName?: string, publicAddress?: string) {
const dao = await fetchDaoData();

const delegates = await fetchDelegates(dao.name);
const delegates = await fetchDelegates(dao.name, publicAddress);

if (!delegates?.length) return;
if (!delegates?.length) return console.log('No delegates found');

const addHandles = [];
let revokeHandles = [];
const revokeHandles = [];

const delegatesBalance = await getTokenBalance(
delegates.map((d) => d.publicAddress),
Expand All @@ -106,16 +114,11 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a

for (const delegate of delegates) {
delegate.balance = +delegate.balance + (delegatesBalance?.[delegate.publicAddress] || 0);
const hasPermission = await delegateHasPermission(delegate, dao);
const delegateWithStatus = await delegateHasPermission(delegate, dao);

(hasPermission ? addHandles : revokeHandles).push(delegate.discordHandle);
(delegateWithStatus.hasPermission ? addHandles : revokeHandles).push(delegateWithStatus);
}

revokeHandles = revokeHandles.filter((handle) => !addHandles.includes(handle));

console.log('addHandles', addHandles);
console.log('revokeHandles', revokeHandles);

const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES]
});
Expand All @@ -131,4 +134,4 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a
});

client.login(process.env.DISCORD_TOKEN);
})();
}
3 changes: 3 additions & 0 deletions src/services/apecoin/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { discordRoleManager } from '.';

(async () => await discordRoleManager())();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DiscordSQSMessage } from 'src/@types/discord-message-update';
import GetPastMessagesService from '../get-messages.service';
import { Client, Intents } from 'discord.js';
import { SentryService } from '../../sentry/sentry.service';
import { discordRoleManager } from '../apecoin';

const LOG_CTX = 'DelegateStatUpdateConsumerService';

Expand Down Expand Up @@ -38,11 +39,16 @@ export class DiscordMessageConsumerService {
parsedMessage = JSON.parse(message.message) as DiscordSQSMessage;

console.log(`[${message.messageId}][${JSON.stringify(parsedMessage)}]`, LOG_CTX);
if (parsedMessage.daos) {
console.log(parsedMessage);
await this.getPastMessagesService.getMessages(client, parsedMessage);
} else {
console.log('no daos');
console.log({ parsedMessage });

if (parsedMessage.reason === 'user-discord-link') {
if (parsedMessage.daos) {
await this.getPastMessagesService.getMessages(client, parsedMessage);
} else {
console.log('No Daos found in the message');
}
} else if (parsedMessage.reason === 'delegate-update-discord-roles') {
await discordRoleManager(parsedMessage.daoName, parsedMessage.publicAddress);
}

console.log(`Time [${Date.now() - startTime}]`, LOG_CTX);
Expand Down
34 changes: 24 additions & 10 deletions src/services/get-messages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,21 @@ export default class GetPastMessagesService {
async createUpdateDelegateStatsMessage(
allMessagesToSave: MessageCustom[],
publicAddress,
daoName,
reason
) {
for (const message of _.uniqBy(allMessagesToSave, 'userId')) {
if (allMessagesToSave.length > 0) {
for (const message of _.uniqBy(allMessagesToSave, 'userId')) {
await this.delegateStatUpdateProducerService.produce({
dao: message.daoName,
publicAddress,
reason,
timestamp: Date.now()
});
}
} else {
await this.delegateStatUpdateProducerService.produce({
dao: message.daoName,
dao: daoName,
publicAddress,
reason,
timestamp: Date.now()
Expand All @@ -152,7 +162,6 @@ export default class GetPastMessagesService {
try {
const allBotGuilds = Array.from(await client.guilds.fetch());
const allUsers = [discordId || users].flat();
const allMessagesToSave = [];
let messagescount = 0;

if (!daos.length) {
Expand All @@ -163,6 +172,7 @@ export default class GetPastMessagesService {
console.log('Servers -> bot is inside: ', allBotGuilds);

for (const guild of daos) {
const allMessagesToSave = [];
if (!allBotGuilds.find((item) => +item[0] === +guild.guildId)) continue;

const formattedGuildChannels = guild.channelIds
Expand Down Expand Up @@ -241,15 +251,19 @@ export default class GetPastMessagesService {
console.log(err.message, channel);
}
}
}

console.log('All messages count: ', messagescount);
console.log('allMessagesToSave length: ', allMessagesToSave.length);
if (allMessagesToSave.length > 0) {
await this.insertAllMessages(allMessagesToSave);

console.log('All messages count: ', messagescount);
console.log('allMessagesToSave length: ', allMessagesToSave.length);
if (allMessagesToSave.length > 0) {
await this.insertAllMessages(allMessagesToSave);
}
if (reason === 'user-discord-link') {
await this.createUpdateDelegateStatsMessage(allMessagesToSave, publicAddress, reason);
await this.createUpdateDelegateStatsMessage(
allMessagesToSave,
publicAddress,
guild.name,
reason
);
}
}
} catch (err) {
Expand Down

0 comments on commit b7fad7d

Please sign in to comment.