-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathCreateToken.tsx
124 lines (118 loc) · 5.33 KB
/
CreateToken.tsx
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
import { FC, useCallback, useState } from 'react';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
import { MINT_SIZE, TOKEN_PROGRAM_ID, createInitializeMintInstruction, getMinimumBalanceForRentExemptMint, getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createMintToInstruction } from '@solana/spl-token';
import { createCreateMetadataAccountV3Instruction, PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata';
export const CreateToken: FC = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const [tokenName, setTokenName] = useState('')
const [symbol, setSymbol] = useState('')
const [metadata, setMetadata] = useState('')
const [amount, setAmount] = useState('')
const [decimals, setDecimals] = useState('')
const onClick = useCallback(async (form) => {
const lamports = await getMinimumBalanceForRentExemptMint(connection);
const mintKeypair = Keypair.generate();
const tokenATA = await getAssociatedTokenAddress(mintKeypair.publicKey, publicKey);
const createMetadataInstruction = createCreateMetadataAccountV3Instruction(
{
metadata: PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
PROGRAM_ID.toBuffer(),
mintKeypair.publicKey.toBuffer(),
],
PROGRAM_ID,
)[0],
mint: mintKeypair.publicKey,
mintAuthority: publicKey,
payer: publicKey,
updateAuthority: publicKey,
},
{
createMetadataAccountArgsV3: {
data: {
name: form.tokenName,
symbol: form.symbol,
uri: form.metadata,
creators: null,
sellerFeeBasisPoints: 0,
uses: null,
collection: null,
},
isMutable: false,
collectionDetails: null,
},
},
);
const createNewTokenTransaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: publicKey,
newAccountPubkey: mintKeypair.publicKey,
space: MINT_SIZE,
lamports: lamports,
programId: TOKEN_PROGRAM_ID,
}),
createInitializeMintInstruction(
mintKeypair.publicKey,
form.decimals,
publicKey,
publicKey,
TOKEN_PROGRAM_ID),
createAssociatedTokenAccountInstruction(
publicKey,
tokenATA,
publicKey,
mintKeypair.publicKey,
),
createMintToInstruction(
mintKeypair.publicKey,
tokenATA,
publicKey,
form.amount * Math.pow(10, form.decimals),
),
createMetadataInstruction
);
await sendTransaction(createNewTokenTransaction, connection, {signers: [mintKeypair]});
}, [publicKey, connection, sendTransaction]);
return (
<div className="my-6">
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Token Name"
onChange={(e) => setTokenName(e.target.value)}
/>
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Symbol"
onChange={(e) => setSymbol(e.target.value)}
/>
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Metadata Url"
onChange={(e) => setMetadata(e.target.value)}
/>
<input
type="number"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Amount"
onChange={(e) => setAmount(e.target.value)}
/>
<input
type="number"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Decimals"
onChange={(e) => setDecimals(e.target.value)}
/>
<button
className="px-8 m-2 btn animate-pulse bg-gradient-to-r from-[#9945FF] to-[#14F195] hover:from-pink-500 hover:to-yellow-500 ..."
onClick={() => onClick({decimals: Number(decimals), amount: Number(amount), metadata: metadata, symbol: symbol, tokenName: tokenName})}>
<span>Create Token</span>
</button>
</div>
)
}