Skip to content

Commit

Permalink
feat: improve deployemnt scripts for nfg-collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Adjimann committed Dec 16, 2024
1 parent 4291ff9 commit be592bc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
} from '../../utils/hardhatDeployUtils';
import {getNamedAccounts} from 'hardhat';

// hardhat-deploy don't support factory and beacons the way we use it
// hardhat-deploy don't support factory and beacons the way we use them
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployments, ethers} = hre;
const {nftCollectionAdmin} = await getNamedAccounts();
const beaconAlias = ethers.encodeBytes32String('nft-collection-v2');
const implementation = await deployments.get('NFTCollection_Implementation');
const beaconAddress = await deployments.read(
Expand All @@ -19,59 +20,31 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
beaconAlias
);
if (beaconAddress == ZeroAddress) {
await deployments.catchUnknownSigner(
deployBeacon(hre, beaconAlias, implementation)
);
} else {
await performSanityChecks(hre, beaconAddress, implementation);
await deployments.catchUnknownSigner(async () => {
const receipt = await deployments.execute(
'CollectionFactory',
{from: nftCollectionAdmin, log: true},
'deployBeacon',
implementation.address,
beaconAlias
);
const eventArgs: {beaconAlias: string; beaconAddress: string} =
getEventArgsFromReceipt(
await ethers.getContract('CollectionFactory'),
receipt,
'BeaconAdded'
);
await saveDeployment(
deployments,
eventArgs.beaconAddress,
'NFTCollection_Beacon',
'UpgradeableBeacon',
receipt
);
});
}
};

async function deployBeacon(hre, beaconAlias, implementation) {
const {deployments, ethers} = hre;
const {nftCollectionAdmin} = await getNamedAccounts();
const receipt = await deployments.execute(
'CollectionFactory',
{from: nftCollectionAdmin, log: true},
'deployBeacon',
implementation.address,
beaconAlias
);
const eventArgs: {beaconAlias: string; beaconAddress: string} =
getEventArgsFromReceipt(
await ethers.getContract('CollectionFactory'),
receipt,
'BeaconAdded'
);
await saveDeployment(
deployments,
eventArgs.beaconAddress,
'NFTCollection_Beacon',
'UpgradeableBeacon',
receipt
);
}

async function performSanityChecks(hre, beaconAddress: string, implementation) {
const {deployments, ethers} = hre;
const beaconArtifact = await deployments.getArtifact('UpgradeableBeacon');
const beacon = await ethers.getContractAt(beaconArtifact.abi, beaconAddress);

const i = await beacon.implementation();
if (i != implementation.address) {
throw new Error(
'something went wrong: Beacon already deployed but has wrong implementation address, must call updateBeaconImplementation'
);
}
const factory = await deployments.get('CollectionFactory');
const o = await beacon.owner();
if (o != factory.address) {
throw new Error(
'something went wrong: Beacon already deployed but has wrong owner'
);
}
}

export default func;
func.tags = [
'PolygonNFTCollection',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {ZeroAddress} from 'ethers';
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import {DEPLOY_TAGS} from '../../hardhat.config';
import {
getEventArgsFromReceipt,
saveDeployment,
} from '../../utils/hardhatDeployUtils';
import {getNamedAccounts} from 'hardhat';

// usually the deployments are done via defender proposals.
// This step lets us continue the deployment after proposal execution
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployments, ethers} = hre;
const beaconAlias = ethers.encodeBytes32String('nft-collection-v2');
const implementation = await deployments.get('NFTCollection_Implementation');
const beaconAddress = await deployments.read(
'CollectionFactory',
'aliasToBeacon',
beaconAlias
);
if (beaconAddress == ZeroAddress) {
throw new Error('Beacon deployment missing');
}
const d = await deployments.getOrNull('NFTCollection_Beacon');
if (!d) {
await saveDeployment(
deployments,
beaconAddress,
'NFTCollection_Beacon',
'UpgradeableBeacon'
);
}

const beaconArtifact = await deployments.getArtifact('UpgradeableBeacon');
const beacon = await ethers.getContractAt(beaconArtifact.abi, beaconAddress);
const i = await beacon.implementation();
if (i != implementation.address) {
throw new Error(
'something went wrong: Beacon already deployed but has wrong implementation address, must call updateBeaconImplementation'
);
}
const factory = await deployments.get('CollectionFactory');
const o = await beacon.owner();
if (o != factory.address) {
throw new Error(
'something went wrong: Beacon already deployed but has wrong owner'
);
}
};

export default func;
func.tags = [
'PolygonNFTCollection',
'PolygonNFTCollection_Beacon',
'PolygonNFTCollectionBeacon_check',
DEPLOY_TAGS.L2,
DEPLOY_TAGS.L2_PROD,
DEPLOY_TAGS.L2_TEST,
];
func.dependencies = ['PolygonNFTCollectionBeacon_deploy'];
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
saveDeployment,
} from '../../utils/hardhatDeployUtils';

// Collections are created via backoffice, this script creates a collection
// for testing (TO BE USED ONLY ON TESTNETS)
// hardhat-deploy don't support factory and beacons the way we use it
// TO BE USED ONLY ON TESTNETS!!!
// Collections are created via backoffice, this script creates a testing collection
// hardhat-deploy don't support factory and beacons the way we use them
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployments, getNamedAccounts, ethers} = hre;

Expand Down
22 changes: 16 additions & 6 deletions packages/deploy/utils/hardhatDeployUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ export async function saveDeployment(
address: string,
artifactName: string,
contractName: string,
receipt: Receipt,
receipt?: Receipt,
implementationAddress?: string
) {
const extendedArtifact = await deployments.getExtendedArtifact(contractName);
console.log(
`saving "${artifactName}" (tx: ${receipt.transactionHash})...: deployed at ${address} with ${receipt.gasUsed} gas`
);
if (receipt) {
console.log(
`saving "${artifactName}" (tx: ${receipt.transactionHash})...: deployed at ${address} with ${receipt.gasUsed} gas`
);
} else {
console.log(
`saving "${artifactName}"...: deployed at ${address} without receipt`
);
}
await deployments.save(artifactName, {
address,
...extendedArtifact,
receipt,
transactionHash: receipt.transactionHash,
...(receipt
? {
receipt,
transactionHash: receipt.transactionHash,
}
: {}),
...(implementationAddress ? {implementation: implementationAddress} : {}),
} as DeploymentSubmission);
}
Expand Down

1 comment on commit be592bc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

90.36%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/avatar/contracts/avatar
   AvatarCollection.sol74.25%56.56%73.81%90.37%285, 329–337, 339–340, 345, 403–407, 425, 442, 459, 483–487, 491–492, 495, 526, 537, 545, 545–546, 565, 599, 621, 631, 712, 732, 745, 745, 745, 763, 763, 765, 774, 774, 776, 786, 786–787, 797, 797–798, 809, 809–810, 823, 832, 861–862, 992–993, 996, 999
   CollectionAccessControl.sol60.87%53.33%63.64%67.86%120, 130–131, 140, 150–151, 161–162, 162, 162, 164–165, 167, 173, 173–174, 185, 192, 192–194, 98–99
   ERC721BurnMemoryEnumerableUpgradeable.sol93.33%75%100%100%62, 75
packages/avatar/contracts/common
   IERC4906.sol100%100%100%100%
   IERC5313.sol100%100%100%100%
packages/avatar/contracts/common/BaseWithStorage/ERC2771
   ERC2771HandlerUpgradeable.sol44.44%25%60%44.44%22, 26, 29, 38, 38, 38–39, 41
packages/avatar/contracts/common/OperatorFilterer
   IOperatorFilterRegistry.sol100%100%100%100%
   UpdatableOperatorFiltererUpgradeable.sol11.11%10%25%9.52%18, 23–24, 24, 24–25, 27, 27, 27–28, 30, 38, 38, 38, 42, 42, 42–44, 46, 46, 46–47, 50, 55, 55, 55–56, 56, 56–57, 60, 68, 68, 68–69, 71
packages/avatar/contracts/nft-collection
   ERC2771HandlerUpgradeable.sol100%100%100%100%
   ERC721BurnMemoryUpgradeable.sol100%100%100%100%
   INFTCollection.sol100%100%100%100%
   NFTCollection.sol99.76%99.28%100%100%300
   NFTCollectionSignature.sol100%100%100%100%
   UpdatableOperatorFiltererUpgradeable.sol100%100%100%100%
packages/avatar/contracts/proxy
   CollectionFactory.sol86.59%89.06%75%87.50%149, 230, 293, 362–366, 368, 385, 402, 410, 410–411, 423, 423–424
   CollectionProxy.sol88.24%50%100%100%55, 70
packages/avatar/contracts/raffle
   DanceFight.sol75%50%100%100%20
   FistOfTheNorthStar.sol75%50%100%100%20
   GenericRaffle.sol67.92%52.22%62.07%84.95%178, 189–195, 225–229, 252–259, 266–267, 270, 288, 288–289, 289, 289–290, 308, 381, 390, 409, 409–410, 419, 419, 421, 430, 430, 432, 442, 442–443, 453, 453–454, 465, 465–466, 566, 575, 626–627, 630, 633
   HellsKitchen.sol75%50%100%100%20
   MadBalls.sol75%50%100%100%20
   ParisHilton.sol75%50%100%100%20
   Rabbids.sol75%50%100%100%20

Please sign in to comment.