Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 839171d

Browse files
committed
update
1 parent d9a33fa commit 839171d

File tree

1 file changed

+28
-28
lines changed
  • docs/docs/guides/17_migration_from_other_libs

1 file changed

+28
-28
lines changed

docs/docs/guides/17_migration_from_other_libs/index.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install ethers
1919

2020
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.
2121

22-
```javascript
22+
```typescript
2323
import { Web3 } from 'web3';
2424

2525
// private RPC endpoint
@@ -31,7 +31,7 @@ console.log(blockNumber);
3131

3232
To migrate this to ethers.js, you'll need to replace it with JsonRpcProvider. Note that ethers.js separates provider types more explicitly:
3333

34-
```javascript
34+
```typescript
3535
import { ethers } from 'ethers';
3636

3737
// ethers.js v6
@@ -45,13 +45,13 @@ console.log(blockNumber);
4545

4646
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:
4747

48-
```javascript
48+
```typescript
4949
const web3 = new Web3(window.ethereum);
5050
```
5151

5252
In ethers.js v6, you'll need to use the BrowserProvider class instead. This provider is specifically designed for browser environments:
5353

54-
```javascript
54+
```typescript
5555
// in v6
5656
const provider = new ethers.BrowserProvider(window.ethereum);
5757
```
@@ -60,7 +60,7 @@ const provider = new ethers.BrowserProvider(window.ethereum);
6060

6161
If your code generates private keys with Web3.js, here's how to migrate that functionality. Your existing Web3.js code:
6262

63-
```javascript
63+
```typescript
6464
// this would generate a private key similar to:
6565
// '0x286f65c4191759fc5c7e6083b8c275ac2238cc7abb5915bd8c905ae4404215c9'
6666
// (Be sure to store it encrypted in a safe place)
@@ -69,7 +69,7 @@ const privateKey = web3.eth.accounts.create().privateKey;
6969

7070
To achieve the same in ethers.js, use the `Wallet.createRandom()` method:
7171

72-
```javascript
72+
```typescript
7373
// this would generate a private key similar to:
7474
// '0x286f65c4191759fc5c7e6083b8c275ac2238cc7abb5915bd8c905ae4404215c9'
7575
// (Be sure to store it encrypted in a safe place)
@@ -80,7 +80,7 @@ const privateKey = ethers.Wallet.createRandom().privateKey;
8080

8181
When migrating wallet creation code, you'll need to change how accounts are added to wallets. Your existing Web3.js code using `wallet.add()`:
8282

83-
```javascript
83+
```typescript
8484
const web3 = new Web3();
8585
const wallet = web3.eth.accounts.wallet.add(
8686
// you can generate a private key using web3.eth.accounts.create().privateKey
@@ -93,7 +93,7 @@ console.log(wallet[0].address);
9393

9494
In ethers.js, wallet creation uses the Wallet constructor:
9595

96-
```javascript
96+
```typescript
9797
const wallet = new ethers.Wallet(
9898
// A private key that you might had generated with:
9999
ethers.Wallet.createRandom().privateKey,
@@ -108,21 +108,21 @@ console.log(wallet.address);
108108

109109
When migrating code that gets the current account, you'll need to change from Web3.js's getAccounts():
110110

111-
```javascript
111+
```typescript
112112
const account = (await web3.eth.getAccounts())[0];
113113
```
114114

115115
To ethers.js's getSigner() method, which returns a signer object instead of just an address:
116116

117-
```javascript
117+
```typescript
118118
const signer = await provider.getSigner();
119119
```
120120

121121
### Signing
122122

123123
When migrating message signing functionality, you'll need to update from Web3.js's sign methods:
124124

125-
```javascript
125+
```typescript
126126
// Sign with web3.js, using a private key:
127127
const signature = web3.eth.accounts.sign('Some data', privateKey).signature;
128128

@@ -135,7 +135,7 @@ const signature = await web3.eth.sign(
135135

136136
In ethers.js, signing is simplified using the signMessage method:
137137

138-
```javascript
138+
```typescript
139139
const signer = new ethers.Wallet(privateKey);
140140
const signature = await signer.signMessage('Some data');
141141
```
@@ -146,7 +146,7 @@ const signature = await signer.signMessage('Some data');
146146

147147
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:
148148

149-
```javascript
149+
```typescript
150150
const web3 = new Web3(url);
151151

152152
// Add wallet to be used as a signer
@@ -163,7 +163,7 @@ console.log(tx);
163163

164164
In ethers.js, transactions are sent using a signer instance, which combines the private key and provider:
165165

166-
```javascript
166+
```typescript
167167
const signer = new ethers.Wallet(privateKey, provider);
168168

169169
const tx = await signer.sendTransaction({
@@ -177,7 +177,7 @@ console.log(tx);
177177

178178
When migrating code that separates transaction signing and broadcasting, you'll need to update from Web3.js's two-step process:
179179

180-
```javascript
180+
```typescript
181181
const transaction = {
182182
from: senderPublicAddress,
183183
to: receiverPublicAddress,
@@ -194,7 +194,7 @@ console.log(tx);
194194

195195
In ethers.js, you can broadcast a pre-signed transaction using the provider's broadcastTransaction method:
196196

197-
```javascript
197+
```typescript
198198
await provider.broadcastTransaction(signedTx);
199199
```
200200

@@ -204,7 +204,7 @@ await provider.broadcastTransaction(signedTx);
204204

205205
When migrating contract deployment code, you'll need to update from Web3.js's deploy and send pattern:
206206

207-
```javascript
207+
```typescript
208208
const contract = new web3.eth.Contract(abi);
209209
const deployTx = await contract
210210
.deploy({
@@ -221,7 +221,7 @@ console.log('contract address', deployTx.options.address);
221221

222222
In ethers.js, contract deployment uses the ContractFactory class:
223223

224-
```javascript
224+
```typescript
225225
const signer = provider.getSigner();
226226
const factory = new ethers.ContractFactory(abi, bytecode, signer);
227227
const contract = await factory.deploy('constructor param');
@@ -235,7 +235,7 @@ await contract.deployTransaction.wait();
235235

236236
When migrating contract method calls, you'll need to update from Web3.js's methods object pattern:
237237

238-
```javascript
238+
```typescript
239239
const contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS);
240240

241241
// For read operations
@@ -247,7 +247,7 @@ const tx = await contract.methods.someFunction().send();
247247

248248
In ethers.js, contract methods are called directly as functions:
249249

250-
```javascript
250+
```typescript
251251
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, provider);
252252
const result = await contract.someFunction();
253253
```
@@ -256,7 +256,7 @@ const result = await contract.someFunction();
256256

257257
When migrating event handling code, you'll need to update from Web3.js's events interface:
258258

259-
```javascript
259+
```typescript
260260
const event = contract.events.SomeEvent({
261261
filter: { val: 100 },
262262
fromBlock: 0,
@@ -268,7 +268,7 @@ event.on('error', reject);
268268

269269
In ethers.js, event listening is :
270270

271-
```javascript
271+
```typescript
272272
contract.on('SomeEvent', (arg1, arg2, event) => {
273273
// event handling
274274
});
@@ -278,15 +278,15 @@ contract.on('SomeEvent', (arg1, arg2, event) => {
278278

279279
When migrating gas estimation code, you'll need to update from Web3.js's estimateGas method:
280280

281-
```javascript
281+
```typescript
282282
const gasAmount = await contract.methods.myMethod(123).estimateGas({
283283
from: transactionSenderAddress,
284284
});
285285
```
286286

287287
In ethers.js, gas estimation is made through a direct method call:
288288

289-
```javascript
289+
```typescript
290290
const gasEstimate = await contract.myMethod.estimateGas(123);
291291
```
292292

@@ -296,7 +296,7 @@ const gasEstimate = await contract.myMethod.estimateGas(123);
296296

297297
When migrating code that computes Keccak-256 hashes, you'll need to update from Web3.js's utility methods:
298298

299-
```javascript
299+
```typescript
300300
// computes the Keccak-256 hash of the input and returns a hexstring
301301
const hash1 = web3.utils.sha3('hello world');
302302

@@ -306,7 +306,7 @@ const hash2 = web3.utils.keccak256('hello world');
306306

307307
In ethers.js, hashing requires explicit conversion of strings to bytes:
308308

309-
```javascript
309+
```typescript
310310
import { keccak256, toUtf8Bytes } from 'ethers';
311311

312312
const message = 'Hello, World!';
@@ -318,7 +318,7 @@ const hash = keccak256(messageBytes);
318318

319319
When migrating code that converts between ether units, you'll need to update from Web3.js's fromWei and toWei methods:
320320

321-
```javascript
321+
```typescript
322322
// Convert Wei to Ether
323323
const fromWeiToEther = web3.utils.fromWei('1000000000000000000', 'ether');
324324
// outputs: 1
@@ -332,7 +332,7 @@ console.log(fromEtherToWei);
332332

333333
In ethers.js, use formatEther and parseEther for common ether conversions:
334334

335-
```javascript
335+
```typescript
336336
// Convert Wei to Ether
337337
const fromWeiToEther = ethers.formatEther('1000000000000000000');
338338
// outputs: 1.0

0 commit comments

Comments
 (0)