1

I am currently in the process of testing my solidity smart contract using hardhat and am a bit confused on how I should do it. This is the very first time I am testing a smart contract so I am bit rusty and its a bit confusing to me. I am using typescript. Here is my smart contract and below I pasted the test I have so far. It is a simple erc1155 smart contract that mints tokens when the the constructor is called.

    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/ERC1155.sol)


    pragma solidity 0.8.19;


    import "@openzeppelin/contracts/access/Ownable.sol";
    import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
    import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
    import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
    import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
    import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol";
    import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";



    contract SpiderVerse is IERC1155, ERC1155{

      //Defining the variables that will be used in this contract along with their ids.
      uint public constant MILES_MORALES = 0;
      uint public constant GWEN_STACY = 1;
      uint public constant PETER_B_PARKER =2;
      uint public constant MIGUEL_O_HARA = 3;
      uint public constant HOBBIE_BROWN = 4;
      uint public constant JESSICA_DREW = 5;
      uint public constant PAVITIR_PRABHAKAR = 6;
      uint public constant PENI_PARKER = 7;
      uint public constant PETER_NOIR_PARKER = 8;
      uint public constant PETER_PORKER = 9;
      uint public constant SPIDEY_TOKENS = 10;

       bytes public constant tokenSymbol = "SDT" ;

     //constructor will mint all NFTS and Fungible Spidey Tokens at once.
      constructor () ERC1155("https://app.pinata.cloud/pinmanager?") {
        _mint(msg.sender, MILES_MORALES , 1, "");
        _mint(msg.sender, GWEN_STACY , 1 , "");
        _mint(msg.sender, PETER_B_PARKER , 1 , "");
        _mint(msg.sender, MIGUEL_O_HARA , 1 , "");
        _mint(msg.sender, HOBBIE_BROWN ,  1 , "");
        _mint(msg.sender, JESSICA_DREW , 1 , "");
        _mint(msg.sender, PAVITIR_PRABHAKAR , 1 , "");
        _mint(msg.sender, PENI_PARKER , 1 , "");
        _mint(msg.sender, PETER_NOIR_PARKER , 1 , "");
        _mint(msg.sender, PETER_PORKER , 1 , "");
        _mint(msg.sender, SPIDEY_TOKENS, 10**20, "");
       }


       function uri(uint256 _tokenId) public pure override returns(string memory){
         return string(abi.encodePacked(
           "https://app.pinata.cloud/pinmanager?",
             Strings.toString(_tokenId),
            ".json"
          )
        );
      }


      function contractURI() public pure returns(string memory) {
         return 
           "https://app.pinata.cloud/pinmanager?";
      }



}

The test I made for my code.

    const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
    const { expect } = require("chai");
    import hre from "hardhat";
    import { ethers } from "hardhat";

    let spiderVerse;
    let SpiderVerse;

    describe("SpiderVerse", function () {
    before(async function (){
    SpiderVerse = await ethers.getContractFactory("SpiderVerse");
    spiderVerse = await SpiderVerse.deploy();
    await SpiderVerse.deployed();
     })

As you can see I barely have anything as I am just starting out. How would I even test for the constructor to mint all the tokens? It is a simple smart contracta so I would imagine I wouldnt have to test too much code and since I inherited multiple contracts from OpenZeppelin which is a battle tested network I would assume I dont have to test al those other contracts correct? This typescript syntax looks simple but I am quite confused on naming schemes and such. Hope to hear from some of you guys! I always learn so muhc on here and get so much help. Thank you!!!!

1 Answer 1

0

You can use this code to test the creation of all tokens:

import { ethers } from 'hardhat';
import { expect } from 'chai';
import { Contract, Signer } from 'ethers';

describe('SpiderVerse Contract', function () {
  let spiderVerse: Contract;
  let owner: Signer;

  beforeEach(async () => {
    const [deployer] = await ethers.getSigners();
    owner = deployer;

    const SpiderVerseFactory = await ethers.getContractFactory('SpiderVerse');
    spiderVerse = await SpiderVerseFactory.connect(owner).deploy();
    await spiderVerse.deployed();
  });

  it('should mint all tokens to the owner in the constructor', async function () {

    const balanceMiles = await spiderVerse.balanceOf(await owner.getAddress(), 0);
    const balanceGwen = await spiderVerse.balanceOf(await owner.getAddress(), 1);
   
    expect(balanceMiles).to.equal(1);
    expect(balanceGwen).to.equal(1);
  
  });

 

});

If I did not understand correctly, please ask more clearly so that I can guide you

6
  • thank you so much! However when I test it I get an error saying that spiderVerse.deployed is not a function. When I tested my previous test it as throwing the same errors. What does this error even mean?
    – EGstacking
    Commented Sep 2, 2023 at 14:52
  • Just comment this line and test again : // await spiderVerse.deployed();
    – Mehran Ra
    Commented Sep 2, 2023 at 15:09
  • actually it worked! Thank you so much! All this is making sense. So since I only have the constructor essentially is that all I need to test for? I am thinking about testing the uri functionalty as well
    – EGstacking
    Commented Sep 2, 2023 at 15:21
  • There doesn't seem to be anything to test URI, the only thing that can be tested is the value of the string that is equal to the desired value.
    – Mehran Ra
    Commented Sep 2, 2023 at 18:09
  • awesome! and one more thing I have to ask. Do I have to deploy my smart contract BEFORE connecting it to my frontend application?
    – EGstacking
    Commented Sep 3, 2023 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.