@forgekit-labs/token 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ForgeKit Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # @forgekit-labs/token
2
+
3
+ Mint Solana tokens with authority revocations and burn in one atomic transaction.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @forgekit-labs/token
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { mint } from '@forgekit-labs/token';
15
+
16
+ const { signature, mintAddress, supply, burned, revoked } = await mint('my-token')
17
+ .supply(1_000_000_000)
18
+ .decimals(6)
19
+ .metadata({ name: 'MY TOKEN', symbol: 'MTK', uri: metadataUri })
20
+ .burn(2)
21
+ .revoke('freeze')
22
+ .revoke('mint')
23
+ .wallet(keypair.secretKey)
24
+ .rpc('https://api.mainnet-beta.solana.com')
25
+ .send();
26
+ ```
27
+
28
+ Everything happens in a single VersionedTransaction (V0): account creation, mint, ATA setup, full-supply mint, burn, and authority revocations. Either it all lands or none of it does.
29
+
30
+ Authority revocation order is fixed: freeze is always revoked before mint. Reversing the order can leave a token in an inconsistent state.
31
+
32
+ ## Error Handling
33
+
34
+ ```js
35
+ import { mint, ForgeTxError, ForgeRpcError } from '@forgekit-labs/token';
36
+
37
+ try {
38
+ await mint('my-token').supply(1_000_000_000).wallet(secretKey).send();
39
+ } catch (err) {
40
+ if (err instanceof ForgeTxError) {
41
+ // transaction failed on-chain
42
+ console.error(err.message, err.cause);
43
+ }
44
+ if (err.retry) {
45
+ // RPC or network issue, safe to retry
46
+ }
47
+ }
48
+ ```
49
+
50
+ ## License
51
+
52
+ MIT
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@forgekit-labs/token",
3
+ "version": "0.1.0",
4
+ "description": "Mint Solana tokens with authority revocations and burn in one atomic transaction.",
5
+ "author": "Res <support@crxcible.io> (https://github.com/solDEv0)",
6
+ "type": "module",
7
+ "main": "./src/index.js",
8
+ "exports": {
9
+ ".": "./src/index.js"
10
+ },
11
+ "engines": {
12
+ "node": ">=18.0.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/solDEv0/forgekit.git",
17
+ "directory": "packages/token"
18
+ },
19
+ "homepage": "https://github.com/solDEv0/forgekit/tree/main/packages/token#readme",
20
+ "bugs": "https://github.com/solDEv0/forgekit/issues",
21
+ "keywords": [
22
+ "solana",
23
+ "token",
24
+ "mint",
25
+ "spl",
26
+ "launchpad"
27
+ ],
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "license": "MIT",
32
+ "dependencies": {
33
+ "@forgekit-labs/errors": "^0.1.0",
34
+ "@solana/web3.js": "^1.98.0",
35
+ "@solana/spl-token": "^0.4.9"
36
+ }
37
+ }
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { mint } from './mint.js';
2
+ export {
3
+ ForgeError,
4
+ ForgeValidationError,
5
+ ForgeTxError,
6
+ ForgeRpcError,
7
+ } from '@forgekit-labs/errors';
package/src/mint.js ADDED
@@ -0,0 +1,259 @@
1
+ import {
2
+ Connection,
3
+ Keypair,
4
+ SystemProgram,
5
+ TransactionMessage,
6
+ VersionedTransaction,
7
+ sendAndConfirmRawTransaction,
8
+ } from '@solana/web3.js';
9
+ import {
10
+ createInitializeMintInstruction,
11
+ createAssociatedTokenAccountInstruction,
12
+ createMintToInstruction,
13
+ createSetAuthorityInstruction,
14
+ AuthorityType,
15
+ getAssociatedTokenAddressSync,
16
+ getMinimumBalanceForRentExemptMint,
17
+ MINT_SIZE,
18
+ TOKEN_PROGRAM_ID,
19
+ } from '@solana/spl-token';
20
+ import { ForgeValidationError, ForgeTxError, ForgeRpcError } from '@forgekit-labs/errors';
21
+
22
+ const VALID_AUTHORITIES = new Set(['freeze', 'mint']);
23
+
24
+ class MintBuilder {
25
+ #name;
26
+ #supply = null;
27
+ #decimals = 6;
28
+ #meta = null;
29
+ #burnBps = 0;
30
+ #revocations = new Set();
31
+ #secretKey = null;
32
+ #rpcUrl = 'https://api.mainnet-beta.solana.com';
33
+
34
+ constructor(name) {
35
+ if (typeof name !== 'string' || !name.trim()) {
36
+ throw new ForgeValidationError(
37
+ 'mint() requires a name string.',
38
+ 'Pass a unique identifier, e.g. mint("my-token").',
39
+ );
40
+ }
41
+ this.#name = name;
42
+ }
43
+
44
+ supply(amount) {
45
+ if (typeof amount !== 'number' || amount <= 0 || !Number.isFinite(amount)) {
46
+ throw new ForgeValidationError(
47
+ 'supply() requires a positive number.',
48
+ 'e.g. .supply(1_000_000_000)',
49
+ );
50
+ }
51
+ this.#supply = amount;
52
+ return this;
53
+ }
54
+
55
+ decimals(d) {
56
+ if (typeof d !== 'number' || d < 0 || d > 9 || !Number.isInteger(d)) {
57
+ throw new ForgeValidationError(
58
+ 'decimals() requires an integer between 0 and 9.',
59
+ );
60
+ }
61
+ this.#decimals = d;
62
+ return this;
63
+ }
64
+
65
+ metadata({ name, symbol, uri } = {}) {
66
+ if (!name) throw new ForgeValidationError('metadata() requires a name field.');
67
+ if (!symbol) throw new ForgeValidationError('metadata() requires a symbol field.');
68
+ if (!uri) throw new ForgeValidationError('metadata() requires a uri field.');
69
+ this.#meta = { name, symbol, uri };
70
+ return this;
71
+ }
72
+
73
+ // Percentage of total supply to burn immediately after mint (0 to 50).
74
+ // Fractional percentages are supported (e.g. burn(2.5) = 250 bps).
75
+ burn(pct) {
76
+ if (typeof pct !== 'number' || pct < 0 || pct > 50) {
77
+ throw new ForgeValidationError(
78
+ 'burn() requires a percentage between 0 and 50.',
79
+ 'e.g. .burn(2) burns 2% of supply on mint.',
80
+ );
81
+ }
82
+ this.#burnBps = Math.round(pct * 100);
83
+ return this;
84
+ }
85
+
86
+ // Call once per authority: .revoke('freeze').revoke('mint')
87
+ revoke(authority) {
88
+ if (!VALID_AUTHORITIES.has(authority)) {
89
+ throw new ForgeValidationError(
90
+ `revoke() received unknown authority "${authority}".`,
91
+ 'Valid authorities are "freeze" and "mint".',
92
+ );
93
+ }
94
+ this.#revocations.add(authority);
95
+ return this;
96
+ }
97
+
98
+ wallet(secretKey) {
99
+ if (!(secretKey instanceof Uint8Array)) {
100
+ throw new ForgeValidationError(
101
+ 'wallet() expects a Uint8Array (your keypair.secretKey).',
102
+ 'e.g. .wallet(keypair.secretKey)',
103
+ );
104
+ }
105
+ this.#secretKey = secretKey;
106
+ return this;
107
+ }
108
+
109
+ rpc(url) {
110
+ if (typeof url !== 'string' || (!url.startsWith('http://') && !url.startsWith('https://'))) {
111
+ throw new ForgeValidationError(
112
+ 'rpc() requires a valid HTTP or HTTPS URL.',
113
+ 'e.g. .rpc("https://api.mainnet-beta.solana.com")',
114
+ );
115
+ }
116
+ this.#rpcUrl = url;
117
+ return this;
118
+ }
119
+
120
+ async send() {
121
+ if (this.#supply === null) {
122
+ throw new ForgeValidationError(
123
+ 'No supply defined.',
124
+ 'Call .supply(1_000_000_000) before .send().',
125
+ );
126
+ }
127
+ if (!this.#secretKey) {
128
+ throw new ForgeValidationError(
129
+ 'No wallet provided.',
130
+ 'Call .wallet(keypair.secretKey) before .send().',
131
+ );
132
+ }
133
+
134
+ const payer = Keypair.fromSecretKey(Buffer.from(this.#secretKey));
135
+ const mintKeypair = Keypair.generate();
136
+ const connection = new Connection(this.#rpcUrl, 'confirmed');
137
+
138
+ let blockhash, lamports;
139
+ try {
140
+ [{ blockhash }, lamports] = await Promise.all([
141
+ connection.getLatestBlockhash('confirmed'),
142
+ getMinimumBalanceForRentExemptMint(connection),
143
+ ]);
144
+ } catch (err) {
145
+ throw new ForgeRpcError(
146
+ `Failed to fetch blockhash or rent: ${err?.message ?? String(err)}`,
147
+ err,
148
+ );
149
+ }
150
+
151
+ const rawSupply = BigInt(this.#supply) * BigInt(10 ** this.#decimals);
152
+ const burnAmount = this.#burnBps > 0
153
+ ? (rawSupply * BigInt(this.#burnBps)) / 10_000n
154
+ : 0n;
155
+ const ata = getAssociatedTokenAddressSync(
156
+ mintKeypair.publicKey,
157
+ payer.publicKey,
158
+ );
159
+
160
+ const instructions = [
161
+ // 1. Create mint account
162
+ SystemProgram.createAccount({
163
+ fromPubkey: payer.publicKey,
164
+ newAccountPubkey: mintKeypair.publicKey,
165
+ space: MINT_SIZE,
166
+ lamports,
167
+ programId: TOKEN_PROGRAM_ID,
168
+ }),
169
+ // 2. Initialise mint
170
+ createInitializeMintInstruction(
171
+ mintKeypair.publicKey,
172
+ this.#decimals,
173
+ payer.publicKey,
174
+ payer.publicKey,
175
+ ),
176
+ // 3. Create payer's associated token account
177
+ createAssociatedTokenAccountInstruction(
178
+ payer.publicKey,
179
+ ata,
180
+ payer.publicKey,
181
+ mintKeypair.publicKey,
182
+ ),
183
+ // 4. Mint full supply to payer ATA
184
+ createMintToInstruction(
185
+ mintKeypair.publicKey,
186
+ ata,
187
+ payer.publicKey,
188
+ rawSupply,
189
+ ),
190
+ ];
191
+
192
+ // 5. Burn immediately if configured
193
+ if (burnAmount > 0n) {
194
+ const { createBurnInstruction } = await import('@solana/spl-token');
195
+ instructions.push(
196
+ createBurnInstruction(ata, mintKeypair.publicKey, payer.publicKey, burnAmount),
197
+ );
198
+ }
199
+
200
+ // 6. Revoke authorities (order: freeze first, then mint)
201
+ if (this.#revocations.has('freeze')) {
202
+ instructions.push(
203
+ createSetAuthorityInstruction(
204
+ mintKeypair.publicKey,
205
+ payer.publicKey,
206
+ AuthorityType.FreezeAccount,
207
+ null,
208
+ ),
209
+ );
210
+ }
211
+ if (this.#revocations.has('mint')) {
212
+ instructions.push(
213
+ createSetAuthorityInstruction(
214
+ mintKeypair.publicKey,
215
+ payer.publicKey,
216
+ AuthorityType.MintTokens,
217
+ null,
218
+ ),
219
+ );
220
+ }
221
+
222
+ const message = new TransactionMessage({
223
+ payerKey: payer.publicKey,
224
+ recentBlockhash: blockhash,
225
+ instructions,
226
+ }).compileToV0Message();
227
+
228
+ const tx = new VersionedTransaction(message);
229
+ tx.sign([payer, mintKeypair]);
230
+
231
+ let signature;
232
+ try {
233
+ signature = await sendAndConfirmRawTransaction(
234
+ connection,
235
+ Buffer.from(tx.serialize()),
236
+ { commitment: 'confirmed' },
237
+ );
238
+ } catch (err) {
239
+ throw new ForgeTxError(
240
+ `Transaction failed: ${err?.message ?? String(err)}`,
241
+ err,
242
+ );
243
+ }
244
+
245
+ // Return raw amounts as strings. BigInt does not serialise to JSON, and
246
+ // Number loses precision above 2^53 (a 1B supply at 9 decimals overflows).
247
+ return {
248
+ signature,
249
+ mintAddress: mintKeypair.publicKey.toBase58(),
250
+ supply: rawSupply.toString(),
251
+ burned: burnAmount.toString(),
252
+ revoked: [...this.#revocations],
253
+ };
254
+ }
255
+ }
256
+
257
+ export function mint(name) {
258
+ return new MintBuilder(name);
259
+ }