@@ -4,4 +4,260 @@ title: 'Migration from Web3.js to Viem'
4
4
position : 18
5
5
---
6
6
7
- Coming soon.
7
+ # Migration from Web3.js to Viem
8
+
9
+ This guide will help you migrate from Web3.js v4 to Viem v2 for interacting with the Ethereum blockchain. The guide provides code examples for both libraries for transition.
10
+
11
+ ## Installation
12
+
13
+ To begin migrating from Web3.js to Viem, first install the Viem package:
14
+
15
+ ``` bash
16
+ npm install viem@2
17
+ ```
18
+
19
+ ## Providers
20
+
21
+ When migrating from Web3.js to Viem, the first step is to update how you connect to the Ethereum network. Both libraries use providers, but their initialization differs.
22
+
23
+ ``` javascript
24
+ import Web3 from ' web3' ;
25
+
26
+ const web3 = new Web3 (providerURL);
27
+ ```
28
+
29
+ To migrate this to Viem, you'll need to replace it with using ` createPublicClient() ` . This function creates a client for interacting with the Ethereum network.
30
+
31
+ ``` javascript
32
+ import { createPublicClient , http } from ' viem' ;
33
+
34
+ const client = createPublicClient ({ transport: http (providerURL) });
35
+ ```
36
+
37
+ ## Browser-injected Provider
38
+
39
+ For browser wallet connections like MetaMask, update how you handle the injected provider.
40
+
41
+ ``` javascript
42
+ const web3 = new Web3 (window .ethereum );
43
+ ```
44
+
45
+ To migrate this to Viem, you'll need to use ` createWalletClient() ` with ` custom() ` instead of creating a new Web3 instance.
46
+
47
+ ``` javascript
48
+ import { createWalletClient , custom } from ' viem' ;
49
+ import { mainnet } from ' viem/chains' ;
50
+
51
+ const client = createWalletClient ({
52
+ chain: mainnet,
53
+ transport: custom (window .ethereum ),
54
+ });
55
+ ```
56
+
57
+ ## Wallets and Accounts - Generate Private Key
58
+
59
+ If your code generates private keys, here’s how to migrate that functionality. In web3.js if you are using:
60
+
61
+ ``` javascript
62
+ const privateKey = web3 .eth .accounts .create ().privateKey ;
63
+ console .log (privateKey);
64
+ ```
65
+
66
+ To migrate this to Viem, you'll use the ` generatePrivateKey() ` function from the 'viem/accounts' module.
67
+
68
+ ``` javascript
69
+ import { generatePrivateKey } from ' viem/accounts' ;
70
+
71
+ const privateKey = generatePrivateKey ();
72
+ console .log (privateKey);
73
+ ```
74
+
75
+ ## Wallets and Accounts - Create a Wallet
76
+
77
+ When migrating from Web3.js to Viem, you'll need to update how you create and manage wallets. The process of adding accounts to wallets differs between the two libraries. In web3.js :
78
+
79
+ ``` javascript
80
+ const web3 = new Web3 ();
81
+ const wallet = web3 .eth .accounts .wallet .add (privateKey);
82
+ console .log (wallet[0 ].address );
83
+ ```
84
+
85
+ To migrate this to Viem, you'll use privateKeyToAccount() to create an account, and then can pass it to createWalletClient() for using it with client.
86
+
87
+ ``` javascript
88
+ import { createWalletClient , http } from ' viem' ;
89
+ import { privateKeyToAccount } from ' viem/accounts' ;
90
+ import { mainnet } from ' viem/chains' ;
91
+
92
+ const account = privateKeyToAccount (privateKey);
93
+
94
+ const client = createWalletClient ({
95
+ account,
96
+ chain: mainnet,
97
+ transport: http (),
98
+ });
99
+ ```
100
+
101
+ ## Signing Messages
102
+
103
+ Update how you handle message signing, following is web3.js example:
104
+
105
+ ``` javascript
106
+ const signature = web3 .eth .accounts .sign (' Some data' , privateKey).signature ;
107
+ console .log (signature);
108
+ ```
109
+
110
+ To sign message using Viem, you can use ` signMessage() ` method.
111
+
112
+ ``` javascript
113
+ import { createWalletClient , custom } from ' viem'
114
+ import { mainnet } from ' viem/chains'
115
+
116
+ const walletClient = createWalletClient ({
117
+ chain: mainnet,
118
+ transport: custom (window .ethereum ! ),
119
+ });
120
+
121
+ const [account ] = await walletClient .getAddresses ();
122
+
123
+ const signature = await walletClient .signMessage ({
124
+ account,
125
+ message: ' Some data' ,
126
+ });
127
+
128
+ ```
129
+
130
+ ## Transaction
131
+
132
+ When migrating transaction sending code, you'll need to update how transactions are signed and sent.
133
+
134
+ ``` javascript
135
+ const tx = await web3 .eth .sendTransaction ({
136
+ from: account,
137
+ to: ' 0x92d3267215Ec56542b985473E73C8417403B15ac' ,
138
+ value: web3 .utils .toWei (' 0.001' , ' ether' ),
139
+ });
140
+ ```
141
+
142
+ In Viem there is ` sendTransaction() ` function avalible with walletClient.
143
+
144
+ ``` javascript
145
+ import { createWalletClient , custom , parseEther } from ' viem'
146
+ import { mainnet } from ' viem/chains'
147
+
148
+ const walletClient = createWalletClient ({
149
+ chain: mainnet,
150
+ transport: custom (window .ethereum ! ),
151
+ });
152
+
153
+ const [account ] = await walletClient .getAddresses ();
154
+
155
+ const hash = await walletClient .sendTransaction ({
156
+ account,
157
+ to: ' 0x92d3267215Ec56542b985473E73C8417403B15ac' ,
158
+ value: parseEther (' 0.001' )
159
+ });
160
+
161
+ ```
162
+
163
+ ## Contracts
164
+
165
+ ### Contract Deployment
166
+
167
+ When migrating contract deployment code, you'll need to update from Web3.js's deploy and send pattern:
168
+
169
+ ``` javascript
170
+ // use existing web3 instance connected with provider
171
+ const contract = new web3.eth.Contract (abi);
172
+ const deployTx = await contract
173
+ .deploy ({
174
+ data: bytecode,
175
+ arguments: [' constructor param' ],
176
+ })
177
+ .send ({
178
+ from: account,
179
+ gas: ' 1000000' ,
180
+ });
181
+ console .log (deployTx .options .address );
182
+ ```
183
+
184
+ In Viem there is ` deployContract() ` function that can be used for contracts deployment.
185
+
186
+ ``` javascript
187
+ // import { deployContract } from 'viem'
188
+ import { createWalletClient , custom } from ' viem' ;
189
+ import { mainnet } from ' viem/chains' ;
190
+
191
+ const walletClient = createWalletClient ({
192
+ chain: mainnet,
193
+ transport: custom (window .ethereum ),
194
+ });
195
+
196
+ const hash = await walletClient .deployContract ({
197
+ abi,
198
+ account, // given account
199
+ args: [' constructor param' ],
200
+ bytecode: bytecode,
201
+ });
202
+ ```
203
+
204
+ ### Contract Method Calls
205
+
206
+ When migrating contract method calls, you'll need to update from Web3.js's ` contract.methods.someFunction().call() `
207
+
208
+ ``` javascript
209
+ const contract = new web3.eth.Contract (ABI , CONTRACT_ADDRESS );
210
+ const result = await contract .methods .someFunction ().call ();
211
+ console .log (result);
212
+ ```
213
+
214
+ In Viem ` readContract() ` function can be used for method calls.
215
+
216
+ ``` javascript
217
+ const data = await publicClient .readContract ({
218
+ address: ' 0x92d3267215Ec56542b985473E73C8417403B15ac' ,
219
+ abi: wagmiAbi,
220
+ functionName: ' tokenTotalSupply' ,
221
+ });
222
+ ```
223
+
224
+ ### Contract Events
225
+
226
+ When migrating event handling code, you'll need to update from Web3.js's events code :
227
+
228
+ ``` javascript
229
+ const event = contract .events .SomeEvent ({ fromBlock: 0 });
230
+ event .on (' data' , resolve);
231
+ event .on (' error' , reject);
232
+ ```
233
+
234
+ In Viem there is ` watchContractEvent() ` function.
235
+
236
+ ``` javascript
237
+ const unwatch = publicClient .watchContractEvent ({
238
+ address: ' 0x92d3267215Ec56542b985473E73C8417403B15ac' ,
239
+ abi: wagmiAbi,
240
+ eventName: ' Transfer' ,
241
+ args: { from: ' 0x34d3267215Ec56542b985473E73C8417403B1533' },
242
+ onLogs : logs => func (logs),
243
+ });
244
+ ```
245
+
246
+ ## Utility Methods
247
+
248
+ ### Hashing
249
+
250
+ When migrating code that computes Keccak-256 hashes, you'll need to update from Web3.js's utility method:
251
+
252
+ ```
253
+ // keccak256 method with broader input support in web3.js
254
+ const hash = web3.utils.keccak256('hello world');
255
+ ```
256
+
257
+ In Viem there is ` keccak256() ` function for keccak256.
258
+
259
+ ```
260
+ import { keccak256 , toHex } from 'viem'
261
+
262
+ keccak256(toHex('hello world'));
263
+ ```
0 commit comments