Last active
July 26, 2022 13:50
-
-
Save guillegette/a8e701beefc7f62e6917a909c9fb762d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// TokenID: Supply | |
const stickers = { | |
'1012': 10, | |
'1013': 11, | |
'1014': 9, | |
'1015': 20, | |
'1016': 15, | |
'1017': 5, | |
'1018': 10, | |
'1019': 17, | |
'1031': 3 | |
}; | |
// Returns the total supply of stickers | |
const totalStickers = () => { | |
let total = 0; | |
Object.values(stickers).forEach(q => total += q); | |
return total; | |
} | |
// Returns a random token ID using the supply as weigth | |
const getRandomSticker = () => { | |
const r = Math.random(); // 0 - 1 | |
const totalSupply = totalStickers(); // 100 | |
let sum = 0; | |
for (i in stickers) { | |
sum += stickers[i]/totalSupply; // 10/100 | |
if (r <= sum) return i; | |
} | |
} | |
// Function to validate distribution | |
const stickersLog = {}; | |
for(let i = 0; i<100; i++) { | |
let sticker = getRandomSticker(); | |
if (!stickersLog[sticker]) { | |
stickersLog[sticker] = 0; | |
} | |
stickersLog[sticker]++; | |
} | |
/* Claim the prize | |
* @param albumHash (String) The list of stickers to claim the prize, all concatanated and sorted. | |
*/ | |
const claimPrize = (albumHash) => { | |
// albumHash = 101031020410312 example, 3 stickers 10103, 10204, 10312 | |
const album = '101102103111112113'; // 3 types per country, 101, 102, 103, 111, 112, 113 | |
let hashToCheck = ''; | |
// we can do some length check already to avoid bad data | |
for(let i = 0; i < albumHash.length; i+=5){ | |
// split into stickers | |
let sticker = albumHash.substr(i, 5); | |
console.log(sticker); | |
// validate that the wallet is owner of the sticker, return error if user is not owner of any | |
// add to hash | |
hashToCheck += sticker.substr(0, 3); // we dont need the last 2 digits | |
} | |
if (album === hashToCheck) { | |
// burn all stickers | |
// send the wallet the cash prize | |
return true; | |
} else { | |
// return error that hash is not valid | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment