0
# Provider Access
1
2
Access the enhanced Hardhat provider with network-specific capabilities and Hardhat RPC method support.
3
4
## Capabilities
5
6
### Enhanced Provider
7
8
The `ethers.provider` property provides an enhanced ethers provider with Hardhat-specific functionality.
9
10
```typescript { .api }
11
interface HardhatEthersProvider extends ethers.Provider {
12
/** Get a signer for a specific address or account index */
13
getSigner(address?: number | string): Promise<HardhatEthersSigner>;
14
/** Send raw RPC calls to the underlying Hardhat provider */
15
send(method: string, params?: any[]): Promise<any>;
16
/** Destroy the provider and clean up resources */
17
destroy(): void;
18
}
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import { network } from "hardhat";
25
26
const { ethers } = await network.connect();
27
28
// Access provider
29
const provider = ethers.provider;
30
31
// Standard provider methods
32
const blockNumber = await provider.getBlockNumber();
33
const balance = await provider.getBalance("0x1234...abcd");
34
const network = await provider.getNetwork();
35
36
// Enhanced getSigner method
37
const signer = await provider.getSigner(0); // By index
38
const signer2 = await provider.getSigner("0x1234...abcd"); // By address
39
40
// Direct RPC calls
41
const accounts = await provider.send("eth_accounts", []);
42
const chainId = await provider.send("eth_chainId", []);
43
```
44
45
### Standard Provider Methods
46
47
The provider supports all standard ethers.js provider methods:
48
49
```typescript { .api }
50
// Block and network information
51
getBlockNumber(): Promise<number>;
52
getNetwork(): Promise<ethers.Network>;
53
getBlock(blockHashOrBlockTag: ethers.BlockTag, prefetchTxs?: boolean): Promise<ethers.Block | null>;
54
getFeeData(): Promise<ethers.FeeData>;
55
56
// Account information
57
getBalance(address: ethers.AddressLike, blockTag?: ethers.BlockTag): Promise<bigint>;
58
getTransactionCount(address: ethers.AddressLike, blockTag?: ethers.BlockTag): Promise<number>;
59
getCode(address: ethers.AddressLike, blockTag?: ethers.BlockTag): Promise<string>;
60
getStorage(address: ethers.AddressLike, position: ethers.BigNumberish, blockTag?: ethers.BlockTag): Promise<string>;
61
62
// Transaction methods
63
call(tx: ethers.TransactionRequest): Promise<string>;
64
estimateGas(tx: ethers.TransactionRequest): Promise<bigint>;
65
broadcastTransaction(signedTx: string): Promise<ethers.TransactionResponse>;
66
getTransaction(hash: string): Promise<ethers.TransactionResponse | null>;
67
getTransactionReceipt(hash: string): Promise<ethers.TransactionReceipt | null>;
68
69
// Logging and events
70
getLogs(filter: ethers.Filter | ethers.FilterByBlockHash): Promise<ethers.Log[]>;
71
on(event: ethers.ProviderEvent, listener: ethers.Listener): Promise<HardhatEthersProvider>;
72
once(event: ethers.ProviderEvent, listener: ethers.Listener): Promise<HardhatEthersProvider>;
73
off(event: ethers.ProviderEvent, listener?: ethers.Listener): Promise<HardhatEthersProvider>;
74
```
75
76
**Usage Examples:**
77
78
```typescript
79
// Block information
80
const latestBlock = await provider.getBlock("latest");
81
console.log(`Latest block: ${latestBlock.number}`);
82
83
// Account balance
84
const balance = await provider.getBalance("0x1234...abcd");
85
console.log(`Balance: ${ethers.formatEther(balance)} ETH`);
86
87
// Transaction count (nonce)
88
const nonce = await provider.getTransactionCount("0x1234...abcd");
89
90
// Contract code
91
const code = await provider.getCode("0x5678...ef01");
92
const isContract = code !== "0x";
93
94
// Gas estimation
95
const gasEstimate = await provider.estimateGas({
96
to: "0x1234...abcd",
97
value: ethers.parseEther("1.0"),
98
});
99
100
// Fee data
101
const feeData = await provider.getFeeData();
102
console.log(`Gas price: ${feeData.gasPrice}`);
103
```
104
105
### Direct RPC Method Access
106
107
Use the `send` method for direct RPC calls, including Hardhat-specific methods:
108
109
```typescript
110
// Standard Ethereum RPC methods
111
const accounts = await provider.send("eth_accounts", []);
112
const chainId = await provider.send("eth_chainId", []);
113
const blockNumber = await provider.send("eth_blockNumber", []);
114
115
// Hardhat-specific RPC methods
116
await provider.send("hardhat_impersonateAccount", ["0x1234...abcd"]);
117
await provider.send("hardhat_stopImpersonatingAccount", ["0x1234...abcd"]);
118
119
// Mining control (Hardhat Network)
120
await provider.send("evm_mine", []); // Mine a block
121
await provider.send("evm_increaseTime", [3600]); // Increase time by 1 hour
122
123
// Snapshot and revert (testing)
124
const snapshotId = await provider.send("evm_snapshot", []);
125
// ... perform some operations
126
await provider.send("evm_revert", [snapshotId]);
127
128
// Set next block timestamp
129
await provider.send("evm_setNextBlockTimestamp", [Math.floor(Date.now() / 1000) + 3600]);
130
```
131
132
### Event Listening
133
134
The provider supports event listening for blocks, transactions, and contract events:
135
136
```typescript
137
// Listen for new blocks
138
provider.on("block", (blockNumber) => {
139
console.log(`New block: ${blockNumber}`);
140
});
141
142
// Listen for specific transaction
143
const txHash = "0x1234...abcd";
144
provider.once(txHash, (receipt) => {
145
console.log(`Transaction mined: ${receipt.status}`);
146
});
147
148
// Listen for contract events
149
const filter = {
150
address: contractAddress,
151
topics: [ethers.id("Transfer(address,address,uint256)")],
152
};
153
154
provider.on(filter, (log) => {
155
console.log("Transfer event:", log);
156
});
157
158
// Remove listeners
159
provider.off("block");
160
provider.removeAllListeners();
161
```
162
163
### Network-Specific Behavior
164
165
The provider adapts to different Hardhat network configurations:
166
167
```typescript
168
// For Hardhat Network (built-in)
169
const { ethers } = await network.connect("hardhat");
170
// Provider supports mining control, snapshots, impersonation
171
172
// For external networks (mainnet, testnets)
173
const { ethers } = await network.connect("mainnet");
174
// Provider connects to external RPC, limited to standard methods
175
176
// For localhost
177
const { ethers } = await network.connect("localhost");
178
// Provider connects to local node, may support some Hardhat methods
179
```
180
181
### Provider Configuration
182
183
The provider inherits configuration from your Hardhat network settings:
184
185
```typescript
186
// hardhat.config.ts
187
export default {
188
networks: {
189
hardhat: {
190
chainId: 31337,
191
gas: "auto",
192
gasPrice: "auto",
193
},
194
localhost: {
195
url: "http://127.0.0.1:8545",
196
timeout: 20000,
197
},
198
mainnet: {
199
url: "https://mainnet.infura.io/v3/your-key",
200
gas: 5000000,
201
gasPrice: 20000000000, // 20 gwei
202
},
203
},
204
};
205
206
// Provider behavior varies based on network configuration
207
const { ethers } = await network.connect("mainnet");
208
const feeData = await ethers.provider.getFeeData();
209
// Uses configured gas price when available
210
```
211
212
### Error Handling
213
214
```typescript
215
try {
216
const block = await provider.getBlock("invalid");
217
} catch (error) {
218
console.error("Block not found:", error.message);
219
}
220
221
try {
222
await provider.send("invalid_method", []);
223
} catch (error) {
224
console.error("RPC method not supported:", error.message);
225
}
226
227
// Network connection errors
228
try {
229
const balance = await provider.getBalance("0x1234...abcd");
230
} catch (error) {
231
if (error.code === "NETWORK_ERROR") {
232
console.error("Network connection failed");
233
}
234
}
235
```