-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ON-553: Add SftRolesResgistry role approval handler and role granted
- Loading branch information
Daniel Lima
committed
Dec 12, 2023
1 parent
3c2b883
commit 6f9bb76
Showing
14 changed files
with
812 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { handleRoleGranted } from './role-granted-handler' | ||
export { handleRoleApprovalForAll } from './role-approval-for-all-handler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { RoleApprovalForAll } from '../../generated/SftRolesRegistry/SftRolesRegistry' | ||
import { findOrCreateRoleApproval, findOrCreateAccount, findOrCreateRolesRegistry } from '../../utils' | ||
import { log } from '@graphprotocol/graph-ts' | ||
|
||
/** | ||
@dev This handler is called when a role approval for all is set. | ||
@param event RoleApprovalForAll The event emitted by the contract. | ||
Example: | ||
event RoleApprovalForAll(address indexed _tokenAddress, address indexed _operator, bool _isApproved); | ||
*/ | ||
export function handleRoleApprovalForAll(event: RoleApprovalForAll): void { | ||
const rolesRegistryAddress = event.address.toHex() | ||
const grantorAddress = event.transaction.from.toHex() | ||
const operatorAddress = event.params._operator.toHex() | ||
const tokenAddress = event.params._tokenAddress.toHex() | ||
const isApproved = event.params._isApproved | ||
|
||
const grantor = findOrCreateAccount(grantorAddress) | ||
const operator = findOrCreateAccount(operatorAddress) | ||
const rolesRegistry = findOrCreateRolesRegistry(rolesRegistryAddress) | ||
const roleApproval = findOrCreateRoleApproval(rolesRegistry, grantor, operator, tokenAddress) | ||
roleApproval.isApproved = isApproved | ||
roleApproval.save() | ||
|
||
log.warning('[sft-roles-registry][handleRoleApprovalForAll] Updated RoleAssignment Approval: {} Tx: {}', [ | ||
roleApproval.id, | ||
event.transaction.hash.toHex(), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { log } from '@graphprotocol/graph-ts' | ||
import { RoleGranted } from '../../generated/SftRolesRegistry/SftRolesRegistry' | ||
import { Nft } from '../../generated/schema' | ||
import { findOrCreateAccount, generateERC1155NftId, upsertERC1155RoleAssignment } from '../../utils' | ||
|
||
/** | ||
@dev This handler is called when a role is granted. | ||
@param event RoleGranted The event emitted by the contract. | ||
Example: | ||
event RoleGranted( | ||
bytes32 indexed _role, | ||
address indexed _tokenAddress, | ||
uint256 indexed _tokenId, | ||
address _grantor, | ||
address _grantee, | ||
uint64 _expirationDate, | ||
bool _revocable, | ||
bytes _data | ||
); | ||
*/ | ||
export function handleRoleGranted(event: RoleGranted): void { | ||
const tokenId = event.params._tokenId | ||
const tokenAddress = event.params._tokenAddress.toHex() | ||
const grantorAddress = event.params._grantor.toHex() | ||
const grantor = findOrCreateAccount(grantorAddress) | ||
const nonce = event.params._nonce | ||
|
||
const nftId = generateERC1155NftId(tokenAddress, tokenId, grantor.id) | ||
const nft = Nft.load(nftId) | ||
|
||
if (!nft) { | ||
log.error('[sft-roles-registry][handleRoleGranted] NFT {} does not exist, tx {} skipping...', [ | ||
nftId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const granteeAccount = findOrCreateAccount(event.params._grantee.toHex()) | ||
const roleAssignment = upsertERC1155RoleAssignment(event, grantor, granteeAccount, nft, nonce) | ||
log.warning('[sft-roles-registry][handleRoleGranted] roleAssignment: {} NFT: {} Tx: {}', [ | ||
roleAssignment.id, | ||
nftId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
utils/entities/role-assignment/erc-1155-role-assignment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { BigInt, Bytes } from '@graphprotocol/graph-ts' | ||
import { Account, Nft, RoleAssignment, RolesRegistry } from '../../../generated/schema' | ||
import { RoleGranted } from '../../../generated/SftRolesRegistry/SftRolesRegistry' | ||
import { findOrCreateRolesRegistry } from '../roles-registry' | ||
import { findOrCreateRole } from '../role' | ||
|
||
/** | ||
* @notice Generate a role assignment id. | ||
* @dev roleRegistry, grantor, grantee, nft should be created/exist before calling this function. | ||
* @param roleRegistry The roles registry used for the role assignment. | ||
* @param grantor The grantor of the role assignment. | ||
* @param grantee The grantee of the role assignment. | ||
* @param nft The nft of the role assignment. | ||
* @param roleHash The role hash of the role assignment. | ||
* @param nonce The nonce of the role assignment. | ||
* @returns The role assignment id. | ||
*/ | ||
export function generateERC1155RoleAssignmentId( | ||
roleRegistry: RolesRegistry, | ||
grantor: Account, | ||
grantee: Account, | ||
nft: Nft, | ||
roleHash: Bytes, | ||
nonce: BigInt, | ||
): string { | ||
return ( | ||
roleRegistry.id + | ||
'-' + | ||
grantor.id + | ||
'-' + | ||
grantee.id + | ||
'-' + | ||
nft.id + | ||
'-' + | ||
roleHash.toHex() + | ||
'-' + | ||
nonce.toString() | ||
) | ||
} | ||
|
||
/** | ||
* @notice Upsert a role assignment. | ||
* @dev roleRegistry, grantor, grantee, nft should be created/exist before calling this function. | ||
* @param event The role granted event. | ||
* @param grantor The grantor of the role assignment. | ||
* @param grantee The grantee of the role assignment. | ||
* @param nft The nft of the role assignment. | ||
* @param nonce The nonce of the role assignment. | ||
* @returns The role assignment entity created (or found). | ||
*/ | ||
export function upsertERC1155RoleAssignment( | ||
event: RoleGranted, | ||
grantor: Account, | ||
grantee: Account, | ||
nft: Nft, | ||
nonce: BigInt, | ||
): RoleAssignment { | ||
const rolesRegistry = findOrCreateRolesRegistry(event.address.toHex()) | ||
const roleAssignmentId = generateERC1155RoleAssignmentId( | ||
rolesRegistry, | ||
grantor, | ||
grantee, | ||
nft, | ||
event.params._role, | ||
nonce, | ||
) | ||
let roleAssignment = RoleAssignment.load(roleAssignmentId) | ||
const role = findOrCreateRole(rolesRegistry, nft, event.params._role) | ||
|
||
if (!roleAssignment) { | ||
roleAssignment = new RoleAssignment(roleAssignmentId) | ||
roleAssignment.role = role.id | ||
roleAssignment.nft = nft.id | ||
roleAssignment.grantor = grantor.id | ||
roleAssignment.grantee = grantee.id | ||
roleAssignment.createdAt = event.block.timestamp | ||
roleAssignment.nonce = nonce | ||
roleAssignment.tokenAmount = event.params._tokenAmount | ||
} | ||
|
||
roleAssignment.expirationDate = event.params._expirationDate | ||
roleAssignment.revocable = event.params._revocable | ||
roleAssignment.data = event.params._data | ||
roleAssignment.updatedAt = event.block.timestamp | ||
|
||
roleAssignment.save() | ||
return roleAssignment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './erc-721-role-assignment' | ||
export * from './erc-1155-role-assignment' |