0.42.14 • Published 3 years ago

cosmos-client v0.42.14

Weekly downloads
85
License
ISC
Repository
github
Last release
3 years ago

cosmos-client-ts

JavaScript / TypeScript client for Cosmos SDK blockchain.

Install

npm install --save cosmos-client

Examples

import { cosmosclient, rest, proto } from 'cosmos-client';

describe('bank', () => {
  it('send', async () => {
    expect.hasAssertions();

    const sdk = new cosmosclient.CosmosSDK('http://localhost:1317', 'testchain');

    const privKey = new proto.cosmos.crypto.secp256k1.PrivKey({
      key: await cosmosclient.generatePrivKeyFromMnemonic('joke door law post fragile cruel torch silver siren mechanic flush surround'),
    });
    const pubKey = privKey.pubKey();
    const address = cosmosclient.AccAddress.fromPublicKey(pubKey);

    expect(address.toString()).toStrictEqual('cosmos14ynfqqa6j5k3kcqm2ymf3l66d9x07ysxgnvdyx');

    const fromAddress = address;
    const toAddress = address;

    // get account info
    const account = await rest.cosmos.auth
      .account(sdk, fromAddress)
      .then((res) => res.data.account && cosmosclient.codec.unpackCosmosAny(res.data.account))
      .catch((_) => undefined);

    if (!(account instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
      console.log(account);
      return;
    }

    // build tx
    const msgSend = new proto.cosmos.bank.v1beta1.MsgSend({
      from_address: fromAddress.toString(),
      to_address: toAddress.toString(),
      amount: [{ denom: 'token', amount: '1' }],
    });

    const txBody = new proto.cosmos.tx.v1beta1.TxBody({
      messages: [cosmosclient.codec.packAny(msgSend)],
    });
    const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
      signer_infos: [
        {
          public_key: cosmosclient.codec.packAny(pubKey),
          mode_info: {
            single: {
              mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
            },
          },
          sequence: account.sequence,
        },
      ],
      fee: {
        gas_limit: cosmosclient.Long.fromString('200000'),
      },
    });

    // sign
    const txBuilder = new cosmosclient.TxBuilder(sdk, txBody, authInfo);
    const signDocBytes = txBuilder.signDocBytes(account.account_number);
    txBuilder.addSignature(privKey.sign(signDocBytes));

    // broadcast
    const res = await rest.cosmos.tx.broadcastTx(sdk, {
      tx_bytes: txBuilder.txBytes(),
      mode: rest.cosmos.tx.BroadcastTxMode.Block,
    });
    console.log(res);

    expect(res.data.tx_response?.raw_log?.match('failed')).toBeFalsy();
  });
});

For library developlers

Use starport to test.

The first digit major version and the second digit minor version should match Cosmos SDK. The third digit patch version can be independently incremented.

for proto.d.ts error

Insert:

import global_tendermint = tendermint;
0.42.14

3 years ago

0.42.13

3 years ago

0.42.12

3 years ago

0.42.11

3 years ago

0.42.10

3 years ago

0.42.8

3 years ago

0.42.9

3 years ago

0.42.6

3 years ago

0.42.7

3 years ago

0.42.5

3 years ago

0.42.4

3 years ago

0.42.3

3 years ago

0.42.2-3

3 years ago

0.42.2-1

3 years ago

0.42.2-2

3 years ago

0.42.2

3 years ago

0.42.1

3 years ago

0.42.1-rc1

3 years ago

0.39.13

3 years ago

0.40.0-rc1

3 years ago

0.39.11

3 years ago

0.39.12

3 years ago

0.39.10

3 years ago

0.39.9

3 years ago

0.39.8

3 years ago

0.39.7

3 years ago

0.39.6

3 years ago

0.39.5

3 years ago

0.39.4

3 years ago

0.39.3

3 years ago

0.39.2

4 years ago

0.39.1

4 years ago

0.39.0

4 years ago

0.38.0

4 years ago

0.38.5

4 years ago

0.8.14

4 years ago

0.8.13

4 years ago

0.8.12

4 years ago

0.8.11

4 years ago

0.8.9

4 years ago

0.8.8

4 years ago

0.8.7

4 years ago

0.8.6

4 years ago

0.8.5

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.10

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.19

4 years ago