Skip to content

Commit 8a1a2b6

Browse files
authored
Merge pull request #4875 from NomicFoundation/upgrade-viem-to-v2
Upgrade hardhat-viem to support viem@2
2 parents b887650 + bdf5520 commit 8a1a2b6

File tree

8 files changed

+92
-44
lines changed

8 files changed

+92
-44
lines changed

.changeset/proud-peas-sort.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-viem": major
3+
---
4+
5+
Upgraded hardhat-viem to support viem@2

.github/dependabot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ updates:
99
interval: "daily"
1010
commit-message:
1111
prefix: "bump"
12-
- package-ecosystem: "pnpm"
12+
- package-ecosystem: "npm"
1313
directory: "/"
1414
schedule:
1515
interval: "daily"

packages/hardhat-viem/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const contractA = await hre.viem.deployContract(
154154
"contractName",
155155
["arg1", 50, "arg3"],
156156
{
157-
walletClient: secondWalletClient,
157+
client: { wallet: secondWalletClient }
158158
gas: 1000000,
159159
value: parseEther("0.0001"),
160160
confirmations: 5, // 1 by default
@@ -185,7 +185,7 @@ const [_, secondWalletClient] = await hre.viem.getWalletClients();
185185
const contract = await hre.viem.getContractAt(
186186
"contractName",
187187
"0x1234567890123456789012345678901234567890",
188-
{ walletClient: secondWalletClient }
188+
{ client: { wallet: secondWalletClient } }
189189
);
190190
```
191191

@@ -210,7 +210,7 @@ const { contract: contractName, deploymentTransaction } =
210210
"contractName",
211211
["arg1", 50, "arg3"],
212212
{
213-
walletClient: secondWalletClient,
213+
client: { wallet: secondWalletClient },
214214
gas: 1000000,
215215
value: parseEther("0.0001"),
216216
}

packages/hardhat-viem/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666
"sinon": "^9.0.0",
6767
"ts-node": "^10.8.0",
6868
"typescript": "~5.0.0",
69-
"viem": "^1.15.1"
69+
"viem": "^2.7.6"
7070
},
7171
"peerDependencies": {
7272
"hardhat": "workspace:^2.17.0",
7373
"typescript": "~5.0.0",
74-
"viem": "^1.15.1"
74+
"viem": "^2.7.6"
7575
},
7676
"dependencies": {
7777
"abitype": "^0.9.8",

packages/hardhat-viem/src/internal/contracts.ts

+12-21
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@ export async function deployContract(
2727
constructorArgs: any[] = [],
2828
config: DeployContractConfig = {}
2929
): Promise<GetContractReturnType> {
30-
const {
31-
walletClient: configWalletClient,
32-
publicClient: configPublicClient,
33-
confirmations,
34-
...deployContractParameters
35-
} = config;
30+
const { client, confirmations, ...deployContractParameters } = config;
3631
const [publicClient, walletClient, contractArtifact] = await Promise.all([
37-
configPublicClient ?? getPublicClient(network.provider),
38-
configWalletClient ??
39-
getDefaultWalletClient(network.provider, network.name),
32+
client?.public ?? getPublicClient(network.provider),
33+
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
4034
artifacts.readArtifact(contractName),
4135
]);
4236

@@ -120,15 +114,10 @@ export async function sendDeploymentTransaction(
120114
contract: GetContractReturnType;
121115
deploymentTransaction: GetTransactionReturnType;
122116
}> {
123-
const {
124-
walletClient: configWalletClient,
125-
publicClient: configPublicClient,
126-
...deployContractParameters
127-
} = config;
117+
const { client, ...deployContractParameters } = config;
128118
const [publicClient, walletClient, contractArtifact] = await Promise.all([
129-
configPublicClient ?? getPublicClient(network.provider),
130-
configWalletClient ??
131-
getDefaultWalletClient(network.provider, network.name),
119+
client?.public ?? getPublicClient(network.provider),
120+
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
132121
artifacts.readArtifact(contractName),
133122
]);
134123

@@ -202,8 +191,8 @@ export async function getContractAt(
202191
config: GetContractAtConfig = {}
203192
): Promise<GetContractReturnType> {
204193
const [publicClient, walletClient, contractArtifact] = await Promise.all([
205-
config.publicClient ?? getPublicClient(network.provider),
206-
config.walletClient ??
194+
config.client?.public ?? getPublicClient(network.provider),
195+
config.client?.wallet ??
207196
getDefaultWalletClient(network.provider, network.name),
208197
artifacts.readArtifact(contractName),
209198
]);
@@ -225,8 +214,10 @@ async function innerGetContractAt(
225214
const viem = await import("viem");
226215
const contract = viem.getContract({
227216
address,
228-
publicClient,
229-
walletClient,
217+
client: {
218+
public: publicClient,
219+
wallet: walletClient,
220+
},
230221
abi: contractAbi,
231222
});
232223

packages/hardhat-viem/src/types.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ export type TestClient = viemT.TestClient<
1313
viemT.Chain
1414
>;
1515

16+
export type KeyedClient =
17+
| {
18+
public?: PublicClient;
19+
wallet: WalletClient;
20+
}
21+
| {
22+
public: PublicClient;
23+
wallet?: WalletClient;
24+
};
25+
1626
export type TestClientMode = Parameters<
1727
typeof viemT.createTestClient
1828
>[0]["mode"];
1929

2030
export interface SendTransactionConfig {
21-
walletClient?: WalletClient;
22-
publicClient?: PublicClient;
31+
client?: KeyedClient;
2332
gas?: bigint;
2433
gasPrice?: bigint;
2534
maxFeePerGas?: bigint;
@@ -34,18 +43,12 @@ export interface DeployContractConfig extends SendTransactionConfig {
3443
export type SendDeploymentTransactionConfig = SendTransactionConfig;
3544

3645
export interface GetContractAtConfig {
37-
walletClient?: WalletClient;
38-
publicClient?: PublicClient;
46+
client?: KeyedClient;
3947
}
4048

4149
export type GetContractReturnType<
4250
TAbi extends viemT.Abi | readonly unknown[] = viemT.Abi
43-
> = viemT.GetContractReturnType<
44-
TAbi,
45-
PublicClient,
46-
WalletClient,
47-
viemT.Address
48-
>;
51+
> = viemT.GetContractReturnType<TAbi, Required<KeyedClient>, viemT.Address>;
4952

5053
export type GetTransactionReturnType = viemT.GetTransactionReturnType<
5154
viemT.Chain,

packages/hardhat-viem/test/integration.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ describe("Integration tests", function () {
6464
const fromAddress = fromWalletClient.account.address;
6565
const toAddress = toWalletClient.account.address;
6666

67-
const fromBalanceBefore: bigint = await publicClient.getBalance({
67+
const fromBalanceBefore = await publicClient.getBalance({
6868
address: fromAddress,
6969
});
70-
const toBalanceBefore: bigint = await publicClient.getBalance({
70+
const toBalanceBefore = await publicClient.getBalance({
7171
address: toAddress,
7272
});
7373

@@ -79,10 +79,10 @@ describe("Integration tests", function () {
7979
const receipt = await publicClient.waitForTransactionReceipt({ hash });
8080
const transactionFee = receipt.gasUsed * receipt.effectiveGasPrice;
8181

82-
const fromBalanceAfter: bigint = await publicClient.getBalance({
82+
const fromBalanceAfter = await publicClient.getBalance({
8383
address: fromAddress,
8484
});
85-
const toBalanceAfter: bigint = await publicClient.getBalance({
85+
const toBalanceAfter = await publicClient.getBalance({
8686
address: toAddress,
8787
});
8888

@@ -141,7 +141,7 @@ describe("Integration tests", function () {
141141
const contract = await this.hre.viem.deployContract(
142142
"WithoutConstructorArgs",
143143
[],
144-
{ walletClient: secondWalletClient }
144+
{ client: { wallet: secondWalletClient } }
145145
);
146146

147147
const owner = await contract.read.getOwner();

pnpm-lock.yaml

+51-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)