npm package: @nerdscomputing/rx-elrond
Description: Rxjs wrapper over the erdjs npm package
Author: NerdsComputing
Latest version: 0.0.7

# @nerdscomputing/rx-elrond
Rxjs wrapper over the [erdjs](https://www.npmjs.com/package/@elrondnetwork/erdjs) npm package.

## Installation
`npm i @nerdscomputing/rx-elrond`

## Dependencies
* [rxjs](https://www.npmjs.com/package/rxjs) version 6.6.7+
* [erdjs](https://www.npmjs.com/package/@elrondnetwork/erdjs) version 4.0.3+
* [ts-pattern](https://www.npmjs.com/package/ts-pattern) version 3.1.2+

## Usage
The package exposes an initialization function (called `init`) which returns an object of type 
`RxElrond` that contains all the implemented functionalities.

These functionalities are:
### `account.get: (input: GetAccountInput) => Observable<Account>`
> Returns a synchronised `Account` object based on an account address or mnemonic  
 
### `account.generateMnemonic: () => Observable<Mnemonic>`
> Generates a new account mnemonic

### `transaction.build: (input: TransactionInput) => Observable<Transaction>`
> Builds and signs a new `Transaction` based on the given input

### `transaction.send: (transaction: Transaction) => Observable<SendResponse>`
> Sends an already signed transaction

### `transaction.status: (txHash: string) => Observable<TransactionStatus>`
> Gets the network status of an already sent transaction

The signature of the `init` function is the following:
### `init: (network: Network<Gateway>): RxElrond`

## Exposed types

### `TransactionInput`
Input type used by the `transaction.build` function. Its declaration is the following:
```ts
type TransactionInput = {
    mnemonic: string,
    receiver: string,
    value: number,
    nonce: number,
    data?: any
}
```

### `SendResponse`
Result type returned by the `transaction.send` function. Its declaration is the following:
```ts
type SendResponse = {
    hash: string,
    transaction: Transaction
}
```

### `GetAccountInput`
Input type used by the `account.get` function. Its declaration is the following:
```ts
type Input = {
    type: 'address' | 'mnemonic',
    value: string
}
```

### `Gateway`
Enum type representing the gateway url. Its declaration is the following:
```ts
enum Gateway {
    TestNet = 'https://testnet-gateway.elrond.com',
    DevNet = 'https://devnet-gateway.elrond.com',
    MainNet = 'https://gateway.elrond.com'
}
```

### `Network`
Input type used by the `init` function to set network parameters. Its declaration is the following:
```ts
type Network<GatewayType extends Gateway> = {
    gateway: GatewayType
}
```


## Example
```ts
import {mergeMap, tap} from 'rxjs/operators'
import {init, Gateway, TransactionInput} from '@nerdscomputing/rx-elrond'

const {account, transaction} = init({gateway: Gateway.TestNet})

// Get an account object and print it
account.get({type: 'address', value: 'exampleAddress'})
    .pipe(tap(account => console.log(account)))
    .subscribe()

account.get({type: 'mnemonic', value: 'exampleMnemonic'})
    .pipe(tap(account => console.log(account)))
    .subscribe()

// Generate an account mnemonic and print it
account.generateMnemonic()
    .pipe(tap(mnemonic => console.log(mnemonic)))
    .subscribe()

// Build and send a transaction
const input: TransactionInput = {
    mnemonic: 'exampleMnemonic',
    receiver: 'exampleAddress2',
    value: 0,
    nonce: 16,
    data: 'exampleData'
}

transaction.build(input)
    .pipe(mergeMap(transaction.send))
    .pipe(tap(response => console.log('Tx hash is ', response.hash)))
    .subscribe()

// Get a transaction's status

const txHash = 'exampleTransactionHash'

transaction.status(txHash)
    .pipe(tap(status => console.log(status)))
    .subscribe()

```