Skip to content

Commit

Permalink
fix: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kolhapuresatyajeet committed Sep 21, 2023
1 parent 2bc79b6 commit 3223055
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/example/NFTPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@ import { Attestation, AttestationPayload } from "../types/Structs.sol";
/**
* @title NFT Portal
* @author Consensys
* @notice This contract aims to provide ERC 721 features
* @dev This Portal implements ERC 721 - balanceOf and ownerOf functions
* @notice This contract aims to provide ERC 721 compatibility
* @dev This Portal implements parts of ERC 721 - balanceOf and ownerOf functions
*/
contract NFTPortal is AbstractPortal, ERC721 {
mapping(bytes owner => uint256 numberOfAttestations) private numberOfAttestationsPerOwner;

constructor(address[] memory modules, address router) AbstractPortal(modules, router) ERC721("1", "2") {}
constructor(
address[] memory modules,
address router
) AbstractPortal(modules, router) ERC721("NFTPortal", "NFTPortal") {}

/** @notice Count all NFTs assigned to an owner
* @dev NFTs assigned to the zero address are considered invalid, and this
* function throws for queries about the zero address.
/** @notice Count all attestations assigned to an owner
* @param owner An address for whom to query the balance
* @return The number of NFTs owned by `owner`, possibly zero
* @return The number of attestations owned by `owner`, possibly zero
*/
function balanceOf(address owner) public view virtual override returns (uint256) {
return numberOfAttestationsPerOwner[abi.encode(owner)];
}

/** @notice Find the owner of an NFT
* @dev NFTs assigned to zero address are considered invalid, and queries
* about them do throw.
* @param tokenId The identifier for an NFT
* @return The address of the owner of the NFT
/** @notice Find the owner of an attestation
* @param tokenId The identifier for an attestation
* @return The address of the owner of the attestation
*/
function ownerOf(uint256 tokenId) public view virtual override returns (address) {
bytes32 attestationId = bytes32(tokenId);
Expand Down
68 changes: 68 additions & 0 deletions test/example/NFTPortal.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import { Test } from "forge-std/Test.sol";
import { NFTPortal } from "../../src/example/NFTPortal.sol";
import { Router } from "../../src/Router.sol";
import { AbstractPortal } from "../../src/interface/AbstractPortal.sol";
import { AttestationPayload } from "../../src/types/Structs.sol";
import { AttestationRegistryMock } from "../mocks/AttestationRegistryMock.sol";
import { ModuleRegistryMock } from "../mocks/ModuleRegistryMock.sol";
import { IERC721 } from "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
import { IERC165 } from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";

contract NFTPortalTest is Test {
address public attester = makeAddr("attester");
NFTPortal public nftPortal;
address[] public modules = new address[](0);
ModuleRegistryMock public moduleRegistryMock = new ModuleRegistryMock();
AttestationRegistryMock public attestationRegistryMock = new AttestationRegistryMock();
Router public router = new Router();

event Initialized(uint8 version);
event AttestationRegistered();
event BulkAttestationsRegistered();

function setUp() public {
router.initialize();
router.updateModuleRegistry(address(moduleRegistryMock));
router.updateAttestationRegistry(address(attestationRegistryMock));

nftPortal = new NFTPortal(modules, address(router));
}

function test_balanceOf_ownerOf() public {
// Create attestation payload
AttestationPayload memory attestationPayload = AttestationPayload(
bytes32(uint256(1)),
uint64(block.timestamp + 1 days),
abi.encode(address(1)), // Convert address(1) to bytes and use as subject
new bytes(1)
);
// Create validation payload
bytes[] memory validationPayload = new bytes[](0);
vm.expectEmit(true, true, true, true);
emit AttestationRegistered();
nftPortal.attest(attestationPayload, validationPayload);
vm.expectEmit(true, true, true, true);
emit AttestationRegistered();
nftPortal.attest(attestationPayload, validationPayload);

uint256 balance = nftPortal.balanceOf(address(1));
assertEq(balance, 2);

address ownerOfFirstAttestation = nftPortal.ownerOf(1);
address ownerOfSecondAttestation = nftPortal.ownerOf(2);
assertEq(ownerOfFirstAttestation, address(1));
assertEq(ownerOfSecondAttestation, address(1));
}

function testSupportsInterface() public {
bool isIERC165Supported = nftPortal.supportsInterface(type(IERC165).interfaceId);
assertEq(isIERC165Supported, true);
bool isIERC721Supported = nftPortal.supportsInterface(type(IERC721).interfaceId);
assertEq(isIERC721Supported, true);
bool isEASAbstractPortalSupported = nftPortal.supportsInterface(type(AbstractPortal).interfaceId);
assertEq(isEASAbstractPortalSupported, true);
}
}

0 comments on commit 3223055

Please sign in to comment.