metaplex-foundation / js

A JavaScript SDK for interacting with Metaplex's programs
357 stars 182 forks source link

I don't know if I missed this in the documentation but is it possible to combine different methods so that users only have to sign one transaction #230

Closed zachzafar closed 2 years ago

zachzafar commented 2 years ago

For example say a user wants to create a new nft, I have to upload the metadata which is one transaction, then create the actual mint which is another transaction. how would I make this one transaction?

lorisleiva commented 2 years ago

Hi there 👋

Sorry for the late reply but yes it is possible. I'm hoping to have enough time to document all of this as soon as I can but here's a snippet of code that explains how to use what we call transaction builders on the SDK.

// Get the transaction builder for the original NFT.
// Note we need to explicitly provide the mint Keypair
// to link the two transaction builders together.
const originalNftKeypair = Keypair.generate();
const originalNftBuilder = await metaplex
  .nfts()
  .builders()
  .create({
    uri: "https://arweave.net/123",
    name: "My NFT",
    sellerFeeBasisPoints: 500,  // 5% royalties.
    maxSupply: toBigNumber(10), // 10 limited editions.
    useNewMint: originalNftKeypair,
  });

// Get the transaction builder for the printed NFT.
const printedNftBuilder = await metaplex
  .nfts()
  .builders()
  .printNewEdition({
    originalMint: originalNftKeypair.publicKey
  });

// Merge the two transaction builders together.
const transactionBuilder = originalNftBuilder.add(printedNftBuilder);

// Execute the merged transaction builder.
await metaplex.rpc().sendAndConfirmTransaction(transactionBuilder);

I hope this helps.

zachzafar commented 2 years ago

Thank you @lorisleiva