Skip to content

Commit

Permalink
feat: suggest chain
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Aug 19, 2023
1 parent 7cf64bb commit 8834eec
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/web-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export { makeAgoricKeplrConnection } from './src/keplr-connection/KeplrConnection.js';
export { makeAgoricWalletConnection } from './src/wallet-connection/walletConnection.js';
export { suggestChain } from './src/wallet-connection/suggestChain.js';
export { Errors as AgoricKeplrConnectionErrors } from './src/errors.js';
export {
DappWalletBridge,
Expand Down
94 changes: 94 additions & 0 deletions packages/web-components/src/wallet-connection/suggestChain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { bech32Config, stableCurrency, stakeCurrency } from './chainInfo.js';

/** @typedef {import('@agoric/casting/src/netconfig').NetworkConfig} NetworkConfig */
/** @typedef {import('@keplr-wallet/types').ChainInfo} ChainInfo */
/** @typedef {import('@keplr-wallet/types').Keplr} Keplr */

export const AGORIC_COIN_TYPE = 564;
export const COSMOS_COIN_TYPE = 118;

/**
*
* @param {NetworkConfig} networkConfig
* @param {string} caption
* @param {number} randomFloat
* @param {string | undefined} walletUrlForStaking
* @returns {ChainInfo}
*/
const makeChainInfo = (
networkConfig,
caption,
randomFloat,
walletUrlForStaking,
) => {
const { chainName, rpcAddrs, apiAddrs } = networkConfig;
const index = Math.floor(randomFloat * rpcAddrs.length);

const rpcAddr = rpcAddrs[index];
const rpc = rpcAddr.match(/:\/\//) ? rpcAddr : `http://${rpcAddr}`;

const rest = apiAddrs
? // pick the same index
apiAddrs[index]
: // adapt from rpc
rpc.replace(/(:\d+)?$/, ':1317');

return {
rpc,
rest,
chainId: chainName,
chainName: caption,
stakeCurrency,
walletUrlForStaking,
bip44: {
coinType: AGORIC_COIN_TYPE,
},
bech32Config,
currencies: [stakeCurrency, stableCurrency],
feeCurrencies: [stableCurrency],
features: ['stargate', 'ibc-transfer'],
};
};

/**
*
* @param {string} networkConfigHref
* @param {string=} caption
*/
export async function suggestChain(networkConfigHref, caption) {
// @ts-expect-error Check for Keplr
const { keplr } = window;

if (!keplr) {
throw Error('Missing Keplr');
}

console.log('suggestChain: fetch', networkConfigHref); // log net IO
const url = new URL(networkConfigHref);
const res = await fetch(url);
if (!res.ok) {
throw Error(`Cannot fetch network: ${res.status}`);
}

const networkConfig = await res.json();
// XXX including this breaks the Jest test
// assertNetworkConfig(harden(networkConfig));

if (!caption) {
const subdomain = url.hostname.split('.')[0];
caption = `Agoric ${subdomain}`;
}

const walletUrlForStaking = `https://${url.hostname}.staking.agoric.app`;

const chainInfo = makeChainInfo(
networkConfig,
caption,
Math.random(),
walletUrlForStaking,
);
console.log('chainInfo', chainInfo);
await keplr.experimentalSuggestChain(chainInfo);

return chainInfo;
}

0 comments on commit 8834eec

Please sign in to comment.