diff --git a/packages/subgraph/package.json b/packages/subgraph/package.json index 3fc54cb6..72648dd9 100644 --- a/packages/subgraph/package.json +++ b/packages/subgraph/package.json @@ -22,7 +22,7 @@ "@graphprotocol/graph-ts": "^0.31.0", "cross-env": "^7.0.3", "dotenv": "^16.3.1", - "matchstick-as": "^0.5.2", + "matchstick-as": "^0.6.0", "mustache": "^4.2.0", "ts-morph": "^17.0.1", "ts-node": "^10.9.1", diff --git a/packages/subgraph/src/plugin/plugin.ts b/packages/subgraph/src/plugin/plugin.ts index 9145723d..0f5a29d0 100644 --- a/packages/subgraph/src/plugin/plugin.ts +++ b/packages/subgraph/src/plugin/plugin.ts @@ -10,7 +10,7 @@ import { ProposalCreated, ProposalExecuted, } from '../../generated/templates/Plugin/Admin'; -import {EXECUTE_PROPOSAL_PERMISSION_HASH} from '../utils/constants'; +import {EXECUTE_PROPOSAL_PERMISSION_ID} from '../utils/constants'; import {generateAdministratorAdminPluginEntityId} from '../utils/ids'; import { generateActionEntityId, @@ -110,6 +110,6 @@ export function handleMembershipContractAnnounced( ): void { const context = new DataSourceContext(); context.setString('pluginAddress', event.address.toHexString()); - context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_HASH); + context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_ID); AdminMembers.createWithContext(event.params.definingContract, context); } diff --git a/packages/subgraph/src/utils/constants.ts b/packages/subgraph/src/utils/constants.ts index ed0e2e91..7b4547f1 100644 --- a/packages/subgraph/src/utils/constants.ts +++ b/packages/subgraph/src/utils/constants.ts @@ -1,3 +1,3 @@ // keccack256 of EXECUTE_PROPOSAL_PERMISSION -export const EXECUTE_PROPOSAL_PERMISSION_HASH = +export const EXECUTE_PROPOSAL_PERMISSION_ID = '0xf281525e53675515a6ba7cc7bea8a81e649b3608423ee2d73be1752cea887889'; diff --git a/packages/subgraph/tests/plugin/adminMembers.test.ts b/packages/subgraph/tests/plugin/adminMembers.test.ts index 901fba94..6997987e 100644 --- a/packages/subgraph/tests/plugin/adminMembers.test.ts +++ b/packages/subgraph/tests/plugin/adminMembers.test.ts @@ -1,6 +1,6 @@ import {Administrator, AdministratorAdminPlugin} from '../../generated/schema'; import {handleGranted, handleRevoked} from '../../src/plugin/adminMembers'; -import {EXECUTE_PROPOSAL_PERMISSION_HASH} from '../../src/utils/constants'; +import {EXECUTE_PROPOSAL_PERMISSION_ID} from '../../src/utils/constants'; import {generateAdministratorAdminPluginEntityId} from '../../src/utils/ids'; import {ADDRESS_ONE, ADDRESS_TWO, DAO_ADDRESS} from '../utils/constants'; import {createGrantedEvent, createRevokedEvent} from '../utils/events/plugin'; @@ -17,17 +17,16 @@ import { } from 'matchstick-as/assembly/index'; const adminAddress = Address.fromString(ADDRESS_ONE); -const adminEntityId = generateEntityIdFromAddress(adminAddress); +const administratorEntityId = generateEntityIdFromAddress(adminAddress); const pluginAddress = Address.fromString(ADDRESS_TWO); const pluginEntityId = generateEntityIdFromAddress(pluginAddress); describe('AdminMembers', function () { // keccack256 of EXECUTE_PROPOSAL_PERMISSION - const AdminPermission = EXECUTE_PROPOSAL_PERMISSION_HASH; beforeEach(function () { let context = new DataSourceContext(); - context.setString('permissionId', AdminPermission); + context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_ID); context.setString('pluginAddress', pluginEntityId); dataSourceMock.setContext(context); }); @@ -37,25 +36,36 @@ describe('AdminMembers', function () { }); test('handleGranted', function () { + // check the entities are not in the store + assert.entityCount('Administrator', 0); + assert.entityCount('AdministratorAdminPlugin', 0); + + // create the event and handle it let event = createGrantedEvent( + EXECUTE_PROPOSAL_PERMISSION_ID, DAO_ADDRESS, pluginEntityId, - adminEntityId, - AdminPermission + administratorEntityId ); handleGranted(event); + // check the administrator entity assert.entityCount('Administrator', 1); - assert.fieldEquals('Administrator', adminEntityId, 'id', adminEntityId); assert.fieldEquals( 'Administrator', - adminEntityId, + administratorEntityId, + 'id', + administratorEntityId + ); + assert.fieldEquals( + 'Administrator', + administratorEntityId, 'address', - adminEntityId + administratorEntityId ); + // check the mapping with the admin pluging entity assert.entityCount('AdministratorAdminPlugin', 1); - let administratorAdminPluginId = generateAdministratorAdminPluginEntityId( pluginAddress, adminAddress @@ -70,7 +80,7 @@ describe('AdminMembers', function () { 'AdministratorAdminPlugin', administratorAdminPluginId, 'administrator', - adminEntityId + administratorEntityId ); assert.fieldEquals( 'AdministratorAdminPlugin', @@ -81,8 +91,8 @@ describe('AdminMembers', function () { }); test('handleRevoked', function () { - let administrator = new Administrator(adminEntityId); - administrator.address = adminEntityId; + let administrator = new Administrator(administratorEntityId); + administrator.address = administratorEntityId; administrator.save(); let administratorAdminPluginId = generateAdministratorAdminPluginEntityId( @@ -92,20 +102,26 @@ describe('AdminMembers', function () { let administratorAdminPluginEntity = new AdministratorAdminPlugin( administratorAdminPluginId ); - administratorAdminPluginEntity.administrator = ADDRESS_TWO; - administratorAdminPluginEntity.plugin = ADDRESS_ONE; + administratorAdminPluginEntity.administrator = administratorEntityId; + administratorAdminPluginEntity.plugin = pluginEntityId; administratorAdminPluginEntity.save(); + // check the entities are in the store + assert.entityCount('Administrator', 1); + assert.entityCount('AdministratorAdminPlugin', 1); + + // create revoke event and handle it let revokedEvent = createRevokedEvent( + EXECUTE_PROPOSAL_PERMISSION_ID, DAO_ADDRESS, - adminEntityId, pluginEntityId, - AdminPermission + administratorEntityId ); - handleRevoked(revokedEvent); + // when revoking the permission the admin is not removed, only the mapping with the admin plugin assert.entityCount('Administrator', 1); - // assert.notInStore('AdministratorAdminPlugin', administratorAdminPluginId); + assert.entityCount('AdministratorAdminPlugin', 0); + assert.notInStore('AdministratorAdminPlugin', administratorAdminPluginId); }); }); diff --git a/packages/subgraph/tests/plugin/plugin.test.ts b/packages/subgraph/tests/plugin/plugin.test.ts index 10df360a..c4a200ec 100644 --- a/packages/subgraph/tests/plugin/plugin.test.ts +++ b/packages/subgraph/tests/plugin/plugin.test.ts @@ -2,6 +2,7 @@ import {Action} from '../../generated/schema'; import { handleProposalExecuted, _handleProposalCreated, + handleMembershipContractAnnounced, } from '../../src/plugin/plugin'; import { ADDRESS_ONE, @@ -16,6 +17,7 @@ import { import { createNewProposalCreatedEvent, createProposalExecutedEvent, + createMembershipContractAnnouncedEvent, createAdminPluginState, createAdminProposalState, } from '../utils/events/plugin'; @@ -31,6 +33,7 @@ import { BigInt, DataSourceContext, } from '@graphprotocol/graph-ts'; +import {dataSource} from '@graphprotocol/graph-ts'; import { assert, afterEach, @@ -66,6 +69,10 @@ describe('Plugin', () => { describe('handleProposalCreated', () => { test('test the event', () => { + // check the entities are not in the store + assert.entityCount('AdminProposal', 0); + assert.entityCount('Action', 0); + // create state createAdminPluginState(pluginEntityId); @@ -89,7 +96,8 @@ describe('Plugin', () => { BigInt.fromString(PLUGIN_PROPOSAL_ID) ); - // checks + // checks proposal + assert.entityCount('AdminProposal', 1); assert.fieldEquals( 'AdminProposal', proposalEntityId, @@ -158,6 +166,7 @@ describe('Plugin', () => { ); // check actions + assert.entityCount('Action', 1); const actionEntityId = generateActionEntityId(proposalEntityId, 0); const actionEntity = Action.load(actionEntityId); if (actionEntity) { @@ -173,8 +182,6 @@ describe('Plugin', () => { proposalEntityId ); } - - clearStore(); }); }); @@ -196,6 +203,8 @@ describe('Plugin', () => { action.proposal = proposalEntityId; action.save(); + assert.entityCount('AdminProposal', 1); + // create event let event = createProposalExecutedEvent( PLUGIN_PROPOSAL_ID, @@ -219,8 +228,29 @@ describe('Plugin', () => { 'executionTxHash', event.transaction.hash.toHexString() ); + }); + }); + + describe('handleMembershipContractAnnounced', () => { + test('test the event', () => { + let context = dataSource.context(); + + assert.dataSourceCount('AdminMembers', 0); + assert.assertNull(context.get('pluginAddress')); + assert.assertNull(context.get('permissionId')); + + // create event + let event = createMembershipContractAnnouncedEvent( + DAO_ADDRESS, + pluginAddress + ); + + // handle event + handleMembershipContractAnnounced(event); - clearStore(); + // check + assert.dataSourceCount('AdminMembers', 1); + assert.dataSourceExists('AdminMembers', DAO_ADDRESS); }); }); }); diff --git a/packages/subgraph/tests/utils/events/plugin.ts b/packages/subgraph/tests/utils/events/plugin.ts index 58d2b75a..2dba9f4d 100644 --- a/packages/subgraph/tests/utils/events/plugin.ts +++ b/packages/subgraph/tests/utils/events/plugin.ts @@ -6,11 +6,10 @@ import { import { ProposalCreated, ProposalExecuted, + MembershipContractAnnounced, } from '../../../generated/templates/Plugin/Admin'; import { ADDRESS_ZERO, - ADDRESS_ONE, - ADDRESS_TWO, STRING_DATA, PLUGIN_PROPOSAL_ID, START_DATE, @@ -18,7 +17,6 @@ import { CONTRACT_ADDRESS, DAO_ADDRESS, } from '../constants'; -import {AdminMembers} from './../../../generated/templates'; import {Address, BigInt, Bytes, ethereum} from '@graphprotocol/graph-ts'; import {newMockEvent} from 'matchstick-as'; @@ -99,10 +97,10 @@ export function createProposalExecutedEvent( } export function createGrantedEvent( + permissionId: string, dao: string, plugin: string, - member: string, - permissionId: string + member: string ): Granted { let newGrantedEvent = changetype(newMockEvent()); @@ -140,10 +138,10 @@ export function createGrantedEvent( } export function createRevokedEvent( + permissionId: string, dao: string, plugin: string, - member: string, - permissionId: string + member: string ): Revoked { let newRevokedEvent = changetype(newMockEvent()); @@ -175,6 +173,26 @@ export function createRevokedEvent( return newRevokedEvent; } +export function createMembershipContractAnnouncedEvent( + definingContract: string, + contractAddress: Address +): MembershipContractAnnounced { + let newMembershipContractAnnouncedEvent = + changetype(newMockEvent()); + + newMembershipContractAnnouncedEvent.address = contractAddress; + newMembershipContractAnnouncedEvent.parameters = []; + + let definingContractParam = new ethereum.EventParam( + 'definingContract', + ethereum.Value.fromAddress(Address.fromString(definingContract)) + ); + + newMembershipContractAnnouncedEvent.parameters.push(definingContractParam); + + return newMembershipContractAnnouncedEvent; +} + // state export function createAdminPluginState( diff --git a/packages/subgraph/yarn.lock b/packages/subgraph/yarn.lock index 7d11039e..d97d0e2c 100644 --- a/packages/subgraph/yarn.lock +++ b/packages/subgraph/yarn.lock @@ -2702,10 +2702,10 @@ make-error@^1.1.1: resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== -matchstick-as@^0.5.2: - version "0.5.2" - resolved "https://registry.yarnpkg.com/matchstick-as/-/matchstick-as-0.5.2.tgz#6a6dde02d1d939c32458bd67bac688891a07a34c" - integrity sha512-fb1OVphDKEvJY06Ue02Eh1CNncuW95vp6b8tNAP7UIqplICSLoU/zgN6U7ge7R0upsoO78C7CRi4EyK/7Jxz7g== +matchstick-as@^0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/matchstick-as/-/matchstick-as-0.6.0.tgz#c65296b1f51b1014d605c52067d9b5321ea630e8" + integrity sha512-E36fWsC1AbCkBFt05VsDDRoFvGSdcZg6oZJrtIe/YDBbuFh8SKbR5FcoqDhNWqSN+F7bN/iS2u8Md0SM+4pUpw== dependencies: wabt "1.0.24"