Lottery script for picking a number of weighted random addresses of FXHash token holders, import from a number of CSV files. These CSV files are per-project and each have the format: `address, numTokens`. Furthermore, the weight of the projects can be adjusted via the `PROJECT_WEIGHTS` object...
This file contains 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
import { float, parseCSVSimpleFromString } from "@thi.ng/csv"; | |
import { readText } from "@thi.ng/file-io"; | |
import { weightedRandomKey } from "@thi.ng/random"; | |
import { | |
assocObj, | |
keys, | |
map, | |
normFrequenciesAuto, | |
repeatedly, | |
transduce, | |
} from "@thi.ng/transducers"; | |
/** | |
* Number of lottery winners | |
*/ | |
const NUM_WINNERS = 150; | |
/** | |
* Project weights adjusted according to Twitter poll: | |
* https://twitter.com/toxi/status/1532095956514131969 | |
*/ | |
const PROJECT_WEIGHTS = { | |
"defrag-v1": 1, | |
"defrag-v2": 0.189, | |
"defrag-v3": 0.089, | |
quasiflock: 0.721, | |
}; | |
/** | |
* Parse CSV into object of weights (addresses as keys, number of tokens held as values) | |
* | |
* CSV obtained from: | |
* https://fxcollectors.stroep.nl/?project=981#page=owners | |
*/ | |
const csvWeightMap = (path) => | |
assocObj( | |
parseCSVSimpleFromString( | |
{ header: false, cols: [true, float()] }, | |
readText(path) | |
) | |
); | |
/** | |
* Load all project CSV files and construct nested object of addresses per | |
* project, i.e. | |
* | |
* ``` | |
* { | |
* "defrag-v1": { "tz1A...": 20, "tz1B...": 5 }, | |
* "defrag-v2": { "tz1C...": 3, "tz1D...": 11 }, | |
* ... | |
* } | |
* ``` | |
*/ | |
const addresses = transduce( | |
map((id) => [id, csvWeightMap(id + ".csv")]), | |
assocObj(), | |
keys(PROJECT_WEIGHTS) | |
); | |
/** | |
* Create a function, if when called returns a weighted random project ID. | |
*/ | |
const pickProject = weightedRandomKey(PROJECT_WEIGHTS); | |
/** | |
* Similar to above, but for picking actual addresses (grouped by project), | |
* weighted by number of tokens per address. | |
*/ | |
const projectAddressPickers = transduce( | |
map((id) => [id, weightedRandomKey(addresses[id])]), | |
assocObj(), | |
keys(PROJECT_WEIGHTS) | |
); | |
/** | |
* Draw a random list of project IDs (weighted) | |
*/ | |
const winnerProjects = [...repeatedly(pickProject, NUM_WINNERS)]; | |
/** | |
* For each picked project ID, pick a weighted winner address | |
*/ | |
const winnerAddresses = winnerProjects.map((id) => projectAddressPickers[id]()); | |
/** | |
* Print (sorted) winners | |
*/ | |
console.log(winnerAddresses.sort().join("\n")); | |
/** | |
* Print out histogram of project & address distributions | |
*/ | |
console.log(normFrequenciesAuto(winnerProjects)); | |
console.log(normFrequenciesAuto(winnerAddresses)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alphabetically sorted list of addresses and their allocated slots (for the C-SCAPE fxhash drop), randomly chosen via the above script (only reformatted as CSV):