diff --git a/contracts/src/ModuleRegistry.sol b/contracts/src/ModuleRegistry.sol index f8c48b2a..c0e2bcce 100644 --- a/contracts/src/ModuleRegistry.sol +++ b/contracts/src/ModuleRegistry.sol @@ -21,7 +21,7 @@ contract ModuleRegistry is OwnableUpgradeable { IRouter public router; /// @dev The list of Modules, accessed by their address mapping(address id => Module module) public modules; - /// @dev The list of Module addresses + /// @dev Deprecated: The `moduleAddresses` variable is no longer used. It was used to store the modules addresses. address[] public moduleAddresses; /// @notice Error thrown when an invalid Router address is given @@ -117,7 +117,6 @@ contract ModuleRegistry is OwnableUpgradeable { if (bytes(modules[moduleAddress].name).length > 0) revert ModuleAlreadyExists(); modules[moduleAddress] = Module(moduleAddress, name, description); - moduleAddresses.push(moduleAddress); emit ModuleRegistered(name, description, moduleAddress); } @@ -239,15 +238,6 @@ contract ModuleRegistry is OwnableUpgradeable { } } - /** - * @notice Get the number of Modules managed by the contract - * @return The number of Modules already registered - * @dev Returns the length of the `moduleAddresses` array - */ - function getModulesNumber() public view returns (uint256) { - return moduleAddresses.length; - } - /** * @notice Checks that a module is registered in the module registry * @param moduleAddress The address of the Module to check diff --git a/contracts/src/PortalRegistry.sol b/contracts/src/PortalRegistry.sol index e5c1091a..c8da6fea 100644 --- a/contracts/src/PortalRegistry.sol +++ b/contracts/src/PortalRegistry.sol @@ -23,6 +23,7 @@ contract PortalRegistry is OwnableUpgradeable { mapping(address issuerAddress => bool isIssuer) private issuers; + /// @dev Deprecated: The `portalAddresses` variable is no longer used. It was used to store the portals addresses. address[] private portalAddresses; bool private isTestnet; @@ -170,7 +171,6 @@ contract PortalRegistry is OwnableUpgradeable { // Add portal to mapping Portal memory newPortal = Portal(id, msg.sender, modules, isRevocable, name, description, ownerName); portals[id] = newPortal; - portalAddresses.push(id); // Emit event emit PortalRegistered(name, description, id); @@ -184,24 +184,7 @@ contract PortalRegistry is OwnableUpgradeable { function revoke(address id) public onlyOwner { if (!isRegistered(id)) revert PortalNotRegistered(); - portals[id] = Portal(address(0), address(0), new address[](0), false, "", "", ""); - - bool found = false; - uint256 portalAddressIndex; - for (uint256 i = 0; i < portalAddresses.length; i = uncheckedInc256(i)) { - if (portalAddresses[i] == id) { - portalAddressIndex = i; - found = true; - break; - } - } - - if (!found) { - revert PortalNotRegistered(); - } - - portalAddresses[portalAddressIndex] = portalAddresses[portalAddresses.length - 1]; - portalAddresses.pop(); + delete portals[id]; emit PortalRevoked(id); } @@ -243,15 +226,6 @@ contract PortalRegistry is OwnableUpgradeable { return portals[id].id != address(0); } - /** - * @notice Get the number of Portals managed by the contract - * @return The number of Portals already registered - * @dev Returns the length of the `portalAddresses` array - */ - function getPortalsCount() public view returns (uint256) { - return portalAddresses.length; - } - /** * @notice Checks if the caller is allowlisted. * @return A flag indicating whether the Verax instance is running on testnet diff --git a/contracts/src/SchemaRegistry.sol b/contracts/src/SchemaRegistry.sol index 93c4a6d2..42d418dc 100644 --- a/contracts/src/SchemaRegistry.sol +++ b/contracts/src/SchemaRegistry.sol @@ -16,7 +16,7 @@ contract SchemaRegistry is OwnableUpgradeable { IRouter public router; /// @dev The list of Schemas, accessed by their ID mapping(bytes32 id => Schema schema) private schemas; - /// @dev The list of Schema IDs + /// @dev Deprecated: The `schemaIds` variable is no longer used. It was used to store the ids of schemas. bytes32[] public schemaIds; /// @dev Associates a Schema ID with the address of the Issuer who created it mapping(bytes32 id => address issuer) private schemasIssuers; @@ -155,7 +155,6 @@ contract SchemaRegistry is OwnableUpgradeable { } schemas[schemaId] = Schema(name, description, context, schemaString); - schemaIds.push(schemaId); schemasIssuers[schemaId] = msg.sender; emit SchemaCreated(schemaId, name, description, context, schemaString); } @@ -184,15 +183,6 @@ contract SchemaRegistry is OwnableUpgradeable { return schemas[schemaId]; } - /** - * @notice Get the number of Schemas managed by the contract - * @return The number of Schemas already registered - * @dev Returns the length of the `schemaIds` array - */ - function getSchemasNumber() public view returns (uint256) { - return schemaIds.length; - } - /** * @notice Check if a Schema is registered * @param schemaId The ID of the Schema diff --git a/contracts/test/ModuleRegistry.t.sol b/contracts/test/ModuleRegistry.t.sol index d925d220..e2e493cd 100644 --- a/contracts/test/ModuleRegistry.t.sol +++ b/contracts/test/ModuleRegistry.t.sol @@ -132,16 +132,6 @@ contract ModuleRegistryTest is Test { moduleRegistry.register(expectedName, expectedDescription, expectedAddress); } - function test_getModulesNumber() public { - uint256 modulesNumber = moduleRegistry.getModulesNumber(); - assertEq(modulesNumber, 0); - vm.prank(user); - moduleRegistry.register(expectedName, expectedDescription, expectedAddress); - - modulesNumber = moduleRegistry.getModulesNumber(); - assertEq(modulesNumber, 1); - } - function test_runModules() public { // Register 2 modules address[] memory moduleAddresses = new address[](2); @@ -343,14 +333,6 @@ contract ModuleRegistryTest is Test { vm.stopPrank(); } - function test_getModuleAddress() public { - vm.prank(user); - moduleRegistry.register(expectedName, expectedDescription, expectedAddress); - - address moduleAddress = moduleRegistry.moduleAddresses(0); - assertEq(moduleAddress, expectedAddress); - } - function test_isRegistered() public { bool isRegistered = moduleRegistry.isRegistered(expectedAddress); assertFalse(isRegistered); diff --git a/contracts/test/PortalRegistry.t.sol b/contracts/test/PortalRegistry.t.sol index f6cb5348..029e7b4f 100644 --- a/contracts/test/PortalRegistry.t.sol +++ b/contracts/test/PortalRegistry.t.sol @@ -179,8 +179,8 @@ contract PortalRegistryTest is Test { vm.prank(user); portalRegistry.register(address(validPortalMock), expectedName, expectedDescription, true, expectedOwnerName); - uint256 portalCount = portalRegistry.getPortalsCount(); - assertEq(portalCount, 1); + bool isRegistered = portalRegistry.isRegistered(address(validPortalMock)); + assertEq(isRegistered, true); // Register a portal implementing IPortal vm.expectEmit(); @@ -194,8 +194,8 @@ contract PortalRegistryTest is Test { expectedOwnerName ); - portalCount = portalRegistry.getPortalsCount(); - assertEq(portalCount, 2); + isRegistered = portalRegistry.isRegistered(address(iPortalImplementation)); + assertEq(isRegistered, true); Portal memory expectedPortal = Portal( address(validPortalMock), @@ -272,8 +272,8 @@ contract PortalRegistryTest is Test { expectedOwnerName ); - uint256 portalCount = portalRegistry.getPortalsCount(); - assertEq(portalCount, 1); + bool isRegistered = portalRegistry.isRegistered(portalAddress); + assertEq(isRegistered, true); Portal memory expectedPortal = Portal( portalAddress, @@ -292,8 +292,8 @@ contract PortalRegistryTest is Test { emit PortalRevoked(portalAddress); portalRegistry.revoke(portalAddress); - portalCount = portalRegistry.getPortalsCount(); - assertEq(portalCount, 0); + isRegistered = portalRegistry.isRegistered(portalAddress); + assertEq(isRegistered, false); vm.expectRevert(PortalRegistry.PortalNotRegistered.selector); portalRegistry.getPortalByAddress(portalAddress); diff --git a/contracts/test/SchemaRegistry.t.sol b/contracts/test/SchemaRegistry.t.sol index 195814e8..dd4f243d 100644 --- a/contracts/test/SchemaRegistry.t.sol +++ b/contracts/test/SchemaRegistry.t.sol @@ -220,26 +220,6 @@ contract SchemaRegistryTest is Test { schemaRegistry.getSchema(bytes32("not registered")); } - function test_getSchemasNumber() public { - uint256 schemasNumber = schemaRegistry.getSchemasNumber(); - assertEq(schemasNumber, 0); - vm.startPrank(user); - schemaRegistry.createSchema(expectedName, expectedDescription, expectedContext, expectedString); - - schemasNumber = schemaRegistry.getSchemasNumber(); - assertEq(schemasNumber, 1); - vm.stopPrank(); - } - - function test_getSchemaIds() public { - vm.startPrank(user); - schemaRegistry.createSchema(expectedName, expectedDescription, expectedContext, expectedString); - - bytes32 schemaId = schemaRegistry.schemaIds(0); - assertEq(schemaId, expectedId); - vm.stopPrank(); - } - function test_isRegistered() public { bool isRegistered = schemaRegistry.isRegistered(expectedId); assertFalse(isRegistered); diff --git a/sdk/doc/cli-examples.md b/sdk/doc/cli-examples.md index 24a492f3..1da7d54f 100644 --- a/sdk/doc/cli-examples.md +++ b/sdk/doc/cli-examples.md @@ -43,7 +43,7 @@ pnpm portal isPortalRegistered '{\"portalAddress\":\"0x8b833796869b5debb9b06370d6d47016f0d7973b\"}' - pnpm portal getPortalsCount + pnpm portal getPortalsNumber ``` @@ -117,8 +117,6 @@ pnpm module isRegistered "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5" - pnpm module getModuleAddress 0 - pnpm module getModule "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5" ``` @@ -151,8 +149,6 @@ pnpm schema getSchemasNumber pnpm schema isRegistered "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738" - - pnpm schema getSchemaIds 0 ``` diff --git a/sdk/examples/module/moduleExamples.ts b/sdk/examples/module/moduleExamples.ts index 2bbe9ff6..1f1d99c5 100644 --- a/sdk/examples/module/moduleExamples.ts +++ b/sdk/examples/module/moduleExamples.ts @@ -182,11 +182,6 @@ export default class ModuleExamples { console.log(await this.veraxSdk.module.isRegistered(moduleAddress)); } - if (methodName.toLowerCase() == "getModuleAddress".toLowerCase() || methodName == "") { - const index: number = argv === "" ? 0 : (argv as unknown as number); - console.log(await this.veraxSdk.module.getModuleAddress(index)); - } - if (methodName.toLowerCase() == "getModule".toLowerCase() || methodName == "") { const moduleAddress: Address = argv === "" ? "0x8DcC1F7e746D6071Eb3ee9012aFB6c707bFf82a5" : (argv as Address); console.log(await this.veraxSdk.module.getModule(moduleAddress)); diff --git a/sdk/examples/portal/portalExamples.ts b/sdk/examples/portal/portalExamples.ts index 7653ca6b..3475a626 100644 --- a/sdk/examples/portal/portalExamples.ts +++ b/sdk/examples/portal/portalExamples.ts @@ -399,6 +399,10 @@ export default class PortalExamples { console.log(await this.veraxSdk.portal.getPortalByAddress(portalAddress)); } + if (methodName.toLowerCase() == "getPortalsNumber".toLowerCase() || methodName == "") { + console.log(await this.veraxSdk.portal.getPortalsNumber()); + } + if (methodName.toLowerCase() == "isPortalRegistered".toLowerCase() || methodName == "") { let params; if (argv !== "") params = JSON.parse(argv); diff --git a/sdk/examples/schema/schemaExamples.ts b/sdk/examples/schema/schemaExamples.ts index af390758..7f001ddd 100644 --- a/sdk/examples/schema/schemaExamples.ts +++ b/sdk/examples/schema/schemaExamples.ts @@ -93,10 +93,5 @@ export default class SchemaExamples { argv === "" ? "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738" : argv; console.log(await this.veraxSdk.schema.isRegistered(schemaId)); } - - if (methodName.toLowerCase() == "getSchemaIds".toLowerCase() || methodName == "") { - const index: number = argv === "" ? 0 : (argv as unknown as number); - console.log(await this.veraxSdk.schema.getSchemaIds(index)); - } } } diff --git a/sdk/examples/utils/countUniqueSubjects.ts b/sdk/examples/utils/countUniqueSubjects.ts index 1ccfaba3..9888384a 100644 --- a/sdk/examples/utils/countUniqueSubjects.ts +++ b/sdk/examples/utils/countUniqueSubjects.ts @@ -11,7 +11,7 @@ const fetchSubjectsFromFile = async (fileSuffix: number): Promise => { async function main() { const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET); - const attestationNumber = await veraxSdk.utils.getAttestationIdCounter(); + const attestationNumber = await veraxSdk.attestation.getAttestationIdCounter(); const filesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE); const allSubjects: string[][] = []; diff --git a/sdk/examples/utils/getAllAttestations.ts b/sdk/examples/utils/getAllAttestations.ts index 42871c1d..b686fa83 100644 --- a/sdk/examples/utils/getAllAttestations.ts +++ b/sdk/examples/utils/getAllAttestations.ts @@ -29,7 +29,7 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) => async function main() { const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET); - const attestationNumber = await veraxSdk.utils.getAttestationIdCounter(); + const attestationNumber = await veraxSdk.attestation.getAttestationIdCounter(); const batchesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE); console.log(`Creating ${batchesNumber} batches of ${BATCH_SIZE} items to get all ${attestationNumber} attestations.`); diff --git a/sdk/examples/utils/getAttestationIdCounter.ts b/sdk/examples/utils/getAttestationIdCounter.ts deleted file mode 100644 index 78f6752d..00000000 --- a/sdk/examples/utils/getAttestationIdCounter.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { VeraxSdk } from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_SEPOLIA); - -veraxSdk.utils.getAttestationIdCounter().then((res) => console.log(res)); diff --git a/sdk/examples/utils/getModulesNumber.ts b/sdk/examples/utils/getModulesNumber.ts deleted file mode 100644 index 747896a8..00000000 --- a/sdk/examples/utils/getModulesNumber.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { VeraxSdk } from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_SEPOLIA); - -veraxSdk.utils.getModulesNumber().then((res) => console.log(res)); diff --git a/sdk/examples/utils/getPortalsCount.ts b/sdk/examples/utils/getPortalsCount.ts deleted file mode 100644 index 88f09904..00000000 --- a/sdk/examples/utils/getPortalsCount.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { VeraxSdk } from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_SEPOLIA); - -veraxSdk.utils.getPortalsCount().then((res) => console.log(res)); diff --git a/sdk/examples/utils/getSchemasNumber.ts b/sdk/examples/utils/getSchemasNumber.ts deleted file mode 100644 index 12dd6ac2..00000000 --- a/sdk/examples/utils/getSchemasNumber.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { VeraxSdk } from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_SEPOLIA); - -veraxSdk.utils.getSchemasNumber().then((res) => console.log(res)); diff --git a/sdk/src/abi/ModuleRegistry.ts b/sdk/src/abi/ModuleRegistry.ts index 6dcb5c18..b770cae2 100644 --- a/sdk/src/abi/ModuleRegistry.ts +++ b/sdk/src/abi/ModuleRegistry.ts @@ -151,19 +151,6 @@ export const abiModuleRegistry = [ stateMutability: "nonpayable", type: "function", }, - { - inputs: [], - name: "getModulesNumber", - outputs: [ - { - internalType: "uint256", - name: "", - type: "uint256", - }, - ], - stateMutability: "view", - type: "function", - }, { inputs: [], name: "initialize", diff --git a/sdk/src/abi/PortalRegistry.ts b/sdk/src/abi/PortalRegistry.ts index 149789f6..13c29868 100644 --- a/sdk/src/abi/PortalRegistry.ts +++ b/sdk/src/abi/PortalRegistry.ts @@ -195,19 +195,6 @@ export const abiPortalRegistry = [ stateMutability: "view", type: "function", }, - { - inputs: [], - name: "getPortalsCount", - outputs: [ - { - internalType: "uint256", - name: "", - type: "uint256", - }, - ], - stateMutability: "view", - type: "function", - }, { inputs: [], name: "initialize", diff --git a/sdk/src/abi/SchemaRegistry.ts b/sdk/src/abi/SchemaRegistry.ts index 91cfb2f6..f8608074 100644 --- a/sdk/src/abi/SchemaRegistry.ts +++ b/sdk/src/abi/SchemaRegistry.ts @@ -191,19 +191,6 @@ export const abiSchemaRegistry = [ stateMutability: "view", type: "function", }, - { - inputs: [], - name: "getSchemasNumber", - outputs: [ - { - internalType: "uint256", - name: "", - type: "uint256", - }, - ], - stateMutability: "view", - type: "function", - }, { inputs: [], name: "initialize", diff --git a/sdk/src/dataMapper/BaseDataMapper.test.ts b/sdk/src/dataMapper/BaseDataMapper.test.ts index cf45f96a..c970a97e 100644 --- a/sdk/src/dataMapper/BaseDataMapper.test.ts +++ b/sdk/src/dataMapper/BaseDataMapper.test.ts @@ -141,4 +141,35 @@ describe("BaseDataMapper", () => { await expect(mockDataMapper.findBy()).rejects.toThrow("Error(s) while fetching TestTypes"); }); }); + + describe("findTotalCount", () => { + it("should call subgraphCall with the correct query and return the result", async () => { + const mockResponse = { data: { data: { counters: [{ TestTypes: 4 }] } }, status: 200 }; + (subgraphCall as jest.Mock).mockResolvedValueOnce(mockResponse); + + const result = await mockDataMapper.findTotalCount(); + + expect(subgraphCall).toHaveBeenCalledWith( + `query get_TestType_Counter { counters { TestTypes } }`, + mockConf.subgraphUrl, + ); + expect(result).toEqual(4); + }); + + it("should throw an error if the status is not 200", async () => { + const mockResponse = { status: 500 }; + (subgraphCall as jest.Mock).mockResolvedValueOnce(mockResponse); + + await expect(mockDataMapper.findTotalCount()).rejects.toThrow("Error(s) while fetching total count of TestTypes"); + }); + + it("should return 0 if no data is found", async () => { + const mockResponse = { data: null, status: 200 }; + (subgraphCall as jest.Mock).mockResolvedValueOnce(mockResponse); + + const result = await mockDataMapper.findTotalCount(); + + expect(result).toEqual(0); + }); + }); }); diff --git a/sdk/src/dataMapper/BaseDataMapper.ts b/sdk/src/dataMapper/BaseDataMapper.ts index eec22304..fe226d23 100644 --- a/sdk/src/dataMapper/BaseDataMapper.ts +++ b/sdk/src/dataMapper/BaseDataMapper.ts @@ -53,4 +53,16 @@ export default abstract class BaseDataMapper { return data?.data ? (data.data[`${this.typeName}s`] as T[]) : []; } + + async findTotalCount() { + const query = `query get_${this.typeName}_Counter { counters { ${this.typeName}s } }`; + + const { data, status } = await subgraphCall(query, this.conf.subgraphUrl); + + if (status != 200) { + throw new Error(`Error(s) while fetching total count of ${this.typeName}s`); + } + + return data?.data ? data.data["counters"][0][`${this.typeName}s`] : 0; + } } diff --git a/sdk/src/dataMapper/ModuleDataMapper.ts b/sdk/src/dataMapper/ModuleDataMapper.ts index a4943923..a2f34187 100644 --- a/sdk/src/dataMapper/ModuleDataMapper.ts +++ b/sdk/src/dataMapper/ModuleDataMapper.ts @@ -18,7 +18,7 @@ export default class ModuleDataMapper extends BaseDataMapper ({ + encode: jest.fn(), + decode: jest.fn(), +})); + +describe("UtilsDataMapper", () => { + let utilsDataMapper: UtilsDataMapper; + const mockConf: Conf = { + subgraphUrl: "http://mock-subgraph.com", + chain: lineaSepolia, + mode: SDKMode.BACKEND, + portalRegistryAddress: "0x1", + moduleRegistryAddress: "0x2", + schemaRegistryAddress: "0x3", + attestationRegistryAddress: "0x4", + }; + const mockWeb3Client = { readContract: jest.fn() } as unknown as PublicClient; + const mockWalletClient = {} as WalletClient; + const mockVeraxSdk = {} as VeraxSdk; + + beforeEach(() => { + utilsDataMapper = new UtilsDataMapper(mockConf, mockWeb3Client, mockVeraxSdk, mockWalletClient); + }); + + describe("getVersionNumber", () => { + it("should call web3Client to get the version number", async () => { + const mockVersion = "1.0.0"; + (mockWeb3Client.readContract as jest.Mock).mockResolvedValue(mockVersion); + + const result = await utilsDataMapper.getVersionNumber(); + expect(result).toBe(mockVersion); + expect(mockWeb3Client.readContract).toHaveBeenCalledWith({ + abi: abiAttestationRegistry, + address: mockConf.attestationRegistryAddress, + functionName: "getVersionNumber", + }); + }); + }); + + describe("encode", () => { + it("should call encode with correct arguments", () => { + const schema = "schema"; + const values = ["value1", "value2"]; + const mockEncoded = "0xencoded"; + + (encode as jest.Mock).mockReturnValue(mockEncoded); + + const result = utilsDataMapper.encode(schema, values); + expect(result).toBe(mockEncoded); + expect(encode).toHaveBeenCalledWith(schema, values); + }); + }); + + describe("decode", () => { + it("should call decode with correct arguments", () => { + const schema = "schema"; + const attestationData = "0xdata"; + const mockDecoded = ["value1", "value2"]; + + (decode as jest.Mock).mockReturnValue(mockDecoded); + + const result = utilsDataMapper.decode(schema, attestationData); + expect(result).toEqual(mockDecoded); + expect(decode).toHaveBeenCalledWith(schema, attestationData); + }); + }); +}); diff --git a/sdk/src/dataMapper/UtilsDataMapper.ts b/sdk/src/dataMapper/UtilsDataMapper.ts index 59e4fa0a..5af46928 100644 --- a/sdk/src/dataMapper/UtilsDataMapper.ts +++ b/sdk/src/dataMapper/UtilsDataMapper.ts @@ -1,8 +1,5 @@ import BaseDataMapper from "./BaseDataMapper"; import { abiAttestationRegistry } from "../abi/AttestationRegistry"; -import { abiModuleRegistry } from "../abi/ModuleRegistry"; -import { abiPortalRegistry } from "../abi/PortalRegistry"; -import { abiSchemaRegistry } from "../abi/SchemaRegistry"; import { decode, encode } from "../utils/abiCoder"; import { Hex } from "viem"; @@ -15,30 +12,6 @@ export default class UtilsDataMapper extends BaseDataMapper