From 87ef363f6363203f5a88eacdaf04fb9f806be0ce Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Thu, 6 Jun 2024 11:06:08 +0700 Subject: [PATCH] ramdom list validators --- .../ValidatorTable/ValidatorTable.js | 6 ++--- src/helpers/helper.js | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/components/ValidatorList/ValidatorTable/ValidatorTable.js b/src/components/ValidatorList/ValidatorTable/ValidatorTable.js index dee824ee..170cc79d 100644 --- a/src/components/ValidatorList/ValidatorTable/ValidatorTable.js +++ b/src/components/ValidatorList/ValidatorTable/ValidatorTable.js @@ -9,7 +9,7 @@ import { _ } from "src/lib/scripts"; import { tableThemes } from "src/constants/tableThemes"; import { sortDirections } from "src/constants/sortDirections"; import consts from "src/constants/consts"; -import { formatPercentage, formatInteger, formatOrai } from "src/helpers/helper"; +import { formatPercentage, formatInteger, formatOrai, groupAndShuffle } from "src/helpers/helper"; import { compareTwoValues } from "src/helpers/compare"; import Delegate from "src/components/common/Delegate"; import ThemedTable from "src/components/common/ThemedTable"; @@ -214,9 +214,9 @@ const ValidatorTable = memo(({ data = [] }) => { const sortData = (data, extraSortField = sortFields.RANK) => { if (!data) return []; - if (isFirstSort) { - return [...data].sort((a, b) => 0.5 - Math.random()); + const groupSize = 10; + return groupAndShuffle(data, groupSize).flat(); } if (canSort) { diff --git a/src/helpers/helper.js b/src/helpers/helper.js index f6e3e149..b6febdd0 100644 --- a/src/helpers/helper.js +++ b/src/helpers/helper.js @@ -376,3 +376,25 @@ export const calculateTallyProposal = ({ totalVote, bonded, tally }) => { vote_percentage: votePercentage, }; }; + +export function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; +} + +export function groupAndShuffle(array, groupSize) { + const sortedArray = array + .map(e => { + return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime }; + }) + .sort((a, b) => b.votingPowerMixUpTime - a.votingPowerMixUpTime); + const groups = []; + for (let i = 0; i < sortedArray.length; i += groupSize) { + groups.push(sortedArray.slice(i, i + groupSize)); + } + const shuffledGroups = groups.map(group => shuffleArray(group)); + return shuffledGroups; +}