Skip to content

Commit

Permalink
Add EthersContract to the README (#18)
Browse files Browse the repository at this point in the history
* Add EthersContract to the README

* update exports
  • Loading branch information
0xslipk authored Apr 21, 2021
1 parent aef4fb0 commit 61088ef
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
81 changes: 66 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NestJS-Ethers
[![supported platforms](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)


Ethereum wallet implementation and utilities for NestJS based on [Ethers.js](https://github.com/ethers-io/ethers.js/)
Ethereum utilities for NestJS based on [Ethers.js](https://github.com/ethers-io/ethers.js/)

## Install

Expand Down Expand Up @@ -274,16 +274,16 @@ export class TestService {

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

Create a `Wallet` from a private key:
Create a `WalletSigner` from a private key:

```ts
import { EthersSigner } from 'nestjs-ethers';
import { EthersSigner, WalletSigner } from 'nestjs-ethers';

@Injectable()
export class TestService {
constructor(private readonly ethersSigner: EthersSigner) {}
async someMethod(): Promise<string> {
const wallet = this.ethersSigner.createWallet(
const wallet: WalletSigner = this.ethersSigner.createWallet(
'0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'
);

Expand All @@ -292,32 +292,32 @@ export class TestService {
}
```

Create a random `Wallet`:
Create a random `WalletSigner`:

```ts
import { EthersSigner } from 'nestjs-ethers';
import { EthersSigner, WalletSigner } from 'nestjs-ethers';

@Injectable()
export class TestService {
constructor(private readonly ethersSigner: EthersSigner) {}
async someMethod(): Promise<string> {
const wallet = this.ethersSigner.createRandomWallet();
const wallet: WalletSigner = this.ethersSigner.createRandomWallet();

return wallet.getAddress();
}
}
```

Create a `Wallet` from an encrypted JSON:
Create a `WalletSigner` from an encrypted JSON:

```ts
import { EthersSigner } from 'nestjs-ethers';
import { EthersSigner, WalletSigner } from 'nestjs-ethers';

@Injectable()
export class TestService {
constructor(private readonly ethersSigner: EthersSigner) {}
async someMethod(): Promise<string> {
const wallet = this.ethersSigner.createWalletfromEncryptedJson(
const wallet: WalletSigner = this.ethersSigner.createWalletfromEncryptedJson(
{
address: '012363d61bdc53d0290a0f25e9c89f8257550fb8',
id: '5ba8719b-faf9-49ec-8bca-21522e3d56dc',
Expand Down Expand Up @@ -353,16 +353,16 @@ export class TestService {
}
```

Create a `Wallet` from a mnemonic:
Create a `WalletSigner` from a mnemonic:

```ts
import { EthersSigner } from 'nestjs-ethers';
import { EthersSigner, WalletSigner } from 'nestjs-ethers';

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

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

```ts
import { EthersSigner } from 'nestjs-ethers';
import { EthersSigner, VoidSigner } from 'nestjs-ethers';

@Injectable()
export class TestService {
constructor(private readonly ethersSigner: EthersSigner) {}
async someMethod(): Promise<string> {
const wallet = this.ethersSigner.createVoidSigner(
const wallet: VoidSigner = this.ethersSigner.createVoidSigner(
'0x012363d61bdc53d0290a0f25e9c89f8257550fb8'
);

Expand All @@ -389,6 +389,57 @@ export class TestService {
}
```

## EthersContract

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

Create a `SmartContract` attached to an address:

```ts
import { EthersContract, SmartContract } from 'nestjs-ethers';
import * as ABI from './utils/ABI.json';

@Injectable()
class TestService {
constructor(private readonly ethersContract: EthersContract) {}
async someMethod(): Promise<string> {
const contract: SmartContract = this.ethersContract.create(
'0x012363d61bdc53d0290a0f25e9c89f8257550fb8',
ABI,
);

return contract.provider.getNetwork();
}
}
```

Create a `SmartContract` with a WalletSigner:

```ts
import { EthersContract, EthersSigner, SmartContract, WalletSigner } from 'nestjs-ethers';
import * as ABI from './utils/ABI.json';

@Injectable()
class TestService {
constructor(
private readonly ethersContract: EthersContract,
private readonly ethersSigner: EthersSigner,
) {}
async someMethod(): Promise<string> {
const wallet: WalletSigner = this.ethersSigner.createWallet(
'0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'
);
const contract: SmartContract = this.ethersContract.create(
'0x012363d61bdc53d0290a0f25e9c89f8257550fb8',
ABI,
wallet,
);

return contract.signer.provider.getNetwork();
}
}
```

## Testing a class that uses @InjectEthersProvider

This package exposes a getEthersToken() function that returns a prepared injection token based on the provided context.
Expand Down
2 changes: 1 addition & 1 deletion __tests__/ethers.contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('EthersSigner', () => {

for (const PlatformAdapter of platforms) {
describe(PlatformAdapter.name, () => {
it('should create an instance of the SmartContract attached to a address with a provider injected', async () => {
it('should create an instance of the SmartContract attached to an address with a provider injected', async () => {
@Injectable()
class TestService {
constructor(private readonly ethersContract: EthersContract) {}
Expand Down
9 changes: 6 additions & 3 deletions src/ethers.contract.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Injectable } from '@nestjs/common';
import { BaseProvider } from '@ethersproject/providers';
import { InjectEthersProvider } from './ethers.decorators';
import { Contract, ContractInterface } from '@ethersproject/contracts';
import {
Contract,
ContractInterface as SmartContractInterface,
} from '@ethersproject/contracts';
import { Wallet as WalletSigner } from '@ethersproject/wallet';
import { VoidSigner } from '@ethersproject/abstract-signer';

export class SmartContract extends Contract {
constructor(
address: string,
abi: ContractInterface,
abi: SmartContractInterface,
provider: BaseProvider,
signer?: WalletSigner | VoidSigner,
) {
Expand All @@ -27,7 +30,7 @@ export class EthersContract {

create(
address: string,
abi: ContractInterface,
abi: SmartContractInterface,
signer?: WalletSigner | VoidSigner,
): SmartContract {
return new SmartContract(address, abi, this.provider, signer);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ export { Wallet as WalletSigner } from '@ethersproject/wallet';
export { ProgressCallback } from '@ethersproject/json-wallets';
export { Wordlist } from '@ethersproject/wordlists';
export {
ContractInterface,
ContractInterface as SmartContractInterface,
ContractFactory as SmartContractFactory,
} from '@ethersproject/contracts';

0 comments on commit 61088ef

Please sign in to comment.