Skip to content

Commit 88cdf0f

Browse files
committed
feat: migration to hh v3
1 parent 31828e0 commit 88cdf0f

4 files changed

Lines changed: 101 additions & 115 deletions

File tree

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,28 @@
1-
import { HardhatRuntimeEnvironment } from "hardhat/types";
2-
import { DeployFunction } from "hardhat-deploy/types";
3-
// import { Contract } from "ethers";
1+
import { artifacts, deployScript } from "../rocketh/deploy.js";
42

53
/**
6-
* Deploys a contract named "YourToken" using the deployer account and
7-
* constructor arguments set to the deployer address
4+
* Deploys a contract named "YourToken" using the deployer account.
85
*
9-
* @param hre HardhatRuntimeEnvironment object.
6+
* On localhost, the deployer account is the one that comes with Hardhat, which is already funded.
7+
*
8+
* When deploying to live networks (e.g `yarn deploy --network sepolia`), the deployer account
9+
* should have sufficient balance to pay for the gas fees for contract creation.
10+
*
11+
* You can generate a random account with `yarn generate` which will fill DEPLOYER_PRIVATE_KEY_ENCRYPTED
12+
* in the .env file (used in hardhat.config.ts).
13+
* Run `yarn account` to check the deployer balance on every network.
1014
*/
11-
const deployYourToken: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
12-
/*
13-
On localhost, the deployer account is the one that comes with Hardhat, which is already funded.
14-
15-
When deploying to live networks (e.g `yarn deploy --network sepolia`), the deployer account
16-
should have sufficient balance to pay for the gas fees for contract creation.
17-
18-
You can generate a random account with `yarn generate` which will fill DEPLOYER_PRIVATE_KEY
19-
with a random private key in the .env file (then used on hardhat.config.ts)
20-
You can run the `yarn account` command to check your balance in every network.
21-
*/
22-
const { deployer } = await hre.getNamedAccounts();
23-
const { deploy } = hre.deployments;
24-
25-
await deploy("YourToken", {
26-
from: deployer,
27-
// Contract constructor arguments
28-
args: [],
29-
log: true,
30-
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
31-
// automatically mining the contract deployment transaction. There is no effect on live networks.
32-
autoMine: true,
33-
});
34-
35-
// Get the deployed contract
36-
// const yourToken = await hre.ethers.getContract<Contract>("YourToken", deployer);
37-
};
38-
39-
export default deployYourToken;
15+
export default deployScript(
16+
async ({ deploy, namedAccounts }) => {
17+
const { deployer } = namedAccounts;
4018

41-
// Tags are useful if you have multiple deploy files and only want to run one of them.
42-
// e.g. yarn deploy --tags YourToken
43-
deployYourToken.tags = ["YourToken"];
19+
await deploy("YourToken", {
20+
account: deployer,
21+
artifact: artifacts.YourToken,
22+
args: [],
23+
});
24+
},
25+
// Tags are useful if you have multiple deploy files and only want to run one of them.
26+
// e.g. yarn deploy --tags YourToken
27+
{ tags: ["YourToken"] },
28+
);
Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,74 @@
1-
import { HardhatRuntimeEnvironment } from "hardhat/types";
2-
import { DeployFunction } from "hardhat-deploy/types";
3-
import { Contract } from "ethers";
1+
import { artifacts, deployScript } from "../rocketh/deploy.js";
2+
import { parseEther } from "viem";
43

54
/**
6-
* Deploys a contract named "Vendor" using the deployer account and
7-
* constructor arguments set to the deployer address
5+
* Deploys a contract named "Vendor" using the deployer account.
86
*
9-
* @param hre HardhatRuntimeEnvironment object.
7+
* On localhost, the deployer account is the one that comes with Hardhat, which is already funded.
8+
*
9+
* When deploying to live networks (e.g `yarn deploy --network sepolia`), the deployer account
10+
* should have sufficient balance to pay for the gas fees for contract creation.
11+
*
12+
* You can generate a random account with `yarn generate` which will fill DEPLOYER_PRIVATE_KEY_ENCRYPTED
13+
* in the .env file (used in hardhat.config.ts).
14+
* Run `yarn account` to check the deployer balance on every network.
1015
*/
11-
const deployVendor: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
12-
/*
13-
On localhost, the deployer account is the one that comes with Hardhat, which is already funded.
16+
export default deployScript(
17+
async env => {
18+
const { deployer } = env.namedAccounts;
19+
const yourToken = env.get<typeof artifacts.YourToken.abi>("YourToken");
1420

15-
When deploying to live networks (e.g `yarn deploy --network sepolia`), the deployer account
16-
should have sufficient balance to pay for the gas fees for contract creation.
21+
/**
22+
* Student TODO:
23+
* - Put the address you're using in the frontend here (leave "" to default to the deployer)
24+
*/
25+
const FRONTEND_ADDRESS = "";
1726

18-
You can generate a random account with `yarn generate` which will fill DEPLOYER_PRIVATE_KEY
19-
with a random private key in the .env file (then used on hardhat.config.ts)
20-
You can run the `yarn account` command to check your balance in every network.
21-
*/
22-
const { deployer } = await hre.getNamedAccounts();
23-
const { deploy } = hre.deployments;
24-
const yourToken = await hre.ethers.getContract<Contract>("YourToken", deployer);
27+
/**
28+
* Mode switch:
29+
* - If true: deploy Vendor and seed it with the token balance
30+
* - If false: send tokens to your frontend address (or deployer if unset)
31+
*/
32+
const SEND_TOKENS_TO_VENDOR = false; // Don't switch until Checkpoint 2!
2533

26-
/**
27-
* Student TODO:
28-
* - Put the address you’re using in the frontend here (leave "" to default to the deployer)
29-
*/
30-
const FRONTEND_ADDRESS: string = "";
34+
const recipientAddress =
35+
FRONTEND_ADDRESS && FRONTEND_ADDRESS.trim().length > 0 ? (FRONTEND_ADDRESS as `0x${string}`) : deployer;
3136

32-
/**
33-
* Mode switch:
34-
* - If true: deploy Vendor and seed it with the token balance
35-
* - If false: send tokens to your frontend address (or deployer if unset)
36-
*/
37-
const SEND_TOKENS_TO_VENDOR = false; // Don't switch until Checkpoint 2!
37+
if (!SEND_TOKENS_TO_VENDOR) {
38+
// Send the entire initial supply to the wallet you use in the UI (useful when deployer != UI wallet).
39+
// If FRONTEND_ADDRESS is "", this defaults to the deployer (no-op transfer).
40+
if (recipientAddress !== deployer) {
41+
await env.execute(yourToken, {
42+
functionName: "transfer",
43+
args: [recipientAddress, parseEther("1000")],
44+
account: deployer,
45+
});
46+
}
47+
return;
48+
} else {
49+
// Deploy Vendor
50+
const vendor = await env.deploy("Vendor", {
51+
account: deployer,
52+
artifact: artifacts.Vendor,
53+
args: [yourToken.address],
54+
});
3855

39-
const recipientAddress = FRONTEND_ADDRESS && FRONTEND_ADDRESS.trim().length > 0 ? FRONTEND_ADDRESS : deployer;
56+
// Transfer tokens to Vendor (seed inventory)
57+
await env.execute(yourToken, {
58+
functionName: "transfer",
59+
args: [vendor.address, parseEther("1000")],
60+
account: deployer,
61+
});
4062

41-
if (!SEND_TOKENS_TO_VENDOR) {
42-
// Send the entire initial supply to the wallet you use in the UI (useful when deployer != UI wallet).
43-
// If FRONTEND_ADDRESS is "", this defaults to the deployer (no-op transfer).
44-
if (recipientAddress != deployer) {
45-
await yourToken.transfer(recipientAddress, hre.ethers.parseEther("1000"));
63+
// Make the UI wallet the owner (for withdraw(), etc). Defaults to deployer if unset.
64+
await env.execute(vendor, {
65+
functionName: "transferOwnership",
66+
args: [recipientAddress],
67+
account: deployer,
68+
});
4669
}
47-
return;
48-
} else {
49-
// Deploy Vendor
50-
const yourTokenAddress = await yourToken.getAddress();
51-
await deploy("Vendor", {
52-
from: deployer,
53-
// Contract constructor arguments
54-
args: [yourTokenAddress],
55-
log: true,
56-
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
57-
// automatically mining the contract deployment transaction. There is no effect on live networks.
58-
autoMine: true,
59-
});
60-
const vendor = await hre.ethers.getContract<Contract>("Vendor", deployer);
61-
const vendorAddress = await vendor.getAddress();
62-
63-
// Transfer tokens to Vendor (seed inventory)
64-
await yourToken.transfer(vendorAddress, hre.ethers.parseEther("1000"));
65-
66-
// Make the UI wallet the owner (for withdraw(), etc). Defaults to deployer if unset.
67-
await vendor.transferOwnership(recipientAddress);
68-
}
69-
};
70-
71-
export default deployVendor;
72-
73-
// Tags are useful if you have multiple deploy files and only want to run one of them.
74-
// e.g. yarn deploy --tags Vendor
75-
deployVendor.tags = ["Vendor"];
70+
},
71+
// Tags are useful if you have multiple deploy files and only want to run one of them.
72+
// e.g. yarn deploy --tags Vendor
73+
{ tags: ["Vendor"] },
74+
);

extension/packages/hardhat/test/Vendor.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// this script executes when you run 'yarn harhat:test'
33
//
44

5-
import hre from "hardhat";
5+
import { network } from "hardhat";
66
import { expect } from "chai";
7-
import { impersonateAccount, stopImpersonatingAccount } from "@nomicfoundation/hardhat-network-helpers";
8-
import { Vendor, YourToken } from "../typechain-types";
9-
10-
const { ethers } = hre;
7+
import type { Vendor, YourToken } from "../types/ethers-contracts/index.js";
118

129
describe("🚩 Challenge: 🏵 Token Vendor 🤖", function () {
1310
// NOTE: The README expects tests grouped by checkpoint so you can run:
@@ -16,6 +13,9 @@ describe("🚩 Challenge: 🏵 Token Vendor 🤖", function () {
1613
// yarn test --grep "Checkpoint3"
1714
// yarn test --grep "Checkpoint4"
1815

16+
let ethers: Awaited<ReturnType<typeof network.create>>["ethers"];
17+
let networkHelpers: Awaited<ReturnType<typeof network.create>>["networkHelpers"];
18+
1919
const contractAddress = process.env.CONTRACT_ADDRESS;
2020

2121
const getYourTokenArtifact = () => {
@@ -29,19 +29,24 @@ describe("🚩 Challenge: 🏵 Token Vendor 🤖", function () {
2929
};
3030

3131
const TOKENS_PER_ETH = 100n;
32-
const INITIAL_SUPPLY = ethers.parseEther("1000");
32+
let INITIAL_SUPPLY: bigint;
33+
34+
before(async function () {
35+
({ ethers, networkHelpers } = await network.create());
36+
INITIAL_SUPPLY = ethers.parseEther("1000");
37+
});
3338

3439
async function deployYourTokenFixture() {
3540
const [deployer, user] = await ethers.getSigners();
3641
const YourTokenFactory = await ethers.getContractFactory(getYourTokenArtifact());
37-
const yourToken = (await YourTokenFactory.deploy()) as YourToken;
42+
const yourToken = (await YourTokenFactory.deploy()) as unknown as YourToken;
3843
await yourToken.waitForDeployment();
3944
return { deployer, user, yourToken, yourTokenAddress: await yourToken.getAddress() };
4045
}
4146

4247
async function deployVendorFixture(yourTokenAddress: string) {
4348
const VendorFactory = await ethers.getContractFactory(getVendorArtifact());
44-
const vendor = (await VendorFactory.deploy(yourTokenAddress)) as Vendor;
49+
const vendor = (await VendorFactory.deploy(yourTokenAddress)) as unknown as Vendor;
4550
await vendor.waitForDeployment();
4651
return { vendor, vendorAddress: await vendor.getAddress() };
4752
}
@@ -170,7 +175,7 @@ describe("🚩 Challenge: 🏵 Token Vendor 🤖", function () {
170175

171176
const vendorEthBefore = await ethers.provider.getBalance(vendorAddress);
172177

173-
await impersonateAccount(vendorAddress);
178+
await networkHelpers.impersonateAccount(vendorAddress);
174179
try {
175180
const vendorAsOwner = await ethers.getSigner(vendorAddress);
176181
// Use a simulation call (no gas is paid from vendorAddress), otherwise the tx gas would
@@ -179,7 +184,7 @@ describe("🚩 Challenge: 🏵 Token Vendor 🤖", function () {
179184
.to.be.revertedWithCustomError(vendor, "EthTransferFailed")
180185
.withArgs(vendorAddress, vendorEthBefore);
181186
} finally {
182-
await stopImpersonatingAccount(vendorAddress);
187+
await networkHelpers.stopImpersonatingAccount(vendorAddress);
183188
}
184189
});
185190
});

extension/packages/nextjs/next.config.ts.args.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ export const configOverrides = {
22
typescript: {
33
ignoreBuildErrors: true,
44
},
5-
eslint: {
6-
ignoreDuringBuilds: true,
7-
},
85
};

0 commit comments

Comments
 (0)