# cudosjs

> JavaScript SDK for Cudos Network

Latest version **2.0.5** (published 2023-11-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install cudosjs
pnpm add cudosjs
yarn add cudosjs
bun add cudosjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2023-11-08 |
| First published | 2021-10-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 13 |
| Unpacked size | 4.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | cudos |

## Links

- npm: https://www.npmjs.com/package/cudosjs
- Repository: https://github.com/CudoVentures/cudosjs
- npm.io page: https://npm.io/package/cudosjs

## Dependencies (13)

- [yarn](https://npm.io/package/yarn.md) ^1.22.19
- [bech32](https://npm.io/package/bech32.md) ^2.0.0
- [@cosmjs/math](https://npm.io/package/@cosmjs/math.md) ^0.31.1
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.2
- [@cosmjs/crypto](https://npm.io/package/@cosmjs/crypto.md) ^0.31.1
- [detect-browser](https://npm.io/package/detect-browser.md) ^5.3.0
- [@cosmjs/stargate](https://npm.io/package/@cosmjs/stargate.md) ^0.31.1
- [@keplr-wallet/cosmos](https://npm.io/package/@keplr-wallet/cosmos.md) ^0.11.23
- [@cosmjs/proto-signing](https://npm.io/package/@cosmjs/proto-signing.md) ^0.31.1
- [@cosmjs/tendermint-rpc](https://npm.io/package/@cosmjs/tendermint-rpc.md) ^0.31.1
- [@cosmjs/cosmwasm-stargate](https://npm.io/package/@cosmjs/cosmwasm-stargate.md) ^0.31.1
- [@cosmostation/cosmos-client](https://npm.io/package/@cosmostation/cosmos-client.md) ^0.0.4
- [@cosmostation/extension-client](https://npm.io/package/@cosmostation/extension-client.md) 0.1.11

## Recent versions

- 2.0.5 (latest) — 2023-11-08
- 2.0.4 — 2023-11-08
- 2.0.3 — 2023-11-07
- 2.0.2 — 2023-10-06
- 2.0.1 — 2023-10-06
- 1.3.5 — 2023-08-18
- 2.0.0 — 2023-08-07
- 1.3.4 — 2023-07-20
- 1.3.3 — 2023-06-13
- 1.3.2 — 2023-06-09
- 1.3.1 — 2023-06-06
- 1.3.0 — 2023-04-18
- 1.2.9 — 2023-04-13
- 1.2.8 — 2023-03-20
- 1.2.7 — 2023-03-10
- … 24 more at https://npm.io/package/cudosjs/versions

## README

<p align="center" >
  <a href="https://www.cudos.org/" target="_blank"><img width="100" src="logo.png" alt="logo"></a>
</p>
<h1 align="center">
    CudosJS - JavaScript Library for the Cudos network
</h1>

*:star: Developed by [Cudos](https://www.cudos.org/)*

A JavasSript Open Source Library for [Cudos](https://cudos.org/) network.


## Installation

In order to fully use this library, you need to run a local or remote full node and set up its rest server, which acts as an intermediary between the front-end and the full-node. You can use the [cudos-cli](github.com/CudoVentures/cudos-cli).

### NPM

```bash
npm install cudosjs
```

### Yarn 

```bash
yarn add cudosjs
```

## Using CudosJS

CudosJS acts as a package that extends the [cosmJS](https://github.com/cosmos/cosmjs) library. It builds-up upon it, so that it's tailored to the specifics of the Cudos Network. If you've used [cosmJS](https://github.com/cosmos/cosmjs) before, than the API should feel quite familiar to you.

CudosJS usage is organized mainly around using it's two clients - `StargateClient` and 
`SigningStargateClient`. 

- `SigningStargateClient` - used to sign and broadcast transactions.

```
import { SigningStargateClient, DirectSecp256k1HdWallet } from "cudosjs"

// Load an account
const mnemonic = "surround miss nominee dream gap cross assault thank captain prosper drop duty group";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic);
const [firstAccount] = await wallet.getAccounts();

const rpc = http://localhost:26657
const signingClient = await SigningStargateClient.connectWithSigner(rpc, wallet)
```
We need an RPC endpoint (that will be used to connect to the network) and a wallet ( used to sign the transactions ). A ready to use SigningStargateClient is created by connecting it to the RPC and the wallet. 

```
// Sending tokens to another cudos address
import { coin, GasPrice } from "cudosjs"

...

const recipient = "cudos1a0kjzmvrkul70nxle63j8day47ve74g00zl66w";
const amount = {           // same as const amount = coin("1234567", "acudos)
  denom: "acudos",
  amount: "1234567",
};

const result = await signingClient.sendTokens(firstAccount.address, recipient, [amount], "Have fun with your cudos coins");

assertIsBroadcastTxSuccess(result);

```
 
```
...
// Sending a message to the NFT module


const gasPrice = GasPrice.fromString('1acudos');

const correctToken = {
    approvedAddresses: [],
    id: '1',
    owner: '',
    name: 'testToken',
    uri: 'testUir',
    data: 'testData',
  }

const result = await signingClient.nftMintToken(firstAccount.address, correctDenom.id, correctToken.name, correctToken.uri, correctToken.data, recipient, gasPrice)

assertIsBroadcastTxSuccess(result);
```

```
...
// Sending a message to the Gravity module


const gasPrice = GasPrice.fromString('1acudos');
const coinAmount = {denom:"acudos", amount: "1000"}
const feeAmount = {denom:"acudos", amount: "100"}
const ethAddr = "0xa677d7229924af63098b9bb70b041a03a1ec7d8c"

const result = await signingClient.gravitySendToEth(firstAccount.address,ethAddr,coinAmount,feeAmount,gasPrice)

assertIsBroadcastTxSuccess(result);
```


- `StargateClient` - this one is used to query the Cudos Network. It's quite simple to work with:

```
import { StargateClient } from "cudosjs"

const rpc = http://localhost:26657
const queryClient = await StargateClient.connect(rpc)
```

First we need an RPC endpoint, that will be used to connect to the network. Then a `StargateClient` is created by connecting it to the network.

** In this example the client is connecting to a local test network, created using [cudos-blast](https://github.com/CudoVentures/cudos-blast).

```
...

const cudosAddress = "some cudos address"

const height = await queryClient.getHeight() // returns the current height of the blockchain  

const isAccount = await queryClient.getAccount(cudosAddress) // check if an account exists

const balance = await queryClient.getBalance(cudosAddress, "acudos") // get the balance of the account in the respective denom

const chainId =  await queryClient.getChainId()
...
```



As shown above - query methods typical to any cosmos-based blockchain are directly available on the `StargateClient`

The Cudos Network however includes some specific, additional modules - each with it's distinct messages and queries ( such examples are the group, nft and gravity modules). Methods for queries related to those module are grouped in objects inside the `StargateClient`, where each object starts with the name of the module (e.g.`nftModule`, `gravityModule`, `groupModule`):


```
// Querying the Nft Module
...
 const correctDenom = {
    creator: '',
    id: 'testdenom',
    name: 'test-denom-name',
    symbol: 'test-denom-symbol',
    schema: 'test-denom-schema',
  }

const correctToken = {
    approvedAddresses: [],
    id: '1',
    owner: '',
    name: 'testToken',
    uri: 'testUir',
    data: 'testData',
  } 

const getDenom = await queryClient.nftModule.getNftDenom(correctDenom.id) // get a denom by it's ID
const getToken = queryClient.nftModule.getNftToken(correctDenom.id, correctToken.id) // get a token
const isApproved = queryClient.nftModule.nftIsApprovedForAll(cudosAddress, cudosAddress2)

```

```
// Querying the Gravity Module
...

const pendingTrxs = await queryClient.gravityModule.getPendingSendToEth(cudosAddress) // check for unbatched transactions for a particular account

```
### Constructing custom Msg's
Most of the times the already provided methods inside `SigningStargateClient` would be enough. But sometimes you need to construct and send your messages manually ( for example when simulating a transaction to calculate the Fees). CudosJS provides an utility function `generateMsg` to make custom message construction easier:
```
import { GasPrice, generateMsg, estimateFee } from "cudosjs"

...

// Calculating the Fees for sending a msgSendToEth message.

const bridgeFee = {denom:"acudos", amount: "10000"}
const amount = coin('1000000000000', "acudos")
const memo = "Have fun spending your cudos"


const gasPrice = GasPrice.fromString(`1acudos`)
const simulatedMsg = generateMsg('msgSendToEth', {
            sender,  // cudos addrs
            ethDest: destination, //eth addrs
            amount
            bridgeFee
        })

const approxFee = await estimateFee(client, sender, [simulatedMsg], gasPrice, gasMultiplier, memo)


const result = await client.signAndBroadcast(
  sender,
  [simulatedMsg],
  approxFee,
  memo
);
```

### Custom Queries
Using the available methods inside `StargateClient` automatically decodes the response. However when constructing and sending queries yourself, you have to decode the responses as well. You can use the `decodeQryResponse` method inside `StargateClient`:

```
...

// Executing a custom query using filters

const filteredTx = await queryClient.searchTx({
                tags: [{ key: "message.action", value: "/gravity.v1.MsgSendToCosmosClaim" }],
            })

// Decoding the result - decodes all messages inside the response

const decodedRes = await qClient.decodeQryResponse(filteredTx)
```

---
_Source: https://npm.io/package/cudosjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
