Skip to content

Commit

Permalink
chore: refactor to support multichain
Browse files Browse the repository at this point in the history
  • Loading branch information
cinnabarhorse committed Dec 7, 2024
1 parent 33ba39e commit 3252b65
Show file tree
Hide file tree
Showing 15 changed files with 1,331 additions and 1,424 deletions.
46 changes: 8 additions & 38 deletions src/fetch/erc1155.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
import { Address, BigInt } from "@graphprotocol/graph-ts";

import {
User,
FakeGotchiCardBalance,
} from "../../generated/schema";

import { constants } from "@amxx/graphprotocol-utils";
import { BigInt } from "@graphprotocol/graph-ts";

export function replaceURI(uri: string, identifier: BigInt): string {
return uri.replaceAll(
"{id}",
identifier
.toHex()
.slice(2)
.padStart(64, "0")
);
}

export function fetchFakeGotchiCardBalance(
token: BigInt,
account: User | null,
contract: Address
): FakeGotchiCardBalance {
let id = token.toString()
.concat("/")
.concat(account ? account.id.toString() : "totalSupply");
let balance = FakeGotchiCardBalance.load(id);

if (balance == null) {
balance = new FakeGotchiCardBalance(id);
balance.contract = contract;
balance.token = token;
balance.account = account ? account.id : null;
balance.value = constants.BIGDECIMAL_ZERO;
balance.valueExact = constants.BIGINT_ZERO;
balance.save();
}

return balance as FakeGotchiCardBalance;
return uri.replaceAll(
"{id}",
identifier
.toHex()
.slice(2)
.padStart(64, "0")
);
}
55 changes: 0 additions & 55 deletions src/fetch/erc721.ts
Original file line number Diff line number Diff line change
@@ -1,55 +0,0 @@
import {
Address,
BigInt,
Bytes,
JSONValue,
TypedMap,
log
} from "@graphprotocol/graph-ts";

import {
User,
FakeGotchiNFTToken,
} from "../../generated/schema";

import { constants } from "@amxx/graphprotocol-utils";

import { IERC721 } from "../../generated/FAKEGotchisNFTDiamond/IERC721";
import { getOrCreateUser } from "../utils/helpers/diamond";

export function fetchFakeGotchiNFTToken(
contract: Address,
identifier: BigInt
): FakeGotchiNFTToken {
let id = contract
.toHex()
.concat("/")
.concat(identifier.toHex());
let token = FakeGotchiNFTToken.load(id);

if (token == null) {
token = new FakeGotchiNFTToken(id);
token.contract = contract;
token.identifier = identifier;
token.approval = getOrCreateUser(constants.ADDRESS_ZERO.toHex()).id;

let erc721 = IERC721.bind(Address.fromBytes(contract));
let try_tokenURI = erc721.try_tokenURI(identifier);
token.uri = try_tokenURI.reverted ? "" : try_tokenURI.value;
}


return token as FakeGotchiNFTToken;
}

export function getFakeGotchiNFTToken(
contract: Address,
identifier: BigInt
): FakeGotchiNFTToken | null {
let id = contract
.toHex()
.concat("/")
.concat(identifier.toHex());
let token = FakeGotchiNFTToken.load(id);
return token;
}
6 changes: 4 additions & 2 deletions src/mappings/diamond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import {
getOrCreateWearableSet,
getOrCreateERC1155Purchase,
updateERC1155PurchaseInfo,
getOrCreateParcel,
updateAavegotchiWearables,
calculateBaseRarityScore,
getOrCreateGotchiLending,
Expand All @@ -96,7 +95,10 @@ import {
getOrCreateERC721BuyOrder,
getOrCreateERC1155BuyOrder,
getOrCreateERC1155BuyOrderExecution,
} from "../utils/helpers/diamond";
} from "../utils/helpers/aavegotchi";

import { getOrCreateParcel } from "../utils/helpers/realm";

import {
BIGINT_ONE,
PORTAL_STATUS_BOUGHT,
Expand Down
59 changes: 34 additions & 25 deletions src/mappings/erc-7589/role-granted-handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { log } from '@graphprotocol/graph-ts'
import { generateTokenCommitmentId, upsertRoleAssignment } from '../../utils/helpers/erc-7589'
import { TokenCommitment } from '../../../generated/schema'
import { RoleGranted } from '../../../generated/AavegotchiDiamond/AavegotchiDiamond'
import { getOrCreateUser } from '../../utils/helpers/diamond'
import { log } from "@graphprotocol/graph-ts";
import {
generateTokenCommitmentId,
upsertRoleAssignment,
} from "../../utils/helpers/erc-7589";
import { TokenCommitment } from "../../../generated/schema";
import { RoleGranted } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { getOrCreateUser } from "../../utils/helpers/aavegotchi";

/**
@dev This handler is called when a role is granted.
Expand All @@ -19,23 +22,26 @@ Example:
);
*/
export function handleRoleGranted(event: RoleGranted): void {
const depositId = event.params._commitmentId
const rolesRegistryAddress = event.address.toHexString()
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
const tokenCommitment = TokenCommitment.load(tokenCommitmentId)
const depositId = event.params._commitmentId;
const rolesRegistryAddress = event.address.toHexString();
const tokenCommitmentId = generateTokenCommitmentId(
rolesRegistryAddress,
depositId
);
const tokenCommitment = TokenCommitment.load(tokenCommitmentId);

if (!tokenCommitment) {
log.error('[erc-7589][handleRoleGranted] TokenCommitment {} not found, tx hash: {}', [
tokenCommitmentId,
event.transaction.hash.toHexString(),
])
return
log.error(
"[erc-7589][handleRoleGranted] TokenCommitment {} not found, tx hash: {}",
[tokenCommitmentId, event.transaction.hash.toHexString()]
);
return;
}

const grantor = getOrCreateUser(tokenCommitment.grantor)
grantor.save()
const grantee = getOrCreateUser(event.params._grantee.toHex())
grantee.save()
const grantor = getOrCreateUser(tokenCommitment.grantor);
grantor.save();
const grantee = getOrCreateUser(event.params._grantee.toHex());
grantee.save();
const roleAssignment = upsertRoleAssignment(
event.params._role,
event.address.toHex(),
Expand All @@ -47,11 +53,14 @@ export function handleRoleGranted(event: RoleGranted): void {
event.params._expirationDate,
event.params._data,
event.params._revocable,
tokenCommitment.id,
)
log.warning('[erc-7589][handleRoleGranted] roleAssignment: {} NFT: {} Tx: {}', [
roleAssignment.id,
`${tokenCommitment.tokenAddress}-${tokenCommitment.tokenId}`,
event.transaction.hash.toHex(),
])
tokenCommitment.id
);
log.warning(
"[erc-7589][handleRoleGranted] roleAssignment: {} NFT: {} Tx: {}",
[
roleAssignment.id,
`${tokenCommitment.tokenAddress}-${tokenCommitment.tokenId}`,
event.transaction.hash.toHex(),
]
);
}
70 changes: 38 additions & 32 deletions src/mappings/erc-7589/role-revoked-handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { log } from '@graphprotocol/graph-ts'
import { log } from "@graphprotocol/graph-ts";
import {
findOrCreateRolesRegistry,
generateRoleAssignmentId,
generateTokenCommitmentId,
updateRoleAssignmentExpiration,
} from '../../utils/helpers/erc-7589'
import { TokenCommitment } from '../../../generated/schema'
import { RoleRevoked } from '../../../generated/AavegotchiDiamond/AavegotchiDiamond'
import { getOrCreateUser } from '../../utils/helpers/diamond'
} from "../../utils/helpers/erc-7589";
import { TokenCommitment } from "../../../generated/schema";
import { RoleRevoked } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { getOrCreateUser } from "../../utils/helpers/aavegotchi";

/**
@dev This handler is called when a role is revoked.
Expand All @@ -17,37 +17,40 @@ Example:
event RoleRevoked(uint256 indexed _commitmentId, bytes32 indexed _role, address indexed _grantee)
*/
export function handleRoleRevoked(event: RoleRevoked): void {
const depositId = event.params._commitmentId
const rolesRegistryAddress = event.address.toHexString()
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistryAddress, depositId)
const tokenCommitment = TokenCommitment.load(tokenCommitmentId)
const depositId = event.params._commitmentId;
const rolesRegistryAddress = event.address.toHexString();
const tokenCommitmentId = generateTokenCommitmentId(
rolesRegistryAddress,
depositId
);
const tokenCommitment = TokenCommitment.load(tokenCommitmentId);

if (!tokenCommitment) {
log.error('[erc-7589][handleRoleRevoked] TokenCommitment {} not found, tx {} skipping...', [
tokenCommitmentId,
event.transaction.hash.toHexString(),
])
return
log.error(
"[erc-7589][handleRoleRevoked] TokenCommitment {} not found, tx {} skipping...",
[tokenCommitmentId, event.transaction.hash.toHexString()]
);
return;
}

const granteeAddress = event.params._grantee.toHex()
const grantee = getOrCreateUser(granteeAddress)
grantee.save()
const granteeAddress = event.params._grantee.toHex();
const grantee = getOrCreateUser(granteeAddress);
grantee.save();
if (!grantee) {
log.error('[erc-7589][handleRoleRevoked] grantee {} does not exist, tx {} skipping...', [
granteeAddress,
event.transaction.hash.toHex(),
])
return
log.error(
"[erc-7589][handleRoleRevoked] grantee {} does not exist, tx {} skipping...",
[granteeAddress, event.transaction.hash.toHex()]
);
return;
}

const rolesRegistry = findOrCreateRolesRegistry(rolesRegistryAddress)
const rolesRegistry = findOrCreateRolesRegistry(rolesRegistryAddress);
const roleAssignmentId = generateRoleAssignmentId(
rolesRegistry,
grantee,
event.params._role,
tokenCommitmentId,
)
tokenCommitmentId
);
updateRoleAssignmentExpiration(
rolesRegistry,
tokenCommitment.tokenAddress,
Expand All @@ -56,12 +59,15 @@ export function handleRoleRevoked(event: RoleRevoked): void {
roleAssignmentId,
event.block.timestamp,
event.transaction.hash.toHexString(),
tokenCommitmentId,
)
tokenCommitmentId
);

log.warning('[erc-7589][handleRoleRevoked] Revoked RoleAssignment: {} NFT: {} tx: {}', [
roleAssignmentId,
`${tokenCommitment.tokenAddress}-${tokenCommitment.tokenId}`,
event.transaction.hash.toHex(),
])
log.warning(
"[erc-7589][handleRoleRevoked] Revoked RoleAssignment: {} NFT: {} tx: {}",
[
roleAssignmentId,
`${tokenCommitment.tokenAddress}-${tokenCommitment.tokenId}`,
event.transaction.hash.toHex(),
]
);
}
51 changes: 27 additions & 24 deletions src/mappings/erc-7589/tokens-committed-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BigInt, log } from '@graphprotocol/graph-ts'
import { BigInt, log } from "@graphprotocol/graph-ts";
import {
findOrCreateRolesRegistry,
generateTokenCommitmentId,
} from '../../utils/helpers/erc-7589'
import { TokensCommitted } from '../../../generated/AavegotchiDiamond/AavegotchiDiamond'
import { getOrCreateUser } from '../../utils/helpers/diamond'
import { TokenCommitment } from '../../../generated/schema'
} from "../../utils/helpers/erc-7589";
import { TokensCommitted } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { getOrCreateUser } from "../../utils/helpers/aavegotchi";
import { TokenCommitment } from "../../../generated/schema";

/**
@dev This handler is called when tokens are committed.
Expand All @@ -21,26 +21,29 @@ Example:
);
*/
export function handleTokensCommitted(event: TokensCommitted): void {
const tokenAddress = event.params._tokenAddress.toHexString()
const tokenId = event.params._tokenId
const grantor = getOrCreateUser(event.params._grantor.toHexString())
const tokenAddress = event.params._tokenAddress.toHexString();
const tokenId = event.params._tokenId;
const grantor = getOrCreateUser(event.params._grantor.toHexString());

const rolesRegistry = findOrCreateRolesRegistry(event.address.toHexString())
const tokenCommitmentId = generateTokenCommitmentId(rolesRegistry.id, event.params._commitmentId)
const rolesRegistry = findOrCreateRolesRegistry(event.address.toHexString());
const tokenCommitmentId = generateTokenCommitmentId(
rolesRegistry.id,
event.params._commitmentId
);

const tokenCommitment = new TokenCommitment(tokenCommitmentId)
tokenCommitment.rolesRegistry = rolesRegistry.id
tokenCommitment.depositId = event.params._commitmentId
tokenCommitment.grantor = grantor.id
tokenCommitment.tokenAddress = tokenAddress
tokenCommitment.tokenId = tokenId
tokenCommitment.amount = event.params._tokenAmount
tokenCommitment.usedBalance = BigInt.zero()
tokenCommitment.isReleased = false
tokenCommitment.save()
const tokenCommitment = new TokenCommitment(tokenCommitmentId);
tokenCommitment.rolesRegistry = rolesRegistry.id;
tokenCommitment.depositId = event.params._commitmentId;
tokenCommitment.grantor = grantor.id;
tokenCommitment.tokenAddress = tokenAddress;
tokenCommitment.tokenId = tokenId;
tokenCommitment.amount = event.params._tokenAmount;
tokenCommitment.usedBalance = BigInt.zero();
tokenCommitment.isReleased = false;
tokenCommitment.save();

log.warning('[erc-7589][handleTokensCommitted] TokensCommitted {} created, tx hash: {}', [
tokenCommitmentId,
event.transaction.hash.toHexString(),
])
log.warning(
"[erc-7589][handleTokensCommitted] TokensCommitted {} created, tx hash: {}",
[tokenCommitmentId, event.transaction.hash.toHexString()]
);
}
Loading

0 comments on commit 3252b65

Please sign in to comment.