Skip to content

Instantly share code, notes, and snippets.

@hashtaggerHQ

hashtaggerHQ/.js Secret

Created July 6, 2022 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashtaggerHQ/c80b73406af49090944ae3bef87c7646 to your computer and use it in GitHub Desktop.
Save hashtaggerHQ/c80b73406af49090944ae3bef87c7646 to your computer and use it in GitHub Desktop.
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