Pharos Docs
  • Introduction
    • About Pharos Network
    • Vision & Mision
    • Why Pharos Network
    • Concepts
      • Degree of Parallelism (DP)
  • Architecture
    • Pharos Modular Stack
    • Node Architecture
      • About Pharos Nodes
  • Core Technologies
    • Pharos Consensus
    • Pharos Execution
      • Why A New Blockchain Compute Model
      • Pharos VM
    • Pharos Pipelining
    • Pharos Store
      • Why We Need a Blockchain-Native Store
    • Pharos SPNs
  • Network Overview
    • Pharos Networks
      • Pharos Testnet Information
    • Pharos Gas Model
    • FAQ
  • Node & Validator Guide
    • Validator Requirements
    • Validator Node Deployment
      • Using Docker (Devnet)
      • Using Docker (Testnet)
    • Node Management
    • Rapid Node Initialization
      • Rapid Node Initialization(Testnet)
      • Rapid Node Initialization(Devnet)
    • Pharos Network Snapshots
    • Node Debugging & Configuration
  • 🛠️ Pharos Testnet: Rebuild Node with Preserved Node Info
  • Pharos Node Monitoring
  • Developer Guide
    • Foundry
      • Write Your First dApp
      • Write Your First Token
      • Write Your First NFT
      • Write Your First Uniswap Contract
    • Hardhat
      • Write Your First dApp
      • Write Your First Token
      • Write Your First NFT
      • Write Your First Uniswap Contract
    • Rust
    • Interoperability
      • Call EVM From WASM
  • API & SDK
    • JSON-RPC API Methods
  • Resources
    • EVM
    • Solidity
Powered by GitBook
On this page
  • Prerequisites
  • Setup 1: Install Foundry
  • Setup 2: Set Up the Project
  • Setup 3: Write the NFT Contract
  • Step 4: Deploy the Token Contract
  • Step 5: Interact with the NFT Contract
  • Troubleshooting
  • Conclusion
Export as PDF
  1. Developer Guide
  2. Foundry

Write Your First NFT

PreviousWrite Your First TokenNextWrite Your First Uniswap Contract

Last updated 25 days ago

Prerequisites

Before you begin, ensure you have the following:

  • Git: Used for code management and obtain examples.

  • Node.js: Install it from .

  • Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain.

Setup 1: Install Foundry

Setup 2: Set Up the Project

Clone the example repo:

git clone https://github.com/PharosNetwork/examples
cd examples/nft/foundry/contract

Install OpenZeppelin Contracts:

  • Foundry uses forge to manage dependencies. Install OpenZeppelin contracts:

forge install OpenZeppelin/openzeppelin-contracts --no-git --no-commit

Setup 3: Write the NFT Contract

Create a New Solidity File:

  • Create a new file for your NFT contract:

touch src/Token.sol

Write the Token Contract:

  • Open src/Token.sol in your favorite text editor and add the following code:

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

import {ERC721} from "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";

contract Token is ERC721 {
    constructor() ERC721("Token", "MTK") {}
}

Compile the Smart Contract:

  • Use forge to compile the contract:

forge build

Test the Smart Contract

  • Use forge to test the contract:

forge test

Step 4: Deploy the Token Contract

Set the private key:

export PRIVATE_KEY=<your private key>

Create a Deployment Script:

  • Create a new file for the deployment script:

touch script/DeployToken.s.sol

Write the Deployment Script:

  • Open script/DeployToken.s.sol and add the following code:

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

import "forge-std/Script.sol";
import "../src/Token.sol";

contract DeployToken is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(deployerPrivateKey);

        Token token = new Token(1000000); // Initial supply of 1,000,000 tokens

        vm.stopBroadcast();
    }
}

Deploy the Contract:

  • Use forge to deploy the contract to the Pharos Testnet:

forge script script/DeployToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast

Step 5: Interact with the NFT Contract

Open script/InteractToken.s.sol and add the following code:

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

import {Script, console} from "forge-std/Script.sol";
import {Token} from "../src/Token.sol";

contract InteractToken is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(deployerPrivateKey);

        Token token = Token(address(0x00)); // Replace with your token contract address

        // Check balance
        uint256 balance = token.balanceOf(msg.sender);
        console.log("Balance:", balance);

        // Transfer tokens
        token.transfer(address(0x00), 100); // Replace with recipient address and amount
        console.log("Tokens transferred");

        vm.stopBroadcast();
    }
}

Execute the script using forge:

forge script script/InteractToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast

Troubleshooting

  • Contract Deployment Fails: Ensure you have enough testnet tokens to cover the deployment cost.

  • Interaction Issues: Verify that the contract address and ABI are correct.

  • Insufficient Balance: Ensure your wallet has enough tokens to transfer.

Conclusion

Now that you’ve created and deployed your first NFT using Foundry.

This guide provides a comprehensive introduction to to creating and deploying an NFT on the Pharos blockchain. If you encounter any issues, refer to the Troubleshooting section or consult the Foundry documentation. Happy building! 🚀

nodejs.org
Foundry