Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/UM-…
Browse files Browse the repository at this point in the history
…6719-erc1155-offers
  • Loading branch information
orionstardust committed Aug 23, 2024
2 parents 96d2b79 + 639ebf4 commit a18dafb
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 97 deletions.
8 changes: 4 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ type RolesRegistry @entity {
}

type Role @entity {
id: ID! # rolesRegistryAddress + commitmentId + roleHash
id: ID! # rolesRegistryAddress + depositId + roleHash
rolesRegistry: RolesRegistry!
roleHash: Bytes!
tokenAddress: String!
Expand All @@ -576,7 +576,7 @@ type Role @entity {
}

type RoleAssignment @entity {
id: ID! # rolesRegistryAddress + commitmentId + granteeAddress + roleHash
id: ID! # rolesRegistryAddress + depositId + granteeAddress + roleHash
role: Role!
tokenAddress: String!
tokenId: BigInt!
Expand All @@ -591,9 +591,9 @@ type RoleAssignment @entity {
}

type TokenCommitment @entity {
id: ID! # rolesRegistryAddress + commitmentId
id: ID! # rolesRegistryAddress + depositId
rolesRegistry: RolesRegistry!
commitmentId: BigInt!
depositId: BigInt!
grantor: User!
tokenAddress: String!
tokenId: BigInt!
Expand Down
16 changes: 8 additions & 8 deletions src/mappings/diamond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,21 @@ export function handleEquipDelegatedWearables(event: EquipDelegatedWearables): v
)!;

if (gotchi.status.equals(STATUS_AAVEGOTCHI)) {
const oldCommitmentIds = event.params._oldCommitmentIds;
const newCommitmentIds = event.params._newCommitmentIds;
const oldDepositIds = event.params._oldCommitmentIds;
const newDepositIds = event.params._newCommitmentIds;

for(let i = 0; i < oldCommitmentIds.length; i++) {
if(oldCommitmentIds[i] == newCommitmentIds[i]) continue
if(oldCommitmentIds[i] != BigInt.zero()) {
const oldTokenCommitment = TokenCommitment.load(generateTokenCommitmentId(event.address.toHexString(), oldCommitmentIds[i]));
for(let i = 0; i < oldDepositIds.length; i++) {
if(oldDepositIds[i] == newDepositIds[i]) continue
if(oldDepositIds[i] != BigInt.zero()) {
const oldTokenCommitment = TokenCommitment.load(generateTokenCommitmentId(event.address.toHexString(), oldDepositIds[i]));
if(oldTokenCommitment) {
oldTokenCommitment.usedBalance = oldTokenCommitment.usedBalance.minus(BigInt.fromI32(1));
oldTokenCommitment.save();
}
}

if(newCommitmentIds[i] != BigInt.zero()) {
const newTokenCommitment = TokenCommitment.load(generateTokenCommitmentId(event.address.toHexString(), newCommitmentIds[i]));
if(newDepositIds[i] != BigInt.zero()) {
const newTokenCommitment = TokenCommitment.load(generateTokenCommitmentId(event.address.toHexString(), newDepositIds[i]));
if(newTokenCommitment) {
newTokenCommitment.usedBalance = newTokenCommitment.usedBalance.plus(BigInt.fromI32(1));
newTokenCommitment.save();
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/erc-7589/role-granted-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Example:
);
*/
export function handleRoleGranted(event: RoleGranted): void {
const commitmentId = event.params._commitmentId
const depositId = event.params._commitmentId
const rolesRegistryAddress = event.address.toHexString()
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, commitmentId)
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
const tokenCommitment = TokenCommitment.load(tokenCommitmentId)

if (!tokenCommitment) {
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/erc-7589/role-revoked-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Example:
event RoleRevoked(uint256 indexed _commitmentId, bytes32 indexed _role, address indexed _grantee)
*/
export function handleRoleRevoked(event: RoleRevoked): void {
const commitmentId = event.params._commitmentId
const depositId = event.params._commitmentId
const rolesRegistryAddress = event.address.toHexString()
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, commitmentId)
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
const tokenCommitment = TokenCommitment.load(tokenCommitmentId)

if (!tokenCommitment) {
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/erc-7589/tokens-committed-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function handleTokensCommitted(event: TokensCommitted): void {

const tokenCommitment = new TokenCommitment(tokenCommitmentId)
tokenCommitment.rolesRegistry = rolesRegistry.id
tokenCommitment.commitmentId = event.params._commitmentId
tokenCommitment.depositId = event.params._commitmentId
tokenCommitment.grantor = grantor.id
tokenCommitment.tokenAddress = tokenAddress
tokenCommitment.tokenId = tokenId
Expand Down
14 changes: 8 additions & 6 deletions src/utils/helpers/erc-7589/role-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { findOrCreateRole } from './role'
* @param rolesRegistry The roles registry used for the role assignment.
* @param grantee The grantee of the role assignment.
* @param roleHash The role hash of the role assignment.
* @param tokenCommitmentId The token commitment id of the role assignment.(only for ERC1155)
* @param depositId The token commitment id of the role assignment.(only for ERC1155)
* @returns The role assignment id.
*/
export function generateRoleAssignmentId(
rolesRegistry: RolesRegistry,
grantee: User,
roleHash: Bytes,
tokenCommitmentId: string,
depositId: string,
): string {
return `${rolesRegistry.id}-${tokenCommitmentId}-${grantee.id}-${roleHash.toHex()}`
return `${rolesRegistry.id}-${depositId}-${grantee.id}-${roleHash.toHex()}`
}

/**
Expand All @@ -28,12 +28,13 @@ export function generateRoleAssignmentId(
* @param rolesRegistryAddress The roles registry address of 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 tokenAddress The tokenAddress of the role assignment.
* @param tokenId The tokenAddress of the role assignment.
* @param timestamp The timestamp of the role assignment.
* @param expirationDate The expiration date of the role assignment.
* @param data The data of the role assignment.
* @param revocable The revocable of the role assignment.
* @param tokenCommitment The tokens commitment of the role assignment.(only for ERC1155)
* @param tokenCommitmentId The tokens commitment of the role assignment.(only for ERC1155)
* @returns The role assignment entity created (or found).
*/
export function upsertRoleAssignment(
Expand Down Expand Up @@ -77,7 +78,8 @@ export function upsertRoleAssignment(
* @notice Update a role assignment expiration date.
* @dev roleRegistry, grantor, grantee, nft should be created/exist before calling this function.
* @param rolesRegistry The roles registry used for the role assignment.
* @param nft The nft of the role assignment.
* @param tokenAddress The tokenAddress of the role assignment.
* @param tokenId The tokenId of the role assignment.
* @param roleHash The role hash of the role assignment.
* @param roleAssignmentId The role assignment id of the role assignment.
* @param blockTimestamp The block timestamp of the role assignment.
Expand Down
18 changes: 9 additions & 9 deletions src/utils/helpers/erc-7589/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { Role, RolesRegistry } from '../../../../generated/schema'
* @dev rolesRegistry, nft, roleHash should be created/exist before calling this function.
* @param rolesRegistry The roles registry used for the role.
* @param roleHash The role hash of the role.
* @param commitmentId The commitment id of the role.(only for ERC1155)
* @param depositId The depositId of the role (only for ERC1155).
* @returns The role id.
*/
export function generateRoleId(
rolesRegistry: RolesRegistry,
roleHash: Bytes,
tokenCommitmentId: string,
depositId: string,
): string {
return `${rolesRegistry.id}-${tokenCommitmentId}-${roleHash.toHex()}`
return `${rolesRegistry.id}-${depositId}-${roleHash.toHex()}`
}

/**
Expand All @@ -32,9 +32,9 @@ export function findOrCreateRole(
tokenAddress: string,
tokenId: BigInt,
roleHash: Bytes,
tokenCommitmentId: string,
depositId: string,
): Role {
const roleId = generateRoleId(rolesRegistry, roleHash, tokenCommitmentId)
const roleId = generateRoleId(rolesRegistry, roleHash, depositId)
let role = Role.load(roleId)

if (!role) {
Expand All @@ -43,7 +43,7 @@ export function findOrCreateRole(
role.tokenAddress = tokenAddress
role.tokenId = tokenId
role.rolesRegistry = rolesRegistry.id
role.tokenCommitment = tokenCommitmentId
role.tokenCommitment = depositId
role.save()
}

Expand All @@ -56,20 +56,20 @@ export function findOrCreateRole(
* @param rolesRegistry The roles registry used for the role.
* @param nft The nft of the role.
* @param roleHashes The array of role hashes of the role.
* @param commitmentId The commitment id of the role.(only for ERC1155)
* @param depositId The commitment id of the role.(only for ERC1155)
* @returns The role entity created (or found).
*/
export function findOrCreateRoles(
rolesRegistry: RolesRegistry,
tokenAddress: string,
tokenId: BigInt,
roleHashes: Bytes[],
tokenCommitmentId: string | null,
depositId: string | null,
): string[] {
const roles: string[] = []

for (let i = 0; i < roleHashes.length; i++) {
roles.push(findOrCreateRole(rolesRegistry, tokenAddress, tokenId, roleHashes[i], tokenCommitmentId).id)
roles.push(findOrCreateRole(rolesRegistry, tokenAddress, tokenId, roleHashes[i], depositId).id)
}

return roles
Expand Down
6 changes: 3 additions & 3 deletions src/utils/helpers/erc-7589/tokens-commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { BigInt } from '@graphprotocol/graph-ts'
/**
* @dev Generate tokens commitment id.
* @param rolesRegistryAddress The RoleRegistry contract.
* @param commitmentId The tokens commitment id.
* @param depositId The tokens commitment id.
* @returns The tokens commitment id.
*/
export function generateTokenCommitmentId(rolesRegistryAddress: string, commitmentId: BigInt): string {
return `${rolesRegistryAddress}-${commitmentId}`
export function generateTokenCommitmentId(rolesRegistryAddress: string, depositId: BigInt): string {
return `${rolesRegistryAddress}-${depositId}`
}
12 changes: 6 additions & 6 deletions tests/erc-7589/equip-delegated-wearables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Aavegotchi } from '../../generated/schema'
import { getAavegotchiMock } from '../mocks'

const tokenId = BigInt.fromI32(123)
const commitmentId = BigInt.fromI32(1)
const depositId = BigInt.fromI32(1)
const address = Address.fromString(
"0x1AD3d72e54Fb0eB46e87F82f77B284FC8a66b16C"
);
Expand All @@ -21,12 +21,12 @@ describe('EquipDelegatedWearables Handler', () => {
test('Should create a update equipDelegatedWearables in aavegotchi entity', () => {
assert.entityCount('TokenCommitment', 0)

const oldCommitmentIds = new Array<BigInt>(16).fill(BigInt.zero())
const newCommitmentIds = new Array<BigInt>(16).fill(BigInt.zero())
newCommitmentIds[0] = commitmentId
const oldDepositIds = new Array<BigInt>(16).fill(BigInt.zero())
const newDepositIds = new Array<BigInt>(16).fill(BigInt.zero())
newDepositIds[0] = depositId
const equippedDelegatedWearables = new Array<i32>(16).fill(0)

const event = createNewEquipDelegatedWearablesEvent(tokenId, oldCommitmentIds, newCommitmentIds)
const event = createNewEquipDelegatedWearablesEvent(tokenId, oldDepositIds, newDepositIds)
const gotchi = new Aavegotchi(tokenId.toString());
gotchi.gotchiId = BigInt.fromString(tokenId.toString());
gotchi.withSetsRarityScore = BIGINT_ONE;
Expand Down Expand Up @@ -105,6 +105,6 @@ describe('EquipDelegatedWearables Handler', () => {
handleEquipDelegatedWearables(event)

assert.entityCount('Aavegotchi', 1)
assert.fieldEquals('Aavegotchi', gotchi.id, 'equippedDelegatedWearables', bigIntArraytoString(newCommitmentIds))
assert.fieldEquals('Aavegotchi', gotchi.id, 'equippedDelegatedWearables', bigIntArraytoString(newDepositIds))
})
})
22 changes: 11 additions & 11 deletions tests/erc-7589/grant-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const revocable = true
const data = Bytes.fromUTF8('0x1234567890')
const expirationDate = BigInt.fromI32(99999)
const rolesRegistry = ZERO_ADDRESS
const commitmentId = BigInt.fromI32(1)
const depositId = BigInt.fromI32(1)

describe('ERC-7589 RoleGranted Handler', () => {
beforeEach(() => {
createMockTokenCommitment(grantor, tokenAddress, tokenId, rolesRegistry, commitmentId, tokenAmount, false)
createMockTokenCommitment(grantor, tokenAddress, tokenId, rolesRegistry, depositId, tokenAmount, false)
})
afterEach(() => {
clearStore()
Expand All @@ -35,18 +35,18 @@ describe('ERC-7589 RoleGranted Handler', () => {

const event1 = createNewRoleGrantedEvent(
RoleAssignmentId,
commitmentId,
depositId,
Addresses[0],
expirationDate,
revocable,
data,
)
const tokenCommitment1 = TokenCommitment.load(generateTokenCommitmentId(event1.address.toHexString(), commitmentId))
const tokenCommitment1 = TokenCommitment.load(generateTokenCommitmentId(event1.address.toHexString(), depositId))
handleRoleGranted(event1)

const event2 = createNewRoleGrantedEvent(
RoleAssignmentId,
commitmentId.plus(BigInt.fromI32(1)),
depositId.plus(BigInt.fromI32(1)),
Addresses[1],
expirationDate,
revocable,
Expand All @@ -57,15 +57,15 @@ describe('ERC-7589 RoleGranted Handler', () => {
tokenAddress,
tokenId,
event2.address.toHexString(),
commitmentId.plus(BigInt.fromI32(1)),
depositId.plus(BigInt.fromI32(1)),
tokenAmount,
false,
)
handleRoleGranted(event2)

const event3 = createNewRoleGrantedEvent(
RoleAssignmentId,
commitmentId.plus(BigInt.fromI32(2)),
depositId.plus(BigInt.fromI32(2)),
Addresses[2],
expirationDate,
revocable,
Expand All @@ -76,7 +76,7 @@ describe('ERC-7589 RoleGranted Handler', () => {
tokenAddress,
tokenId,
event3.address.toHexString(),
commitmentId.plus(BigInt.fromI32(2)),
depositId.plus(BigInt.fromI32(2)),
tokenAmount,
false,
)
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('ERC-7589 RoleGranted Handler', () => {

const event1 = createNewRoleGrantedEvent(
RoleAssignmentId,
tokenCommitment1.commitmentId,
tokenCommitment1.depositId,
Addresses[0],
expirationDate,
revocable,
Expand All @@ -170,7 +170,7 @@ describe('ERC-7589 RoleGranted Handler', () => {
handleRoleGranted(event1)
const event2 = createNewRoleGrantedEvent(
RoleAssignmentId,
tokenCommitment2.commitmentId,
tokenCommitment2.depositId,
Addresses[1],
expirationDate,
revocable,
Expand All @@ -180,7 +180,7 @@ describe('ERC-7589 RoleGranted Handler', () => {
handleRoleGranted(event2)
const event3 = createNewRoleGrantedEvent(
RoleAssignmentId,
tokenCommitment3.commitmentId,
tokenCommitment3.depositId,
Addresses[2],
expirationDate,
revocable,
Expand Down
6 changes: 3 additions & 3 deletions tests/erc-7589/helpers/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ export function validateRole(
}

export function validateTokenCommitment(
commitmentId: BigInt,
depositId: BigInt,
grantor: string,
tokenAddress: string,
tokenId: BigInt,
amount: BigInt,
rolesRegistryAddress: string,
isReleased: boolean,
): void {
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, commitmentId)
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'rolesRegistry', rolesRegistryAddress)
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'commitmentId', commitmentId.toString())
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'depositId', depositId.toString())
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'grantor', grantor)
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'tokenAddress', tokenAddress)
assert.fieldEquals('TokenCommitment', tokenCommitmentId, 'tokenId', tokenId.toString())
Expand Down
6 changes: 3 additions & 3 deletions tests/erc-7589/mocks/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export function createMockTokenCommitment(
tokenAddress: string,
tokenId: BigInt,
rolesRegistryAddress: string,
commitmentId: BigInt,
depositId: BigInt,
tokenAmount: BigInt,
isReleased: boolean,
): TokenCommitment {
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, commitmentId)
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
const tokenCommitment = new TokenCommitment(tokenCommitmentId)
const grantorUser = getOrCreateUser(grantor)
grantorUser.save()
Expand All @@ -71,7 +71,7 @@ export function createMockTokenCommitment(
tokenCommitment.amount = tokenAmount
tokenCommitment.usedBalance = BigInt.zero()
tokenCommitment.rolesRegistry = rolesRegistryAddress
tokenCommitment.commitmentId = commitmentId
tokenCommitment.depositId = depositId
tokenCommitment.isReleased = isReleased
tokenCommitment.save()

Expand Down
Loading

0 comments on commit a18dafb

Please sign in to comment.