Files
SHiNE-server/shine-solana/shine/create-SHiNE-DAO/scripts/01a_mint_governance_token.js
T

69 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
"use strict";
const path = require("path");
const { Connection, PublicKey, Transaction, sendAndConfirmTransaction } = require("@solana/web3.js");
const {
TOKEN_PROGRAM_ID,
getAssociatedTokenAddressSync,
createAssociatedTokenAccountIdempotentInstruction,
createMintToInstruction,
getMint,
} = require("@solana/spl-token");
const { clusterUrl, loadKeypair, parseEnvConfig, resolveConfigPath } = require("./js_common");
async function main() {
const targetWallet = process.argv[3];
const amountRaw = process.argv[4] || "1";
if (!targetWallet) {
throw new Error("Usage: node 01a_mint_governance_token.js <config.env> <target_wallet> [amount]");
}
const cfg = parseEnvConfig(resolveConfigPath(process.argv[2], "dao.config.env"));
if (!cfg.DAO_GOV_TOKEN_MINT) throw new Error("В конфиге пустой DAO_GOV_TOKEN_MINT");
if (!cfg.DAO_ISSUER_KEYPAIR) throw new Error("В конфиге пустой DAO_ISSUER_KEYPAIR");
const conn = new Connection(clusterUrl(cfg.DAO_CLUSTER || "devnet"), "confirmed");
const issuer = loadKeypair(path.resolve(cfg.DAO_ISSUER_KEYPAIR));
const mint = new PublicKey(cfg.DAO_GOV_TOKEN_MINT);
const target = new PublicKey(targetWallet);
const amount = BigInt(amountRaw);
const mintInfo = await getMint(conn, mint, "confirmed", TOKEN_PROGRAM_ID);
if (mintInfo.decimals !== 0) {
throw new Error(`Ожидался governance mint с decimals=0, а получено ${mintInfo.decimals}`);
}
const ata = getAssociatedTokenAddressSync(mint, target, false, TOKEN_PROGRAM_ID);
const tx = new Transaction().add(
createAssociatedTokenAccountIdempotentInstruction(
issuer.publicKey,
ata,
target,
mint,
TOKEN_PROGRAM_ID
),
createMintToInstruction(
mint,
ata,
issuer.publicKey,
amount,
[],
TOKEN_PROGRAM_ID
)
);
const sig = await sendAndConfirmTransaction(conn, tx, [issuer], { commitment: "confirmed" });
console.log("Mint done.");
console.log("target:", target.toBase58());
console.log("ata:", ata.toBase58());
console.log("amount:", amount.toString());
console.log("signature:", sig);
}
main().catch((e) => {
console.error("MintGovernanceToken error:", e?.message || e);
process.exit(1);
});