Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oussamahamdaoui/fb56c1a27c466a2b4bdbf9322f14dc0e to your computer and use it in GitHub Desktop.
Save oussamahamdaoui/fb56c1a27c466a2b4bdbf9322f14dc0e to your computer and use it in GitHub Desktop.
const bech = require('bech32');
const { Tendermint34Client } = require('@cosmjs/tendermint-rpc');
const { setupBankExtension } = require('@cosmjs/stargate');
// create a rpc client to fetch data from a blockchain
const tendermintClient = QueryClient.withExtensions(
(await Tendermint34Client.connect("rpc-url")),
setupBankExtension );
// use this to get the balances
const balances = await tendermintClient.client.bank.allBalances('cosmos...');
// to change from one address to another you could use those functions
const hexToBech = (str, prefix) => {
const words = bech.bech32.toWords(Buffer.from(str.replace(/0x/, ''), 'hex'));
return bech.bech32.encode(prefix, words);
};
const bechToHex = (str = '') => {
const y = bech.bech32.decode(str);
return Buffer.from(bech.bech32.fromWords(y.words)).toString('hex');
};
// this gives you an eth like address
bechToHex('cosmos.....');
// this will convert your cosmos address to osmosis
hexToBech(bechToHex('cosmos.....'), 'osmo');
// this will convert your cosmos address to akash
hexToBech(bechToHex('cosmos.....'), 'akash');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment