-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathcreate-mint-interface.ts
More file actions
148 lines (137 loc) · 4.62 KB
/
create-mint-interface.ts
File metadata and controls
148 lines (137 loc) · 4.62 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
import {
ComputeBudgetProgram,
ConfirmOptions,
Keypair,
PublicKey,
Signer,
TransactionSignature,
} from '@solana/web3.js';
import {
Rpc,
buildAndSignTx,
dedupeSigner,
sendAndConfirmTx,
TreeInfo,
AddressTreeInfo,
selectStateTreeInfo,
getBatchAddressTreeInfo,
DerivationMode,
CTOKEN_PROGRAM_ID,
getDefaultAddressTreeInfo,
assertBetaEnabled,
} from '@lightprotocol/stateless.js';
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import {
createMintInstruction,
TokenMetadataInstructionData,
} from '../instructions/create-mint';
import { deriveCMintAddress, findMintAddress } from '../derivation';
import { createMint } from '../../actions/create-mint';
export { TokenMetadataInstructionData };
/**
* Create and initialize a new mint for SPL/T22/light-token.
*
* @param rpc RPC connection to use
* @param payer Fee payer
* @param mintAuthority Account that will control minting (signer for light-token mints)
* @param freezeAuthority Account that will control freeze and thaw (optional)
* @param decimals Location of the decimal place
* @param keypair Mint keypair (defaults to a random keypair)
* @param confirmOptions Confirm options
* @param programId Token program ID (defaults to CTOKEN_PROGRAM_ID)
* @param tokenMetadata Optional token metadata (light-token mints only)
* @param outputStateTreeInfo Optional output state tree info (light-token mints only)
* @param addressTreeInfo Optional address tree info (light-token mints only)
*
* @returns Object with mint address and transaction signature
*/
export async function createMintInterface(
rpc: Rpc,
payer: Signer,
mintAuthority: PublicKey | Signer,
freezeAuthority: PublicKey | Signer | null,
decimals: number,
keypair: Keypair = Keypair.generate(),
confirmOptions?: ConfirmOptions,
programId: PublicKey = CTOKEN_PROGRAM_ID,
tokenMetadata?: TokenMetadataInstructionData,
outputStateTreeInfo?: TreeInfo,
addressTreeInfo?: AddressTreeInfo,
): Promise<{ mint: PublicKey; transactionSignature: TransactionSignature }> {
assertBetaEnabled();
// Dispatch to SPL/Token-2022 mint creation
if (
programId.equals(TOKEN_PROGRAM_ID) ||
programId.equals(TOKEN_2022_PROGRAM_ID)
) {
return createMint(
rpc,
payer,
mintAuthority,
decimals,
keypair,
confirmOptions,
programId,
freezeAuthority,
);
}
// Default: compressed token mint creation
if (!('secretKey' in mintAuthority)) {
throw new Error(
'mintAuthority must be a Signer for compressed token mints',
);
}
if (
addressTreeInfo &&
!addressTreeInfo.tree.equals(getDefaultAddressTreeInfo().tree)
) {
throw new Error(
`addressTreeInfo ${addressTreeInfo?.tree.toString()} must be the default address tree info: ${getDefaultAddressTreeInfo().tree.toString()}`,
);
}
const resolvedFreezeAuthority =
freezeAuthority && 'secretKey' in freezeAuthority
? freezeAuthority.publicKey
: (freezeAuthority as PublicKey | null);
addressTreeInfo = addressTreeInfo ?? getBatchAddressTreeInfo();
outputStateTreeInfo =
outputStateTreeInfo ??
selectStateTreeInfo(await rpc.getStateTreeInfos());
const compressedMintAddress = deriveCMintAddress(
keypair.publicKey,
addressTreeInfo,
);
const validityProof = await rpc.getValidityProofV2(
[],
[
{
address: Uint8Array.from(compressedMintAddress),
treeInfo: addressTreeInfo,
},
],
DerivationMode.standard,
);
const ix = createMintInstruction(
keypair.publicKey,
decimals,
mintAuthority.publicKey,
resolvedFreezeAuthority,
payer.publicKey,
validityProof,
addressTreeInfo,
outputStateTreeInfo,
tokenMetadata,
);
const additionalSigners = dedupeSigner(payer, [keypair, mintAuthority]);
const { blockhash } = await rpc.getLatestBlockhash();
const tx = buildAndSignTx(
[ComputeBudgetProgram.setComputeUnitLimit({ units: 500_000 }), ix],
payer,
blockhash,
additionalSigners,
);
const txId = await sendAndConfirmTx(rpc, tx);
console.log('txId', txId);
const mint = findMintAddress(keypair.publicKey);
return { mint: mint[0], transactionSignature: txId };
}