Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Last active October 25, 2022 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postspectacular/69a969906ba0226e57181bd9c0b0f4d2 to your computer and use it in GitHub Desktop.
Save postspectacular/69a969906ba0226e57181bd9c0b0f4d2 to your computer and use it in GitHub Desktop.
NFT giveaway lottery code
import { int, parseCSVSimple } from "@thi.ng/csv";
import { readText } from "@thi.ng/file-io";
import { pickRandom } from "@thi.ng/random";
import { split } from "@thi.ng/strings";
import {
comp,
distinct,
mapcat,
push,
repeat,
repeatedly,
run,
take,
trace,
transduce,
} from "@thi.ng/transducers";
// CSV row format: address,numtokens
const addresses = transduce(
comp(
// parse CSV and coerce 2nd column to number
parseCSVSimple({ header: false, cols: [true, int()] }),
// repeat each row as many times as stated in 2nd column
mapcat(([addr, n]) => repeat(addr, n))
),
// collect results as array
push(),
// pre-split into lines
split(readText("owners.csv"))
);
// pick unique winners
run(
// composed transducer:
comp(
// distinct/unique values only
distinct(),
// max number of results
take(32),
// side effect: output to console
trace()
),
// an infinite sequence of random samples
repeatedly(() => pickRandom(addresses))
);
tz1aJNTKvCH7Rr9CxAJLkrgWfF7qdzvzcN69
tz1Mw8qDtpeg2cwSVQDCQhgsXvG4T7NSLkQJ
tz1gsoi2YoqkS7Z4VpBzZuH7tnjKqbDMSX4T
tz1Zn99fL2u22sdioKZs3GbV2YRP12ktsiDE
tz1VQVTRYijbXCZQ9dukcRhHbVFYepKZwhAR
tz1KvLFwVtkMLcnRrtnRof4L8GKkL9LP2wtb
tz1XqvtucEe9kweXdCMohPtr2FqQR6u1Fio1
tz1MQ4gnVPqFUbUz3emtzW1H58gevX7yAUrw
tz1RRfSJ7m1MVRuPmL4qmXax1tbTkn5hrv6r
tz1fTC3BXuCjQx1AJp6JNSL5GTuQ9yK9Q2df
tz1PVDLTat9RRtfn6BbKxHbKR6RsjBEWL2dp
tz1fQYbiJkwkjdU4PCs1cqefvQpD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