Skip to content

Commit

Permalink
fix score type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rbennettcw committed Oct 23, 2024
1 parent b657d15 commit 4ded634
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 40 deletions.
1 change: 1 addition & 0 deletions libs/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@cosmjs/encoding": "0.32.3",
"@cosmjs/stargate": "^0.31.3",
"@cosmjs/tendermint-rpc": "^0.31.3",
"@ethersproject/bignumber": "^5.7.0",
"@faker-js/faker": "^8.4.1",
"@hicommonwealth/chains": "workspace:*",
"@hicommonwealth/core": "workspace:*",
Expand Down
26 changes: 18 additions & 8 deletions libs/model/src/community/GetTopics.query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumber } from '@ethersproject/bignumber';
import { Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { QueryTypes } from 'sequelize';
Expand Down Expand Up @@ -95,14 +96,23 @@ WITH topic_data AS (
${contest_managers}
`;

return await models.sequelize.query<z.infer<typeof schemas.TopicView>>(
sql,
{
replacements: { community_id },
type: QueryTypes.SELECT,
raw: true,
},
);
const results = await models.sequelize.query<
z.infer<typeof schemas.TopicView>
>(sql, {
replacements: { community_id },
type: QueryTypes.SELECT,
raw: true,
});

results.forEach((r) => {
r.active_contest_managers.forEach((cm) => {
cm.content.forEach((c) => {
c.voting_power = BigNumber.from(c.voting_power).toString();
});
});
});

return results;
},
};
}
19 changes: 10 additions & 9 deletions libs/model/src/contest/Contests.projection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumber } from '@ethersproject/bignumber';
import {
EvmEventSignatures,
InvalidState,
Expand Down Expand Up @@ -221,22 +222,22 @@ export async function updateScore(contest_address: string, contest_id: number) {
oneOff,
);

const prizePool =
(Number(contestBalance) *
Number(oneOff ? 100 : details.prize_percentage)) /
100;
const prizePool = BigNumber.from(contestBalance)
.mul(oneOff ? 100 : details.prize_percentage)
.div(100);
const score: z.infer<typeof ContestScore> = scores.map((s, i) => ({
content_id: s.winningContent.toString(),
creator_address: s.winningAddress,
votes: Number(s.voteCount),
votes: BigNumber.from(s.voteCount).toString(),
prize:
i < Number(details.payout_structure.length)
? (
(Number(prizePool) * Number(details.payout_structure[i])) /
100
).toString()
? BigNumber.from(prizePool)
.mul(details.payout_structure[i])
.div(100)
.toString()
: '0',
}));
console.log('SCORE: ', score);
await models.Contest.update(
{
score,
Expand Down
1 change: 1 addition & 0 deletions libs/model/src/contest/GetAllContests.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ order by
r.contests.forEach((c) => {
c.score?.forEach((w) => {
w.tickerPrize = Number(w.prize) / 10 ** r.decimals;
console.log(w.votes, typeof w.votes);
});
c.start_time = new Date(c.start_time);
c.end_time = new Date(c.end_time);
Expand Down
4 changes: 2 additions & 2 deletions libs/model/src/services/commonProtocol/contestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ContestScores = {
winningAddress: string;
voteCount: string;
}[];
contestBalance: number;
contestBalance: string;
};

/**
Expand Down Expand Up @@ -257,7 +257,7 @@ export const getContestBalance = async (
rpcNodeUrl: string,
contest: string,
oneOff?: boolean,
): Promise<number> => {
): Promise<string> => {
const web3 = new Web3(rpcNodeUrl);

const contestInstance = new web3.eth.Contract(
Expand Down
2 changes: 1 addition & 1 deletion libs/schemas/src/projections/contest.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ContestScore = z
z.object({
creator_address: z.string(),
content_id: z.string(),
votes: PG_INT,
votes: z.string(),
prize: z.string(),
tickerPrize: z.number().optional(),
}),
Expand Down
20 changes: 11 additions & 9 deletions libs/shared/src/commonProtocol/contractHelpers/Contest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumber } from '@ethersproject/bignumber';
import { ZERO_ADDRESS } from '@hicommonwealth/shared';

export const getTotalContestBalance = async (
Expand All @@ -9,7 +10,7 @@ export const getTotalContestBalance = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
feeManagerAbi: any[],
oneOff?: boolean,
): Promise<number> => {
): Promise<string> => {
const promises = [contestContract.methods.contestToken().call()];

if (!oneOff) {
Expand All @@ -18,7 +19,7 @@ export const getTotalContestBalance = async (

const results = await Promise.all(promises);

const balancePromises: Promise<number>[] = [];
const balancePromises: Promise<string>[] = [];

if (!oneOff) {
const feeManager = new web3.eth.Contract(feeManagerAbi, String(results[1]));
Expand All @@ -30,8 +31,8 @@ export const getTotalContestBalance = async (
}
if (String(results[0]) === ZERO_ADDRESS) {
balancePromises.push(
web3.eth.getBalance(contestAddress).then((v: bigint) => {
return Number(v);
web3.eth.getBalance(contestAddress).then((v: any) => {
return v.toString();
}),
);
} else {
Expand All @@ -45,16 +46,17 @@ export const getTotalContestBalance = async (
data: calldata,
})
.then((v: string) => {
return Number(web3.eth.abi.decodeParameter('uint256', v));
return web3.eth.abi.decodeParameter('uint256', v);
}),
);
}

const balanceResults = await Promise.all(balancePromises);

return Number(
const balance =
balanceResults.length === 2
? BigInt(balanceResults[0]) + BigInt(balanceResults[1])
: BigInt(balanceResults[0]),
);
? BigNumber.from(balanceResults[0]).add(balanceResults[1])
: BigNumber.from(balanceResults[0]);

return BigNumber.from(balance).toString();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.query(`
UPDATE "Contests"
SET "score" = (
SELECT jsonb_agg(
jsonb_set(
elem,
'{votes}',
to_jsonb((elem->>'votes')::numeric::text)
)
)
FROM jsonb_array_elements("Contests"."score") as elem
)
WHERE "score" IS NOT NULL;
`);
},

async down(queryInterface, Sequelize) {},
};
Loading

0 comments on commit 4ded634

Please sign in to comment.