0
# Account Operations
1
2
The account operations functionality provides comprehensive access to Ethereum account information including balances, transaction counts, contract code, storage inspection, and cryptographic proof generation.
3
4
## Balance and Basic Information
5
6
### getBalance
7
8
Retrieves the Ether balance of an account at a specific block.
9
10
```typescript { .api }
11
getBalance(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Numbers>;
12
```
13
14
**Parameters:**
15
- `address`: The account address to query
16
- `blockNumber`: Block number or tag (defaults to "latest")
17
- `returnFormat`: Output format configuration
18
19
**Usage Example:**
20
```typescript
21
// Get current balance
22
const balance = await eth.getBalance("0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E");
23
console.log(`Balance: ${balance} wei`);
24
25
// Get historical balance
26
const historicalBalance = await eth.getBalance(
27
"0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E",
28
15000000
29
);
30
31
// Convert to ETH for display
32
import { fromWei } from "web3-utils";
33
const balanceInEth = fromWei(balance, "ether");
34
console.log(`Balance: ${balanceInEth} ETH`);
35
```
36
37
### getTransactionCount
38
39
Returns the number of transactions sent from an address (account nonce).
40
41
```typescript { .api }
42
getTransactionCount(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Numbers>;
43
```
44
45
**Usage Example:**
46
```typescript
47
// Get current nonce (for next transaction)
48
const nonce = await eth.getTransactionCount("0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E");
49
50
// Get pending nonce (including pending transactions)
51
const pendingNonce = await eth.getTransactionCount(
52
"0x742d35Cc6634C0532925a3b8D7389Fc3C1b6c5E",
53
"pending"
54
);
55
```
56
57
## Contract Code Access
58
59
### getCode
60
61
Retrieves the bytecode of a smart contract at a given address.
62
63
```typescript { .api }
64
getCode(address: Address, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Bytes>;
65
```
66
67
**Usage Example:**
68
```typescript
69
// Check if address is a contract
70
const code = await eth.getCode("0x1234567890123456789012345678901234567890");
71
if (code !== "0x") {
72
console.log("Address is a smart contract");
73
console.log("Bytecode:", code);
74
} else {
75
console.log("Address is an externally owned account (EOA)");
76
}
77
```
78
79
## Storage Access
80
81
### getStorageAt
82
83
Reads a value from a smart contract's storage at a specific slot.
84
85
```typescript { .api }
86
getStorageAt(address: Address, storageSlot: Numbers, blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<Bytes>;
87
```
88
89
**Parameters:**
90
- `address`: Contract address
91
- `storageSlot`: Storage slot index (0-based)
92
- `blockNumber`: Block number or tag
93
- `returnFormat`: Output format configuration
94
95
**Usage Example:**
96
```typescript
97
// Read storage slot 0 (often contains important state)
98
const storageValue = await eth.getStorageAt(
99
"0x1234567890123456789012345678901234567890", // contract address
100
0 // storage slot
101
);
102
console.log("Storage slot 0:", storageValue);
103
104
// Read multiple storage slots
105
const slots = [0, 1, 2, 3];
106
const values = await Promise.all(
107
slots.map(slot =>
108
eth.getStorageAt("0x1234567890123456789012345678901234567890", slot)
109
)
110
);
111
console.log("Storage values:", values);
112
```
113
114
## Account Management
115
116
### getAccounts
117
118
Returns a list of accounts controlled by the connected wallet/provider.
119
120
```typescript { .api }
121
getAccounts(): Promise<Address[]>;
122
```
123
124
**Usage Example:**
125
```typescript
126
const accounts = await eth.getAccounts();
127
console.log("Available accounts:", accounts);
128
129
if (accounts.length > 0) {
130
console.log("Default account:", accounts[0]);
131
}
132
```
133
134
### requestAccounts
135
136
Requests access to accounts from the user (e.g., MetaMask popup).
137
138
```typescript { .api }
139
requestAccounts(): Promise<Address[]>;
140
```
141
142
**Usage Example:**
143
```typescript
144
try {
145
const accounts = await eth.requestAccounts();
146
console.log("User granted access to accounts:", accounts);
147
} catch (error) {
148
console.log("User denied account access");
149
}
150
```
151
152
## Cryptographic Proofs
153
154
### getProof
155
156
Generates a Merkle proof for an account and its storage, useful for state verification.
157
158
```typescript { .api }
159
getProof(address: Address, storageKeys?: HexString32Bytes[], blockNumber?: BlockNumberOrTag, returnFormat?: DataFormat): Promise<AccountObject>;
160
```
161
162
**Parameters:**
163
- `address`: Account address to prove
164
- `storageKeys`: Array of storage slot keys to include in proof
165
- `blockNumber`: Block number for proof generation
166
- `returnFormat`: Output format configuration
167
168
**Usage Example:**
169
```typescript
170
// Get proof for account and specific storage slots
171
const proof = await eth.getProof(
172
"0x1234567890123456789012345678901234567890",
173
[
174
"0x0000000000000000000000000000000000000000000000000000000000000000", // slot 0
175
"0x0000000000000000000000000000000000000000000000000000000000000001" // slot 1
176
],
177
"latest"
178
);
179
180
console.log("Account proof:", {
181
address: proof.address,
182
balance: proof.balance,
183
codeHash: proof.codeHash,
184
nonce: proof.nonce,
185
storageProof: proof.storageProof
186
});
187
188
// Verify the proof contains expected storage values
189
proof.storageProof.forEach((storage, index) => {
190
console.log(`Storage slot ${index}:`, {
191
key: storage.key,
192
value: storage.value,
193
proof: storage.proof
194
});
195
});
196
```
197
198
## Account Information Helpers
199
200
```typescript
201
// Check if address is a contract
202
async function isContract(address: Address): Promise<boolean> {
203
const code = await eth.getCode(address);
204
return code !== "0x";
205
}
206
207
// Get comprehensive account info
208
async function getAccountInfo(address: Address) {
209
const [balance, nonce, code] = await Promise.all([
210
eth.getBalance(address),
211
eth.getTransactionCount(address),
212
eth.getCode(address)
213
]);
214
215
return {
216
address,
217
balance,
218
nonce,
219
isContract: code !== "0x",
220
codeSize: code === "0x" ? 0 : (code.length - 2) / 2 // bytes
221
};
222
}
223
```
224
225
## Core Types
226
227
```typescript { .api }
228
interface AccountObject {
229
address: Address;
230
balance: Numbers;
231
codeHash: HexString32Bytes;
232
nonce: Numbers;
233
storageHash: HexString32Bytes;
234
accountProof: HexString[];
235
storageProof: StorageProof[];
236
}
237
238
interface StorageProof {
239
key: HexString32Bytes;
240
value: Numbers;
241
proof: HexString[];
242
}
243
244
type Address = HexString20Bytes;
245
type Numbers = HexString | number | bigint;
246
type Bytes = HexString;
247
type BlockNumberOrTag = Numbers | "latest" | "earliest" | "pending" | "safe" | "finalized";
248
249
interface DataFormat {
250
number: NumberFormat;
251
bytes: BytesFormat;
252
}
253
254
type NumberFormat = "NUMBER_HEX" | "NUMBER_NUMBER" | "NUMBER_BIGINT";
255
type BytesFormat = "BYTES_HEX" | "BYTES_UINT8ARRAY";
256
```