npm package: @mantrachain/mantrajs
Description: JS and TS types relating to Protocol Buffers for MANTRA Chain
Author: Matt
Latest version: 1.0.0-beta.2

# MANTRA Chain Kit - MANTRAJS

<div align="center">
  <img src="https://assets.mantra.finance/images/logo.svg" alt="Mantra Connect Logo" width="200"/>
</div>

<div align="center" style="margin: 1rem">

[![npm downloads](https://img.shields.io/npm/dm/@mantrachain/mantrajs.svg?style=for-the-badge)](https://www.npmjs.com/package/@mantrachain/mantrajs)
[![npm](https://img.shields.io/npm/dt/@mantrachain/mantrajs.svg?style=for-the-badge)](https://www.npmjs.com/package/@mantrachain/mantrajs)
[![npm](https://img.shields.io/npm/l/@mantrachain/mantrajs?style=for-the-badge)](https://github.com/MANTRA-Chain/mantra-kit/blob/main/LICENSE)

</div>

<p align="center">
  <a href="https://www.mantrachain.io/">Get Started</a> • 
  <a href="https://docs.mantrachain.io/">Docs</a> •
  <a href="https://explorer.mantrachain.io">Hongbai</a>
</p>



## Installation
```
npm install @mantrachain/mantrajs
```

## Usage
Import the package
```
import { mantrachain } from "@mantrachain/mantrajs/index";
```
or
```
const mantrachain = require('@mantrachain/mantrajs');
```

### Query Balances (LCD Client)
```
    const client = await mantrachain.ClientFactory.createAppApiClient({
      restEndpoint,
    });

    const response = await client.cosmos.bank.v1beta1.allBalances({
      address: "mantra15m77x4pe6w9vtpuqm22qxu0ds7vn4ehzwx8pls",
      resolveDenom: false,
    });
```
Response:
```
    {
      balances: [
        {
          denom: 'factory/mantra15m77x4pe6w9vtpuqm22qxu0ds7vn4ehzwx8pls/MATT2',
          amount: '250'
        },
        { denom: 'uom', amount: '78883121160383' }
      ],
      pagination: { next_key: null, total: '2' }
    }
```

### Query Balance (RPC Client)
```
    const client = await mantrachain.ClientFactory.createAppRPCQueryClient({
      rpcEndpoint,
    });

    const response = await client.cosmos.bank.v1beta1.allBalances({
      address: "mantra15m77x4pe6w9vtpuqm22qxu0ds7vn4ehzwx8pls",
      resolveDenom: false,
    });
```
Response:
```
    {
      balances: [
        {
          denom: 'factory/mantra15m77x4pe6w9vtpuqm22qxu0ds7vn4ehzwx8pls/MATT2',
          amount: '250'
        },
        { denom: 'uom', amount: '78883121160383' }
      ],
      pagination: { nextKey: Uint8Array(0) [], total: 2n }
    }
```

### Signing Transactions
Use the MessageComposer in the required namespace to create the message and the `SigningMantrachainClient` to sign and broadcast. For example to using the Cosmos SDK Bank Send:
```
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonicSender, {
      prefix: "mantra",
    });
    const accounts = await wallet.getAccounts();
    const address = accounts[0].address;

    const signingClient = await getSigningMantrachainClient({
      rpcEndpoint,
      signer: wallet,
    });

    const { send } = cosmos.bank.v1beta1.MessageComposer.withTypeUrl;

    const sendMsg = send({
      fromAddress: address,
      toAddress: "mantra12z5kd26nlggsrmt3dx3v3eyxy7pmgdz5h7qf4t",
      amount: [{ denom: "uom", amount: "1000" }],
    });

    const fee = { amount: [{ denom: "uom", amount: "5000" }], gas: "200000" };

    const response = await signingClient.signAndBroadcast(
      address,
      [sendMsg],
      fee,
      "Test transfer"
    );
```

## Contributors

### First Build?
This repo uses submodules that need to be synced. The first time you clone this repo and build follow the following steps:
1. Run `git submodule update --init --recursive --remote`
2. Run `cd packages/mantrajs && ./scripts/set-versions.sh`
3. Run `npm install`
4. Run `npm run rebuild`


### Tests
Test are run against the DuKong Test Network and only test a limited number of items. For more comprehensive tests use the E2E test repo.

To run the tests:
```
npm test
```

### Submodules
The proto files are referenced from the submodules. You can modify and use `./scripts/set-versions.sh` to set the versions/commits you wish to use for each submodule.

If it's the first time you check-out a repo you need to use `--init` first:
```
git submodule update --recursive --remote
```

otherwise use:
```
git submodule update --recursive
```

or

```
git pull --recurse-submodules
```

### Adding a new Cosmos SDK Module

1. To add a new submodule use:
```
git submodule add -f git@github.com:skip-mev/feemarket.git packages/mantrajs/protos/feemarket-src
```
then checkout the version/commit you want use the script or manually run it
```
git -C protos/feemarket-src checkout v1.1.1
```
> NOTE: you should update `./scripts/set-versions.sh` and run it

2. then in `package.json`
add the new `protogen:newxxx` task and update the `clean` task

3. next update `./scripts/codegen.js`

git submodule add -f git@github.com:MANTRA-Chain/mantrachain.git packages/mantrajs/protos/mantrachain-src
git submodule add -f git@github.com:MANTRA-Chain/cosmos-sdk.git packages/mantrajs/protos/cosmos-sdk-src
git submodule add -f git@github.com:skip-mev/connect.git packages/mantrajs/protos/connect-src
git submodule add -f git@github.com:cosmos/ibc-go.git packages/mantrajs/protos/ibc-go-src