diff --git a/packages/subgraph/tests/dao/dao_v1_0_0.test.ts b/packages/subgraph/tests/dao/dao_v1_0_0.test.ts index 55c2b6506..4dd3bf59d 100644 --- a/packages/subgraph/tests/dao/dao_v1_0_0.test.ts +++ b/packages/subgraph/tests/dao/dao_v1_0_0.test.ts @@ -155,7 +155,7 @@ test('Run dao (handleNewURI) mappings with mock event', () => { dao.daoURI = newDAOURI; // Assert dao entity - dao.assertEntity(true); + dao.assertEntity(); clearStore(); }); diff --git a/packages/subgraph/tests/dao/permission-manager.test.ts b/packages/subgraph/tests/dao/permission-manager.test.ts index 1642f3f7a..d26bfe519 100644 --- a/packages/subgraph/tests/dao/permission-manager.test.ts +++ b/packages/subgraph/tests/dao/permission-manager.test.ts @@ -1,138 +1,46 @@ import {assert, clearStore, test} from 'matchstick-as/assembly/index'; -import {Address, ByteArray, Bytes, crypto} from '@graphprotocol/graph-ts'; +import {Address} from '@graphprotocol/graph-ts'; import {handleGranted, handleRevoked} from '../../src/dao/dao_v1_0_0'; -import {Permission} from '../../generated/schema'; -import { - DAO_ADDRESS, - ADDRESS_ONE, - CONTRACT_ADDRESS, - ADDRESS_TWO, - ADDRESS_THREE -} from '../constants'; -import { - createNewGrantedEvent, - createNewRevokedEvent, - getEXECUTE_PERMISSION_ID, - getEXECUTE_PERMISSION_IDreverted -} from './utils'; -const contractPermissionId = Bytes.fromByteArray( - crypto.keccak256(ByteArray.fromUTF8('EXECUTE_PERMISSION')) -); +import {DAO_ADDRESS} from '../constants'; +import {getEXECUTE_PERMISSION_ID} from './utils'; +import {ExtendedPermission} from '../helpers/extended-schema'; const daoId = Address.fromString(DAO_ADDRESS).toHexString(); -const where = Address.fromString(CONTRACT_ADDRESS); -const who = Address.fromString(ADDRESS_ONE); -const actor = Address.fromString(ADDRESS_TWO); -const conditionAddress = ADDRESS_THREE; test('Run dao (handleGranted) mappings with mock event', () => { - // create event and run it's handler - let grantedEvent = createNewGrantedEvent( - contractPermissionId, - actor.toHexString(), - where.toHexString(), - who.toHexString(), - conditionAddress, - daoId - ); - - // handle event - handleGranted(grantedEvent); - - // checks - let permissionEntityID = [ - daoId, - where.toHexString(), - contractPermissionId.toHexString(), - who.toHexString() - ].join('_'); - - assert.fieldEquals( - 'Permission', - permissionEntityID, - 'id', - permissionEntityID - ); - - clearStore(); -}); - -test('Run dao (handleGranted) mappings with reverted mocke call', () => { - // create event and run it's handler - let grantedEvent = createNewGrantedEvent( - contractPermissionId, - ADDRESS_ONE, - DAO_ADDRESS, - CONTRACT_ADDRESS, - ADDRESS_TWO, - DAO_ADDRESS - ); + let permission = new ExtendedPermission().withDefaultValues(daoId); + permission.dao = daoId; - // launch calls - getEXECUTE_PERMISSION_IDreverted(DAO_ADDRESS); + let grantedEvent = permission.createEvent_Granted(daoId); // handle event handleGranted(grantedEvent); // checks - - // governance - let daoPluginEntityID = - Address.fromString(DAO_ADDRESS).toHexString() + - '_' + - Address.fromString(CONTRACT_ADDRESS).toHexString(); - - assert.notInStore('DaoPlugin', daoPluginEntityID); + permission.assertEntity(); clearStore(); }); test('Run dao (handleRevoked) mappings with mock event', () => { - // permission - let permissionEntityID = [ - daoId, - where.toHexString(), - contractPermissionId.toHexString(), - who.toHexString() - ].join('_'); + let permission = new ExtendedPermission().withDefaultValues(daoId); + permission.dao = daoId; - let permissionEntity = new Permission(permissionEntityID); - permissionEntity.where = Address.fromString(CONTRACT_ADDRESS); - permissionEntity.permissionId = contractPermissionId; - permissionEntity.who = Address.fromString(ADDRESS_ONE); - permissionEntity.actor = Address.fromString(ADDRESS_ONE); - - permissionEntity.dao = Address.fromString(DAO_ADDRESS).toHexString(); - - permissionEntity.save(); + permission.save(); // check state exist - assert.fieldEquals( - 'Permission', - permissionEntityID, - 'id', - permissionEntityID - ); + permission.assertEntity(); // create event and run it's handler - let revokedEvent = createNewRevokedEvent( - contractPermissionId, - actor.toHexString(), - where.toHexString(), - who.toHexString(), - daoId - ); - - getEXECUTE_PERMISSION_ID(DAO_ADDRESS, contractPermissionId); + let revokedEvent = permission.createEvent_Revoked(daoId); // handle event handleRevoked(revokedEvent); // checks - - assert.notInStore('Permission', permissionEntityID); + assert.notInStore('Permission', permission.id); clearStore(); }); diff --git a/packages/subgraph/tests/helpers/method-classes.ts b/packages/subgraph/tests/helpers/method-classes.ts index 0c3eae48f..16b78dda5 100644 --- a/packages/subgraph/tests/helpers/method-classes.ts +++ b/packages/subgraph/tests/helpers/method-classes.ts @@ -3,7 +3,14 @@ * The classes of this file are meant to be incorporated into the classes of ./extended-schema.ts */ -import {Address, BigInt, Bytes, ethereum} from '@graphprotocol/graph-ts'; +import { + Address, + BigInt, + ByteArray, + Bytes, + crypto, + ethereum +} from '@graphprotocol/graph-ts'; import { Dao, ERC20Balance, @@ -23,13 +30,16 @@ import { TokenVotingPlugin, TokenVotingProposal, TokenVotingVote, - TokenVotingVoter + TokenVotingVoter, + Permission } from '../../generated/schema'; import { CallbackReceived, Deposited, + Granted, NativeTokenDeposited, - NewURI + NewURI, + Revoked } from '../../generated/templates/DaoTemplateV1_0_0/DAO'; import { DelegateChanged, @@ -77,12 +87,15 @@ import { STRING_DATA, ADDRESS_TWO, ADDRESS_THREE, - ADDRESS_ZERO + ADDRESS_ZERO, + ADDRESS_FOUR } from '../constants'; import { createCallbackReceivedEvent, createNewDepositedEvent, + createNewGrantedEvent, createNewNativeTokenDepositedEvent, + createNewRevokedEvent, createNewURIEvent, getBalanceOf, getSupportsInterface @@ -106,6 +119,76 @@ import { } from '../utils'; /* eslint-disable @typescript-eslint/no-unused-vars */ +// PermissionManager +class PermissionMethods extends Permission { + withDefaultValues( + emittingContract: string = Address.fromString( + CONTRACT_ADDRESS + ).toHexString() + ): PermissionMethods { + const permissionId = Bytes.fromByteArray( + crypto.keccak256(ByteArray.fromUTF8('EXECUTE_PERMISSION')) + ); + + const where = Address.fromString(ADDRESS_ONE); + const who = Address.fromString(ADDRESS_TWO); + const actor = Address.fromString(ADDRESS_THREE); + const condition = Address.fromString(ADDRESS_FOUR); + + this.id = [ + emittingContract, + where.toHexString(), + permissionId.toHexString(), + who.toHexString() + ].join('_'); + this.where = where; + this.permissionId = permissionId; + this.who = who; + this.actor = actor; + this.condition = condition; + + return this; + } + + // events + createEvent_Granted( + emittingContract: string = Address.fromString( + CONTRACT_ADDRESS + ).toHexString() + ): Granted { + if (this.condition === null) { + throw new Error('Condition is null'); + } + + let event = createNewGrantedEvent( + this.permissionId, + this.actor.toHexString(), + this.where.toHexString(), + this.who.toHexString(), + (this.condition as Bytes).toHexString(), + emittingContract + ); + + return event; + } + + createEvent_Revoked( + emittingContract: string = Address.fromString( + CONTRACT_ADDRESS + ).toHexString() + ): Revoked { + let event = createNewRevokedEvent( + this.permissionId, + this.actor.toHexString(), + this.where.toHexString(), + this.who.toHexString(), + emittingContract + ); + + return event; + } +} + // ERC1155Contract class ERC1155ContractMethods extends ERC1155Contract { withDefaultValues(): ERC1155ContractMethods {