2

How can I upload nft image and metadata to arweave by using @metaplex-foundation/js package and get data back before inserting them to candy machine? Also, it would be helpful to see a working code to insert nft's into candy machine using the js sdk of metaplex.

1

2 Answers 2

1

I tried uploading to metaplex using their library at some point and was unsuccessful on devnet.

But per the Solana Cookbook you can upload directly to arweave and use the generated JSON URL when minting your NFT:

import fs from "fs";
import Arweave from "arweave";

export const uploadImageAndMetadataToArweave = async () => {

  const arweave = Arweave.init({
        host: "arweave.net",
        port: 443,
        protocol: "https",
        timeout: 20000,
        logging: false,
    });
    // Upload image to Arweave
    const data = fs.readFileSync("img/nft_img.png");
    const transaction = await arweave.createTransaction({
        data: data,
    });
    transaction.addTag("Content-Type", "image/png");

    const wallet = 
  JSON.parse(fs.readFileSync("tkeys/arweave_wallet.json", "utf-8"))

    await arweave.transactions.sign(transaction, wallet)
    const response = await arweave.transactions.post(transaction);
    console.log(response);


    const id = transaction.id;
    const imageUrl = id ? `https://arweave.net/${id}` : undefined;
    console.log("imageUrl", imageUrl);

    // Upload metadata to Arweave
    const owner = getKeypair("owner");//a prior created Keypair

    const metadata = {
        name: "NFT name",
        symbol: "NFT symbol",
        description: "nft Description",
        seller_fee_basis_points: 500,
        external_url: "https://www.customnft.com/",
        attributes: [
            {
                trait_type: "NFT type",
                value: "Custom",
            },
        ],
        properties: {
            files: [
                {
                    uri: imageUrl,
                    type: "image/png",
                },
            ],
            category: "image",
            maxSupply: 0,
            creators: [
                {
                    address: owner.publicKey,
                    share: 100,
                },
            ],
        },
        image: imageUrl,
    };

    const metadataRequest = JSON.stringify(metadata);

    const metadataTransaction = await arweave.createTransaction({
        data: metadataRequest,
    });

    metadataTransaction.addTag("Content-Type", "application/json");

    await arweave.transactions.sign(metadataTransaction, wallet);

    console.log("metadata txid", metadataTransaction.id);
    const metadataUrl = id ? `https://arweave.net/${metadataTransaction.id}` : undefined;
    console.log("metadataUrl", metadataUrl);
    const result = await arweave.transactions.post(metadataTransaction);
    console.log(result);
}

Get arweave wallet and token here

3
  • This is a workaround, rather than an answer. it would be better as a comment.
    – mikemaccana
    Nov 18, 2022 at 12:18
  • metaplex uses arweave under the hood @mikemaccana thanks for the edit tho
    – iMEF
    Nov 21, 2022 at 15:43
  • 1
    I know, but Metaplex has inbuilt tools to use ARWeave as you mentioned in this answer (you also mentioned they didn't work when you tried them). This answer has instructions on using Metaplex's inbuilt tools: solana.stackexchange.com/a/4454/294
    – mikemaccana
    Nov 21, 2022 at 17:00
1

Here's a guide for uploading the img and metadata (and minting NFT): https://www.quicknode.com/guides/solana-development/how-to-mint-an-nft-on-solana-using-typescript

Here are steps for inserting items into the Candy Machine: https://docs.metaplex.com/programs/candy-machine/inserting-items.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.