Skip to content

Instantly share code, notes, and snippets.

@sbauch
Created September 2, 2021 14:56
Show Gist options
  • Save sbauch/2d0dde4d92e51eb11f517c10026d650d to your computer and use it in GitHub Desktop.
Save sbauch/2d0dde4d92e51eb11f517c10026d650d to your computer and use it in GitHub Desktop.
Script for picking Wrassler giveaway winner
import 'dotenv/config';
import {ethers} from 'hardhat';
import RandomOrg from 'random-org';
import {
abi as arenaAbi,
address as arenaAddress,
} from '../deployments/matic/WrasslingArenaV2.json';
import {
address as betaAddress,
abi as betaAbi,
} from '../deployments/matic/BetaWrasslers.json';
import {utils} from 'ethers';
async function main() {
const accounts = await ethers.getSigners();
const WrasslingArena = await ethers.getContractAt(
arenaAbi,
arenaAddress,
accounts[1]
);
const WrasslersBeta = await ethers.getContractAt(
betaAbi,
betaAddress,
accounts[1]
);
const count = await WrasslersBeta.totalSupply();
const tokenIds = Array.from({length: count.toNumber()}, (_, i) => i + 1);
const ADMINS = [
utils.getAddress('0x4DB119936A4141b63e12446C6349880D19560690'), // shawn
utils.getAddress('0x0090720FeD7Fed66eD658118b7B3BB0189D3A495'), // sam
utils.getAddress('0x67364ad8F3128A514dAC47032Aa4fbA3C5E65495'), // jonny
utils.getAddress('0xc0F5b93Bb77271d9CE898d08013f4958d1478534'), // wrasslers.eth
utils.getAddress('0x32A0fA5EE62c78dde4B74ae1d4B22e7E63EB337d'), // james
utils.getAddress('0x173974Ec39dD85307c4171f4ccd75491bD96b405'), // ashe
utils.getAddress('0x6240dbA61fA6377B9Fe79523133a64A2aAd9178A'), // jonny
utils.getAddress('0x3D45A968ABa09D2124354c7EC4CB0125afd91810'), // alex
];
const ticketsMap: Record<string, number> = {};
for (const id of tokenIds) {
const owner = await WrasslersBeta.ownerOf(id);
if (ADMINS.includes(utils.getAddress(owner))) {
continue;
}
const player = await WrasslingArena.players(owner);
const playerEntries = player.wins.toNumber();
if (playerEntries > 0) ticketsMap[owner] = playerEntries;
}
const tickets = Object.entries(ticketsMap).reduce(
(all, [address, count]) => [
...all,
...Array.from({length: count}, () => address),
],
[]
);
console.warn(tickets.length);
const shuffled = shuffle(tickets);
const random = new RandomOrg({
apiKey: process.env.RANDOM_ORG_KEY,
});
const {
random: {
data: [randomIndex],
},
} = await random.generateIntegers({
min: 0,
max: shuffled.length,
n: 1,
});
return tickets[randomIndex];
}
/**
* Shuffles array in place. ES6 version
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
main()
.then((winner) => {
console.log(winner);
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment