|
| 1 | +import { SigningCosmosClient } from '@cosmjs/launchpad'; |
| 2 | + |
| 3 | +export const AGORIC_COIN_TYPE = 564; |
| 4 | +export const COSMOS_COIN_TYPE = 118; |
| 5 | +export const NETWORK_CONFIGS = [ |
| 6 | + ['https://main.agoric.net/network-config', 'Agoric Mainnet'], |
| 7 | + ['https://testnet.agoric.net/network-config', 'Agoric Testnet'], |
| 8 | + ['https://devnet.agoric.net/network-config', 'Agoric Devnet'], |
| 9 | + ['https://stage.agoric.net/network-config', 'Agoric Stage'], |
| 10 | +]; |
| 11 | + |
| 12 | +export async function suggestChain(networkConfig, caption = undefined) { |
| 13 | + const coinType = Number( |
| 14 | + new URL(networkConfig).searchParams.get('coinType') || AGORIC_COIN_TYPE, |
| 15 | + ); |
| 16 | + const res = await fetch(networkConfig); |
| 17 | + if (!res.ok) { |
| 18 | + throw Error(`Cannot fetch network: ${res.status}`); |
| 19 | + } |
| 20 | + const { chainName: chainId, rpcAddrs } = await res.json(); |
| 21 | + const hostname = new URL(networkConfig).hostname; |
| 22 | + const network = hostname.split('.')[0]; |
| 23 | + let rpc; |
| 24 | + let api; |
| 25 | + if (network !== hostname) { |
| 26 | + rpc = `https://${network}.rpc.agoric.net`; |
| 27 | + api = `https://${network}.api.agoric.net`; |
| 28 | + } else { |
| 29 | + rpc = `http://${rpcAddrs[Math.floor(Math.random() * rpcAddrs.length)]}`; |
| 30 | + api = rpc.replace(/(:\d+)?$/, ':1317'); |
| 31 | + } |
| 32 | + const stakeCurrency = { |
| 33 | + coinDenom: 'BLD', |
| 34 | + coinMinimalDenom: 'ubld', |
| 35 | + coinDecimals: 6, |
| 36 | + coinGeckoId: undefined, |
| 37 | + }; |
| 38 | + const stableCurrency = { |
| 39 | + coinDenom: 'RUN', |
| 40 | + coinMinimalDenom: 'urun', |
| 41 | + coinDecimals: 6, |
| 42 | + coinGeckoId: undefined, |
| 43 | + }; |
| 44 | + const chainInfo = { |
| 45 | + rpc, |
| 46 | + rest: api, |
| 47 | + chainId, |
| 48 | + chainName: caption || `Agoric ${network}`, |
| 49 | + stakeCurrency, |
| 50 | + walletUrlForStaking: `https://${network}.staking.agoric.app`, |
| 51 | + bip44: { |
| 52 | + coinType, |
| 53 | + }, |
| 54 | + bech32Config: { |
| 55 | + bech32PrefixAccAddr: 'agoric', |
| 56 | + bech32PrefixAccPub: 'agoricpub', |
| 57 | + bech32PrefixValAddr: 'agoricvaloper', |
| 58 | + bech32PrefixValPub: 'agoricvaloperpub', |
| 59 | + bech32PrefixConsAddr: 'agoricvalcons', |
| 60 | + bech32PrefixConsPub: 'agoricvalconspub', |
| 61 | + }, |
| 62 | + currencies: [stakeCurrency, stableCurrency], |
| 63 | + feeCurrencies: [stableCurrency], |
| 64 | + features: ['stargate', 'ibc-transfer'], |
| 65 | + }; |
| 66 | + await window.keplr.experimentalSuggestChain(chainInfo); |
| 67 | + await window.keplr.enable(chainId); |
| 68 | + |
| 69 | + const offlineSigner = window.getOfflineSigner(chainId); |
| 70 | + const accounts = await offlineSigner.getAccounts(); |
| 71 | + const cosmJS = new SigningCosmosClient( |
| 72 | + 'https://node-cosmoshub-3.keplr.app/rest', // TODO: Provide correct rest API |
| 73 | + accounts[0].address, |
| 74 | + offlineSigner, |
| 75 | + ); |
| 76 | + |
| 77 | + return cosmJS; |
| 78 | +} |
0 commit comments