-
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.
- Loading branch information
Daniel Lima
committed
Dec 12, 2023
1 parent
9fca751
commit 785fe0b
Showing
43 changed files
with
1,633 additions
and
363 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
File renamed without changes.
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 @@ | ||
export { handleTransfer } from './transfer-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,28 @@ | ||
import { Transfer } from '../../generated/ERC721/ERC721' | ||
import { upsertERC721Nft, upsertHelperNftOwnership } from '../../utils' | ||
import { log } from '@graphprotocol/graph-ts' | ||
|
||
/** | ||
@dev This handler is called when a token is transferred. | ||
@param event Transfer The event emitted by the contract. | ||
Example: | ||
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); | ||
*/ | ||
export function handleTransfer(event: Transfer): void { | ||
const tokenAddress = event.address.toHex() | ||
const tokenId = event.params.tokenId | ||
const from = event.params.from.toHex() | ||
const to = event.params.to.toHex() | ||
|
||
const nft = upsertERC721Nft(tokenAddress, tokenId, to) | ||
upsertHelperNftOwnership(nft, from) | ||
upsertHelperNftOwnership(nft, to) | ||
|
||
log.warning('[erc-721][handleTransfer] NFT {} transferred from {} to {} tx {}', [ | ||
tokenId.toString(), | ||
from, | ||
to, | ||
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,3 @@ | ||
export { handleRoleGranted } from './role-granted-handler' | ||
export { handleRoleRevoked } from './role-revoked-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/ERC7432/ERC7432' | ||
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('[erc-7432][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,61 @@ | ||
import { log } from '@graphprotocol/graph-ts' | ||
import { RoleGranted } from '../../generated/ERC7432/ERC7432' | ||
import { Account, Nft } from '../../generated/schema' | ||
import { generateERC721NftId, findOrCreateAccount, upsertRoleAssignment } 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 nftId = generateERC721NftId(tokenAddress, tokenId) | ||
const nft = Nft.load(nftId) | ||
if (!nft) { | ||
log.error('[erc-7432][handleRoleGranted] NFT {} does not exist, tx {} skipping...', [ | ||
nftId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const grantorAddress = event.params._grantor.toHex() | ||
const grantorAccount = Account.load(grantorAddress) | ||
if (!grantorAccount) { | ||
log.error('[erc-7432][handleRoleGranted] grantor {} does not exist, tx {} skipping...', [ | ||
grantorAddress, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
if (grantorAccount.id != nft.owner) { | ||
log.error('[erc-7432][handleRoleGranted] NFT {} is not owned by {}, tx {} skipping...', [ | ||
nftId, | ||
grantorAccount.id, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const granteeAccount = findOrCreateAccount(event.params._grantee.toHex()) | ||
const roleAssignment = upsertRoleAssignment(event, grantorAccount, granteeAccount, nft) | ||
log.warning('[erc-7432][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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { BigInt, log } from '@graphprotocol/graph-ts' | ||
import { RoleRevoked } from '../../generated/ERC7432/ERC7432' | ||
import { Account, Nft, RoleAssignment } from '../../generated/schema' | ||
import { findOrCreateRole, findOrCreateRolesRegistry, generateERC721NftId, generateRoleAssignmentId } from '../../utils' | ||
|
||
/** | ||
@dev This handler is called when a role is revoked. | ||
@param event RoleRevoked The event emitted by the contract. | ||
Example: | ||
event RoleRevoked( | ||
bytes32 indexed _role, | ||
address indexed _tokenAddress, | ||
uint256 indexed _tokenId, | ||
address _revoker, | ||
address _grantee | ||
); | ||
*/ | ||
export function handleRoleRevoked(event: RoleRevoked): void { | ||
const tokenId = event.params._tokenId | ||
const tokenAddress = event.params._tokenAddress.toHexString() | ||
|
||
const nftId = generateERC721NftId(tokenAddress, tokenId) | ||
const nft = Nft.load(nftId) | ||
if (!nft) { | ||
log.error('[erc-7432][handleRoleRevoked] NFT {} does not exist, tx {} skipping...', [ | ||
nftId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const revokerAddress = event.params._revoker.toHex() | ||
const revoker = Account.load(revokerAddress) | ||
if (!revoker) { | ||
log.error('[erc-7432][handleRoleGranted] revoker {} does not exist, tx {} skipping...', [ | ||
revokerAddress, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const granteeAddress = event.params._grantee.toHex() | ||
const grantee = Account.load(granteeAddress) | ||
if (!grantee) { | ||
log.error('[erc-7432][handleRoleGranted] grantee {} does not exist, tx {} skipping...', [ | ||
granteeAddress, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
const rolesRegistry = findOrCreateRolesRegistry(event.address.toHex()) | ||
const roleAssignmentId = generateRoleAssignmentId(rolesRegistry, revoker, grantee, nft, event.params._role) | ||
const roleAssignment = RoleAssignment.load(roleAssignmentId) | ||
if (!roleAssignment) { | ||
log.error('[erc-7432][handleRoleRevoked] RoleAssignment {} does not exist, tx {} skipping...', [ | ||
roleAssignmentId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
if (event.block.timestamp > roleAssignment.expirationDate) { | ||
log.error('[erc-7432][handleRoleRevoked] RoleAssignment {} already expired, tx {} skipping...', [ | ||
roleAssignmentId, | ||
event.transaction.hash.toHex(), | ||
]) | ||
return | ||
} | ||
|
||
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, | ||
event.transaction.hash.toHex(), | ||
]) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { handleRoleGranted } from './role-granted-handler' | ||
export { handleRoleRevoked } from './role-revoked-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('[sftRolesRegistry][handleRoleApprovalForAll] Updated RoleAssignment Approval: {} Tx: {}', [ | ||
roleApproval.id, | ||
event.transaction.hash.toHex(), | ||
]) | ||
} |
6 changes: 3 additions & 3 deletions
6
src/erc7432/role/grant-handler.ts → src/sftRolesRegistry/role-granted-handler.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
Oops, something went wrong.