-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path\
185 lines (161 loc) · 5.68 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Token, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, MintLayout } from '@solana/spl-token';
import { Connection, PublicKey, Transaction, SystemProgram, Keypair, TransactionInstruction, clusterApiUrl } from '@solana/web3.js';
import { WalletContextState } from "@solana/wallet-adapter-react";
import { Dispatch, SetStateAction } from 'react';
import { PROGRAM_ID, DataV2, createCreateMetadataAccountV3Instruction } from '@metaplex-foundation/mpl-token-metadata';
import { bundlrStorage, Metaplex, MetaplexFileTag, walletAdapterIdentity } from '@metaplex-foundation/js';
console.log(PublicKey)
const hehe = "6bdr2FS3ka7UiPhh3A8smRPDXm3QWxwdhfaRZfMrWK2t"
export async function createSPLToken(owner: PublicKey, wallet: WalletContextState, connection: Connection, quantity: number, decimals: number, isChecked: boolean, tokenName: string, symbol: string, metadataURL: string, description: string, file: Readonly<{
buffer: Buffer;
fileName: string;
displayName: string;
uniqueName: string;
contentType: string | null;
extension: string | null;
tags: MetaplexFileTag[];
}> | undefined,
metadataMethod: string,
setIscreating: Dispatch<SetStateAction<boolean>>, setTokenAddresss: Dispatch<SetStateAction<string>>, setSignature: Dispatch<SetStateAction<string>>, setError: Dispatch<SetStateAction<string>>) {
try {
setIscreating(true)
setTokenAddresss('')
const connection = new Connection("https://mainnet.helius-rpc.com/?api-key=86b83f7a-3136-4296-ae05-38ed877f6650")
const metaplex = Metaplex.make(connection)
metaplex.use(walletAdapterIdentity(wallet)).use(bundlrStorage()); console.log(metaplex)
console.log(walletAdapterIdentity(wallet))
const mint_rent = await Token.getMinBalanceRentForExemptMint(connection);
const mint_account = Keypair.generate();
let InitMint: TransactionInstruction
const [metadataPDA] = PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
PROGRAM_ID.toBuffer(),
mint_account.publicKey.toBuffer(),
], PROGRAM_ID
);
console.log(metadataPDA)
let URI: string = ''
if (metadataMethod == 'url') {
if (metadataURL != '') {
URI = metadataURL
}
else {
setIscreating(false)
setError('Please provide a metadata URL!')
}
}
else {
if (file) {
const ImageUri = await metaplex.storage().upload(file);
if (ImageUri) {
const { uri } = await metaplex.nfts().uploadMetadata({
name: tokenName,
symbol: symbol,
description: description,
image: ImageUri,
})
if (uri) {
URI = uri
}
}
}
else {
setIscreating(false)
setError('Please provide an image file!')
}
}
if (URI != '') {
const tokenMetadata: DataV2 = {
name: tokenName,
symbol: symbol,
uri: URI,
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null
};
const args = {
data: tokenMetadata,
isMutable: true,
collectionDetails: null
};
const createMintAccountInstruction = SystemProgram.createAccount({
fromPubkey: owner,
newAccountPubkey: mint_account.publicKey,
space: MintLayout.span,
lamports: mint_rent,
programId: TOKEN_PROGRAM_ID,
});
if (isChecked) {
InitMint = Token.createInitMintInstruction(
TOKEN_PROGRAM_ID,
mint_account.publicKey,
decimals,
owner,
owner
);
} else {
InitMint = Token.createInitMintInstruction(
TOKEN_PROGRAM_ID,
mint_account.publicKey,
decimals,
owner,
null
);
};
const associatedTokenAccount = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mint_account.publicKey,
owner
);
const createATAInstruction = Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mint_account.publicKey,
associatedTokenAccount,
owner,
owner
);
const mintInstruction = Token.createMintToInstruction(
TOKEN_PROGRAM_ID,
mint_account.publicKey,
associatedTokenAccount,
owner,
[],
quantity * 10 ** decimals
);
const MetadataInstruction = createCreateMetadataAccountV3Instruction(
{
metadata: metadataPDA,
mint: mint_account.publicKey,
mintAuthority: owner,
payer: owner,
updateAuthority: owner,
},
{
createMetadataAccountArgsV3: args,
}
);
const createAccountTransaction = new Transaction().add(createMintAccountInstruction, InitMint, createATAInstruction, mintInstruction, MetadataInstruction);
const createAccountSignature = await wallet.sendTransaction(createAccountTransaction, connection, { signers: [mint_account] });
const latestBlockHash = await connection.getLatestBlockhash();
const createAccountconfirmed = await connection.confirmTransaction({
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, signature: createAccountSignature
});
const signature = createAccountSignature.toString()
console.log(signature)
if (createAccountconfirmed) {
setIscreating(false);
setTokenAddresss(mint_account.publicKey.toBase58());
setSignature(signature)
}
}
} catch (error) {
setIscreating(false);
const err = (error as any)?.message;
setError(err)
}
}