diff --git a/contracts/Aavegotchi/facets/ItemsRolesRegistryFacet.sol b/contracts/Aavegotchi/facets/ItemsRolesRegistryFacet.sol index df09d2539..b7599f29d 100644 --- a/contracts/Aavegotchi/facets/ItemsRolesRegistryFacet.sol +++ b/contracts/Aavegotchi/facets/ItemsRolesRegistryFacet.sol @@ -151,13 +151,7 @@ contract ItemsRolesRegistryFacet is Modifiers, IERC7589, ERC1155Holder { /// @notice Releases tokens back to grantor. /// @param _depositId The deposit identifier. - function releaseTokens( - uint256 _depositId - ) - external - override - onlyOwnerOrApproved(s.itemRolesDepositInfo[_depositId].deposit.grantor, s.itemRolesDepositInfo[_depositId].deposit.tokenAddress) - { + function releaseTokens(uint256 _depositId) public override onlyDaoOrOwner { ItemRolesInfo storage _depositInfo = s.itemRolesDepositInfo[_depositId]; Deposit memory _deposit = _depositInfo.deposit; require(_deposit.tokenAmount > 0, "ItemsRolesRegistryFacet: deposit does not exist"); @@ -305,7 +299,7 @@ contract ItemsRolesRegistryFacet is Modifiers, IERC7589, ERC1155Holder { "ItemsRolesRegistryFacet: token has an active role" ); - if(_depositInfo.roleAssignment.grantee != _grantee) { + if (_depositInfo.roleAssignment.grantee != _grantee) { // if depositId is being delegated to a new user, we need to make sure that Aavegotchis not owned by the new user are using these Wearables _unequipAllDelegatedWearables(_depositId, _depositInfo.deposit.tokenId); } @@ -350,7 +344,7 @@ contract ItemsRolesRegistryFacet is Modifiers, IERC7589, ERC1155Holder { uint256 _unequippedBalance; uint16[EQUIPPED_WEARABLE_SLOTS] memory _previousEquippedWearables = _aavegotchi.equippedWearables; uint256[EQUIPPED_WEARABLE_SLOTS] memory _previousEquippedDepositIds = _gotchiInfo.equippedDepositIds; - + for (uint256 slot; slot < EQUIPPED_WEARABLE_SLOTS; slot++) { // if the item is not equipped in the slot or the deposit is not the same, continue if (_aavegotchi.equippedWearables[slot] != _tokenIdToUnequip || _gotchiInfo.equippedDepositIds[slot] != _depositId) continue; diff --git a/scripts/upgrades/upgrade-tempReleaseDelegatedWearables.ts b/scripts/upgrades/upgrade-tempReleaseDelegatedWearables.ts new file mode 100644 index 000000000..2b45c8050 --- /dev/null +++ b/scripts/upgrades/upgrade-tempReleaseDelegatedWearables.ts @@ -0,0 +1,114 @@ +import { ethers, network, run } from "hardhat"; +import { + convertFacetAndSelectorsToString, + DeployUpgradeTaskArgs, + FacetsAndAddSelectors, +} from "../../tasks/deployUpgrade"; +import { + impersonate, + maticDiamondAddress, + maticDiamondUpgrader, +} from "../helperFunctions"; + +// New imports for fetching subgraph data +import { GraphQLClient, gql } from "graphql-request"; +import { LedgerSigner } from "@anders-t/ethers-ledger"; + +// Small helper to chunk an array +function chunk(arr: T[], size: number): T[][] { + const out: T[][] = []; + for (let i = 0; i < arr.length; i += size) { + out.push(arr.slice(i, i + size)); + } + return out; +} + +export async function upgrade() { + const facets: FacetsAndAddSelectors[] = [ + { + facetName: "ItemsRolesRegistryFacet", + addSelectors: [], + removeSelectors: [], + }, + ]; + + const joined = convertFacetAndSelectorsToString(facets); + + const args: DeployUpgradeTaskArgs = { + diamondOwner: maticDiamondUpgrader, + diamondAddress: maticDiamondAddress, + facetsAndAddSelectors: joined, + useLedger: true, + useMultisig: false, + }; + + await run("deployUpgrade", args); + + const uri = process.env.SUBGRAPH_CORE_MATIC; + if (!uri) { + throw new Error("SUBGRAPH_CORE_MATIC environment variable not set"); + } + + const client = new GraphQLClient(uri); + + // Fetch all depositIds where `isReleased` is false + const query = gql` + { + tokenCommitments(where: { isReleased: false }) { + depositId + } + } + `; + + const response = await client.request<{ + tokenCommitments: { depositId: string }[]; + }>(query); + const depositIds: string[] = response.tokenCommitments.map( + (c) => c.depositId + ); + + console.log(`Fetched ${depositIds.length} unreleased depositIds`); + + let signer; + + const testing = ["hardhat", "localhost"].includes(network.name); + let itemRolesRegistryFacet = await ethers.getContractAt( + "ItemsRolesRegistryFacet", + maticDiamondAddress + ); + + if (testing) { + itemRolesRegistryFacet = await impersonate( + maticDiamondUpgrader, + itemRolesRegistryFacet, + ethers, + network + ); + } else if (network.name === "matic") { + signer = new LedgerSigner(ethers.provider, "m/44'/60'/1'/0/0"); + itemRolesRegistryFacet = itemRolesRegistryFacet.connect(signer); + } else throw Error("Incorrect network selected"); + + for (let i = 0; i < depositIds.length; i++) { + const id = depositIds[i]; + console.log( + `\nReleasing depositId ${i + 1}/${depositIds.length} (size: ${id})` + ); + + const tx = await itemRolesRegistryFacet.releaseTokens(id); + console.log(` → releaseTokens(${id}) tx: ${tx.hash}`); + await tx.wait(); + } + + console.log("\nAll unreleased deposits have been processed."); +} + +if (require.main === module) { + upgrade() + .then(() => process.exit(0)) + // .then(() => console.log('upgrade completed') /* process.exit(0) */) + .catch((error) => { + console.error(error); + process.exit(1); + }); +}