cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
649 stars 333 forks source link

Function getAccount in SigningCosmosClient doesn't work properly #993

Open ixa94 opened 2 years ago

ixa94 commented 2 years ago
await window.keplr.enable(keplrChainId);

  const client = LcdClient.withExtensions(
    { apiUrl: 'https://lcd-cosmoshub.keplr.app' },
    setupAuthExtension,
    setupBankExtension,
    setupDistributionExtension,
    setupGovExtension,
    setupMintExtension,
    setupSlashingExtension,
    setupStakingExtension,
    setupSupplyExtension,
  );
  console.log('client : ', client);

  const offlineSigner = window.keplr.getOfflineSigner(keplrChainId);

  const accounts = await offlineSigner.getAccounts();
  const balances = await client.bank.balances(accounts[0].address); //How can I get balance from my wallet?

  console.log('balances : ', balances);
  // This what I got in console
   //balances :  
   //{height: '9122542', result: Array(0)}
   //height: "9122542"
  //result: Array(0)
  //length: 0
//Why is result:Array empty?
  const cosmJS = new SigningCosmosClient(
    'https://lcd-cosmoshub.keplr.app',
    accounts[0].address,
    offlineSigner,
  );
  const account = await cosmJS.getAccount(accounts[0].address);
  console.log('account:', account);
// why doesnt account  work correctly and my code doesnt work after this part?

// if i use @cosmjs/stargate

import {
  SigningStargateClient,
} from '@cosmjs/stargate';
import { keplrChainId } from 'appConstants';

async function connectKeprl(): Promise<SigningStargateClient | false> {
  if (!window.keplr) {
    return false;
  }

  await window.keplr.enable(keplrChainId);

  const offlineSigner = window.keplr.getOfflineSigner(keplrChainId);

  const accounts = await offlineSigner.getAccounts();

  const cosmJS = await SigningStargateClient.connectWithSigner(
    'https://rpc.my_tendermint_rpc_node',
    offlineSigner,
  );
  const account = await cosmJS.getAccount(accounts[0].address);
  console.log('account:', account);

//i have error like this POST https://rpc.my_tendermint_rpc_node/ net::ERR_NAME_NOT_RESOLVED
// so how can I resolve these errors
webmaster128 commented 2 years ago

Using LcdClient (and anything else from @cosmjs/launchpad) is a dead end. This package is for connecting to Cosmos SDK 0.37–0.39 backends only and is about to be removed.

If you have questions to @cosmjs/stargate related integration, please format the code properly to make it readable. See https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks and https://commonmark.org/.

ixa94 commented 2 years ago

@webmaster128 i have formatted the code

ixa94 commented 2 years ago

@webmaster128 `// if i use @cosmjs/stargate

import { SigningStargateClient, } from '@cosmjs/stargate'; import { keplrChainId } from 'appConstants';

async function connectKeprl(): Promise<SigningStargateClient | false> { if (!window.keplr) { return false; }

await window.keplr.enable(keplrChainId);

const offlineSigner = window.keplr.getOfflineSigner(keplrChainId);

const accounts = await offlineSigner.getAccounts();

const cosmJS = await SigningStargateClient.connectWithSigner( 'https://rpc.my_tendermint_rpc_node', offlineSigner, ); const account = await cosmJS.getAccount(accounts[0].address); console.log('account:', account);

////How can I get balance from my wallet? //i have error like this POST https://rpc.my_tendermint_rpc_node/ net::ERR_NAME_NOT_RESOLVED // so how can I resolve these errors`

webmaster128 commented 2 years ago

https://rpc.my_tendermint_rpc_node is a placeholder. You need to replace it with our Tendermint RPC URL.

ixa94 commented 2 years ago

@webmaster128 Hey Could you please send me your Tendermint RPC URL. Because I couldn’t find it by myself. Thank you!

webmaster128 commented 2 years ago

If you want to connect to the Cosmos Hub, you can try https://rpc.cosmos.network/. There are many other RPC endpoints out there, but you'd have to go and search for them.

ixa94 commented 2 years ago

@webmaster128 I have some questions 1)I want to get balance from my wallet.Am I using the correct code? 2)When I am trying to call "connectWithSigner" I catch CORS error,so how can I resolve it. I'd be grateful If you helped me.Thanks a lot.

import { SigningStargateClient } from '@cosmjs/stargate';
import { keplrChainId } from 'appConstants';

async function connectKeprl(): Promise<SigningStargateClient | false> {
  if (!window.keplr) {
    return false;
  }

  await window.keplr.enable('cosmoshub-4');

  const offlineSigner = window.keplr.getOfflineSigner('cosmoshub-4');

  const accounts = await offlineSigner.getAccounts();

  const cosmJS = await SigningStargateClient.connectWithSigner(
    'https://rpc.cosmos.network',
    offlineSigner,
  );
  console.log(cosmJS.getBalance(accounts[0].address, 'uatom'));

  return cosmJS;
webmaster128 commented 2 years ago
  1. You need to await the result of getBalance: const balance = await cosmJS.getBalance(accounts[0].address, 'uatom'). The rest LGTM auf first glance.
  2. Okay this is a confuguration problem of the RPC endpoint. You can try https://rpc-cosmoshub.keplr.app/ to see if it helps.
ixa94 commented 2 years ago

@webmaster128 I'm trying to do the way you advised, but I get the CORS again. Probably I'm writing or using something in a wrong way.

import { 
  SigningStargateClient,
} from '@cosmjs/stargate';
import { keplrChainId } from 'appConstants';

async function connectKeprl(): Promise<SigningStargateClient | false> {
  if (!window.keplr) {
    return false;
  }

  await window.keplr.enable(keplrChainId);

  const offlineSigner = window.keplr.getOfflineSigner(keplrChainId);

  const accounts = await offlineSigner.getAccounts();

  const cosmJS = await SigningStargateClient.connectWithSigner(
    'https://rpc-cosmoshub.keplr.app',
    offlineSigner,
  );
  const balance = await cosmJS.getBalance(accounts[0].address, 'uatom');
  console.log(balance);
webmaster128 commented 2 years ago

Yeah, sorry this does not work too. You have to find a Tendermint RPC node that has CORS configures for access from any origin for the Cosmos Hub. There is no obvious solution to that. You need to ask your war through Cosmos Hub community channels. Maby you find some nice validators that provide such an endpoint.