-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy-token-script.js
More file actions
158 lines (131 loc) · 5.3 KB
/
Copy pathdeploy-token-script.js
File metadata and controls
158 lines (131 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require('dotenv').config({ path: '.env.local' });
const fs = require('fs');
const { Connection, PublicKey, Keypair } = require('@solana/web3.js');
const { createMint, getOrCreateAssociatedTokenAccount, mintTo } = require('@solana/spl-token');
// Modified import to access the default export for bs58
const bs58 = require('bs58').default;
// Configuration
// Default RPC_URL is set to the local validator endpoint
const NETWORK_URL = process.env.RPC_URL || "http://127.0.0.1:8899";
const TOKEN_CONFIG = {
name: "NURO",
symbol: "$NURO",
decimals: 9,
initialSupply: 1000000, // Much lower initial supply - just 1000 tokens
};
// Load wallet from private key
function loadWallet() {
const privateKeyStr = process.env.SOLANA_PRIVATE_KEY;
if (!privateKeyStr) {
console.error("❌ SOLANA_PRIVATE_KEY not set in .env.local");
process.exit(1);
}
try {
// Decode base58 private key
const privateKeyBytes = bs58.decode(privateKeyStr);
return Keypair.fromSecretKey(privateKeyBytes);
} catch (error) {
console.error("❌ Error loading wallet:", error.message);
process.exit(1);
}
}
async function deployToken() {
console.log(`🚀 Initializing token deployment on ${NETWORK_URL}...`);
try {
// Set up connection
const connection = new Connection(NETWORK_URL, 'confirmed');
// Load wallet
const wallet = loadWallet();
console.log(`📝 Using wallet: ${wallet.publicKey.toString()}`);
// Check wallet balance
const balance = await connection.getBalance(wallet.publicKey);
console.log(`💰 Wallet balance: ${balance / 1e9} SOL`);
if (balance < 10000000) { // 0.01 SOL minimum
console.error("❌ Insufficient balance to pay for token creation. Need at least 0.01 SOL");
process.exit(1);
}
console.log(`📝 Creating token with the following configuration:`);
console.log(` Name: ${TOKEN_CONFIG.name}`);
console.log(` Symbol: ${TOKEN_CONFIG.symbol}`);
console.log(` Decimals: ${TOKEN_CONFIG.decimals}`);
console.log(` Initial Supply: ${TOKEN_CONFIG.initialSupply}`);
// Create new token mint
console.log("⏳ Creating token mint...");
const mint = await createMint(
connection, // connection
wallet, // payer
wallet.publicKey, // mint authority
wallet.publicKey, // freeze authority
TOKEN_CONFIG.decimals // decimals
);
console.log(`✅ Token mint created: ${mint.toString()}`);
// Create token account to hold balance
console.log("⏳ Creating token account...");
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
wallet,
mint,
wallet.publicKey
);
console.log(`✅ Token account created: ${tokenAccount.address.toString()}`);
// Mint initial token supply
console.log(`⏳ Minting ${TOKEN_CONFIG.initialSupply} tokens...`);
// Calculate supply with decimals (1000 tokens with 9 decimals = 1000 * 10^9)
const initialSupplyWithDecimals = BigInt(TOKEN_CONFIG.initialSupply) * BigInt(10 ** TOKEN_CONFIG.decimals);
await mintTo(
connection,
wallet,
mint,
tokenAccount.address,
wallet.publicKey,
initialSupplyWithDecimals
);
console.log(`✅ Tokens minted successfully!`);
// Save token details to a file
const tokenData = {
name: TOKEN_CONFIG.name,
symbol: TOKEN_CONFIG.symbol,
decimals: TOKEN_CONFIG.decimals,
mintAddress: mint.toString(),
tokenAccount: tokenAccount.address.toString(),
ownerAddress: wallet.publicKey.toString(),
network: NETWORK_URL.includes("testnet")
? "testnet"
: NETWORK_URL.includes("devnet")
? "devnet"
: "local",
deploymentDate: new Date().toISOString()
};
const outputFile = "token-details.json";
fs.writeFileSync(outputFile, JSON.stringify(tokenData, null, 2));
console.log(`💾 Token details saved to ${outputFile}`);
// Generate Solana Explorer links
// For a local validator, there's no public explorer available
const explorerBase = NETWORK_URL.includes("testnet")
? "https://explorer.solana.com/?cluster=testnet"
: NETWORK_URL.includes("devnet")
? "https://explorer.solana.com/?cluster=devnet"
: "Local Validator - no explorer available";
console.log("\n🔎 Solana Explorer Links:");
console.log(` Token: ${explorerBase}&address=${mint.toString()}`);
console.log("\n📋 Next steps:");
console.log(` 1. Add token ${mint.toString()} to your wallet`);
console.log(" 2. Create liquidity pools if needed");
console.log(" 3. Share the token with your community");
return tokenData;
} catch (error) {
console.error("❌ Error deploying token:", error);
if (error.message.includes("timed out")) {
console.error("Network connection timed out. This can happen on testnet/devnet. Try again or use a different RPC endpoint.");
} else if (error.message.includes("insufficient funds")) {
console.error("Your wallet doesn't have enough SOL to complete this transaction.");
}
process.exit(1);
}
}
// Run the deployment
deployToken().then(() => {
console.log("🎉 Deployment process completed");
}).catch(err => {
console.error("Fatal error:", err);
});