Skip to content

Commit

Permalink
Merge pull request #985 from oraichain/feat/sort-validator
Browse files Browse the repository at this point in the history
update shuffle list vaildators
  • Loading branch information
quangdz1704 authored Jun 6, 2024
2 parents fd8a9e9 + 87ef363 commit 5df1669
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/components/ValidatorList/ValidatorTable/ValidatorTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -67,7 +67,7 @@ const toggleDirection = direction => {
};

const ValidatorTable = memo(({ data = [] }) => {
const [sortField, setSortField] = useState(sortFields.UPTIME);
const [sortField, setSortField] = useState();
const [sortDirection, setSortDirection] = useState(sortDirections.DESC);
const [canSort, setCanSort] = useState(false);
const [isFirstSort, setIsFirstSort] = useState(true);
Expand Down Expand Up @@ -214,19 +214,9 @@ const ValidatorTable = memo(({ data = [] }) => {

const sortData = (data, extraSortField = sortFields.RANK) => {
if (!data) return [];

if (isFirstSort) {
return [...data]
.map(e => {
return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime };
})
.sort(function(a, b) {
if (a["votingPowerMixUpTime"] === b["votingPowerMixUpTime"]) {
return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], toggleDirection(sortDirection));
} else {
return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], sortDirection);
}
});
const groupSize = 10;
return groupAndShuffle(data, groupSize).flat();
}

if (canSort) {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 5df1669

Please sign in to comment.