Run the DApp on your local system.
when you run
npm run dev
you will lend here
First, connect your desired Solana wallet (Phantom is recommended).
After connecting your wallet, you will see a screen like this.
Now, when we click the Mint NFT button, we are using two consecutive transactions.
The tx’s setup is here
Go again into dapp’s root → src → components → MintNFTButton and look at this file
Process of first transaction
const opts: { preflightCommitment: Commitment } = {
preflightCommitment: "processed",
};
const [txSig, setTxSig] = useState("");
const [loading, setLoading] = useState(false);
const [nftDetails, setNftDetails] = useState<Sft | Nft | null>(null);
const [allNftByWallet, setAllNftByWallet] = useState<any>();
const { connection } = useConnection();
const wallet = useWallet();
const { publicKey, sendTransaction } = useWallet();
const getProgram = () => {
/* create the provider and return it to the caller */
const provider = new AnchorProvider(connection, wallet as any, opts);
/* create the program interface combining the idl, program ID, and provider */
const program = new Program(idl as Idl, programId, provider);
return program;
};
const MetaData = {
name: "oggggg",
symbol: "Oggy",
uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
};
const [dataAccountPDA, bump] = PublicKey.findProgramAddressSync([Buffer.from("mint_authority")], program.programId);
const mintNft = async (e) => {
setLoading(true);
try {
e.preventDefault();
...
}
catch (err) {
console.log(err);
notify({ message: err.message });
}
setLoading(false);
};
// create a metaplex connection and fetch metadata address for our token mint account
const metaplex = Metaplex.make(connection);
const metadataAddress = await metaplex.nfts().pdas().metadata({ mint: mintKeypair.publicKey });
// build rpc call by providing required arguments and accounts that we used in this tx
const createTx = await program.methods
.createTokenMint(
dataAccountPDA,
0, // 0 decimals for NFT
MetaData.name, // NFT name
MetaData.symbol, // NFT symbol
MetaData.uri // NFT URI
)
.accounts({
payer: wallet.publicKey,
mint: mintKeypair.publicKey,
metadata: metadataAddress,
mintAuthority: dataAccountPDA,
rentAddress: SYSVAR_RENT_PUBKEY,
metaplexId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
})
.signers([mintKeypair])
.rpc({ skipPreflight: true });
const tx = await program.methods
.mintTo()
.accounts({
pdaAccount: dataAccountPDA,
payer: wallet.publicKey,
tokenAccount: tokenAccount,
owner: wallet.publicKey,
mint: mintKeypair.publicKey,
})
.rpc({ skipPreflight: true });
after this transaction minter have nft in their wallet and our dapp’s screen looks like this
congratulation you minted your first nft.
Go into
root→ src→ components→ nft→ nftcard
and look at the file
Process of second transaction→
Next look at transferNFT fn
Purpose→ fetch seller(minter) token account , create buyer token account and create a rpc call on program api for transfer nft to buyer.
Process→
const transaction = new Transaction();
const sellerTokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
publicKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
const [buyer, setBuyer] = useState("");
const buyerPublicKey = new anchor.web3.PublicKey(buyer);
const buyerTokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
buyerPublicKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
try {
await getAccount(connection, buyerTokenAccount);
} catch (e) {
transaction.add(
createAssociatedTokenAccountInstruction(
publicKey,
buyerTokenAccount,
buyerPublicKey,
mint.publicKey,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
)
);
// transaction.add(instruction);
transaction.feePayer = wallet.publicKey;
const tx1 = await sendTransaction(transaction, connection);
}
const tx = await program.methods
.transferNft()
.accounts({
owner: wallet.publicKey,
from: sellerTokenAccount,
to: buyerTokenAccount,
})
.rpc({ skipPreflight: true });