-
-
Save hashtaggerHQ/c80b73406af49090944ae3bef87c7646 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
import pick from "@stdlib/random-sample"; | |
export const drawWinnerAirdrop = ( | |
participants, | |
hash, | |
numberBigAmount, | |
numberSmallAmount, | |
bigAmount, | |
smallAmount | |
) => { | |
const seed = Number(`0x${hash.slice(-6)}`); | |
const sum = participants.reduce((prev, e) => prev + Number(e.points), 0); | |
const sample = pick.factory({ | |
replace: false, | |
seed, | |
size: Math.min(numberBigAmount + numberSmallAmount, participants.length), | |
}); | |
const draw = sample(participants.map((e) => e.address), | |
{ probs: participants.map((e) => Number(e.points) / sum)}); | |
const winnersObj = Object.fromEntries( | |
draw.map((e, i) => [e, i < numberBigAmount ? bigAmount : smallAmount]) | |
); | |
return participants.map((e) => ({ | |
address: e.address, | |
points: e.points, | |
rewards: String(winnersObj[e.address] || 0), | |
})); | |
}; | |
export default drawWinnerAirdrop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment