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

draft: burton/cycle detection with fixes #9073

Closed
wants to merge 19 commits into from
Closed
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
9 changes: 9 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:import/recommended',
],
plugins: ['@typescript-eslint', 'n'],
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -33,5 +34,13 @@ module.exports = {
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
'n/no-process-exit': 'error',
'import/no-cycle': 2,
'import/named': 0,
'import/export': 0,
'import/namespace': 0,
'import/default': 0, // TODO should this be on?
'import/no-duplicates': 0, // TODO should this be on?
'import/no-named-as-default': 0,
'import/no-named-as-default-member': 0,
},
};
1 change: 0 additions & 1 deletion libs/adapters/src/rabbitmq/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './RabbitMQAdapter';
export * from './rabbitMQConfig';
export { getRabbitMQConfig } from './rabbitMQConfig';
export * from './types';
export * from './util';
12 changes: 6 additions & 6 deletions libs/core/src/integration/chain-event.utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EventNames, EventPairs } from '@hicommonwealth/core';
import { ETHERS_BIG_NUMBER, EVM_ADDRESS } from '@hicommonwealth/schemas';
import ethers from 'ethers';
import { decodeLog } from 'web3-eth-abi';
import { z } from 'zod';
import { EventNames, EventPairs } from './events';

/**
* To add a new chain-event:
Expand Down Expand Up @@ -260,17 +260,17 @@ export const EvmEventAbis = {
type AbiTypeToTS<T> = T extends 'address'
? z.infer<typeof EVM_ADDRESS>
: T extends 'uint256'
? z.infer<typeof ETHERS_BIG_NUMBER>
: T extends 'bool'
? boolean
: never;
? z.infer<typeof ETHERS_BIG_NUMBER>
: T extends 'bool'
? boolean
: never;

type Transform<T extends ReadonlyArray<{ name: string; type: string }>> = {
[K in T[number] as K['name']]: AbiTypeToTS<K['type']>;
};

type DecodedEvmEvent<Signature extends EvmEventSignature> = Transform<
typeof EvmEventAbis[Signature]
(typeof EvmEventAbis)[Signature]
>;

// EvmMapper maps chain event args as input to a zod event schema type as output
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/logging/rollbar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '@hicommonwealth/core';
import Rollbar from 'rollbar';
import { config } from '../config';

export const rollbar = new Rollbar({
accessToken: config.LOGGING.ROLLBAR_SERVER_TOKEN,
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/ports/in-memory-brokers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RoutingKey } from '@hicommonwealth/core';
import { RoutingKey } from './interfaces';

export const successfulInMemoryBroker = {
name: 'successful-in-memory-broker',
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/ports/in-memory-notification-providers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NotificationsProvider } from '@hicommonwealth/core';
import sinon from 'sinon';
import { NotificationsProvider } from './interfaces';

export function SpyNotificationsProvider(
sandbox: sinon.SinonSandbox,
Expand Down
9 changes: 3 additions & 6 deletions libs/model/src/chain-events/ChainEventCreated.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
parseEvmEvent,
type Command,
} from '@hicommonwealth/core';
import {
config,
emitEvent,
equalEvmAddresses,
models,
} from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import { commonProtocol as cp } from '@hicommonwealth/shared';
import { Hmac, createHmac } from 'crypto';
import { config } from '../config';
import { models } from '../database';

Check failure on line 11 in libs/model/src/chain-events/ChainEventCreated.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./chain-events:2

Check failure on line 11 in libs/model/src/chain-events/ChainEventCreated.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./chain-events:2
import { emitEvent, equalEvmAddresses } from '../utils/utils';

// TODO: how do we handle chain re-orgs
// Alchemy re-emits logs with `removed: true` -> modify event handlers to rollback changes if `removed: true`.
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/community/CreateGroup.command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InvalidState, type Command } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { Op } from 'sequelize';
import { models, sequelize } from '../database';

Check failure on line 4 in libs/model/src/community/CreateGroup.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4

Check failure on line 4 in libs/model/src/community/CreateGroup.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4
import { isCommunityAdminOrModerator } from '../middleware';
import { isCommunityAdminOrModerator } from '../middleware/authorization';
import { mustNotExist } from '../middleware/guards';
import { GroupAttributes } from '../models';

Expand Down
7 changes: 2 additions & 5 deletions libs/model/src/community/SetCommunityStake.command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { InvalidState, type Command } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';

Check failure on line 3 in libs/model/src/community/SetCommunityStake.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4

Check failure on line 3 in libs/model/src/community/SetCommunityStake.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4
import { isCommunityAdmin } from '../middleware';
import { mustExist } from '../middleware/guards';
import { commonProtocol } from '../services';
import { validateCommunityStakeConfig } from '../services/commonProtocol/communityStakeConfigValidator';

export function SetCommunityStake(): Command<typeof schemas.SetCommunityStake> {
return {
Expand Down Expand Up @@ -40,10 +40,7 @@
);

// !domain, application, and infrastructure services (stateless, not related to entities or value objects)
await commonProtocol.communityStakeConfigValidator.validateCommunityStakeConfig(
community,
rest.stake_id,
);
await validateCommunityStakeConfig(community, rest.stake_id);

// !side effects
const [updated] = await models.CommunityStake.upsert({
Expand Down
23 changes: 12 additions & 11 deletions libs/model/src/contest/Contests.projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import { ContestScore } from '@hicommonwealth/schemas';
import { QueryTypes } from 'sequelize';
import { z } from 'zod';
import { models } from '../database';

Check failure on line 11 in libs/model/src/contest/Contests.projection.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./contest:5

Check failure on line 11 in libs/model/src/contest/Contests.projection.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./contest:5
import { mustExist } from '../middleware/guards';
import { EvmEventSourceAttributes } from '../models';
import * as protocol from '../services/commonProtocol';
import { contestHelper, contractHelpers } from '../services/commonProtocol';
import { decodeThreadContentUrl } from '../utils';

const log = logger(import.meta);
Expand Down Expand Up @@ -67,10 +67,12 @@
`Chain node url not found on namespace ${namespace}`,
);

const { ticker, decimals } =
await protocol.contractHelpers.getTokenAttributes(contest_address, url);
const { ticker, decimals } = await contractHelpers.getTokenAttributes(
contest_address,
url,
);

const { startTime, endTime } = await protocol.contestHelper.getContestStatus(
const { startTime, endTime } = await contestHelper.getContestStatus(
url,
contest_address,
isOneOff,
Expand Down Expand Up @@ -205,13 +207,12 @@
`Chain node url not found on contest ${contest_address}`,
);

const { scores, contestBalance } =
await protocol.contestHelper.getContestScore(
details.url,
contest_address,
undefined,
oneOff,
);
const { scores, contestBalance } = await contestHelper.getContestScore(
details.url,
contest_address,
undefined,
oneOff,
);

const prizePool =
(Number(contestBalance) *
Expand Down
4 changes: 2 additions & 2 deletions libs/model/src/emails/GetDigestEmailData.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
GetDigestEmailData,
Query,
} from '@hicommonwealth/core';
import { models } from '@hicommonwealth/model';
import { QueryTypes } from 'sequelize';
import { z } from 'zod';
import { models } from '../database';

Check failure on line 9 in libs/model/src/emails/GetDigestEmailData.query.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./emails:6

Check failure on line 9 in libs/model/src/emails/GetDigestEmailData.query.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./emails:6

export function GetDigestEmailDataQuery(): Query<typeof GetDigestEmailData> {
return {
Expand Down Expand Up @@ -42,7 +42,7 @@

if (!threads.length) return {};

const result: z.infer<typeof GetDigestEmailData['output']> = {};
const result: z.infer<(typeof GetDigestEmailData)['output']> = {};
for (const thread of threads) {
if (!result[thread.community_id]) {
result[thread.community_id] = [thread];
Expand Down
17 changes: 9 additions & 8 deletions libs/model/src/emails/GetRecapEmailData.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
} from '@hicommonwealth/core';
import { QueryTypes } from 'sequelize';
import z from 'zod';
import { config, models } from '..';
import { config } from '../config';
import { models } from '../database';

Check failure on line 19 in libs/model/src/emails/GetRecapEmailData.query.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./emails:6

Check failure on line 19 in libs/model/src/emails/GetRecapEmailData.query.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./emails:6

const log = logger(import.meta);

type AdditionalMetaData<Key extends keyof typeof EnrichedNotificationNames> = {
event_name: typeof EnrichedNotificationNames[Key];
event_name: (typeof EnrichedNotificationNames)[Key];
inserted_at: string;
};

Expand Down Expand Up @@ -123,11 +124,11 @@

async function enrichDiscussionNotifications(
discussion: DiscussionNotifications,
): Promise<z.infer<typeof GetRecapEmailData['output']>['discussion']> {
): Promise<z.infer<(typeof GetRecapEmailData)['output']>['discussion']> {
if (!discussion.length) return [];

const enrichedDiscussion: z.infer<
typeof GetRecapEmailData['output']
(typeof GetRecapEmailData)['output']
>['discussion'] = [];

const unfilteredIds: number[] = [];
Expand Down Expand Up @@ -186,17 +187,17 @@
governance: GovernanceNotifications;
protocol: ProtocolNotifications;
}): Promise<{
governance: z.infer<typeof GetRecapEmailData['output']>['governance'];
protocol: z.infer<typeof GetRecapEmailData['output']>['protocol'];
governance: z.infer<(typeof GetRecapEmailData)['output']>['governance'];
protocol: z.infer<(typeof GetRecapEmailData)['output']>['protocol'];
}> {
if (!governance.length && !protocol.length)
return { governance: [], protocol: [] };

const enrichedGovernance: z.infer<
typeof GetRecapEmailData['output']
(typeof GetRecapEmailData)['output']
>['governance'] = [];
const enrichedProtocol: z.infer<
typeof GetRecapEmailData['output']
(typeof GetRecapEmailData)['output']
>['protocol'] = [];

const unfilteredCommunityIds: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/load-testing/CreateJWTs.command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ExternalServiceUserIds, type Command } from '@hicommonwealth/core';
import { config } from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import jwt from 'jsonwebtoken';
import { QueryTypes } from 'sequelize';
import { config } from '../config';
import { models } from '../database';

Check failure on line 6 in libs/model/src/load-testing/CreateJWTs.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./load-testing:8

Check failure on line 6 in libs/model/src/load-testing/CreateJWTs.command.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./load-testing:8

export function CreateJWTs(): Command<typeof schemas.CreateJWTs> {
return {
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/middleware/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import { Address, Group, GroupPermissionAction } from '@hicommonwealth/schemas';
import { Role } from '@hicommonwealth/shared';
import { Op, QueryTypes } from 'sequelize';
import { ZodObject, ZodSchema, ZodString, z } from 'zod';

Check failure on line 15 in libs/model/src/middleware/authorization.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4=>./CreateGroup.command:2

Check failure on line 15 in libs/model/src/middleware/authorization.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:4=>./AddressAttributes:46=>@hicommonwealth/model:4=>./community:4=>./CreateGroup.command:2
import { models } from '..';
import { models } from '../database';

export type CommunityMiddleware = CommandHandler<CommandInput, ZodSchema>;
export type ThreadMiddleware = CommandHandler<
Expand Down
12 changes: 12 additions & 0 deletions libs/model/src/models/AddressAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {

Check failure on line 1 in libs/model/src/models/AddressAttributes.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:32

Check failure on line 1 in libs/model/src/models/AddressAttributes.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:32
CommunityAttributes,
MembershipAttributes,
} from '@hicommonwealth/model';
import { Address } from '@hicommonwealth/schemas';
import { z } from 'zod';

export type AddressAttributes = z.infer<typeof Address> & {
// associations
Community?: CommunityAttributes;
Memberships?: MembershipAttributes[];
};
12 changes: 12 additions & 0 deletions libs/model/src/models/GroupAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {

Check failure on line 1 in libs/model/src/models/GroupAttributes.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Dependency cycle via ./models:32

Check failure on line 1 in libs/model/src/models/GroupAttributes.ts

View workflow job for this annotation

GitHub Actions / Code Quality checks (20)

Dependency cycle via ./models:32
CommunityAttributes,
MembershipAttributes,
} from '@hicommonwealth/model';
import { Group } from '@hicommonwealth/schemas';
import z from 'zod';

export type GroupAttributes = z.infer<typeof Group> & {
// associations
community?: CommunityAttributes;
memberships?: MembershipAttributes[];
};
14 changes: 14 additions & 0 deletions libs/model/src/models/MembershipAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AddressAttributes } from './AddressAttributes';
import { GroupAttributes } from './GroupAttributes';
import { MembershipRejectReason } from './MembershipRejectReason';

export type MembershipAttributes = {
group_id: number;
address_id: number;
reject_reason?: MembershipRejectReason;
last_checked: Date;

// associations
group?: GroupAttributes;
address?: AddressAttributes;
};
10 changes: 10 additions & 0 deletions libs/model/src/models/MembershipRejectReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type MembershipRejectReason =
| {
message: string;
requirement: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
rule: string;
};
}[]
| null;
12 changes: 2 additions & 10 deletions libs/model/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Address } from '@hicommonwealth/schemas';
import { WalletId } from '@hicommonwealth/shared';
import Sequelize from 'sequelize';
import { z } from 'zod';
import type { CommunityAttributes, CommunityInstance } from './community';
import { MembershipAttributes } from './membership';
import { AddressAttributes } from './AddressAttributes';
import type { CommunityInstance } from './community';
import type { SsoTokenInstance } from './sso_token';
import type { ModelInstance } from './types';
import type { UserInstance } from './user';

export type AddressAttributes = z.infer<typeof Address> & {
// associations
Community?: CommunityAttributes;
Memberships?: MembershipAttributes[];
};

export type AddressInstance = ModelInstance<AddressAttributes> & {
getCommunity: Sequelize.BelongsToGetAssociationMixin<CommunityInstance>;
getUser: Sequelize.BelongsToGetAssociationMixin<UserInstance>;
Expand Down
3 changes: 2 additions & 1 deletion libs/model/src/models/collaboration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Sequelize from 'sequelize';
import type { AddressAttributes, AddressInstance } from './address';
import { AddressAttributes } from './AddressAttributes';
import type { AddressInstance } from './address';
import type { ThreadAttributes, ThreadInstance } from './thread';
import type { ModelInstance } from './types';

Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/contract_abi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AbiType } from '@hicommonwealth/shared';
import Sequelize from 'sequelize'; // must use "* as" to avoid scope errors
import { hashAbi } from '../utils';
import { hashAbi } from '../utils/utils';
import type { ModelInstance } from './types';

export type ContractAbiAttributes = {
Expand Down
11 changes: 1 addition & 10 deletions libs/model/src/models/group.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { Group } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import z from 'zod';
import type { CommunityAttributes } from './community';
import type { MembershipAttributes } from './membership';
import { GroupAttributes } from './GroupAttributes';
import type { ModelInstance } from './types';

export type GroupAttributes = z.infer<typeof Group> & {
// associations
community?: CommunityAttributes;
memberships?: MembershipAttributes[];
};

export type GroupInstance = ModelInstance<GroupAttributes>;

export default (
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/groupPermission.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GroupPermission } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize'; // must use "* as" to avoid scope errors
import { z } from 'zod';
import { GroupAttributes } from './group';
import { GroupAttributes } from './GroupAttributes';
import type { ModelInstance } from './types';

export type GroupPermissionAttributes = z.infer<typeof GroupPermission> & {
Expand Down
4 changes: 4 additions & 0 deletions libs/model/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const buildDb = (sequelize: Sequelize): DB => {
};

// TODO: avoid legacy exports to /packages/commonwealth/server (keep db models encapsulated behind DB)
export { AddressAttributes } from './AddressAttributes';
export { GroupAttributes } from './GroupAttributes';
export { MembershipAttributes } from './MembershipAttributes';
export { MembershipRejectReason } from './MembershipRejectReason';
export * from './address';
export * from './chain_node';
export * from './collaboration';
Expand Down
Loading
Loading