ERC-721 tokens have revolutionized blockchain by enabling the creation of unique, non-fungible assets such as digital collectibles and artwork. In this blog, we will guide you through the process of coding your own ERC-721 token named "Scaleap" with the symbol "SL" using Foundry, a robust Ethereum smart contract development framework. We will also write Solidity tests to ensure your Scaleap token functions correctly, complete with tokenURIs. Let's embark on this exciting journey of NFT creation!
1. Understanding ERC-721 Tokens
ERC-721 tokens are a standardized way of creating non-fungible tokens (NFTs) on the Ethereum blockchain. Each token is unique and indivisible, making them ideal for representing one-of-a-kind assets, digital collectibles, and more.
2. Setting Up the Development Environment with Foundry
Before we begin, ensure you have Node.js and npm installed. To use Foundry, follow these steps:
Install Foundry globally:
npm install -g @openzeppelin/foundry
3. Writing the Scaleap (SL) ERC-721 Token
Create a new directory for your project and navigate to it. Inside the project folder, run the following command to initialize a Foundry project:
foundry init
Now, create a new Solidity file, e.g., ScaleapToken.sol, and define the Scaleap ERC-721 token contract:
// ScaleapToken.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract ScaleapToken is ERC721, Ownable {
constructor() ERC721("Scaleap", "SL") {}
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI) public onlyOwner {
_mint(to, tokenId);
_setTokenURI(tokenId, tokenURI);
}
}This code imports the OpenZeppelin ERC721 library, creates the ScaleapToken contract, and allows the owner to mint tokens with corresponding tokenURIs.
4. Compiling and Deploying the Token
Compile and deploy the contract to your local Ganache blockchain using Foundry:
foundry deploy
5. Writing Solidity Tests for the Scaleap Token
Write Solidity-based tests within Foundry to ensure your NFT contract works correctly, including the tokenURI. Create a test file, e.g., ScaleapTokenTest.sol, with the following content:
// ScaleapTokenTest.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/ScaleapToken.sol";
contract ScaleapTokenTest {
ScaleapToken public scaleap;
constructor() {
scaleap = new ScaleapToken();
}
function testDeployment() public {
Assert.equal(scaleap.name(), "Scaleap", "Token name should be Scaleap");
Assert.equal(scaleap.symbol(), "SL", "Token symbol should be SL");
}
function testMintWithTokenURI() public {
address owner = address(this);
string memory tokenURI = "https://example.com/token1.json";
scaleap.mintWithTokenURI(owner, 1, tokenURI);
string memory retrievedTokenURI = IERC721Metadata(address(scaleap)).tokenURI(1);
Assert.equal(retrievedTokenURI, tokenURI, "TokenURI should match");
}
}Run your Solidity tests:
foundry test
Conclusion
You've successfully created and tested your Scaleap (SL) ERC-721 token using Foundry, including the tokenURI. ERC-721 tokens open up endless possibilities, from representing digital art to unique in-game assets.
Explore more about ERC-721 tokens and leverage Foundry's capabilities for Ethereum smart contract development. Now, you're ready to embark on your NFT journey and bring your unique assets to the blockchain. Happy coding and enjoy using your Scaleap NFTs!


