diff --git a/schema.graphql b/schema.graphql index ac418f1..4cbbcd6 100644 --- a/schema.graphql +++ b/schema.graphql @@ -39,7 +39,6 @@ type Role @entity { rolesRegistry: RolesRegistry! roleHash: Bytes! nft: Nft! - lastNonRevocableExpirationDate: BigInt! roleAssignments: [RoleAssignment!] @derivedFrom(field: "role") } @@ -56,13 +55,4 @@ type RolesRegistry @entity { id: ID! # contractAddress roles: [Role!] @derivedFrom(field: "rolesRegistry") roleApprovals: [RoleApproval!] @derivedFrom(field: "rolesRegistry") -} - -# Helper Entities - -type HelperNftOwnership @entity { - id: ID! # tokenAddress + tokenId + owner - nft: Nft! - originalOwner: Account! - isSameOwner: Boolean! } \ No newline at end of file diff --git a/src/erc-721/transfer-handler.ts b/src/erc-721/transfer-handler.ts index 9544f63..239e3fb 100644 --- a/src/erc-721/transfer-handler.ts +++ b/src/erc-721/transfer-handler.ts @@ -1,5 +1,5 @@ import { Transfer } from '../../generated/ERC721/ERC721' -import { upsertERC721Nft, upsertHelperNftOwnership } from '../../utils' +import { upsertERC721Nft } from '../../utils' import { log } from '@graphprotocol/graph-ts' /** @@ -15,9 +15,7 @@ export function handleTransfer(event: Transfer): void { const from = event.params.from.toHex() const to = event.params.to.toHex() - const nft = upsertERC721Nft(tokenAddress, tokenId, to) - upsertHelperNftOwnership(nft, from) - upsertHelperNftOwnership(nft, to) + upsertERC721Nft(tokenAddress, tokenId, to) log.warning('[erc-721][handleTransfer] NFT {} transferred from {} to {} tx {}', [ tokenId.toString(), diff --git a/src/erc-7432/role-revoked-handler.ts b/src/erc-7432/role-revoked-handler.ts index 20d4328..1017e09 100644 --- a/src/erc-7432/role-revoked-handler.ts +++ b/src/erc-7432/role-revoked-handler.ts @@ -71,14 +71,6 @@ export function handleRoleRevoked(event: RoleRevoked): void { roleAssignment.expirationDate = event.block.timestamp roleAssignment.save() - if (!roleAssignment.revocable) { - // smart contract validate that if a role is not revocable, it can only be revoked by the grantee - // in that case, we can set the lastNonRevocableExpirationDate to zero, assuming that the grantee is revoking its own role - const role = findOrCreateRole(rolesRegistry, nft, event.params._role) - role.lastNonRevocableExpirationDate = BigInt.zero() - role.save() - } - log.warning('[[erc-7432]handleRoleRevoked] Revoked RoleAssignment: {} NFT: {} tx: {}', [ roleAssignmentId, nftId, diff --git a/tests/erc-721/transfer-handler.test.ts b/tests/erc-721/transfer-handler.test.ts index 65e56bc..7cb8ee6 100644 --- a/tests/erc-721/transfer-handler.test.ts +++ b/tests/erc-721/transfer-handler.test.ts @@ -1,19 +1,11 @@ -import { assert, describe, test, clearStore, afterAll, beforeEach, afterEach } from 'matchstick-as' +import { assert, describe, test, clearStore, afterAll } from 'matchstick-as' import { BigInt } from '@graphprotocol/graph-ts' import { handleTransfer } from '../../src/erc-721' -import { NftType, generateERC721NftId, generateHelperNftOwnershipId, upsertHelperNftOwnership } from '../../utils' +import { generateERC721NftId } from '../../utils' import { createTransferEvent } from '../mocks/events' import { Addresses, ZERO_ADDRESS } from '../helpers/contants' -import { MockAddresses } from '../mocks/values' -import { Nft } from '../../generated/schema' -const originalOwnerAddress = MockAddresses[0] -const newOwnerAddress = MockAddresses[1] -const tokenAddress = MockAddresses[2] const tokenId = BigInt.fromI32(1) -const nftId = generateERC721NftId(tokenAddress, tokenId) - -let nft: Nft describe('ERC-721 Transfer Handler', () => { afterAll(() => { @@ -68,36 +60,3 @@ describe('ERC-721 Transfer Handler', () => { assert.fieldEquals('Nft', _id, 'owner', Addresses[2]) }) }) - -describe('HelperNftOwnership', () => { - beforeEach(() => { - nft = new Nft(nftId) - nft.tokenAddress = tokenAddress - nft.tokenId = tokenId - nft.owner = originalOwnerAddress - nft.type = NftType.ERC721 - nft.save() - }) - - test('Should change "isSameOwner" to false when NFT is transferred', () => { - const helperNftOwnership = upsertHelperNftOwnership(nft, nft.owner) - assert.entityCount('HelperNftOwnership', 1) - assert.fieldEquals('HelperNftOwnership', helperNftOwnership.id, 'isSameOwner', 'true') - - const event = createTransferEvent(originalOwnerAddress, newOwnerAddress, tokenId.toString(), tokenAddress) - handleTransfer(event) - - assert.entityCount('HelperNftOwnership', 2) - assert.fieldEquals('HelperNftOwnership', helperNftOwnership.id, 'isSameOwner', 'false') - assert.fieldEquals( - 'HelperNftOwnership', - generateHelperNftOwnershipId(tokenAddress, tokenId, newOwnerAddress), - 'isSameOwner', - 'true', - ) - }) - - afterEach(() => { - clearStore() - }) -}) diff --git a/tests/erc-7432/grant-handler.test.ts b/tests/erc-7432/grant-handler.test.ts index 601ee7d..41c071c 100644 --- a/tests/erc-7432/grant-handler.test.ts +++ b/tests/erc-7432/grant-handler.test.ts @@ -143,7 +143,6 @@ describe('ERC-7432 RoleGranted Handler', () => { expirationDate, data, event1.address.toHex(), - BigInt.zero(), ) validateRole( grantorAccount, @@ -153,7 +152,6 @@ describe('ERC-7432 RoleGranted Handler', () => { expirationDate, data, event2.address.toHex(), - BigInt.zero(), ) validateRole( grantorAccount, @@ -163,7 +161,6 @@ describe('ERC-7432 RoleGranted Handler', () => { expirationDate, data, event3.address.toHex(), - BigInt.zero(), ) }) @@ -218,66 +215,8 @@ describe('ERC-7432 RoleGranted Handler', () => { assert.entityCount('Account', 3) const grantorAccount = new Account(grantor) - validateRole( - grantorAccount, - new Account(Addresses[0]), - nft1, - RoleAssignmentId, - expirationDate, - data, - rolesRegistry, - BigInt.zero(), - ) - validateRole( - grantorAccount, - new Account(Addresses[1]), - nft2, - RoleAssignmentId, - expirationDate, - data, - rolesRegistry, - BigInt.zero(), - ) - validateRole( - grantorAccount, - new Account(Addresses[2]), - nft3, - RoleAssignmentId, - expirationDate, - data, - rolesRegistry, - BigInt.zero(), - ) - }) - - test('should update lastNonRevocableExpirationDate when revocable is false', () => { - const nft = createMockNft(tokenAddress, tokenId, grantor) - assert.entityCount('RoleAssignment', 0) - assert.entityCount('Role', 0) - assert.entityCount('Account', 1) - - const event1 = createNewRoleGrantedEvent( - RoleAssignmentId, - tokenId, - tokenAddress, - Addresses[0], - grantor, - expirationDate, - false, - data, - ) - handleRoleGranted(event1) - - const grantorAccount = new Account(grantor) - validateRole( - grantorAccount, - new Account(Addresses[0]), - nft, - RoleAssignmentId, - expirationDate, - data, - event1.address.toHex(), - expirationDate, - ) + validateRole(grantorAccount, new Account(Addresses[0]), nft1, RoleAssignmentId, expirationDate, data, rolesRegistry) + validateRole(grantorAccount, new Account(Addresses[1]), nft2, RoleAssignmentId, expirationDate, data, rolesRegistry) + validateRole(grantorAccount, new Account(Addresses[2]), nft3, RoleAssignmentId, expirationDate, data, rolesRegistry) }) }) diff --git a/tests/erc-7432/revoke-handler.test.ts b/tests/erc-7432/revoke-handler.test.ts index fac323e..376fcce 100644 --- a/tests/erc-7432/revoke-handler.test.ts +++ b/tests/erc-7432/revoke-handler.test.ts @@ -4,7 +4,7 @@ import { handleRoleRevoked } from '../../src/erc-7432' import { Bytes, BigInt } from '@graphprotocol/graph-ts' import { createMockAccount, createMockNft, createMockRoleAssignment } from '../mocks/entities' import { Addresses, ONE, TWO, ZERO_ADDRESS } from '../helpers/contants' -import { findOrCreateRole, findOrCreateRolesRegistry, generateERC721NftId, generateRoleAssignmentId } from '../../utils' +import { findOrCreateRolesRegistry, generateERC721NftId, generateRoleAssignmentId } from '../../utils' import { Account, Nft } from '../../generated/schema' import { validateRole } from '../helpers/assertion' @@ -106,9 +106,9 @@ describe('ERC-7432 RoleRevoked Handler', () => { assert.entityCount('RoleAssignment', 3) assert.entityCount('Role', 1) const revokerAccount = new Account(revoker) - validateRole(revokerAccount, account1, nft, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) - validateRole(revokerAccount, account2, nft, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) - validateRole(revokerAccount, account3, nft, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) + validateRole(revokerAccount, account1, nft, RoleAssignmentId, ONE, data, rolesRegistry) + validateRole(revokerAccount, account2, nft, RoleAssignmentId, ONE, data, rolesRegistry) + validateRole(revokerAccount, account3, nft, RoleAssignmentId, ONE, data, rolesRegistry) }) test('should revoke multiple roles for different NFTs', () => { @@ -134,37 +134,8 @@ describe('ERC-7432 RoleRevoked Handler', () => { assert.entityCount('RoleAssignment', 3) assert.entityCount('Role', 3) const revokerAccount = new Account(revoker) - validateRole(revokerAccount, granteeAccount, nft1, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) - validateRole(revokerAccount, granteeAccount, nft2, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) - validateRole(revokerAccount, granteeAccount, nft3, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) - }) - - test('should update lastNonRevocableExpirationDate when grantee is revoking a non revocable role', () => { - const nft = createMockNft(tokenAddress, tokenId, revoker) - const granteeAccount = createMockAccount(grantee) - const roleAssignment = createMockRoleAssignment( - RoleAssignmentId, - revoker, - grantee, - nft, - expirationDate, - rolesRegistry, - ) - roleAssignment.revocable = false - roleAssignment.save() - const role = findOrCreateRole(findOrCreateRolesRegistry(rolesRegistry), nft, RoleAssignmentId) - role.lastNonRevocableExpirationDate = expirationDate - role.save() - assert.entityCount('RoleAssignment', 1) - assert.entityCount('Role', 1) - assert.fieldEquals('Role', role.id, 'lastNonRevocableExpirationDate', expirationDate.toString()) - - const event = createNewRoleRevokedEvent(RoleAssignmentId, nft, revoker, grantee) - handleRoleRevoked(event) - - assert.entityCount('RoleAssignment', 1) - assert.entityCount('Role', 1) - const revokerAccount = new Account(revoker) - validateRole(revokerAccount, granteeAccount, nft, RoleAssignmentId, ONE, data, rolesRegistry, BigInt.zero()) + validateRole(revokerAccount, granteeAccount, nft1, RoleAssignmentId, ONE, data, rolesRegistry) + validateRole(revokerAccount, granteeAccount, nft2, RoleAssignmentId, ONE, data, rolesRegistry) + validateRole(revokerAccount, granteeAccount, nft3, RoleAssignmentId, ONE, data, rolesRegistry) }) }) diff --git a/tests/helpers/assertion.ts b/tests/helpers/assertion.ts index aa4ea30..a753789 100644 --- a/tests/helpers/assertion.ts +++ b/tests/helpers/assertion.ts @@ -16,13 +16,11 @@ export function validateRole( expirationDate: BigInt, data: Bytes, rolesRegistryAddress: string, - lastNonRevocableExpirationDate: BigInt, ): void { const rolesRegistry = findOrCreateRolesRegistry(rolesRegistryAddress) const roleId = generateRoleId(rolesRegistry, nft, roleAssignment) assert.fieldEquals('Role', roleId, 'roleHash', roleAssignment.toHex()) assert.fieldEquals('Role', roleId, 'nft', nft.id) - assert.fieldEquals('Role', roleId, 'lastNonRevocableExpirationDate', lastNonRevocableExpirationDate.toString()) const roleAssignmentId = generateRoleAssignmentId(rolesRegistry, grantor, grantee, nft, roleAssignment) assert.fieldEquals( diff --git a/tests/mocks/entities.ts b/tests/mocks/entities.ts index 17515ea..d91141f 100644 --- a/tests/mocks/entities.ts +++ b/tests/mocks/entities.ts @@ -42,7 +42,6 @@ export function createMockRoleAssignment( role.roleHash = roleHash role.nft = nft.id role.rolesRegistry = rolesRegistryAddress - role.lastNonRevocableExpirationDate = BigInt.zero() role.save() const roleAssignmentId = generateRoleAssignmentId( diff --git a/utils/entities/helper-nft-ownership.ts b/utils/entities/helper-nft-ownership.ts deleted file mode 100644 index 8bd9cd7..0000000 --- a/utils/entities/helper-nft-ownership.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { HelperNftOwnership, Nft } from '../../generated/schema' -import { BigInt } from '@graphprotocol/graph-ts' - -/** - @notice Generate HelperNftOwnership id. - @dev The id of the HelperNftOwnership is generated using the token address, the token id, and the owner address. - * @param tokenAddress The token address of the ERC721 NFT. - * @param tokenId The token id of the ERC721 NFT. - * @param owner The owner of the ERC721 NFT. - */ -export function generateHelperNftOwnershipId(tokenAddress: string, tokenId: BigInt, owner: string): string { - return tokenAddress + tokenId.toString() + owner -} - -/** - * @notice Insert or update an HelperNftOwnership. - * @dev It updates the "isSameOwner" as a way to check if the owner is the same. - * @param nft The nft of the given entity. - * @param owner The owner to be checked. - */ -export function upsertHelperNftOwnership(nft: Nft, owner: string): HelperNftOwnership { - const helperNftOwnershipId = generateHelperNftOwnershipId(nft.tokenAddress, nft.tokenId, owner) - let helperNftOwnership = HelperNftOwnership.load(helperNftOwnershipId) - if (!helperNftOwnership) { - helperNftOwnership = new HelperNftOwnership(helperNftOwnershipId) - helperNftOwnership.nft = nft.id - helperNftOwnership.originalOwner = nft.owner - } - helperNftOwnership.isSameOwner = nft.owner == owner - helperNftOwnership.save() - return helperNftOwnership -} diff --git a/utils/entities/index.ts b/utils/entities/index.ts index f8c58a9..2de9953 100644 --- a/utils/entities/index.ts +++ b/utils/entities/index.ts @@ -4,4 +4,3 @@ export * from './role-assignment' export * from './role-approval' export * from './roles-registry' export * from './role' -export * from './helper-nft-ownership' diff --git a/utils/entities/role-assignment.ts b/utils/entities/role-assignment.ts index bbb5caa..75b5c0e 100644 --- a/utils/entities/role-assignment.ts +++ b/utils/entities/role-assignment.ts @@ -53,14 +53,6 @@ export function upsertRoleAssignment(event: RoleGranted, grantor: Account, grant roleAssignment.data = event.params._data roleAssignment.updatedAt = event.block.timestamp - if (!roleAssignment.revocable) { - // if the role is not revocable, we update the lastNonRevocableExpirationDate - // since the grantor will not able to revoke the role before this date - // this is useful for the revocation check - role.lastNonRevocableExpirationDate = event.params._expirationDate - role.save() - } - roleAssignment.save() return roleAssignment } diff --git a/utils/entities/role.ts b/utils/entities/role.ts index c75df10..0601fb7 100644 --- a/utils/entities/role.ts +++ b/utils/entities/role.ts @@ -30,7 +30,6 @@ export function findOrCreateRole(rolesRegistry: RolesRegistry, nft: Nft, roleHas role.roleHash = roleHash role.nft = nft.id role.rolesRegistry = rolesRegistry.id - role.lastNonRevocableExpirationDate = BigInt.zero() role.save() }