@@ -19,7 +19,7 @@ npm install ethers
19
19
20
20
When migrating from Web3.js to ethers.js, the first step is to update how you connect to the Ethereum network. Both libraries use providers, but their initialization differs.
21
21
22
- ``` javascript
22
+ ``` typescript
23
23
import { Web3 } from ' web3' ;
24
24
25
25
// private RPC endpoint
@@ -31,7 +31,7 @@ console.log(blockNumber);
31
31
32
32
To migrate this to ethers.js, you'll need to replace it with JsonRpcProvider. Note that ethers.js separates provider types more explicitly:
33
33
34
- ``` javascript
34
+ ``` typescript
35
35
import { ethers } from ' ethers' ;
36
36
37
37
// ethers.js v6
@@ -45,13 +45,13 @@ console.log(blockNumber);
45
45
46
46
When migrating browser wallet connections, you'll need to update how you handle the injected provider (like MetaMask). Here's your existing Web3.js code:
47
47
48
- ``` javascript
48
+ ``` typescript
49
49
const web3 = new Web3 (window .ethereum );
50
50
```
51
51
52
52
In ethers.js v6, you'll need to use the BrowserProvider class instead. This provider is specifically designed for browser environments:
53
53
54
- ``` javascript
54
+ ``` typescript
55
55
// in v6
56
56
const provider = new ethers .BrowserProvider (window .ethereum );
57
57
```
@@ -60,7 +60,7 @@ const provider = new ethers.BrowserProvider(window.ethereum);
60
60
61
61
If your code generates private keys with Web3.js, here's how to migrate that functionality. Your existing Web3.js code:
62
62
63
- ``` javascript
63
+ ``` typescript
64
64
// this would generate a private key similar to:
65
65
// '0x286f65c4191759fc5c7e6083b8c275ac2238cc7abb5915bd8c905ae4404215c9'
66
66
// (Be sure to store it encrypted in a safe place)
@@ -69,7 +69,7 @@ const privateKey = web3.eth.accounts.create().privateKey;
69
69
70
70
To achieve the same in ethers.js, use the ` Wallet.createRandom() ` method:
71
71
72
- ``` javascript
72
+ ``` typescript
73
73
// this would generate a private key similar to:
74
74
// '0x286f65c4191759fc5c7e6083b8c275ac2238cc7abb5915bd8c905ae4404215c9'
75
75
// (Be sure to store it encrypted in a safe place)
@@ -80,7 +80,7 @@ const privateKey = ethers.Wallet.createRandom().privateKey;
80
80
81
81
When migrating wallet creation code, you'll need to change how accounts are added to wallets. Your existing Web3.js code using ` wallet.add() ` :
82
82
83
- ``` javascript
83
+ ``` typescript
84
84
const web3 = new Web3 ();
85
85
const wallet = web3 .eth .accounts .wallet .add (
86
86
// you can generate a private key using web3.eth.accounts.create().privateKey
@@ -93,7 +93,7 @@ console.log(wallet[0].address);
93
93
94
94
In ethers.js, wallet creation uses the Wallet constructor:
95
95
96
- ``` javascript
96
+ ``` typescript
97
97
const wallet = new ethers .Wallet (
98
98
// A private key that you might had generated with:
99
99
ethers .Wallet .createRandom ().privateKey ,
@@ -108,21 +108,21 @@ console.log(wallet.address);
108
108
109
109
When migrating code that gets the current account, you'll need to change from Web3.js's getAccounts():
110
110
111
- ``` javascript
111
+ ``` typescript
112
112
const account = (await web3 .eth .getAccounts ())[0 ];
113
113
```
114
114
115
115
To ethers.js's getSigner() method, which returns a signer object instead of just an address:
116
116
117
- ``` javascript
117
+ ``` typescript
118
118
const signer = await provider .getSigner ();
119
119
```
120
120
121
121
### Signing
122
122
123
123
When migrating message signing functionality, you'll need to update from Web3.js's sign methods:
124
124
125
- ``` javascript
125
+ ``` typescript
126
126
// Sign with web3.js, using a private key:
127
127
const signature = web3 .eth .accounts .sign (' Some data' , privateKey ).signature ;
128
128
@@ -135,7 +135,7 @@ const signature = await web3.eth.sign(
135
135
136
136
In ethers.js, signing is simplified using the signMessage method:
137
137
138
- ``` javascript
138
+ ``` typescript
139
139
const signer = new ethers .Wallet (privateKey );
140
140
const signature = await signer .signMessage (' Some data' );
141
141
```
@@ -146,7 +146,7 @@ const signature = await signer.signMessage('Some data');
146
146
147
147
When migrating transaction sending code, you'll need to update how transactions are signed and sent. Your existing Web3.js code where transactions are signed using an unlocked or added account:
148
148
149
- ``` javascript
149
+ ``` typescript
150
150
const web3 = new Web3 (url );
151
151
152
152
// Add wallet to be used as a signer
@@ -163,7 +163,7 @@ console.log(tx);
163
163
164
164
In ethers.js, transactions are sent using a signer instance, which combines the private key and provider:
165
165
166
- ``` javascript
166
+ ``` typescript
167
167
const signer = new ethers .Wallet (privateKey , provider );
168
168
169
169
const tx = await signer .sendTransaction ({
@@ -177,7 +177,7 @@ console.log(tx);
177
177
178
178
When migrating code that separates transaction signing and broadcasting, you'll need to update from Web3.js's two-step process:
179
179
180
- ``` javascript
180
+ ``` typescript
181
181
const transaction = {
182
182
from: senderPublicAddress ,
183
183
to: receiverPublicAddress ,
@@ -194,7 +194,7 @@ console.log(tx);
194
194
195
195
In ethers.js, you can broadcast a pre-signed transaction using the provider's broadcastTransaction method:
196
196
197
- ``` javascript
197
+ ``` typescript
198
198
await provider .broadcastTransaction (signedTx );
199
199
```
200
200
@@ -204,7 +204,7 @@ await provider.broadcastTransaction(signedTx);
204
204
205
205
When migrating contract deployment code, you'll need to update from Web3.js's deploy and send pattern:
206
206
207
- ``` javascript
207
+ ``` typescript
208
208
const contract = new web3 .eth .Contract (abi );
209
209
const deployTx = await contract
210
210
.deploy ({
@@ -221,7 +221,7 @@ console.log('contract address', deployTx.options.address);
221
221
222
222
In ethers.js, contract deployment uses the ContractFactory class:
223
223
224
- ``` javascript
224
+ ``` typescript
225
225
const signer = provider .getSigner ();
226
226
const factory = new ethers .ContractFactory (abi , bytecode , signer );
227
227
const contract = await factory .deploy (' constructor param' );
@@ -235,7 +235,7 @@ await contract.deployTransaction.wait();
235
235
236
236
When migrating contract method calls, you'll need to update from Web3.js's methods object pattern:
237
237
238
- ``` javascript
238
+ ``` typescript
239
239
const contract = new web3 .eth .Contract (ABI , CONTRACT_ADDRESS );
240
240
241
241
// For read operations
@@ -247,7 +247,7 @@ const tx = await contract.methods.someFunction().send();
247
247
248
248
In ethers.js, contract methods are called directly as functions:
249
249
250
- ``` javascript
250
+ ``` typescript
251
251
const contract = new ethers .Contract (CONTRACT_ADDRESS , ABI , provider );
252
252
const result = await contract .someFunction ();
253
253
```
@@ -256,7 +256,7 @@ const result = await contract.someFunction();
256
256
257
257
When migrating event handling code, you'll need to update from Web3.js's events interface:
258
258
259
- ``` javascript
259
+ ``` typescript
260
260
const event = contract .events .SomeEvent ({
261
261
filter: { val: 100 },
262
262
fromBlock: 0 ,
@@ -268,7 +268,7 @@ event.on('error', reject);
268
268
269
269
In ethers.js, event listening is :
270
270
271
- ``` javascript
271
+ ``` typescript
272
272
contract .on (' SomeEvent' , (arg1 , arg2 , event ) => {
273
273
// event handling
274
274
});
@@ -278,15 +278,15 @@ contract.on('SomeEvent', (arg1, arg2, event) => {
278
278
279
279
When migrating gas estimation code, you'll need to update from Web3.js's estimateGas method:
280
280
281
- ``` javascript
281
+ ``` typescript
282
282
const gasAmount = await contract .methods .myMethod (123 ).estimateGas ({
283
283
from: transactionSenderAddress ,
284
284
});
285
285
```
286
286
287
287
In ethers.js, gas estimation is made through a direct method call:
288
288
289
- ``` javascript
289
+ ``` typescript
290
290
const gasEstimate = await contract .myMethod .estimateGas (123 );
291
291
```
292
292
@@ -296,7 +296,7 @@ const gasEstimate = await contract.myMethod.estimateGas(123);
296
296
297
297
When migrating code that computes Keccak-256 hashes, you'll need to update from Web3.js's utility methods:
298
298
299
- ``` javascript
299
+ ``` typescript
300
300
// computes the Keccak-256 hash of the input and returns a hexstring
301
301
const hash1 = web3 .utils .sha3 (' hello world' );
302
302
@@ -306,7 +306,7 @@ const hash2 = web3.utils.keccak256('hello world');
306
306
307
307
In ethers.js, hashing requires explicit conversion of strings to bytes:
308
308
309
- ``` javascript
309
+ ``` typescript
310
310
import { keccak256 , toUtf8Bytes } from ' ethers' ;
311
311
312
312
const message = ' Hello, World!' ;
@@ -318,7 +318,7 @@ const hash = keccak256(messageBytes);
318
318
319
319
When migrating code that converts between ether units, you'll need to update from Web3.js's fromWei and toWei methods:
320
320
321
- ``` javascript
321
+ ``` typescript
322
322
// Convert Wei to Ether
323
323
const fromWeiToEther = web3 .utils .fromWei (' 1000000000000000000' , ' ether' );
324
324
// outputs: 1
@@ -332,7 +332,7 @@ console.log(fromEtherToWei);
332
332
333
333
In ethers.js, use formatEther and parseEther for common ether conversions:
334
334
335
- ``` javascript
335
+ ``` typescript
336
336
// Convert Wei to Ether
337
337
const fromWeiToEther = ethers .formatEther (' 1000000000000000000' );
338
338
// outputs: 1.0
0 commit comments