Skip to content

Commit

Permalink
feat: 1 transaction create token
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeusofsky committed Aug 15, 2024
1 parent 3cf5fd2 commit 2e59ca3
Show file tree
Hide file tree
Showing 3 changed files with 1,245 additions and 133 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@bundlr-network/client": "^0.7.1",
"@headlessui/react": "^1.5.0",
"@heroicons/react": "^1.0.5",
"@metaplex/js": "^4.12.0",
"@metaplex-foundation/js": "^0.11.1",
"@metaplex-foundation/mpl-token-metadata": "^2.1.2",
"@solana/buffer-layout": "^4.0.0",
"@solana/spl-token": "^0.2.0",
"@solana/wallet-adapter-base": "^0.9.3",
Expand All @@ -23,6 +24,7 @@
"@solana/web3.js": "^1.36.0",
"@tailwindcss/typography": "^0.5.0",
"browserify-zlib": "^0.2.0",
"buffer-layout": "^1.2.2",
"crypto-browserify": "^3.12.0",
"daisyui": "^1.24.3",
"fs-extra": "^10.0.1",
Expand Down
40 changes: 21 additions & 19 deletions src/components/CreateToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { FC, useCallback, useState } from 'react';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, SystemProgram, Transaction } from '@solana/web3.js';
import { MINT_SIZE, TOKEN_PROGRAM_ID, createInitializeMintInstruction, getMinimumBalanceForRentExemptMint, getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createMintToInstruction } from '@solana/spl-token';
import { CreateMetadataV2, Metadata, DataV2 } from '@metaplex-foundation/mpl-token-metadata';
import { DataV2, createCreateMetadataAccountV2Instruction } from '@metaplex-foundation/mpl-token-metadata';
import { findMetadataPda } from '@metaplex-foundation/js';

export const CreateToken: FC = () => {
const { connection } = useConnection();
Expand All @@ -16,19 +17,19 @@ export const CreateToken: FC = () => {
const onClick = useCallback(async (form) => {
const lamports = await getMinimumBalanceForRentExemptMint(connection);
const mintKeypair = Keypair.generate();
const metadataPDA = await Metadata.getPDA(mintKeypair.publicKey);
const metadataPDA = await findMetadataPda(mintKeypair.publicKey);
const tokenATA = await getAssociatedTokenAddress(mintKeypair.publicKey, publicKey);
const tokenMetadata = new DataV2({
const tokenMetadata = {
name: form.tokenName,
symbol: form.symbol,
uri: form.metadata,
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null
});
} as DataV2;

const createMintTransaction = new Transaction().add(
const createNewTokenTransaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: publicKey,
newAccountPubkey: mintKeypair.publicKey,
Expand All @@ -53,22 +54,23 @@ export const CreateToken: FC = () => {
tokenATA,
publicKey,
form.amount * Math.pow(10, form.decimals),
),
createCreateMetadataAccountV2Instruction({
metadata: metadataPDA,
mint: mintKeypair.publicKey,
mintAuthority: publicKey,
payer: publicKey,
updateAuthority: publicKey,
},
{ createMetadataAccountArgsV2:
{
data: tokenMetadata,
isMutable: false
}
}
)
);

const createMetadataTransaction = new CreateMetadataV2(
{ feePayer: publicKey },
{
metadata: metadataPDA,
metadataData: tokenMetadata,
updateAuthority: publicKey,
mint: mintKeypair.publicKey,
mintAuthority: publicKey,
},
)
const mintSignature = await sendTransaction(createMintTransaction, connection, {signers: [mintKeypair]});
await connection.confirmTransaction(mintSignature);
await sendTransaction(createMetadataTransaction, connection);
await sendTransaction(createNewTokenTransaction, connection, {signers: [mintKeypair]});
}, [publicKey, connection, sendTransaction]);

return (
Expand Down
Loading

0 comments on commit 2e59ca3

Please sign in to comment.