@cosmos-client/core

中文文档
0.47.4发布于 2023/10/18ISCESM + CJS近 30 天下载 3258

REST API client for Cosmos SDK blockchain

原始使用文档
文档目录8 节
  1. Install
  2. Caution
  3. Discord community
  4. Plugin modules
  5. Examples
  6. For frontend developers
  7. For library developlers
  8. for proto.d.ts error

@cosmos-client/core

REST API client for Cosmos SDK blockchain

Install

npm install @cosmos-client/core

Caution

The package name cosmos-client is deprecated. Please use @cosmos-client/core alternatively.

Discord community

Invitation link

Plugin modules

Examples

import cosmosclient from '@cosmos-client/core';
import Long from 'long';

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

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

    const privKey = new cosmosclient.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 cosmosclient.rest.auth
      .account(sdk, fromAddress)
      .then((res) => cosmosclient.codec.protoJSONToInstance(cosmosclient.codec.castProtoJSONOfProtoAny(res.data.account)))
      .catch((_) => undefined);

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

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

    const txBody = new cosmosclient.proto.cosmos.tx.v1beta1.TxBody({
      messages: [cosmosclient.codec.instanceToProtoAny(msgSend)],
    });
    const authInfo = new cosmosclient.proto.cosmos.tx.v1beta1.AuthInfo({
      signer_infos: [
        {
          public_key: cosmosclient.codec.instanceToProtoAny(pubKey),
          mode_info: {
            single: {
              mode: cosmosclient.proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
            },
          },
          sequence: account.sequence,
        },
      ],
      fee: {
        gas_limit: 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 cosmosclient.rest.tx.broadcastTx(sdk, {
      tx_bytes: txBuilder.txBytes(),
      mode: rest.tx.BroadcastTxMode.Block,
    });
    console.log(res);

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

For frontend developers

In something like polyfill.ts in Angular (of course something like that in other frontend frameworks), don't forget to input this statement. If there isn't this statement, the outcome of serializing JSON of instances including Long type field will be wrong.

import Long from 'long';
import * as $protobuf from 'protobufjs/minimal';

$protobuf.util.Long = Long;
$protobuf.configure();

For library developlers

Install Protocol Buffer Compiler

sudo apt -y install protobuf-compiler

or

brew install protobuf 

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

Add the following line to the end of the proto.d.ts file and replace tendermint with global_tendermint at the error points.

import global_tendermint = tendermint;

版本与发布记录

@cosmos-client/core 共发布 34 个版本,首次发布于 2021/10/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.47.4latest2023/10/183 年前
0.47.4-rc12023/10/063 年前
0.47.32023/09/273 年前
0.47.3-rc22023/09/223 年前
0.47.3-rc12023/09/123 年前
0.47.12023/06/023 年前

关于 @cosmos-client/core 的常见问题

@cosmos-client/core 是什么?

REST API client for Cosmos SDK blockchain

如何安装 @cosmos-client/core?

在项目目录运行 npm install @cosmos-client/core,或使用 pnpm add / yarn add / bun add。

@cosmos-client/core 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cosmos-client__core 是否存在。

@cosmos-client/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cosmos-client/core 可以商用吗?

@cosmos-client/core 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cosmos-client/core 的下载量有多少?

根据 npm 官方下载统计,@cosmos-client/core 在 2026/10/11 – 2026/11/09 这 30 天内被下载 3,258 次,日均约 109 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cosmos-client/core 还在维护吗?

最新版本 0.47.4 发布于 2023/10/18(3 年前);至今共发布 34 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/10/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。