Skip to content

Commit

Permalink
Align client configuration with viem@2 standards for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Feb 19, 2024
1 parent e4b1c07 commit 2409659
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
6 changes: 3 additions & 3 deletions packages/hardhat-viem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const contractA = await hre.viem.deployContract(
"contractName",
["arg1", 50, "arg3"],
{
walletClient: secondWalletClient,
client: { wallet: secondWalletClient }
gas: 1000000,
value: parseEther("0.0001"),
confirmations: 5, // 1 by default
Expand Down Expand Up @@ -185,7 +185,7 @@ const [_, secondWalletClient] = await hre.viem.getWalletClients();
const contract = await hre.viem.getContractAt(
"contractName",
"0x1234567890123456789012345678901234567890",
{ walletClient: secondWalletClient }
{ client: { wallet: secondWalletClient } }
);
```

Expand All @@ -210,7 +210,7 @@ const { contract: contractName, deploymentTransaction } =
"contractName",
["arg1", 50, "arg3"],
{
walletClient: secondWalletClient,
client: { wallet: secondWalletClient },
gas: 1000000,
value: parseEther("0.0001"),
}
Expand Down
27 changes: 8 additions & 19 deletions packages/hardhat-viem/src/internal/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ export async function deployContract(
constructorArgs: any[] = [],
config: DeployContractConfig = {}
): Promise<GetContractReturnType> {
const {
walletClient: configWalletClient,
publicClient: configPublicClient,
confirmations,
...deployContractParameters
} = config;
const { client, confirmations, ...deployContractParameters } = config;
const [publicClient, walletClient, contractArtifact] = await Promise.all([
configPublicClient ?? getPublicClient(network.provider),
configWalletClient ??
getDefaultWalletClient(network.provider, network.name),
client?.public ?? getPublicClient(network.provider),
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
artifacts.readArtifact(contractName),
]);

Expand Down Expand Up @@ -120,15 +114,10 @@ export async function sendDeploymentTransaction(
contract: GetContractReturnType;
deploymentTransaction: GetTransactionReturnType;
}> {
const {
walletClient: configWalletClient,
publicClient: configPublicClient,
...deployContractParameters
} = config;
const { client, ...deployContractParameters } = config;
const [publicClient, walletClient, contractArtifact] = await Promise.all([
configPublicClient ?? getPublicClient(network.provider),
configWalletClient ??
getDefaultWalletClient(network.provider, network.name),
client?.public ?? getPublicClient(network.provider),
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
artifacts.readArtifact(contractName),
]);

Expand Down Expand Up @@ -202,8 +191,8 @@ export async function getContractAt(
config: GetContractAtConfig = {}
): Promise<GetContractReturnType> {
const [publicClient, walletClient, contractArtifact] = await Promise.all([
config.publicClient ?? getPublicClient(network.provider),
config.walletClient ??
config.client?.public ?? getPublicClient(network.provider),
config.client?.wallet ??
getDefaultWalletClient(network.provider, network.name),
artifacts.readArtifact(contractName),
]);
Expand Down
25 changes: 13 additions & 12 deletions packages/hardhat-viem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ export type TestClient = viemT.TestClient<
viemT.Chain
>;

export type KeyedClient =
| {
public?: PublicClient;
wallet: WalletClient;
}
| {
public: PublicClient;
wallet?: WalletClient;
};

export type TestClientMode = Parameters<
typeof viemT.createTestClient
>[0]["mode"];

export interface SendTransactionConfig {
walletClient?: WalletClient;
publicClient?: PublicClient;
client?: KeyedClient;
gas?: bigint;
gasPrice?: bigint;
maxFeePerGas?: bigint;
Expand All @@ -34,20 +43,12 @@ export interface DeployContractConfig extends SendTransactionConfig {
export type SendDeploymentTransactionConfig = SendTransactionConfig;

export interface GetContractAtConfig {
walletClient?: WalletClient;
publicClient?: PublicClient;
client?: KeyedClient;
}

export type GetContractReturnType<
TAbi extends viemT.Abi | readonly unknown[] = viemT.Abi
> = viemT.GetContractReturnType<
TAbi,
{
public: PublicClient;
wallet: WalletClient;
},
viemT.Address
>;
> = viemT.GetContractReturnType<TAbi, Required<KeyedClient>, viemT.Address>;

export type GetTransactionReturnType = viemT.GetTransactionReturnType<
viemT.Chain,
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-viem/test/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe("Integration tests", function () {
const contract = await this.hre.viem.deployContract(
"WithoutConstructorArgs",
[],
{ walletClient: secondWalletClient }
{ client: { wallet: secondWalletClient } }
);

const owner = await contract.read.getOwner();
Expand Down

0 comments on commit 2409659

Please sign in to comment.