Storing NFT Metadata on BNB Greenfield

How to store NFT Metadata on Greenfield

Uploading NFT metadata to Greenfield using DCellar

Create a bucket with DCellar to store all the metadata for your NFT.In the example below, we create a bucket named zkbridgeexample.

The URL format to access the bucket is https://${SP-URL}/view/${bucket-name}/${ObjectName}.

You can access different files under the bucket using the URL https://gnfd-testnet-sp-1.nodereal.io/view/zkbridgeexample/${ObjectName}.

Upload the metadata of the first NFT, and we will name it according to the NFT’s TokenId. Its URL is https://gnfd-testnet-sp-1.nodereal.io/view/zkbridgeexample/1.

The above demonstrates the process of uploading metadata information for NFT tokenId 1. Other metadata information can be uploaded in sequence.

Creating NFT Contract on BNB Chain

Using ERC721 contract from OpenZeppelin, the contract is controlled by Ownable and Pausable permissions, and tokenId is allocated using Counters’ self-incrementing method.

Now let’s start deploying the NFT contract. Taking the uploaded zkbridgeexample bucket as an example, the metadataUri parameter would be https://gnfd-testnet-sp-1.nodereal.io/view/zkbridgeexample/

The Solidity code is as follows:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract GreenfieldzkBridge is ERC721, Ownable, Pausable {
    using Counters for Counters.Counter;
    Counters.Counter private _nextTokenId;
    string private metadataUri;

    constructor(
        string memory metadataUri_
    ) ERC721("zkBridge on Greenfield", "zkBridge on Greenfield") {
        _nextTokenId.increment();
        metadataUri = metadataUri_;
    }

    function mint(address account) external whenNotPaused returns (uint256 tokenId) {
        tokenId = _nextTokenId.current();
        _safeMint(account, tokenId);
        _nextTokenId.increment();
    }

    function tokenURI(
        uint256 tokenId
    ) public view virtual override returns (string memory) {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );
        return string(abi.encodePacked(metadataUri, Strings.toString(tokenId)));
    }

    function setMetadataUri(string memory _newMetadataUri) external onlyOwner {
        metadataUri = _newMetadataUri;
    }

    function pause() external onlyOwner {
        _pause();
    }

    function unpause() external onlyOwner {
        _unpause();
    }
}

Compile the smart contract using the Remix IDE and deploy it on the BNB chain.

Configure Batch Upload of NFT Metadata to Greenfield Using Command Line

Install Greenfield CLI

Download link: https://github.com/bnb-chain/greenfield-cmd/releases. Current version is 0.04.

Unzip greenfield-cmd-0.0.4.tar.gz.

> cd greenfield-cmd-0.0.4
# compile greenfield-cmd
> make build
# the binary file gnfd-cmd will be in the build repositroy after compilation
❯ ls ./build
gnfd-cmd
> gnfd-cmd -h
# add to environment variables
> mv ./build/gnfd-cmd /usr/local/bin/

Configuration File

Before interacting with Greenfield, you must first configure the CLI using a configuration file. This will ensure that your settings are properly set up to communicate with the Greenfield network.

Example of how to configure the Greenfield CLI:

# the primary storage provider endpoint
endpoint = "https://gnfd-testnet-sp-1.nodereal.io"
# the grpc address of greenfield
grpcAddr = "gnfd-testnet-fullnode-cosmos-us.bnbchain.org:9090"
# the chain id info of greenfield
chainId = "greenfield_5600-1"
privateKey = "{YOUR-PRIVATE-KEY}"

Creating a payment account and transferring tBNB (test token) to the payment account

❯ gnfd-cmd -c config.toml create-payment-account

create-payment-account for 0xF319De60014ED1628D2983f3E267BBab88a2CfF6 succ, txHash: 43C9B44126F58B07F3E8BA92ADD396B924D912F96318221FC2193BA91E5146DE

View the transaction through the blockchain explorer GreenfieldScan at https://greenfieldscan.com/tx/0x43C9B44126F58B07F3E8BA92ADD396B924D912F96318221FC2193BA91E5146DE

The payment address is: 0x9A578Ee2c97267474CB5E9434704c0a58fD5d229.

Then transfer tBNB to your wallet.

gnfd-cmd -c config.toml transfer --toAddress 0x9A578Ee2c97267474CB5E9434704c0a58fD5d229 --amount 12345
transfer 12345 BNB to 0xB69d057bf23B0fb538b3705C5374840aFFEa375A succ, txHash: 7729E186D26FD8A162275F7217B13FC1F2DAC9B1B693C57E3AD728EC84A7B616

Check the balance, and you can see that the BNB transferred just now has been received.

// query payments account under owner or a address with optional flag --user
❯ gnfd-cmd -c config.toml balance --address 0x9A578Ee2c97267474CB5E9434704c0a58fD5d229
balance: 12345BNB

Deposit tBNB into your payment account.

// deposit from owner's account to the payment account
gnfd-cmd -c config.toml payment-deposit --toAddress 0x9A578Ee2c97267474CB5E9434704c0a58fD5d229 --amount 5000000

Check the balance of 0x9A578Ee2c97267474CB5E9434704c0a58fD5d229.

Create a bucket and upload tokenURI metadata file

Now we can create a bucket.

// create bucket
❯ gnfd-cmd -c config.toml make-bucket gnfd://zkbridge-bucket
create bucket zkbridge-bucket succ, txn hash: A81A98746DC089317D7DBC0D83900CB2AD9BD1525A1B1113D461AAF656AB3C01

It can be seen that zkbridge-bucket has been successfully created.

Put your file into the storage bucket.

❯ gnfd-cmd -c config.toml  put --visibility public-read ./polyhedra.gif  gnfd://zkbridge-bucket/polyhedra.gif
create object polyhedra.gif on chain finish
put object: polyhedra.gif successfully

Image uploaded successfully, https://gnfd-testnet-sp-1.nodereal.io/view/zkbridge-bucket/polyhedra.gif

Here is the JSON file for tokenId 2, which is uploaded below. The process is the same as uploading an image in the example above. For a more detailed tutorial, please check out the greenfield-cmd Github page.

Reference Links:

Greenfield Official Website Quick Start Tutorial: https://greenfield.bnbchain.org/docs/guide/get-started.html

Nodereal: Embrace the future of NFTs with BNB Greenfield: https://nodereal.io/tutorials/embrace-the-future-of-nfts-with-bnb-greenfield/

greenfield-cmd Github Address: https://github.com/bnb-chain/greenfield-cmd

Subscribe to Polyhedra Network
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.