How to create and send an NFT to a user's wallet after payment using Metaplex?

I’m working on a project where users can buy specific NFTs. I’ve got it working client-side, but I want to make it more secure by handling the minting process on the server. Here’s what I’ve tried so far:

  1. Created a MintButton component that sends a request to the server when clicked.
  2. Set up a server route to handle the minting request.
  3. Implemented a createMintingTransaction function to set up the NFT creation.

I’m using Metaplex and Solana, but I’m having trouble with the transaction signing process. The server creates the transaction, but when I try to sign it on the client side, it seems to be missing some signatures.

Can anyone help me figure out how to properly create, sign, and send the transaction so that the user pays and receives the NFT? I’m especially unsure about how to handle the multiple signatures needed (server, collection, and user).

Here’s a simplified version of my code:

// Client-side
const handleMint = async () => {
  const response = await axios.post('/api/mint', { walletAddress: wallet.publicKey.toString() });
  await signAndSendTransaction(response.data.transaction);
};

// Server-side
const createMintingTransaction = async (userPublicKey, mintPrice) => {
  // Set up connection and identities
  // Create NFT metadata
  // Build transaction
  // Add payment instruction
  // Partial sign with server and collection keys
  return serializedTransaction;
};

// Back on client
const signAndSendTransaction = async (serializedTransaction) => {
  // Deserialize transaction
  // Sign with user's wallet
  // Send to network
};

Any ideas on what I’m missing or doing wrong? Thanks!

hey SingingSnow, sounds like a cool project! have you tried using @solana/web3.js for transaction building? it helped me a ton with multi-sig stuff. also, make sure you’re using the right version of @solana/wallet-adapter-react. oh, and don’t forget to handle network fees! goodluck with ur project, keep us posted :slight_smile:

hey there SingingSnow! your project sounds super interesting :slight_smile: i’ve been tinkering with nfts and metaplex too, and it can definitely be tricky to get everything working right. have you considered using the candymachine v3 for minting? it might simplify things a bit for you. also, i’m curious - what kind of nfts are you creating? are they art, music, or something else cool?

one thing that helped me was breaking down the process into smaller steps and testing each part separately. maybe try minting a test nft without the payment first, just to make sure that part is working? then you could add the payment instruction afterwards.

oh, and don’t forget to check your transaction size! i got caught out by that once - if it’s too big, it might fail. have you run into any issues with that?

anyways, good luck with your project! let us know how it goes :slight_smile:

I encountered a similar issue in my project. My approach was to construct the transaction on the server by adding all necessary instructions, including minting, metadata, and payment processes, and then partially sign the transaction with both the server’s keypair and the needed collection authority. Once the partially signed transaction reached the client, it was deserialized, signed with the user’s wallet, and then submitted to the network. It is important to use partialSign on the server to avoid finalizing the transaction prematurely, and in some cases, using VersionedTransaction may be necessary for proper serialization.