From 7a398ddfeb21fef3f9a030f8fbe6d6123636193f Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure Date: Mon, 11 Nov 2024 22:09:07 +0000 Subject: [PATCH] used subgraph to get total count of entities instead of onchain function --- contracts/src/ModuleRegistry.sol | 10 ------- contracts/src/PortalRegistry.sol | 29 +------------------ contracts/src/SchemaRegistry.sol | 10 ------- contracts/test/ModuleRegistry.t.sol | 18 ------------ contracts/test/PortalRegistry.t.sol | 16 +++++------ contracts/test/SchemaRegistry.t.sol | 20 ------------- sdk/src/abi/ModuleRegistry.ts | 13 --------- sdk/src/abi/PortalRegistry.ts | 13 --------- sdk/src/abi/SchemaRegistry.ts | 13 --------- sdk/src/dataMapper/BaseDataMapper.test.ts | 31 +++++++++++++++++++++ sdk/src/dataMapper/BaseDataMapper.ts | 12 ++++++++ sdk/src/dataMapper/ModuleDataMapper.ts | 2 +- sdk/src/dataMapper/SchemaDataMapper.ts | 2 +- sdk/src/dataMapper/UtilsDataMapper.ts | 34 +++++++++++------------ 14 files changed, 70 insertions(+), 153 deletions(-) diff --git a/contracts/src/ModuleRegistry.sol b/contracts/src/ModuleRegistry.sol index f8c48b2a..f2ad6e1e 100644 --- a/contracts/src/ModuleRegistry.sol +++ b/contracts/src/ModuleRegistry.sol @@ -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..2f684173 100644 --- a/contracts/src/PortalRegistry.sol +++ b/contracts/src/PortalRegistry.sol @@ -170,7 +170,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 +183,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 +225,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..b077c9d3 100644 --- a/contracts/src/SchemaRegistry.sol +++ b/contracts/src/SchemaRegistry.sol @@ -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/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..0e0bddd5 100644 --- a/sdk/src/dataMapper/ModuleDataMapper.ts +++ b/sdk/src/dataMapper/ModuleDataMapper.ts @@ -107,7 +107,7 @@ export default class ModuleDataMapper extends BaseDataMapper { typeName = "counter"; @@ -16,27 +14,15 @@ export default class UtilsDataMapper extends BaseDataMapper