0

I'm writing a test for my diamond smart contract

This is my diamond solidity smart contract: Crud.sol

//I want to test diamond smart contract using crud operation

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

//Libraries -->
import "@solidstate/contracts/proxy/diamond/SolidStateDiamond.sol";

contract Crud is SolidStateDiamond {
    function getSelector(
        string calldata _funcSignature
    ) external pure returns (bytes4) {
        return bytes4(keccak256(bytes(_funcSignature)));
    }
}

and this is my facet smart contract: Facet.sol

//I want to test diamond smart contract using crud operation

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

//Libraries -->
//Commencing the code -->
contract Facet {
    uint a;
    bool __initFacet__;

    function __initFacet() external returns (bytes memory) {
        require(!__initFacet__, "This function can only be ran once");
        a = 3;
        __initFacet__ = true;
        bytes memory data = msg.data;
        return data;
    }

    function set(uint _a) external {
        a = _a;
    }

    function get() external view returns (uint) {
        return a;
    }
}

This is my test script

const { expect } = require("chai");
const { ethers } = require("hardhat");
const { abi: abiCrud } = require("../artifacts/contracts/crud.sol/Crud.json");
const {
  abi: abiFacet,
} = require("../artifacts/contracts/facet.sol/Facet.json");
const Ether = require("./library");

describe("Crud", () => {
  let Diamond, _diamond, signer1, signer2, signer3;

  beforeEach(async () => {
    [signer1, signer2, signer3] = await ethers.getSigners();
    Diamond = await ethers.getContractFactory("Crud");
    _diamond = await Diamond.deploy();
    await _diamond.deployed();
  });

  //This handles the test for the access control functions of the diamond smart contract
  describe("\nAccess Control", () => {
    let nominee;

    it("should set owner", async () => {
      //   console.log(
      //     "Facet contract was deployed to",
      //     _diamond.address,
      //     "by",
      //     _diamond
      //   );
      expect(await _diamond.owner()).to.equal(signer1.address);
    });
    it("should transfer ownership, set the nominee owner and accept ownership", async () => {
      initialOwner = await _diamond.owner();
      await _diamond.transferOwnership(signer2.address);
      expect(await _diamond.transferOwnership(signer2.address))
        .to.emit(_diamond, "OwnershipTransferred")
        .withArgs(initialOwner, signer2.address);

      nominee = await _diamond.nomineeOwner();
      expect(signer2.address).to.equal(nominee);

      await _diamond.connect(signer2).acceptOwnership();
      expect(await _diamond.owner()).to.equal(signer2.address);
      //   console.log("Owner: ", await _diamond.owner());
      //   console.log("Nominee owner: ", await _diamond.nomineeOwner());
    });
  });

  //This handles the test for immutable functions
  //   describe("\nImmutable Functions", () => {
  //     let functionSelector;

  //     it("should get the function selector of a function", async () => {
  //       functionSelector = await _diamond.getSelector("acceptOwnership()");
  //       //   func1 = await _diamond.interface.errors;
  //       //   console.log("function selector: ", functionSelector);
  //       //console.log("selector: ", func1);
  //     });
  //   });

  //This handles the test for the diamond and facet functionalities
  describe("\nDiamonds and Facets", () => {
    let Facet, _facet, facetCut, facetAddr, facetFuncs, funcSelectors, callData;

    beforeEach(async () => {
      Facet = await ethers.getContractFactory("Facet");
      _facet = await Facet.deploy();
      await _facet.deployed();
      facetAddr = _facet.address;
      facetFuncs = Object.keys(_facet.interface.functions);
      funcSelectors = [
        await _diamond.getSelector(facetFuncs[0]),
        await _diamond.getSelector(facetFuncs[1]),
      ];
    });

    it("should add a facet", async () => {
      //console.log("Facet: ", _facet);
      _action = "0";
      let addr = _diamond.address;
      facetCut = [
        {
          facetAddr,
          _action,
          funcSelectors,
        },
      ];
      let _callData = new Ether(abiFacet).getFuncCallData("__initFacet", []);
      callData = await _facet.__initFacet().data;
      try {
        let addr1 = _facet.address;
        console.log("t ", addr1);
        let addFacet = await _diamond
          .connect(signer1)
          .diamondCut(facetCut, _diamond.address, "0x", { gasLimit: 800000 });
        await addFacet.wait();
      } catch (e) {
        console.log("Error: ", e);
      }

      //console.log("Diamond: ", callDataBytes);

      //   let addFacet = await _diamond.diamondCut(
      //     [
      //       {
      //         facetAddr,
      //         _action,
      //         funcSelectors,
      //       },
      //     ],
      //     facetAddr,
      //     callDataBytes
      // );

      //   console.log("Facet: ", Object.keys(_facet.interface.functions));
      //   facets = await _diamond.facets();
      //   console.log("Contract Address: ", _diamond.address);
      //   console.log("Facet address: ", facetAddr);
      //   console.log("Facets addresses: ", await _diamond.facetAddresses());
    });
  });
});

And I keep getting this error whenever I run the test script, all other test runs successfully apart from "Diamonds and Facets"

Error: invalid address or ENS name (argument="name", value=undefined, code=INVALID_ARGUMENT, version=contracts/5.7.0)
    at Logger.makeError (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\logger\src.ts\index.ts:269:28)
    at Logger.throwError (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\logger\src.ts\index.ts:281:20)
    at Logger.throwArgumentError (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\logger\src.ts\index.ts:285:21)
    at C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\contracts\src.ts\index.ts:123:16
    at step (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at fulfilled (C:\Users\Favour\OneDrive\Documents\Code School\Blockchain\Projects\freezable-token\node_modules\@ethersproject\contracts\lib\index.js:20:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'invalid address or ENS name',
  code: 'INVALID_ARGUMENT',
  argument: 'name',
  value: undefined
}

please I would really appreciate any help with this issue, if possible write a test script to test the diamondCut function. Thanks in advance

Syre Musk
  • 57
  • 7

1 Answers1

0

I later resolved it by naming the keys of FacetCut struct arguments accordingly

const { expect } = require("chai");
const { ethers } = require("hardhat");
const { abi: abiCrud } = require("../artifacts/contracts/crud.sol/Crud.json");
const {
  abi: abiFacet,
} = require("../artifacts/contracts/facet.sol/Facet.json");
const { Ether, FacetCutAction } = require("./library");

describe("Crud", () => {
  let Diamond, _diamond, signer1, signer2, signer3;

  beforeEach(async () => {
    [signer1, signer2, signer3] = await ethers.getSigners();
    Diamond = await ethers.getContractFactory("Crud");
    _diamond = await Diamond.deploy();
    await _diamond.deployed();
  });

  //This handles the test for the access control functions of the diamond smart contract
  describe("\nAccess Control", () => {
    let nominee;

    it("should set owner", async () => {
      expect(await _diamond.owner()).to.equal(signer1.address);
    });
    it("should transfer ownership, set the nominee owner and accept ownership", async () => {
      initialOwner = await _diamond.owner();
      await _diamond.transferOwnership(signer2.address);
      expect(await _diamond.transferOwnership(signer2.address))
        .to.emit(_diamond, "OwnershipTransferred")
        .withArgs(initialOwner, signer2.address);

      nominee = await _diamond.nomineeOwner();
      expect(signer2.address).to.equal(nominee);

      await _diamond.connect(signer2).acceptOwnership();
      expect(await _diamond.owner()).to.equal(signer2.address);
      //   console.log("Owner: ", await _diamond.owner());
      //   console.log("Nominee owner: ", await _diamond.nomineeOwner());
    });
  });

  //This handles the test for immutable functions
  //   describe("\nImmutable Functions", () => {
  //     let functionSelector;

  //     it("should get the function selector of a function", async () => {
  //       functionSelector = await _diamond.getSelector("acceptOwnership()");
  //       //   func1 = await _diamond.interface.errors;
  //       //   console.log("function selector: ", functionSelector);
  //       //console.log("selector: ", func1);
  //     });
  //   });

  //This handles the test for the diamond and facet functionalities
  describe("\nDiamonds and Facets", () => {
    let Facet,
      _facet,
      facetCuts,
      _abiFacet,
      facetFuncs,
      action,
      selectors,
      target,
      _callData;

    beforeEach(async () => {
      Facet = await ethers.getContractFactory("Facet");
      _facet = await Facet.deploy();
      await _facet.deployed();
      target = _facet.address;
      _abiFacet = new Ether(abiFacet);
      facetFuncs = Object.keys(_facet.interface.functions);
      selectors = [
        _abiFacet.getFuncSelector(facetFuncs[0]),
        _abiFacet.getFuncSelector(facetFuncs[1]),
        _abiFacet.getFuncSelector(facetFuncs[2]),
      ];
      action = FacetCutAction.Add;
      facetCuts = [
        {
          target,
          action,
          selectors,
        },
      ];
      _callData = _abiFacet.getFuncCallData(facetFuncs[0], []);
    });

    it("should allow only owner to access the diamondCut function", async () => {
      // let addFacet = await _diamond.connect(signer2).diamondCut(
      //     facetCuts,
      //     _diamond.address,
      //     _callData,
      //     { gasLimit: 800000 }
      //   );
      //   await addFacet.wait();
      //   console.log("Facet: ", selectors);
      //   console.log("Test: ", _abiFacet.getFuncSelector(facetFuncs[0]));
    });

    it("should add a facet", async () => {
      //console.log("Facet: ", _facet);
      //   let addr = _diamond.address;

      let addFacet = await _diamond.diamondCut(
        facetCuts,
        _diamond.address,
        _callData,
        { gasLimit: 800000 }
      );
      await addFacet.wait();
      // let data = _diamond.get();
      console.log("Data: ", _diamond);
      //   console.log("Facet: ", Object.keys(_facet.interface.functions));
      //   facets = await _diamond.facets();
      //   console.log("Contract Address: ", _diamond.address);
      //   console.log("Facet address: ", facetAddr);
      console.log("Facets addresses: ", await _diamond.facetAddresses());
    });
  });
});
Syre Musk
  • 57
  • 7