Last active
April 9, 2024 10:51
-
-
Save ZaK3939/cbc4e13674f8cf2aaf62e6f98161fc8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Credential = { | |
id: number; | |
votes: number; | |
art: boolean; | |
}; | |
function getQualifiedCredentials( | |
credentials: Credential[], | |
prevThreshold: number, | |
prevMedianVotes: number, | |
minVotes: number | |
): { qualifiedCredentials: number[]; nextThreshold: number } { | |
// Filter credentials with art set to true | |
const artCredentials = credentials.filter((c) => c.art); | |
// Exclude credentials with 0 votes | |
const filteredCredentials = artCredentials.filter((c) => c.votes > 0); | |
// Exclude credentials below the minimum number of votes | |
const validCredentials = filteredCredentials.filter( | |
(c) => c.votes >= minVotes | |
); | |
// Step 1: Calculate the median | |
const sortedVotes = validCredentials.map((c) => c.votes).sort((a, b) => a - b); | |
const medianVotes = | |
(sortedVotes[Math.floor((sortedVotes.length - 1) / 2)] + | |
sortedVotes[Math.ceil((sortedVotes.length - 1) / 2)]) / | |
2; | |
// Step 2: Determine qualified credentials using the current block's threshold | |
const qualifiedCredentials = validCredentials | |
.filter((c) => c.votes > prevThreshold) | |
.map((c) => c.id); | |
// Step 3: Adjust the threshold for the next block | |
let nextThreshold; | |
if (medianVotes > prevMedianVotes) { | |
// If the actual median votes exceed the target, increase the threshold by 12.5% for the next block | |
nextThreshold = prevThreshold * 1.125; | |
} else { | |
// If the actual median votes are below or equal to the target, decrease the threshold by 12.5% for the next block | |
nextThreshold = prevThreshold * 0.875; | |
} | |
// Step 4: Check the minimum threshold | |
const minThreshold = medianVotes * 0.1; | |
nextThreshold = Math.max(nextThreshold, minThreshold); | |
return { qualifiedCredentials, nextThreshold }; | |
} | |
function calculateCoverageRatio( | |
credentials: Credential[], | |
selectedCredentialIds: number[] | |
): number { | |
const totalVotes = credentials | |
.filter((c) => c.art) | |
.reduce((sum, c) => sum + c.votes, 0); | |
const selectedCredentialVotes = credentials | |
.filter((c) => selectedCredentialIds.includes(c.id) && c.art) | |
.reduce((sum, c) => sum + c.votes, 0); | |
return selectedCredentialVotes / totalVotes; | |
} | |
// Usage example | |
const credentials: Credential[] = [ | |
{ id: 1, votes: 0, art: false }, | |
{ id: 2, votes: 0, art: true }, | |
{ id: 3, votes: 0, art: true }, | |
{ id: 4, votes: 0, art: false }, | |
{ id: 5, votes: 100, art: true }, | |
{ id: 6, votes: 0, art: false }, | |
{ id: 7, votes: 20, art: true }, | |
{ id: 8, votes: 30, art: false }, | |
{ id: 9, votes: 10, art: true }, | |
{ id: 10, votes: 10, art: true }, | |
{ id: 11, votes: 0, art: false }, | |
{ id: 12, votes: 50, art: true }, | |
{ id: 13, votes: 0, art: false }, | |
{ id: 14, votes: 0, art: true }, | |
{ id: 15, votes: 200, art: true }, | |
{ id: 16, votes: 0, art: false }, | |
{ id: 17, votes: 75, art: true }, | |
{ id: 18, votes: 0, art: false }, | |
{ id: 19, votes: 120, art: true }, | |
{ id: 20, votes: 0, art: false }, | |
]; | |
// Calculate minVotes as 5% of the total number of credentials with art set to true | |
const minVotes = Math.ceil( | |
credentials.filter((c) => c.art).length * 0.05 | |
); | |
let prevThreshold = 38.63; | |
let prevMedianVotes = 88.3; | |
const { qualifiedCredentials, nextThreshold } = getQualifiedCredentials( | |
credentials, | |
prevThreshold, | |
prevMedianVotes, | |
minVotes | |
); | |
console.log("Qualified Credentials:", qualifiedCredentials); | |
console.log("Next Threshold:", nextThreshold); | |
// Calculate coverage ratio for a given array of credential IDs | |
const selectedCredentialIds = [5, 12, 15, 19]; | |
const coverageRatio = calculateCoverageRatio(credentials, selectedCredentialIds); | |
console.log("Coverage Ratio:", coverageRatio); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment