Creating an NFT with Foundry & Solmate

This article is a summary of all commands of the tutorial “Creating an NFT with Solmate” offered by Foundry

Create project and install dependencies

Initialize the project

forge init hello_foundry

Install dependencies

forge install transmissions11/solmate Openzeppelin/openzeppelin-contracts

Print tree tree -L 2 This commands is used to check the installation

.
├── foundry.toml
├── lib
│   ├── forge-std
│   ├── openzeppelin-contracts
│   └── solmate
├── script
│   └── Counter.s.sol
├── src
│   └── Counter.sol
└── test
    └── Counter.t.sol

Configure your environment

Run local node anvil Set environnement variable

  • Url of the local node export RPC_URL=http://127.0.0.1:8545
  • Private key of the ethereum account export PRIVATE_KEY=<PRIVATE_KEY_TEST> This private key will be use to deploy the contract

Implement a basic NFT

Deployment

Deploy your NFT contract

  • Template
forge create NFT --rpc-url=$RPC_URL --private-key=$PRIVATE_KEY --constructor-args <name> <symbol>
  • Full command
forge create NFT --rpc-url=$RPC_URL --private-key=$PRIVATE_KEY --constructor-args Bitcoin BTC
  • Result

alt text

alt text

Minting

export CONTRACT_ADDRESS=0x5FbDB2315678afecb367f032d93F642f64180aa3
export RECIPIENT_ADDRESS=0x70997970c51812dc3a010c7d01b50e0d17dc79c8
  • Minting from your contract
cast send --rpc-url=$RPC_URL $CONTRACT_ADDRESS  "mintTo(address)" $RECIPIENT_ADDRESS --private-key=$PRIVATE_KEY

alt text

alt text

  • Check the owner of the created NFT
cast call --rpc-url=$RPC_URL --private-key=$PRIVATE_KEY $CONTRACT_ADDRESS "ownerOf(uint256)" 1
  • Result

alt text

alt text

Test

Warning : this part is performed on an extended version of the basic NFT contract

  • Run tests
forge test

​ Warning : you may have some warnings when you are running the test ​ alt text

  • Run tests with gas report
forge test --gas-report

alt text

alt text

Reference

Foundry Book - Creating an NFT with Solmate

You might also enjoy