HelloERC721
is an ERC721
token implementation showcasing cross-chain NFT functionality without the need for traditional bridges, making the NFT cross-chain native. Utilizing the VIA Labs package it is trivial to add native cross-chain functionality to any NFT contract. Below is a bare-bones ERC721
contract to showcase the ease of use.
- ERC721 NFT Implementation: A standard
ERC721
NFT - Cross-Chain Functionality: Native support for cross-chain interactions without using a bridge.
- VIA Labs Integration: Leverages the VIA Labs package for seamless cross-chain communication.
- Configurable on Multiple Networks: Can be deployed and configured across various blockchain networks.
Before you begin, ensure you have the following installed:
- Node.js and npm (Node Package Manager)
- A text editor such as VSCode for editing
.sol
and.ts
files - GIT installed
- Testnet Tokens (ethereum sepolia faucet and polygon testnet faucet)
Please visit node documentation link and the git install documentation for more information.
Please open a terminal to run the following commands. You can use any terminal of your choice, including the built in terminal in vscode (Terminal -> New Terminal)
- Clone the Repository:
git clone https://github.com/VIALabs-io/hello-erc721.git
- After cloning the repository, if using vscode or a similar IDE, you can now open the hello-erc721 in your IDE of choice.
code hello-erc721
- Install Dependencies: In vscode (Terminal -> New Terminal)
npm install
- Set Up Environment Variables:
Create a new
.env
file to set your EVM private key for contract deployment or copy and edit the existing.env.example
to.env
PRIVATE_KEY=0000000000000000000000000000
Deploy the HelloERC721
contract to your desired networks. This must be done for each network you wish to operate on. You can see a list of our networks in the Package documentation
- Ethereum Sepolia Deployment:
npx hardhat --network ethereum-sepolia deploy
- Polygon Testnet Deployment:
npx hardhat --network polygon-amoy deploy
Edit the networks.ts
file and include all of the networks the contract is deployed on.
[
"ethereum-sepolia",
"polygon-amoy"
]
Once all contracts are deployed across the desired networks and listed in networks.ts
, configure them using the provided script. Remember, if a new network is added later, all contracts must be reconfigured.
- Ethereum Sepolia Configuration:
npx hardhat --network ethereum-sepolia configure
- Polygon Testnet Configuration:
npx hardhat --network polygon-amoy configure
To mint an NFT on a chain:
npx hardhat --network polygon-amoy mint-nft
You will get the next available NFT. NFTs start at [chain-id]0000 so the first NFT minted on Polygon Amoy Testnet will be 800020000 and the next 800020001 etc.. You can look up the chain ids in the Package documentation. You can also look up the transaction on the testnet explorer to see the NFT details using your wallet address.
To view the details of an NFT including its Metadata and Owner:
npx hardhat --network polygon-amoy get-nft ---nftid 800020000
To send NFTs to another chain it is required to set the --dest
parameter to the destination chain id. The example below uses the id for the Ethereum Sepolia testnet. Chain IDs can be looked up in the Package documentation.
npx hardhat --network polygon-amoy bridge-nft --dest 11155111 --nftid 800020000
The HelloERC721
contract is an example of an ERC721 token designed for cross-chain operations, leveraging the VIA Labs framework for seamless blockchain interactions.
- ERC721 Token: Inherits from OpenZeppelin's ERC721 standard.
- Cross-Chain Functionality: Enabled via the MessageClient from the
@vialabs-io/contracts
package. - Unique NFT ID Generation: Utilizes the blockchain's chain ID to generate unique NFT IDs.
constructor() ERC721("Hello ERC721!", "H721") {
nextNftId = block.chainid * 10**4;
}
Initializes the NFT with a unique name and symbol, and sets up the initial NFT ID based on the blockchain's chain ID.
function mint() external {
_mint(msg.sender, nextNftId);
nextNftId++;
}
Allows users to mint a new NFT. The NFT ID is automatically incremented after each minting.
function bridge(uint _destChainId, address _recipient, uint _nftId) external {
// Implementation details...
}
Enables NFTs to be bridged to a different chain. The NFT is burned on the current chain and a message is sent for minting on the destination chain.
function messageProcess(uint, uint _sourceChainId, address _sender, address, uint, bytes calldata _data) external override {
// Implementation details...
}
Handles incoming messages for cross-chain NFT minting.
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
// Implementation details...
}
Generates a token URI for each NFT, providing its metadata in a base64-encoded JSON format.