cosmology-tech / cosmos-kit

Universal wallet connector for the Cosmos ⚛️
https://cosmology.zone/products/cosmos-kit
BSD 3-Clause Clear License
183 stars 132 forks source link

Connecting multi chain at once and getting addresses #262

Closed vinodhum closed 1 year ago

vinodhum commented 1 year ago

Hi, How to connect multi chain at once and get their addresses. here is what I have tired based on chat on discord but looks like wallet is not getting initialized

mutli chain connect

twhy commented 1 year ago

Let's take keplr-extension for example.

import { useWalletClient } from '@cosmos-kit/react';

export default function Home() {
  const { client } = useWalletClient('keplr-extension');

  useEffect(() => {
    if (client) {
      client.enable?.(['cosmoshub-4', 'osmosis-1', 'juno-1'])
      client.getAccount?.('juno-4').then((account) => console.log(account))
      client.getSimpleAccount?.('juno-4').then((account) => console.log(account))
      client.getAccount?.('osmosis-1').then((account) => console.log(account))
      client.getSimpleAccount?.('osmosis-1').then((account) => console.log(account))
      client.getAccount?.('cosmoshub-4').then((account) => console.log(account))
      client.getSimpleAccount?.('cosmoshub-4').then((account) => console.log(account))
    }
  })
...
}
image
twhy commented 1 year ago

@vinodhum Is this what you want to do ?

twhy commented 1 year ago

The best you can do for now

export default function Home() {
  const { connect } = useChain('cosmoshub');
  const { client } = useWalletClient();

  useEffect(() => {
    if (client) {
      client.enable?.(['cosmoshub-4', 'osmosis-1', 'juno-1'])
      client.getAccount?.('juno-4').then((account) => console.log(account))
      client.getSimpleAccount?.('juno-4').then((account) => console.log(account))
      client.getAccount?.('osmosis-1').then((account) => console.log(account))
      client.getSimpleAccount?.('osmosis-1').then((account) => console.log(account))
      client.getAccount?.('cosmoshub-4').then((account) => console.log(account))
      client.getSimpleAccount?.('cosmoshub-4').then((account) => console.log(account))
    }
  })

  return (
    <div style={{ textAlign: 'center', margin: '4rem' }}>
      <Button onClick={connect}>Connect</Button>
    </div>   
  );
}
liujun93 commented 1 year ago

@vinodhum the wallet initializing issue has been solved, please upgade all the cosmos-kit deps to the latest v2 version

twhy commented 1 year ago

use the useChains hook https://docs.cosmoskit.com/hooks/use-chains