Skip to content

Commit

Permalink
Merge pull request #74 from ensdomains/ranking
Browse files Browse the repository at this point in the history
Improve ranking function
  • Loading branch information
Arachnid authored Nov 15, 2021
2 parents 6b37d84 + 8bfb9bd commit 9774bd4
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@ import { useLocation } from "react-router-dom";
import { useEffect, useState } from "react";
import { normalize } from "@ensdomains/eth-ens-namehash";
import { keccak_256 as sha3 } from "js-sha3";
import { Contract } from "ethers";
import {
BrowserRouter as Router,
Switch,
Route,
useHistory,
} from "react-router-dom";
import styled from "styled-components/macro";
import { Contract, ethers } from "ethers";
import { gql } from "graphql-tag";
import { useQuery } from "@apollo/client";
import { getEthersProvider } from "../web3modal";
import { apolloClientInstance } from "../apollo";
import ENSDelegateAbi from "../assets/abis/ENSDelegate.json";
import ReverseRecordsAbi from "../assets/abis/ReverseRecords.json";
import ENSTokenAbi from "../assets/abis/ENSToken.json";

import { getDelegateReferral } from "../pages/ENSConstitution/delegateHelpers";
import { delegates as delegatesReactive } from "../apollo";

import {
ALLOCATION_ENDPOINT,
getENSDelegateContractAddress,
getENSTokenContractAddress,
getReverseRecordsAddress,
} from "./consts";

Expand Down Expand Up @@ -126,15 +115,15 @@ const generateRankingScore = (score, total, name) => {

const addBalance = async (
cleanList,
tokensClaimed
tokensDelegated
) => {
return cleanList.map((item) => {
return {
...item,
ranking:
generateRankingScore(
item.votes,
tokensClaimed,
tokensDelegated,
item.name
) * Math.random(),
};
Expand All @@ -143,13 +132,13 @@ const addBalance = async (

const rankDelegates = async (
delegateList,
tokensClaimed,
tokensDelegated,
prepopDelegate
) => {
const cleanList = cleanDelegatesList(delegateList);
const withTokenBalance = await addBalance(
cleanList,
tokensClaimed,
tokensDelegated,
prepopDelegate
);
const sortedList = withTokenBalance.sort((x, y) => {
Expand Down Expand Up @@ -189,12 +178,6 @@ export const useGetDelegates = (isConnected) => {
(result) => result.domain.id
);

const ENSTokenContract = new Contract(
getENSTokenContractAddress(),
ENSTokenAbi.abi,
provider
);

const ENSDelegateContract = new Contract(
getENSDelegateContractAddress(),
ENSDelegateAbi.abi,
Expand Down Expand Up @@ -225,17 +208,10 @@ export const useGetDelegates = (isConnected) => {
(d, i) => d.name == names[i]
);

const tokensLeft = await ENSTokenContract.balanceOf(
ENSTokenContract.address
);
// Actual number of tokens claimed, plus total of delegates' own airdrops.
const tokensClaimed =
25000000 -
bigNumberToDecimal(tokensLeft);
console.log({ target: tokensClaimed * TARGET_DELEGATE_SIZE });
const tokensDelegated = processedDelegateDataWithReverse.map((delegate) => delegate.votes).reduce((a, b) => a.add(b)).div(ethers.utils.parseEther("1")).toNumber();
const rankedDelegates = await rankDelegates(
processedDelegateDataWithReverse,
tokensClaimed,
tokensDelegated,
getDelegateReferral()
);
setDelegates(rankedDelegates);
Expand Down

0 comments on commit 9774bd4

Please sign in to comment.