# @zetachain/sdk-cosmos
> JavaScript library to interact with ZetaChain blockchain

## Installation

```bash
npm install @zetachain/sdk-cosmos
```

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
    - [RPC Clients](#rpc-clients)
    - [Composing Messages](#composing-messages)
        - [Cosmos Messages](#cosmos-messages)
- [Connecting with Wallets and Signing Messages](#connecting-with-wallets-and-signing-messages)
    - [Creating Signers](#creating-signers)
    - [Amino Signer](#amino-signer)
    - [Proto Signer](#proto-signer)
    - [Broadcasting Messages](#broadcasting-messages)

## Usage

### RPC Clients

```js
import { zeta } from '@zetachain/sdk-cosmos';

const { createRPCQueryClient } = zeta.ClientFactory;
const client = await createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT });

// now you can query the cosmos modules
const balance = await client.cosmos.bank.v1beta1
    .allBalances({ address: 'zeta1addresshere' });

```

### Composing Messages

#### Cosmos Messages

```js
import { cosmos } from '@zetachain/sdk-cosmos';

const {
    fundCommunityPool,
    setWithdrawAddress,
    withdrawDelegatorReward,
    withdrawValidatorCommission
} = cosmos.distribution.v1beta1.MessageComposer.fromPartial;

const {
    multiSend,
    send
} = cosmos.bank.v1beta1.MessageComposer.fromPartial;

const {
    beginRedelegate,
    createValidator,
    delegate,
    editValidator,
    undelegate
} = cosmos.staking.v1beta1.MessageComposer.fromPartial;

const {
    deposit,
    submitProposal,
    vote,
    voteWeighted
} = cosmos.gov.v1beta1.MessageComposer.fromPartial;
```

## Connecting with Wallets and Signing Messages

### Creating Signers

To broadcast messages, you can create signers with a variety of options:

* [cosmjs](https://gist.github.com/webmaster128/8444d42a7eceeda2544c8a59fbd7e1d9)

### Amino Signer
```js
import { getOfflineSignerAmino as getOfflineSigner } from 'cosmjs-utils';
```

### Proto Signer
```js
import { getOfflineSignerProto as getOfflineSigner } from 'cosmjs-utils';
```

> ⚠️ **WARNING**: NOT RECOMMENDED TO USE PLAIN-TEXT MNEMONICS. Please take care of your security and use best practices such as AES encryption and/or methods from 2factor applications.

```js
import { chains } from 'chain-registry';

const mnemonic =
  'unfold client turtle either pilot stock floor glow toward bullet car science';
const chain = chains.find(({ chain_name }) => chain_name === 'zeta');
const signer = await getOfflineSigner({
    mnemonic,
    chain
});
```

### Broadcasting Messages

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

const msg = send({
    amount: [
        {
            denom: 'coin',
            amount: '1000'
        }
    ],
    toAddress: address,
    fromAddress: address
});

const fee = {
    amount: [
        {
            denom: 'coin',
            amount: '864'
        }
    ],
    gas: '86364'
};

const response = await stargateClient.signAndBroadcast(address, [msg], fee);
```

## License
MIT