Skip to main content
edited tags
Link
Source Link

How can I enforce royalty?

I'm creating NFT using metaplex library in Javascript. I need to enforce royalty on my NFT but can't find any resource that explains how to achieve this. Can someone help find a solution?

This a the function I wrote that creates an NFT:

async function createNFT(connection, privateKey, nftSymbol, nftName, description, imagePath, imageType, imageName, attributes, collection, creators) {
    const secret = bs58.decode(privateKey)
    const fromWallet = web3.Keypair.fromSecretKey(new Uint8Array(secret));
    const MPX = Metaplex.Metaplex.make(connection).use(Metaplex.keypairIdentity(fromWallet)).use(
        Metaplex.bundlrStorage({
            address: 'https://devnet.bundlr.network',
            providerUrl: SOLANA_CLUSTER_URL,
            timeout: 60000,
        })
    );
    const imageBuffer = fs.readFileSync(imagePath);
    const imageMetaplexFile = Metaplex.toMetaplexFile(imageBuffer, imageName);
    const imageUri = await MPX.storage().upload(imageMetaplexFile);
    console.log("image uri:", imageUri);
    const metadataUri = await MPX.nfts().uploadMetadata({
        name: nftName, description: description,
        image: imageUri,
        attributes: attributes,
        properties: {
            // creators: creators,
            files: [
                {
                    type: imageType,
                    uri: imageUri,
                },
            ]
        },
        collection: collection
    });
    console.log(metadataUri);
    if (creators == null || creators.length == 0) {
        creators = [
            {address: fromWallet.publicKey, share: 100}
        ]
    } else {
        creators = creators.map((c) => ({ address: new web3.PublicKey(c.address), share: c.share }))
    }
    const generatedNft = await MPX.nfts().create({
        uri: metadataUri.uri,
        name: nftName,
        symbol: nftSymbol,
        sellerFeeBasisPoints: 500,
        creators: creators,
        isMutable: true,
        isCollection: true,
    });
    console.log(generatedNft);
    return generatedNft;
}