Skip to content

Commit

Permalink
Merge pull request #9561 from hicommonwealth/ryan/remove-topic-chain-…
Browse files Browse the repository at this point in the history
…node

Remove chain node from topics + Fix ERC20 voting
  • Loading branch information
rbennettcw authored Oct 18, 2024
2 parents 8805d8c + f03a4a6 commit 9f6ac71
Show file tree
Hide file tree
Showing 54 changed files with 329 additions and 130 deletions.
2 changes: 1 addition & 1 deletion libs/model/src/comment/CreateComment.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function CreateComment(): Command<
text,
address_id: address.id!,
reaction_count: 0,
reaction_weights_sum: 0,
reaction_weights_sum: '0',
created_by: '',
search: getCommentSearchVector(text),
content_url: contentUrl,
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/comment/CreateCommentReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function CreateCommentReaction(): Command<
address_id: address.id!,
comment_id: comment.id,
reaction: payload.reaction,
calculated_voting_weight,
calculated_voting_weight: calculated_voting_weight?.toString(),
canvas_msg_id: payload.canvas_msg_id,
canvas_signed_data: payload.canvas_signed_data,
},
Expand Down
1 change: 0 additions & 1 deletion libs/model/src/community/CreateTopic.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function CreateTopic(): Command<
options = {
...options,
weighted_voting: payload.weighted_voting,
chain_node_id: payload.chain_node_id || undefined,
token_address: payload.token_address || undefined,
token_symbol: payload.token_symbol || undefined,
vote_weight_multiplier: payload.vote_weight_multiplier || undefined,
Expand Down
3 changes: 1 addition & 2 deletions libs/model/src/models/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export const buildAssociations = (db: DB) => {

db.ChainNode.withMany(db.Community)
.withMany(db.EvmEventSource)
.withOne(db.LastProcessedEvmBlock)
.withMany(db.Topic);
.withOne(db.LastProcessedEvmBlock);

db.ContractAbi.withMany(db.EvmEventSource, { foreignKey: 'abi_id' });

Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default (
defaultValue: 0,
},
reaction_weights_sum: {
type: Sequelize.INTEGER,
type: Sequelize.DECIMAL(78, 0),
allowNull: false,
defaultValue: 0,
},
Expand Down
5 changes: 4 additions & 1 deletion libs/model/src/models/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export default (
comment_id: { type: Sequelize.INTEGER, allowNull: true },
address_id: { type: Sequelize.INTEGER, allowNull: false },
reaction: { type: Sequelize.ENUM('like'), allowNull: false },
calculated_voting_weight: { type: Sequelize.INTEGER, allowNull: true },
calculated_voting_weight: {
type: Sequelize.DECIMAL(78, 0),
allowNull: true,
},
// canvas-related columns
canvas_signed_data: { type: Sequelize.JSONB, allowNull: true },
canvas_msg_id: { type: Sequelize.STRING, allowNull: true },
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default (
defaultValue: 0,
},
reaction_weights_sum: {
type: Sequelize.INTEGER,
type: Sequelize.DECIMAL(78, 0),
allowNull: false,
defaultValue: 0,
},
Expand Down
13 changes: 1 addition & 12 deletions libs/model/src/models/topic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Topic } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import { z } from 'zod';
import { ChainNodeAttributes } from './chain_node';
import type { CommunityAttributes } from './community';
import type { ThreadAttributes } from './thread';
import type { ModelInstance } from './types';
Expand All @@ -10,7 +9,6 @@ export type TopicAttributes = z.infer<typeof Topic> & {
// associations
community?: CommunityAttributes;
threads?: ThreadAttributes[] | TopicAttributes['id'][];
ChainNode?: ChainNodeAttributes;
};
export type TopicInstance = ModelInstance<TopicAttributes>;

Expand Down Expand Up @@ -50,18 +48,9 @@ export default (
},
telegram: { type: Sequelize.STRING, allowNull: true },
weighted_voting: { type: Sequelize.STRING, allowNull: true },
chain_node_id: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: 'ChainNodes',
key: 'id',
},
onUpdate: 'CASCADE',
},
token_address: { type: Sequelize.STRING, allowNull: true },
token_symbol: { type: Sequelize.STRING, allowNull: true },
vote_weight_multiplier: { type: Sequelize.INTEGER, allowNull: true },
vote_weight_multiplier: { type: Sequelize.FLOAT, allowNull: true },
},
{
timestamps: true,
Expand Down
35 changes: 15 additions & 20 deletions libs/model/src/services/stakeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { contractHelpers } from '../services/commonProtocol';
export async function getVotingWeight(
topic_id: number,
address: string,
): Promise<number | null> {
): Promise<BigNumber | null> {
if (config.STAKE.REACTION_WEIGHT_OVERRIDE)
return config.STAKE.REACTION_WEIGHT_OVERRIDE;
return BigNumber.from(config.STAKE.REACTION_WEIGHT_OVERRIDE);

const topic = await models.Topic.findByPk(topic_id, {
include: [
Expand All @@ -38,25 +38,21 @@ export async function getVotingWeight(
},
],
},
{
model: models.ChainNode,
required: false,
},
],
});

mustExist('Topic', topic);

const { community } = topic;

mustExist('Community', community);

const chain_node = community.ChainNode;

if (topic.weighted_voting === TopicWeightedVoting.Stake) {
mustExist('Chain Node Eth Chain Id', chain_node?.eth_chain_id);
mustExist('Community Namespace Address', community.namespace_address);

const stake = topic.community?.CommunityStakes?.at(0);
mustExist('Community Stake', stake);
const chain_node = community.ChainNode;
mustExist('Chain Node Eth Chain Id', chain_node?.eth_chain_id);

const stakeBalances = await contractHelpers.getNamespaceBalance(
community.namespace_address,
Expand All @@ -70,24 +66,23 @@ export async function getVotingWeight(

return commonProtocol.calculateVoteWeight(stakeBalance, stake.vote_weight);
} else if (topic.weighted_voting === TopicWeightedVoting.ERC20) {
const {
ChainNode: chain_node,
token_address,
vote_weight_multiplier,
} = topic;
mustExist('Topic Chain Node Eth Chain Id', chain_node?.eth_chain_id);
mustExist('Chain Node Eth Chain Id', chain_node?.eth_chain_id);

const balances = await tokenBalanceCache.getBalances({
balanceSourceType: BalanceSourceType.ERC20,
addresses: [address],
sourceOptions: {
evmChainId: chain_node?.eth_chain_id,
contractAddress: token_address!,
evmChainId: chain_node.eth_chain_id,
contractAddress: topic.token_address!,
},
cacheRefresh: true,
});
const balance = balances[address];
return commonProtocol.calculateVoteWeight(balance, vote_weight_multiplier!);
const result = commonProtocol.calculateVoteWeight(
balances[address],
topic.vote_weight_multiplier!,
);
// only count full ERC20 tokens
return result?.div(BigNumber.from(10).pow(18)) || null;
}

// no weighted voting
Expand Down
6 changes: 4 additions & 2 deletions libs/model/src/tester/e2eSeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const e2eTestEntities = async function (
stage: 'discussion',
view_count: 0,
reaction_count: 0,
reaction_weights_sum: 0,
reaction_weights_sum: '0',
comment_count: 0,
search: getThreadSearchVector(
`testThread Title ${-i - 1}`,
Expand Down Expand Up @@ -233,7 +233,7 @@ export const e2eTestEntities = async function (
stage: 'discussion',
view_count: 0,
reaction_count: 0,
reaction_weights_sum: 0,
reaction_weights_sum: '0',
comment_count: 0,
search: getThreadSearchVector(
`testThread Title ${-i - 1 - 2}`,
Expand Down Expand Up @@ -276,6 +276,7 @@ export const e2eTestEntities = async function (
text: '',
thread_id: -1,
reaction_count: 0,
reaction_weights_sum: '0',
search: getCommentSearchVector(''),
},
})
Expand All @@ -298,6 +299,7 @@ export const e2eTestEntities = async function (
text: '',
thread_id: -2,
reaction_count: 0,
reaction_weights_sum: '0',
search: getCommentSearchVector(''),
},
})
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/thread/CreateThread.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function CreateThread(): Command<
view_count: 0,
comment_count: 0,
reaction_count: 0,
reaction_weights_sum: 0,
reaction_weights_sum: '0',
search: getThreadSearchVector(rest.title, body),
content_url: contentUrl,
},
Expand Down
2 changes: 1 addition & 1 deletion libs/model/src/thread/CreateThreadReaction.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function CreateThreadReaction(): Command<
address_id: address.id!,
thread_id: thread.id,
reaction: payload.reaction,
calculated_voting_weight,
calculated_voting_weight: calculated_voting_weight?.toString(),
canvas_msg_id: payload.canvas_msg_id,
canvas_signed_data: payload.canvas_signed_data,
},
Expand Down
2 changes: 1 addition & 1 deletion libs/model/test/community/community-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ describe('Community lifecycle', () => {
});

test('should create topic (stake weighted)', async () => {
// when community is staked, topic will automatically be staked
await models.CommunityStake.create({
community_id: community.id,
stake_id: 1,
Expand All @@ -495,6 +494,7 @@ describe('Community lifecycle', () => {
description: 'boohoo',
featured_in_sidebar: false,
featured_in_new_post: false,
weighted_voting: TopicWeightedVoting.Stake,
},
});
const { topic } = result!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Contest Worker Policy', () => {
stage: '',
view_count: 0,
reaction_count: 0,
reaction_weights_sum: 0,
reaction_weights_sum: '0',
comment_count: 0,
deleted_at: undefined,
pinned: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('Contests projection lifecycle', () => {
topic_id: undefined,
view_count: 1,
reaction_count: 1,
reaction_weights_sum: 1,
reaction_weights_sum: '1',
comment_count: 1,
discord_meta: undefined,
deleted_at: undefined, // so we can find it!
Expand Down
2 changes: 2 additions & 0 deletions libs/model/test/email/recap-email-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ describe('Recap email lifecycle', () => {
topic_id: community?.topics?.at(0)?.id,
pinned: false,
read_only: false,
reaction_weights_sum: '0',
});

[comment] = await seed('Comment', {
address_id: community?.Addresses?.at(0)?.id,
thread_id: thread!.id!,
reaction_weights_sum: '0',
});
});

Expand Down
5 changes: 5 additions & 0 deletions libs/model/test/email/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export async function generateThreads(
topic_id: communityThree?.topics?.at(0)?.id,
pinned: false,
read_only: false,
reaction_weights_sum: '0',
});

// 3 threads for communityOne and 1 thread for communityTwo
Expand All @@ -334,6 +335,7 @@ export async function generateThreads(
pinned: false,
read_only: false,
view_count: 10,
reaction_weights_sum: '0',
});
const [threadTwo] = await seed('Thread', {
address_id: communityOne?.Addresses?.at(0)?.id,
Expand All @@ -342,6 +344,7 @@ export async function generateThreads(
pinned: false,
read_only: false,
view_count: 5,
reaction_weights_sum: '0',
});

const [threadThree] = await seed('Thread', {
Expand All @@ -351,6 +354,7 @@ export async function generateThreads(
pinned: false,
read_only: false,
view_count: 1,
reaction_weights_sum: '0',
});

const [threadFour] = await seed('Thread', {
Expand All @@ -360,6 +364,7 @@ export async function generateThreads(
pinned: false,
read_only: false,
view_count: 10,
reaction_weights_sum: '0',
});

return {
Expand Down
3 changes: 2 additions & 1 deletion libs/model/test/reaction/reaction-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('Reactions lifecycle', () => {
deleted_at: undefined, // so we can find it!
pinned: false,
read_only: false,
reaction_weights_sum: '0',
},
//{ mock: true, log: true },
);
Expand All @@ -68,7 +69,7 @@ describe('Reactions lifecycle', () => {
reaction: 'like',
canvas_signed_data: '',
canvas_msg_id: '',
calculated_voting_weight: 0,
calculated_voting_weight: '0',
});

const lastOutboxEntry = await models.Outbox.findOne({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ describe('Comment subscription lifecycle', () => {
topic_id: community?.topics?.at(0)?.id,
pinned: false,
read_only: false,
reaction_weights_sum: '0',
});

[commentOne] = await seed('Comment', {
address_id: community?.Addresses?.at(0)?.id,
thread_id: thread!.id!,
reaction_weights_sum: '0',
});
[commentTwo] = await seed('Comment', {
address_id: community?.Addresses?.at(0)?.id,
thread_id: thread!.id!,
reaction_weights_sum: '0',
});
actor = {
user: { id: user!.id!, email: user!.email! },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ describe('Thread subscription lifecycle', () => {
topic_id: community!.topics![0].id,
pinned: false,
read_only: false,
reaction_weights_sum: '0',
});
[threadTwo] = await seed('Thread', {
address_id: community!.Addresses![0].id!,
community_id: community?.id,
topic_id: community!.topics![0].id,
pinned: false,
read_only: false,
reaction_weights_sum: '0',
});
actor = {
user: { id: user!.id!, email: user!.email! },
Expand Down
Loading

0 comments on commit 9f6ac71

Please sign in to comment.