0

I am trying to create an API call for my backend that would allow a user to mint an NFT through my frontend. How would I go about doing this? Specifically, how would I make the user pay seeing as the payer attribute in the function call asks for a Signer type and I'm not sure how to access that from the backend. Is this something that I would have to do on the frontend?

Is there another way I should go about making a user pay for a mint via frontend? How would I charge a user a % fee to mint?

Also, how do I make this NFT programmable such that royalties are always enforced? The Candy Machine in reference is the one created in this post: How to create a Candy Machine using Metaplex JS SDK

import { keypairIdentity, Metaplex, toBigNumber, token } from '@metaplex-foundation/js';
import { clusterApiUrl, Connection, Keypair, PublicKey } from '@solana/web3.js';


const connection = new Connection(clusterApiUrl('devnet'));
const metaplex = new Metaplex(connection);
const authority = createKeypairFromFileSync('devnet_keypair.json');  // Custom method that turns a keypair json into a Keypair object
metaplex.use(keypairIdentity(authority));


routes.post('/api/mintsong', async (req, res) => {
  const { buyer } = req.body.buyerWallet;
  const { candyMachineAddressStr } = req.body.candyMachineAddress;
  const candyMachineAddress = new PublicKey(candyMachineAddressStr); 
  const candyMachine = await metaplex.candyMachines().findByAddress({ address: candyMachineAddress });

  const { nft } = await metaplex.candyMachines().mint(
    {
      candyMachine,
      authority,  // Collection Update Authority. Having this as a Keypair is causing an error but idk why?
      owner: buyer.publicKey,
      guards: {
        thirdPartySigner: {
          signer: authority,
        },
      }
    },
    {
      payer: buyer,  // Do I have access to a user's full wallet through the front end? How else would I do this?
    },
  );
})
1
  • 1
    You're asking a few questions here, but I think you're going to need to change your architecture a bit. You need to send the transaction on the frontend if you want the user to sign it. You'll also need to sign it yourself since it looks like you need an authority signer - so you want your API to return a serialized partially signed tx. We have an example of doing that with NextJS here which should point you in the right direction: github.com/solana-labs/solana-pay/blob/master/examples/… (pages/api/checkout.ts is the backend, pages/index.tsx frontend)
    – Callum M
    Mar 7, 2023 at 15:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.