Skip to content

Commit

Permalink
ON-553: refactor remove helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Dec 12, 2023
1 parent 19f4c14 commit 3c2b883
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 210 deletions.
10 changes: 0 additions & 10 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Role @entity {
rolesRegistry: RolesRegistry!
roleHash: Bytes!
nft: Nft!
lastNonRevocableExpirationDate: BigInt!
roleAssignments: [RoleAssignment!] @derivedFrom(field: "role")
}

Expand All @@ -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!
}
6 changes: 2 additions & 4 deletions src/erc-721/transfer-handler.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -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(),
Expand Down
8 changes: 0 additions & 8 deletions src/erc-7432/role-revoked-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 2 additions & 43 deletions tests/erc-721/transfer-handler.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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()
})
})
67 changes: 3 additions & 64 deletions tests/erc-7432/grant-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ describe('ERC-7432 RoleGranted Handler', () => {
expirationDate,
data,
event1.address.toHex(),
BigInt.zero(),
)
validateRole(
grantorAccount,
Expand All @@ -153,7 +152,6 @@ describe('ERC-7432 RoleGranted Handler', () => {
expirationDate,
data,
event2.address.toHex(),
BigInt.zero(),
)
validateRole(
grantorAccount,
Expand All @@ -163,7 +161,6 @@ describe('ERC-7432 RoleGranted Handler', () => {
expirationDate,
data,
event3.address.toHex(),
BigInt.zero(),
)
})

Expand Down Expand Up @@ -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)
})
})
43 changes: 7 additions & 36 deletions tests/erc-7432/revoke-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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)
})
})
2 changes: 0 additions & 2 deletions tests/helpers/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion tests/mocks/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 0 additions & 32 deletions utils/entities/helper-nft-ownership.ts

This file was deleted.

1 change: 0 additions & 1 deletion utils/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export * from './role-assignment'
export * from './role-approval'
export * from './roles-registry'
export * from './role'
export * from './helper-nft-ownership'
8 changes: 0 additions & 8 deletions utils/entities/role-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion utils/entities/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 3c2b883

Please sign in to comment.