@@ -10,7 +10,7 @@ NestJS-Ethers
10
10
[ ![ supported platforms] ( https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green )] ( https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green )
11
11
12
12
13
- Ethereum wallet implementation and utilities for NestJS based on [ Ethers.js] ( https://github.com/ethers-io/ethers.js/ )
13
+ Ethereum utilities for NestJS based on [ Ethers.js] ( https://github.com/ethers-io/ethers.js/ )
14
14
15
15
## Install
16
16
@@ -274,16 +274,16 @@ export class TestService {
274
274
275
275
` EthersSigner ` implements methods to create a [ WalletSigner] ( https://docs.ethers.io/v5/api/signer/#Wallet ) or [ VoidSigner] ( https://docs.ethers.io/v5/api/signer/#VoidSigner ) . A ` Signer ` in ethers is an abstraction of an Ethereum Account, which can be used to sign messages and transactions and send signed transactions to the Ethereum Network. This service will also inject the ` EthersBaseProvider ` into the wallet.
276
276
277
- Create a ` Wallet ` from a private key:
277
+ Create a ` WalletSigner ` from a private key:
278
278
279
279
``` ts
280
- import { EthersSigner } from ' nestjs-ethers' ;
280
+ import { EthersSigner , WalletSigner } from ' nestjs-ethers' ;
281
281
282
282
@Injectable ()
283
283
export class TestService {
284
284
constructor (private readonly ethersSigner : EthersSigner ) {}
285
285
async someMethod(): Promise <string > {
286
- const wallet = this .ethersSigner .createWallet (
286
+ const wallet: WalletSigner = this .ethersSigner .createWallet (
287
287
' 0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'
288
288
);
289
289
@@ -292,32 +292,32 @@ export class TestService {
292
292
}
293
293
```
294
294
295
- Create a random ` Wallet ` :
295
+ Create a random ` WalletSigner ` :
296
296
297
297
``` ts
298
- import { EthersSigner } from ' nestjs-ethers' ;
298
+ import { EthersSigner , WalletSigner } from ' nestjs-ethers' ;
299
299
300
300
@Injectable ()
301
301
export class TestService {
302
302
constructor (private readonly ethersSigner : EthersSigner ) {}
303
303
async someMethod(): Promise <string > {
304
- const wallet = this .ethersSigner .createRandomWallet ();
304
+ const wallet: WalletSigner = this .ethersSigner .createRandomWallet ();
305
305
306
306
return wallet .getAddress ();
307
307
}
308
308
}
309
309
```
310
310
311
- Create a ` Wallet ` from an encrypted JSON:
311
+ Create a ` WalletSigner ` from an encrypted JSON:
312
312
313
313
``` ts
314
- import { EthersSigner } from ' nestjs-ethers' ;
314
+ import { EthersSigner , WalletSigner } from ' nestjs-ethers' ;
315
315
316
316
@Injectable ()
317
317
export class TestService {
318
318
constructor (private readonly ethersSigner : EthersSigner ) {}
319
319
async someMethod(): Promise <string > {
320
- const wallet = this .ethersSigner .createWalletfromEncryptedJson (
320
+ const wallet: WalletSigner = this .ethersSigner .createWalletfromEncryptedJson (
321
321
{
322
322
address: ' 012363d61bdc53d0290a0f25e9c89f8257550fb8' ,
323
323
id: ' 5ba8719b-faf9-49ec-8bca-21522e3d56dc' ,
@@ -353,16 +353,16 @@ export class TestService {
353
353
}
354
354
```
355
355
356
- Create a ` Wallet ` from a mnemonic:
356
+ Create a ` WalletSigner ` from a mnemonic:
357
357
358
358
``` ts
359
- import { EthersSigner } from ' nestjs-ethers' ;
359
+ import { EthersSigner , WalletSigner } from ' nestjs-ethers' ;
360
360
361
361
@Injectable ()
362
362
export class TestService {
363
363
constructor (private readonly ethersSigner : EthersSigner ) {}
364
364
async someMethod(): Promise <string > {
365
- const wallet = this .ethersSigner .createWalletfromMnemonic (
365
+ const wallet: WalletSigner = this .ethersSigner .createWalletfromMnemonic (
366
366
' service basket parent alcohol fault similar survey twelve hockey cloud walk panel'
367
367
);
368
368
@@ -374,13 +374,13 @@ export class TestService {
374
374
Create a ` VoidSigner ` from an address:
375
375
376
376
``` ts
377
- import { EthersSigner } from ' nestjs-ethers' ;
377
+ import { EthersSigner , VoidSigner } from ' nestjs-ethers' ;
378
378
379
379
@Injectable ()
380
380
export class TestService {
381
381
constructor (private readonly ethersSigner : EthersSigner ) {}
382
382
async someMethod(): Promise <string > {
383
- const wallet = this .ethersSigner .createVoidSigner (
383
+ const wallet: VoidSigner = this .ethersSigner .createVoidSigner (
384
384
' 0x012363d61bdc53d0290a0f25e9c89f8257550fb8'
385
385
);
386
386
@@ -389,6 +389,57 @@ export class TestService {
389
389
}
390
390
```
391
391
392
+ ## EthersContract
393
+
394
+ ` EthersContract ` implements a method for the creation of a [ SmartContract] ( https://docs.ethers.io/v5/api/contract/ ) instance. This service will also inject the ` EthersBaseProvider ` into the contract.
395
+
396
+ Create a ` SmartContract ` attached to an address:
397
+
398
+ ``` ts
399
+ import { EthersContract , SmartContract } from ' nestjs-ethers' ;
400
+ import * as ABI from ' ./utils/ABI.json' ;
401
+
402
+ @Injectable ()
403
+ class TestService {
404
+ constructor (private readonly ethersContract : EthersContract ) {}
405
+ async someMethod(): Promise <string > {
406
+ const contract: SmartContract = this .ethersContract .create (
407
+ ' 0x012363d61bdc53d0290a0f25e9c89f8257550fb8' ,
408
+ ABI ,
409
+ );
410
+
411
+ return contract .provider .getNetwork ();
412
+ }
413
+ }
414
+ ```
415
+
416
+ Create a ` SmartContract ` with a WalletSigner:
417
+
418
+ ``` ts
419
+ import { EthersContract , EthersSigner , SmartContract , WalletSigner } from ' nestjs-ethers' ;
420
+ import * as ABI from ' ./utils/ABI.json' ;
421
+
422
+ @Injectable ()
423
+ class TestService {
424
+ constructor (
425
+ private readonly ethersContract : EthersContract ,
426
+ private readonly ethersSigner : EthersSigner ,
427
+ ) {}
428
+ async someMethod(): Promise <string > {
429
+ const wallet: WalletSigner = this .ethersSigner .createWallet (
430
+ ' 0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'
431
+ );
432
+ const contract: SmartContract = this .ethersContract .create (
433
+ ' 0x012363d61bdc53d0290a0f25e9c89f8257550fb8' ,
434
+ ABI ,
435
+ wallet ,
436
+ );
437
+
438
+ return contract .signer .provider .getNetwork ();
439
+ }
440
+ }
441
+ ```
442
+
392
443
## Testing a class that uses @InjectEthersProvider
393
444
394
445
This package exposes a getEthersToken() function that returns a prepared injection token based on the provided context.
0 commit comments