npm package: @gluwa/creditcoin-js
Description: ## Getting started
Author: Creditcoin Devs
Latest version: 0.9.5

# creditcoin-js

## Getting started

### Preqrequisites

Creditcoin-js requires the following to be installed:

-   [Node.js](https://nodejs.org/en/)
-   [Yarn](https://yarnpkg.com/)

### Install

Adding Creditcoin-JS to your project is easy. Install it by using a JavaScript package manager like `npm` or `yarn`:

```shell
yarn add @gluwa/creditcoin-js
```

This will install the latest release version, which should allow you to interact with Creditcoin's main network and your own local chains that use the latest Creditcoin binaries.

## Usage

### Import

Importing the library into your project:

```typescript
import { creditcoinApi } from '@gluwa/creditcoin-js';

const { api } = await CreditcoinApi('ws://localhost:9944');
```

### Using the API

The API is a collection of modules that provide access to the various functions of the Creditcoin blockchain.

```typescript
const { api, extrinsics, utils } = await CreditcoinApi('ws://localhost:9944');
```

### Creating transactions

```typescript
const { api } = await CreditcoinApi('ws://localhost:9944');

const tx = api.
    .tx
    .balances
    .transfer(
        "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
        "1000000000000000"  // CTC amount in microunits
                            // (1 CTC = 1e18 microunits)
    )
```

### Signing & sending

```typescript
import { Keyring } from '@gluwa/creditcoin-js';

const keyring = new Keyring({ type: 'sr25519' });
// const fromSeed = keyring.addFromUri("bottom drive obey lake curtain smoke basket hold race lonely fit walk");
const alice = keyring.addFromUri('//Alice');

await tx.signAndSend(alice);
```

### Batching transactions

```typescript
const tx1 = api.tx.balances.transfer(addrBob, 10);
const tx2 = api.tx.balances.transfer(addrCharlie, 10);
const txs = [tx1, tx2];

const batch_tx = api.tx.utility.batch(txs);

await batch_tx.signAndSend(alice);
```

## Development

### Build

To build the project, run the following command from the root directory:

```shell
yarn build
```

### Updating Type definitions

Creditcoin-JS uses actual chain metadata to generate the API types and augmented endpoints. When the Creditcoin blockchain gets updated and includes new extrinsics or storage fields in it’s pallets, Creditcoin-JS must regenerate its types to include the newly available methods.

1. Fetch Chain Metadata

This process begins with pulling the current metadata from a running `creditcoin-node` by making an RPC call. You can use the `get-metadata.sh` script to do this. It will save the metadata to a file called `creditcoin.json`.

```shell
./get-metadata.sh
```

2. Generate Types

The types can be generated by running the following command:

```shell
yarn build:types
```

## Errors & Troubleshooting

If after following the build process you run into errors where credicoin-js isn't reflecting the changes in the rust code you may need to clear your cache. The following command (run from root directory) can help:

```shell
cd creditcoin-js && rm -rf lib && yarn install && yarn build && yarn pack && cd ../integration-tests/ && yarn cache clean && rm -rf node_modules && yarn upgrade creditcoin-js && yarn install
```