Skip to content

Commit 61088ef

Browse files
authored
Add EthersContract to the README (#18)
* Add EthersContract to the README * update exports
1 parent aef4fb0 commit 61088ef

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

README.md

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NestJS-Ethers
1010
[![supported platforms](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)
1111

1212

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/)
1414

1515
## Install
1616

@@ -274,16 +274,16 @@ export class TestService {
274274

275275
`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.
276276

277-
Create a `Wallet` from a private key:
277+
Create a `WalletSigner` from a private key:
278278

279279
```ts
280-
import { EthersSigner } from 'nestjs-ethers';
280+
import { EthersSigner, WalletSigner } from 'nestjs-ethers';
281281

282282
@Injectable()
283283
export class TestService {
284284
constructor(private readonly ethersSigner: EthersSigner) {}
285285
async someMethod(): Promise<string> {
286-
const wallet = this.ethersSigner.createWallet(
286+
const wallet: WalletSigner = this.ethersSigner.createWallet(
287287
'0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'
288288
);
289289

@@ -292,32 +292,32 @@ export class TestService {
292292
}
293293
```
294294

295-
Create a random `Wallet`:
295+
Create a random `WalletSigner`:
296296

297297
```ts
298-
import { EthersSigner } from 'nestjs-ethers';
298+
import { EthersSigner, WalletSigner } from 'nestjs-ethers';
299299

300300
@Injectable()
301301
export class TestService {
302302
constructor(private readonly ethersSigner: EthersSigner) {}
303303
async someMethod(): Promise<string> {
304-
const wallet = this.ethersSigner.createRandomWallet();
304+
const wallet: WalletSigner = this.ethersSigner.createRandomWallet();
305305

306306
return wallet.getAddress();
307307
}
308308
}
309309
```
310310

311-
Create a `Wallet` from an encrypted JSON:
311+
Create a `WalletSigner` from an encrypted JSON:
312312

313313
```ts
314-
import { EthersSigner } from 'nestjs-ethers';
314+
import { EthersSigner, WalletSigner } from 'nestjs-ethers';
315315

316316
@Injectable()
317317
export class TestService {
318318
constructor(private readonly ethersSigner: EthersSigner) {}
319319
async someMethod(): Promise<string> {
320-
const wallet = this.ethersSigner.createWalletfromEncryptedJson(
320+
const wallet: WalletSigner = this.ethersSigner.createWalletfromEncryptedJson(
321321
{
322322
address: '012363d61bdc53d0290a0f25e9c89f8257550fb8',
323323
id: '5ba8719b-faf9-49ec-8bca-21522e3d56dc',
@@ -353,16 +353,16 @@ export class TestService {
353353
}
354354
```
355355

356-
Create a `Wallet` from a mnemonic:
356+
Create a `WalletSigner` from a mnemonic:
357357

358358
```ts
359-
import { EthersSigner } from 'nestjs-ethers';
359+
import { EthersSigner, WalletSigner } from 'nestjs-ethers';
360360

361361
@Injectable()
362362
export class TestService {
363363
constructor(private readonly ethersSigner: EthersSigner) {}
364364
async someMethod(): Promise<string> {
365-
const wallet = this.ethersSigner.createWalletfromMnemonic(
365+
const wallet: WalletSigner = this.ethersSigner.createWalletfromMnemonic(
366366
'service basket parent alcohol fault similar survey twelve hockey cloud walk panel'
367367
);
368368

@@ -374,13 +374,13 @@ export class TestService {
374374
Create a `VoidSigner` from an address:
375375

376376
```ts
377-
import { EthersSigner } from 'nestjs-ethers';
377+
import { EthersSigner, VoidSigner } from 'nestjs-ethers';
378378

379379
@Injectable()
380380
export class TestService {
381381
constructor(private readonly ethersSigner: EthersSigner) {}
382382
async someMethod(): Promise<string> {
383-
const wallet = this.ethersSigner.createVoidSigner(
383+
const wallet: VoidSigner = this.ethersSigner.createVoidSigner(
384384
'0x012363d61bdc53d0290a0f25e9c89f8257550fb8'
385385
);
386386

@@ -389,6 +389,57 @@ export class TestService {
389389
}
390390
```
391391

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+
392443
## Testing a class that uses @InjectEthersProvider
393444

394445
This package exposes a getEthersToken() function that returns a prepared injection token based on the provided context.

__tests__/ethers.contract.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('EthersSigner', () => {
3131

3232
for (const PlatformAdapter of platforms) {
3333
describe(PlatformAdapter.name, () => {
34-
it('should create an instance of the SmartContract attached to a address with a provider injected', async () => {
34+
it('should create an instance of the SmartContract attached to an address with a provider injected', async () => {
3535
@Injectable()
3636
class TestService {
3737
constructor(private readonly ethersContract: EthersContract) {}

src/ethers.contract.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Injectable } from '@nestjs/common';
22
import { BaseProvider } from '@ethersproject/providers';
33
import { InjectEthersProvider } from './ethers.decorators';
4-
import { Contract, ContractInterface } from '@ethersproject/contracts';
4+
import {
5+
Contract,
6+
ContractInterface as SmartContractInterface,
7+
} from '@ethersproject/contracts';
58
import { Wallet as WalletSigner } from '@ethersproject/wallet';
69
import { VoidSigner } from '@ethersproject/abstract-signer';
710

811
export class SmartContract extends Contract {
912
constructor(
1013
address: string,
11-
abi: ContractInterface,
14+
abi: SmartContractInterface,
1215
provider: BaseProvider,
1316
signer?: WalletSigner | VoidSigner,
1417
) {
@@ -27,7 +30,7 @@ export class EthersContract {
2730

2831
create(
2932
address: string,
30-
abi: ContractInterface,
33+
abi: SmartContractInterface,
3134
signer?: WalletSigner | VoidSigner,
3235
): SmartContract {
3336
return new SmartContract(address, abi, this.provider, signer);

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ export { Wallet as WalletSigner } from '@ethersproject/wallet';
5555
export { ProgressCallback } from '@ethersproject/json-wallets';
5656
export { Wordlist } from '@ethersproject/wordlists';
5757
export {
58-
ContractInterface,
58+
ContractInterface as SmartContractInterface,
5959
ContractFactory as SmartContractFactory,
6060
} from '@ethersproject/contracts';

0 commit comments

Comments
 (0)