-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraffle.js
72 lines (59 loc) · 2.22 KB
/
raffle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Totemancer.com
* Copyright by Totemancer.com
**/
const crypto = require('crypto');
function seededRandom(seed) {
let m = 0x80000000;
return () => {
seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF;
return (seed / m);
};
}
function shuffleArray(array, seed) {
let random = seededRandom(seed);
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function calculateRarities(transactions) {
return {
ultraRare: Math.floor(transactions * 0.0132),
rare: Math.floor(transactions * 0.0658),
uncommon: Math.floor(transactions * 0.2632),
common: transactions - Math.floor(transactions * 0.0132) - Math.floor(transactions * 0.0658) - Math.floor(transactions * 0.2632)
};
}
function runRaffle(txHash, transactions) {
const hash = crypto.createHash('sha256');
hash.update(txHash);
const buffer = hash.digest();
// Use first 4 bytes to create a manageable integer
const seed = buffer.readInt32BE(0);
const { ultraRare, rare, uncommon, common } = calculateRarities(transactions);
const rarities = Array(ultraRare).fill('Ultra Rare')
.concat(Array(rare).fill('Rare'))
.concat(Array(uncommon).fill('Uncommon'))
.concat(Array(common).fill('Common'));
shuffleArray(rarities, seed);
const results = rarities.map((rarity, index) => ({ tx: index + 1, rarity }));
// Display results and summary
results.forEach((result) => {
console.log(`TX/MintID: ${result.tx}, Rarity: ${result.rarity}`);
});
console.log('\nRarity Summary:');
console.log(`Ultra Rare: ${ultraRare} (${(ultraRare / transactions * 100).toFixed(2)}%)`);
console.log(`Rare: ${rare} (${(rare / transactions * 100).toFixed(2)}%)`);
console.log(`Uncommon: ${uncommon} (${(uncommon / transactions * 100).toFixed(2)}%)`);
console.log(`Common: ${common} (${(common / transactions * 100).toFixed(2)}%)`);
}
// Get inputs from command line arguments
const [, , txHash, transactions] = process.argv;
// Check if inputs are valid
if (!txHash || !transactions) {
console.error('Please provide valid values for Transaction Hash and Total TX Number.');
process.exit(1);
}
// Run the raffle
runRaffle(txHash, parseInt(transactions));