Skip to content

Commit

Permalink
feat: fetchEnvNetworkConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Nov 26, 2024
1 parent 36375fd commit 9bdba57
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 81 deletions.
7 changes: 3 additions & 4 deletions packages/agoric-cli/src/commands/auction.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// @ts-check
/* eslint-env node */
import { InvalidArgumentError } from 'commander';
import { fetchEnvNetworkConfig, makeVstorageKit } from '@agoric/client-utils';
import { Fail } from '@endo/errors';
import { makeVstorageKit } from '@agoric/client-utils';
import { InvalidArgumentError } from 'commander';
import { outputActionAndHint } from '../lib/wallet.js';
import { getNetworkConfig } from '../lib/network-config.js';

/**
* @import {ParamTypesMap, ParamTypesMapFromRecord} from '@agoric/governance/src/contractGovernance/typedParamManager.js'
* @import {ParamValueForType} from '@agoric/governance/src/types.js'
*/

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

/**
* @template {ParamTypesMap} M
Expand Down
5 changes: 2 additions & 3 deletions packages/agoric-cli/src/commands/gov.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @ts-check
/* eslint-disable func-names */
/* eslint-env node */
import { makeVstorageKit } from '@agoric/client-utils';
import { fetchEnvNetworkConfig, makeVstorageKit } from '@agoric/client-utils';
import { execFileSync as execFileSyncAmbient } from 'child_process';
import { Command, CommanderError } from 'commander';
import { normalizeAddressWithOptions, pollBlocks } from '../lib/chain.js';
import { getNetworkConfig } from '../lib/network-config.js';
import {
findContinuingIds,
getCurrent,
Expand All @@ -26,7 +25,7 @@ const collectValues = (val, memo) => {

const defaultKeyring = process.env.AGORIC_KEYRING_BACKEND || 'test';

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

/**
* @param {import('anylogger').Logger} _logger
Expand Down
7 changes: 3 additions & 4 deletions packages/agoric-cli/src/commands/inter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
*/

// @ts-check
import { makeWalletUtils } from '@agoric/client-utils';
import { fetchEnvNetworkConfig, makeWalletUtils } from '@agoric/client-utils';
import { makeOfferSpecShape } from '@agoric/inter-protocol/src/auction/auctionBook.js';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { objectMap } from '@agoric/internal';
import { M, matches } from '@endo/patterns';
import { CommanderError, InvalidArgumentError } from 'commander';
import { normalizeAddressWithOptions, pollBlocks } from '../lib/chain.js';
import { getCurrent, outputActionAndHint, sendAction } from '../lib/wallet.js';
import {
asBoardRemote,
bigintReplacer,
makeAmountFormatter,
} from '../lib/format.js';
import { getNetworkConfig } from '../lib/network-config.js';
import { getCurrent, outputActionAndHint, sendAction } from '../lib/wallet.js';

const { values } = Object;

Expand Down Expand Up @@ -232,7 +231,7 @@ export const makeInterCommand = (
try {
// XXX pass fetch to getNetworkConfig() explicitly
// await null above makes this await safe
const networkConfig = await getNetworkConfig({ env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env, fetch });
return makeWalletUtils({ fetch, delay }, networkConfig);
} catch (err) {
// CommanderError is a class constructor, and so
Expand Down
9 changes: 6 additions & 3 deletions packages/agoric-cli/src/commands/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable func-names */
/* eslint-env node */
import {
fetchEnvNetworkConfig,
makeVstorageKit,
makeWalletUtils,
storageHelper,
Expand All @@ -14,14 +15,13 @@ import * as cp from 'child_process';
import { Command } from 'commander';
import { inspect } from 'util';
import { normalizeAddressWithOptions } from '../lib/chain.js';
import { getNetworkConfig } from '../lib/network-config.js';
import { bigintReplacer } from '../lib/format.js';
import {
getCurrent,
outputAction,
sendAction,
sendHint,
} from '../lib/wallet.js';
import { bigintReplacer } from '../lib/format.js';

/** @import {PriceAuthority, PriceDescription, PriceQuote, PriceQuoteValue, PriceQuery,} from '@agoric/zoe/tools/types.js'; */

Expand Down Expand Up @@ -86,7 +86,10 @@ export const makeOracleCommand = (logger, io = {}) => {

const rpcTools = async () => {
// XXX pass fetch to getNetworkConfig() explicitly
const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({
env: process.env,
fetch,
});
const utils = await makeVstorageKit({ fetch }, networkConfig);

const lookupPriceAggregatorInstance = ([brandIn, brandOut]) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/agoric-cli/src/commands/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
makeFollower,
makeLeaderFromRpcAddresses,
} from '@agoric/casting';
import { fetchEnvNetworkConfig } from '@agoric/client-utils';
import { slotToRemotable } from '@agoric/internal/src/storage-test-utils.js';
import { boardSlottingMarshaller } from '@agoric/vats/tools/board-utils.js';
import { Command } from 'commander';
Expand All @@ -17,12 +18,11 @@ import {
execSwingsetTransaction,
normalizeAddressWithOptions,
} from '../lib/chain.js';
import { getNetworkConfig } from '../lib/network-config.js';

// tight for perf testing but less than this tends to hang.
const SLEEP_SECONDS = 0.1;

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

/**
* @param {import('anylogger').Logger} logger
Expand Down
11 changes: 7 additions & 4 deletions packages/agoric-cli/src/commands/psm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// @ts-check
/* eslint-disable func-names */
/* eslint-env node */
import { makeVstorageKit, storageHelper } from '@agoric/client-utils';
import {
fetchEnvNetworkConfig,
makeVstorageKit,
storageHelper,
} from '@agoric/client-utils';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { Command } from 'commander';
import { getNetworkConfig } from '../lib/network-config.js';
import { outputExecuteOfferAction } from '../lib/wallet.js';
import { asPercent } from '../lib/format.js';
import { outputExecuteOfferAction } from '../lib/wallet.js';

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

// Adapted from https://gist.github.com/dckc/8b5b2f16395cb4d7f2ff340e0bc6b610#file-psm-tool

Expand Down
5 changes: 2 additions & 3 deletions packages/agoric-cli/src/commands/reserve.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// @ts-check
/* eslint-disable func-names */
/* eslint-env node */
import { makeVstorageKit } from '@agoric/client-utils';
import { fetchEnvNetworkConfig, makeVstorageKit } from '@agoric/client-utils';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { Command } from 'commander';
import { getNetworkConfig } from '../lib/network-config.js';
import { outputActionAndHint } from '../lib/wallet.js';

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

/**
* @param {import('anylogger').Logger} _logger
Expand Down
7 changes: 3 additions & 4 deletions packages/agoric-cli/src/commands/test-upgrade.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @ts-check
/* eslint-env node */
import { makeWalletUtils } from '@agoric/client-utils';
import { fetchEnvNetworkConfig, makeWalletUtils } from '@agoric/client-utils';
import { Fail } from '@endo/errors';
import { CommanderError } from 'commander';
import { normalizeAddressWithOptions } from '../lib/chain.js';
import { getNetworkConfig } from '../lib/network-config.js';
import { sendAction } from '../lib/wallet.js';
import { bigintReplacer } from '../lib/format.js';
import { sendAction } from '../lib/wallet.js';

/**
* Make commands for testing.
Expand Down Expand Up @@ -39,7 +38,7 @@ export const makeTestCommand = (
try {
// XXX pass fetch to getNetworkConfig() explicitly
// await null above makes this await safe
const networkConfig = await getNetworkConfig({ env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env, fetch });
return makeWalletUtils({ fetch, delay }, networkConfig);
} catch (err) {
// CommanderError is a class constructor, and so
Expand Down
5 changes: 2 additions & 3 deletions packages/agoric-cli/src/commands/vaults.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// @ts-check
/* eslint-disable func-names */
/* eslint-env node */
import { makeVstorageKit } from '@agoric/client-utils';
import { fetchEnvNetworkConfig, makeVstorageKit } from '@agoric/client-utils';
import {
lookupOfferIdForVault,
Offers,
} from '@agoric/inter-protocol/src/clientSupport.js';
import { Command } from 'commander';
import { normalizeAddressWithOptions } from '../lib/chain.js';
import { getCurrent, outputExecuteOfferAction } from '../lib/wallet.js';
import { getNetworkConfig } from '../lib/network-config.js';

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

/**
* @param {import('anylogger').Logger} logger
Expand Down
9 changes: 4 additions & 5 deletions packages/agoric-cli/src/commands/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
makeLeader,
makeLeaderFromRpcAddresses,
} from '@agoric/casting';
import { makeVstorageKit } from '@agoric/client-utils';
import { makeVstorageKit, fetchEnvNetworkConfig } from '@agoric/client-utils';
import { execFileSync } from 'child_process';
import fs from 'fs';
import util from 'util';
Expand All @@ -19,15 +19,14 @@ import {
fetchSwingsetParams,
normalizeAddressWithOptions,
} from '../lib/chain.js';
import { getNetworkConfig } from '../lib/network-config.js';
import { coalesceWalletState, getCurrent } from '../lib/wallet.js';
import {
summarize,
fmtRecordOfLines,
parseFiniteNumber,
summarize,
} from '../lib/format.js';
import { coalesceWalletState, getCurrent } from '../lib/wallet.js';

const networkConfig = await getNetworkConfig({ env: process.env, fetch });
const networkConfig = await fetchEnvNetworkConfig({ env: process.env, fetch });

const SLEEP_SECONDS = 3;

Expand Down
3 changes: 2 additions & 1 deletion packages/agoric-cli/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

/** @import { ChildProcess } from 'child_process' */

export { getNetworkConfig } from './lib/network-config.js';
// Backwards compatibility
export { fetchEnvNetworkConfig as getNetworkConfig } from '@agoric/client-utils';

export const getSDKBinaries = ({
jsPfx = '../..',
Expand Down
41 changes: 0 additions & 41 deletions packages/agoric-cli/src/lib/network-config.js

This file was deleted.

20 changes: 20 additions & 0 deletions packages/client-utils/src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { LOCAL_CONFIG_KEY, fetchNetworkConfig } from './network-config.js';

/**
* @import {MinimalNetworkConfig} from './network-config.js';
*/

/**
* Fetch the network config for the AGORIC_NET environment variable.
*
* If none is set or it's 'local', return a local chain config.
*
* @param {{ env: typeof process.env, fetch: typeof fetch }} io
* @returns {Promise<MinimalNetworkConfig>}
*/

export const fetchEnvNetworkConfig = async ({ env, fetch }) => {
const net = env.AGORIC_NET || LOCAL_CONFIG_KEY;

return fetchNetworkConfig(net, { fetch });
};
2 changes: 2 additions & 0 deletions packages/client-utils/src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './cli.js';
export * from './network-config.js';
export * from './rpc.js';
export * from './sync-tools.js';
export * from './vstorage.js';
Expand Down
42 changes: 42 additions & 0 deletions packages/client-utils/src/network-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @typedef {{ rpcAddrs: string[], chainName: string }} MinimalNetworkConfig
*/

export const toNetworkConfigUrl = agoricNetSubdomain =>
`https://${agoricNetSubdomain}.agoric.net/network-config`;

export const toRpcUrl = agoricNetSubdomain =>
`https://${agoricNetSubdomain}.rpc.agoric.net:443`;

/** @satisfies {MinimalNetworkConfig} */
export const LOCAL_CONFIG = {
rpcAddrs: ['http://0.0.0.0:26657'],
chainName: 'agoriclocal',
};

export const LOCAL_CONFIG_KEY = 'local';

/**
* Fetches the network config for the given network specifier.
*
* @param {string} spec
* @param {{ fetch: typeof fetch }} io
* @returns {Promise<MinimalNetworkConfig>}
*/
export const fetchNetworkConfig = async (spec, { fetch }) => {
const [netName, chainName] = spec.split(',');

if (netName === LOCAL_CONFIG_KEY) {
return LOCAL_CONFIG;
}

if (chainName) {
return { chainName, rpcAddrs: [toRpcUrl(netName)] };
}

return fetch(toNetworkConfigUrl(netName))
.then(res => res.json())
.catch(err => {
throw Error(`cannot get network config (${spec}): ${err.message}`);
});
};
2 changes: 1 addition & 1 deletion packages/client-utils/src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StargateClient } from '@cosmjs/stargate';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';

/**
* @typedef {{ rpcAddrs: string[], chainName: string }} MinimalNetworkConfig
* @import {MinimalNetworkConfig} from './network-config.js';
*/

// TODO distribute load
Expand Down
2 changes: 1 addition & 1 deletion packages/client-utils/src/vstorage-kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { makeVStorage } from './vstorage.js';
export { boardSlottingMarshaller };

/**
* @import {MinimalNetworkConfig} from './rpc.js';
* @import {MinimalNetworkConfig} from './network-config.js';
* @import {TypedPublished} from './types.js';
* @import {VStorage} from './vstorage.js';
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/client-utils/src/vstorage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global Buffer */

/**
* @import {MinimalNetworkConfig} from './rpc.js';
* @import {MinimalNetworkConfig} from './network-config.js';
*/

/**
Expand Down
Loading

0 comments on commit 9bdba57

Please sign in to comment.