Skip to content

Instantly share code, notes, and snippets.

@mxjxn
Last active June 27, 2024 17:22
Show Gist options
  • Save mxjxn/761f3b36b0a67b31582a39f0bd0152f2 to your computer and use it in GitHub Desktop.
Save mxjxn/761f3b36b0a67b31582a39f0bd0152f2 to your computer and use it in GitHub Desktop.
Based on a Hypersub subscriber csv, conduct a raffle
import fs from "fs/promises";
import chalk from "chalk";
const dequote = (str) => str.replace(/"/g, "");
async function conductRaffle(amount = 1) {
// get the entrants' tickets and the total-ticket-count
let csv = await fs.readFile("muse-june.27.24.csv", "utf8");
let lines = csv.split("\n").slice(1);
let entries = lines.map((line) => {
let dequoted = dequote(line);
const [
ens,
address,
email,
farcaster,
_status,
_startedAt,
_expiresAt,
purchasedPeriods,
] = dequoted.split(",");
return {
ens,
address,
farcaster,
email,
tickets: Number(purchasedPeriods),
};
});
let totalTickets = entries.reduce((acc, entry) => entry.tickets + acc, 0);
let tCount = 0;
for (let i = 0; i < entries.length; i++) {
let entry = entries[i];
entry.firstTicket = tCount;
tCount += entry.tickets;
}
console.log("\n", chalk.blue("_________________________"), "\n");
console.log(chalk.blue("totalTickets: "), totalTickets);
console.log(chalk.blue("subscribers: "), entries.length);
console.log(
chalk.blue("Average # of tickets per subscriber"),
Math.round(totalTickets / entries.length)
);
console.log("\n", chalk.blue("_________________________"), "\n");
let winners = [],
j = 0;
while (j < amount) {
let winningTicket = Math.floor(Math.random() * totalTickets);
loop2: for (let i = 0; i < entries.length; i++) {
let entry = entries[i];
if (
entry.firstTicket <= winningTicket &&
winningTicket < entry.firstTicket + entry.tickets
) {
winners.push(entry);
entry.tickets--;
// announce winner
console.log(chalk.green("winning ticket is: "), winningTicket);
console.log(chalk.green("The winner is: "));
console.log(
chalk.greenBright.bold(
`ENS: ${entry.ens}\nHandle: ${entry.farcaster}\nAddress: ${entry.address}`
)
);
j++;
break loop2;
}
}
}
console.log("πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰");
}
conductRaffle(7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment