Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example distribution #12

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"arktype": "^2.0.0-dev.26",
"cardano-ts": "^0.2.0",
"commander": "^12.1.0",
"decimal.js": "^10.4.3",
"es-toolkit": "^1.16.0",
"inquirer": "^10.1.8",
"ts-handling": "^0.2.2"
Expand Down
88 changes: 59 additions & 29 deletions src/distribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import {
import { type } from "arktype";
import { Seed } from "cardano-ts";
import { Command } from "commander";
import Decimal from "decimal.js";
import { chunk } from "es-toolkit/array";
import { isProblem, mayFail, Problem } from "ts-handling";
import { loadLucid } from "wallet";
import { Config, logThenExit, Options, validate } from "./inputs";
import { loadPlutus } from "./script";
import { getNetwork, loadWalletFromSeed } from "./wallet";

const amountPerTx = 45;
const amountPerTx = 65;

const program = new Command()
.name("distribute")
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -40,8 +41,11 @@ const program = new Command()
const config = validate(Config, process.env);
const projectId = config.BLOCKFROST_API_KEY;
const lucid = await loadLucid(projectId);
const addresses = validate(Addresses(lucid.config().network), $filename);
const amount = addresses.length;
const distributions = validate(
Addresses(lucid.config().network),
$filename
);
const amount = distributions.length;
const options = validate(Options, $options);

const seed = await password({ message: "Enter your seed phrase" });
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -76,7 +80,7 @@ const program = new Command()
const script = createScript(plutus, refs[0]);
const policy = mintingPolicyToId(script);
const tokenChunks = chunk(generateTokens(policy, amount), amountPerTx);
const addressChunks = chunk(addresses, amountPerTx);
const distributionChunks = chunk(distributions, amountPerTx);

const deploy = lucid
.newTx()
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -92,31 +96,33 @@ const program = new Command()

for (let i = 0; i < tokenChunks.length; i++) {
const tokens = tokenChunks[i];
const addresses = addressChunks[i];
const distribution = distributionChunks[i];
const ref = refs[i];
lucid.selectWallet.fromAddress(wallet.address, [ref]);
const tx = lucid.newTx().readFrom([refScript]);
const metadata = options.metadata || {};
const data = tokens.reduce<Record<string, Record<string, string>>>(
(tokens, token) => {
tokens[token.substring(56)] = metadata;
return tokens;
},
{}
);

for (const [j, token] of tokens.entries()) {
const { address, amount } = distribution[j];
data[token.substring(56)] = { ...data[token.substring(56)], amount };
tx.mintAssets({ [token]: 1n }, Data.void()).pay.ToAddress(address, {
[token]: 1n,
});
}

if (options.metadata) {
const metadata = options.metadata;
const data = tokens.reduce<Record<string, Record<string, string>>>(
(tokens, token) => {
tokens[token.substring(56)] = metadata;
return tokens;
},
{}
);
tx.attachMetadata(721, {
[policy]: data,
});
}

for (const [j, token] of tokens.entries())
tx.mintAssets({ [token]: 1n }, Data.void()).pay.ToAddress(
addresses[j],
{ [token]: 1n }
);

const [, , mintTx] = await tx.chain();
txs.push(mintTx);
}
Expand All @@ -134,7 +140,16 @@ const Addresses = (network: Network) =>
const data = mayFail(() => readFileSync(v, "utf8")).unwrap();
if (isProblem(data)) return ctx.error("valid filename");

const addresses = data.split(/\r?\n/).filter((line) => line.trim() !== "");
const lines = data.split(/\r?\n/).filter((line) => line.trim() !== "");
const addresses = lines.map((line) => line.split(",")[0]);
const amounts = mayFail(() =>
lines.map((line) =>
new Decimal(BigInt(line.split(",")[1]).toString())
.div(10 ** 6)
.toFixed(6)
)
).unwrap();
if (isProblem(amounts)) return ctx.error("valid numbers in csv");
const stakeKeys: Record<string, string> = {};

for (const address of addresses) {
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -158,8 +173,15 @@ const Addresses = (network: Network) =>
}

if (!addresses.length) return ctx.error("a list of addresses");

return addresses;
if (addresses.length != amounts.length)
return ctx.error("a list of addresses with amounts");

return addresses.map((address, index) => {
return {
address,
amount: amounts[index],
};
});
});

const createScript = (plutus: string, ref: UTxO): MintingPolicy => {
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -182,13 +204,21 @@ const createBlackholeAddress = (network: Network) => {
});
};

const generateTokens = (policy: string, amount: number) =>
Array.from({ length: amount }).map(
() =>
const Hex = "0123456789abcdef";

const generateTokens = (policy: string, amount: number, length: number = 4) => {
const names = new Set<string>();

while (names.size < amount)
names.add(
policy +
[...Array(64)]
.map(() => Math.floor(Math.random() * 16).toString(16))
.join("")
);
Array.from(
{ length },
() => Hex[Math.floor(Math.random() * Hex.length)]
).join("")
);

return [...names];
};

program.parseAsync(process.argv);
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
SynthLuvr marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading