Skip to content

Commit

Permalink
Merge pull request #7299 from Agoric/dc-delay-cli-io
Browse files Browse the repository at this point in the history
fix: agops: no I/O (errors) until command action
  • Loading branch information
mergify[bot] authored Apr 1, 2023
2 parents 6ee9369 + 58b9d4e commit c548d92
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 45 deletions.
8 changes: 6 additions & 2 deletions packages/agoric-cli/src/commands/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { Command } from 'commander';
import { makeRpcUtils, storageHelper } from '../lib/rpc.js';
import { outputExecuteOfferAction } from '../lib/wallet.js';

const { vstorage, fromBoard, agoricNames } = await makeRpcUtils({ fetch });

/**
*
* @param {import('anylogger').Logger} _logger
Expand All @@ -24,6 +22,8 @@ export const makeEconomicCommiteeCommand = async _logger => {
`ecCommittee-${Date.now()}`,
)
.action(async function (opts) {
const { agoricNames } = await makeRpcUtils({ fetch });

const { economicCommittee } = agoricNames.instance;
assert(economicCommittee, 'missing economicCommittee');

Expand All @@ -47,6 +47,8 @@ export const makeEconomicCommiteeCommand = async _logger => {
.description('prepare an offer to accept the charter invitation')
.option('--offerId [string]', 'Offer id', String, `ecCharter-${Date.now()}`)
.action(async function (opts) {
const { agoricNames } = await makeRpcUtils({ fetch });

const { econCommitteeCharter } = agoricNames.instance;
assert(econCommitteeCharter, 'missing econCommitteeCharter');

Expand Down Expand Up @@ -79,6 +81,8 @@ export const makeEconomicCommiteeCommand = async _logger => {
Number,
)
.action(async function (opts) {
const { vstorage, fromBoard } = await makeRpcUtils({ fetch });

const questionHandleCapDataStr = await vstorage.readLatest(
'published.committees.Economic_Committee.latestQuestion',
);
Expand Down
26 changes: 16 additions & 10 deletions packages/agoric-cli/src/commands/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { inspect } from 'util';
import { makeRpcUtils, storageHelper } from '../lib/rpc.js';
import { outputAction } from '../lib/wallet.js';

const { agoricNames, fromBoard, vstorage } = await makeRpcUtils({ fetch });

// XXX support other decimal places
const COSMOS_UNIT = 1_000_000n;
const scaleDecimals = num => BigInt(num * Number(COSMOS_UNIT));
Expand Down Expand Up @@ -43,14 +41,20 @@ export const makeOracleCommand = async logger => {
`,
);

const lookupPriceAggregatorInstance = ([brandIn, brandOut]) => {
const name = `${brandIn}-${brandOut} price feed`;
const instance = agoricNames.instance[name];
if (!instance) {
logger.debug('known instances:', agoricNames.instance);
throw new Error(`Unknown instance ${name}`);
}
return instance;
const rpcTools = async () => {
const utils = await makeRpcUtils({ fetch });

const lookupPriceAggregatorInstance = ([brandIn, brandOut]) => {
const name = `${brandIn}-${brandOut} price feed`;
const instance = utils.agoricNames.instance[name];
if (!instance) {
logger.debug('known instances:', utils.agoricNames.instance);
throw new Error(`Unknown instance ${name}`);
}
return instance;
};

return { ...utils, lookupPriceAggregatorInstance };
};

oracle
Expand All @@ -64,6 +68,7 @@ export const makeOracleCommand = async logger => {
)
.option('--offerId [number]', 'Offer id', Number, Date.now())
.action(async function (opts) {
const { lookupPriceAggregatorInstance } = await rpcTools();
const instance = lookupPriceAggregatorInstance(opts.pair);

/** @type {import('@agoric/smart-wallet/src/offers.js').OfferSpec} */
Expand Down Expand Up @@ -160,6 +165,7 @@ export const makeOracleCommand = async logger => {
)
.action(async function (opts) {
const { pair } = opts;
const { vstorage, fromBoard } = await rpcTools();

const capDataStr = await vstorage.readLatest(
`published.priceFeed.${pair[0]}-${pair[1]}_price_feed`,
Expand Down
64 changes: 37 additions & 27 deletions packages/agoric-cli/src/commands/psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ function collectValues(val, memo) {
return memo;
}

const { vstorage, fromBoard, agoricNames } = await makeRpcUtils({ fetch });

/**
*
* @param {[Minted: string, Anchor: string]} pair
*/
const getGovernanceState = async ([Minted, Anchor]) => {
const govContent = await vstorage.readLatest(
`published.psm.${Minted}.${Anchor}.governance`,
);
assert(govContent, 'no gov content');
const { current: governance } = last(
storageHelper.unserializeTxt(govContent, fromBoard),
);
const { [`psm.${Minted}.${Anchor}`]: instance } = agoricNames.instance;

return { instance, governance };
};

/**
*
* @param {import('anylogger').Logger} logger
Expand Down Expand Up @@ -81,20 +62,45 @@ export const makePsmCommand = async logger => {
`,
);

const lookupPsmInstance = ([minted, anchor]) => {
const name = `psm-${minted}-${anchor}`;
const instance = agoricNames.instance[name];
if (!instance) {
logger.debug('known instances:', agoricNames.instance);
throw new Error(`Unknown instance ${name}`);
}
return instance;
const rpcTools = async () => {
const utils = await makeRpcUtils({ fetch });

const lookupPsmInstance = ([minted, anchor]) => {
const name = `psm-${minted}-${anchor}`;
const instance = utils.agoricNames.instance[name];
if (!instance) {
logger.debug('known instances:', utils.agoricNames.instance);
throw new Error(`Unknown instance ${name}`);
}
return instance;
};

/**
*
* @param {[Minted: string, Anchor: string]} pair
*/
const getGovernanceState = async ([Minted, Anchor]) => {
const govContent = await utils.vstorage.readLatest(
`published.psm.${Minted}.${Anchor}.governance`,
);
assert(govContent, 'no gov content');
const { current: governance } = last(
storageHelper.unserializeTxt(govContent, utils.fromBoard),
);
const { [`psm.${Minted}.${Anchor}`]: instance } =
utils.agoricNames.instance;

return { instance, governance };
};

return { ...utils, lookupPsmInstance, getGovernanceState };
};

psm
.command('list')
.description('list all PSMs in network')
.action(async function () {
const { vstorage } = await rpcTools();
const mints = await vstorage.keys('published.psm');
for (const minted of mints) {
const anchors = await vstorage.keys(`published.psm.${minted}`);
Expand All @@ -116,6 +122,7 @@ export const makePsmCommand = async logger => {
)
.action(async function (opts) {
const { pair } = opts;
const { getGovernanceState } = await rpcTools();
const { governance } = await getGovernanceState(pair);
console.log('psm governance params', Object.keys(governance));
console.log('MintLimit', governance.MintLimit.value);
Expand Down Expand Up @@ -149,6 +156,7 @@ export const makePsmCommand = async logger => {
.option('--offerId [string]', 'Offer id', String, `swap-${Date.now()}`)
.action(async function (opts) {
console.warn('running with options', opts);
const { agoricNames, lookupPsmInstance } = await rpcTools();
const instance = await lookupPsmInstance(opts.pair);
const offer = Offers.psm.swap(instance, agoricNames.brand, {
offerId: opts.offerId,
Expand Down Expand Up @@ -191,6 +199,7 @@ export const makePsmCommand = async logger => {
1,
)
.action(async function (opts) {
const { lookupPsmInstance } = await rpcTools();
const psmInstance = lookupPsmInstance(opts.pair);

/** @type {import('@agoric/smart-wallet/src/offers.js').OfferSpec} */
Expand Down Expand Up @@ -242,6 +251,7 @@ export const makePsmCommand = async logger => {
1,
)
.action(async function (opts) {
const { agoricNames, lookupPsmInstance } = await rpcTools();
const psmInstance = lookupPsmInstance(opts.pair);

const istBrand = agoricNames.brand.IST;
Expand Down
4 changes: 2 additions & 2 deletions packages/agoric-cli/src/commands/reserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { Command } from 'commander';
import { makeRpcUtils } from '../lib/rpc.js';
import { outputExecuteOfferAction } from '../lib/wallet.js';

const { agoricNames } = await makeRpcUtils({ fetch });

/**
*
* @param {import('anylogger').Logger} _logger
Expand Down Expand Up @@ -36,6 +34,8 @@ export const makeReserveCommand = async _logger => {
1,
)
.action(async function (opts) {
const { agoricNames } = await makeRpcUtils({ fetch });

const reserveInstance = agoricNames.instance.reserve;
assert(reserveInstance, 'missing reserve in names');

Expand Down
9 changes: 5 additions & 4 deletions packages/agoric-cli/src/commands/vaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import { normalizeAddressWithOptions } from '../lib/chain.js';
import { makeRpcUtils } from '../lib/rpc.js';
import { getCurrent, outputExecuteOfferAction } from '../lib/wallet.js';

const { agoricNames, readLatestHead } = await makeRpcUtils({
fetch,
});

/**
*
* @param {import('anylogger').Logger} logger
Expand Down Expand Up @@ -43,6 +39,8 @@ export const makeVaultsCommand = async logger => {
normalizeAddress,
)
.action(async function (opts) {
const { readLatestHead } = await makeRpcUtils({ fetch });

const current = await getCurrent(opts.from, {
readLatestHead,
});
Expand All @@ -66,6 +64,7 @@ export const makeVaultsCommand = async logger => {
.option('--collateralBrand [string]', 'Collateral brand key', 'IbcATOM')
.action(async function (opts) {
logger.warn('running with options', opts);
const { agoricNames } = await makeRpcUtils({ fetch });

const offer = Offers.vaults.OpenVault(agoricNames.brand, {
giveCollateral: opts.giveCollateral,
Expand Down Expand Up @@ -95,6 +94,7 @@ export const makeVaultsCommand = async logger => {
.requiredOption('--vaultId [string]', 'Key of vault (e.g. vault1)')
.action(async function (opts) {
logger.warn('running with options', opts);
const { agoricNames, readLatestHead } = await makeRpcUtils({ fetch });

const previousOfferId = await lookupOfferIdForVault(
opts.vaultId,
Expand Down Expand Up @@ -130,6 +130,7 @@ export const makeVaultsCommand = async logger => {
.requiredOption('--vaultId [string]', 'Key of vault (e.g. vault1)')
.action(async function (opts) {
logger.warn('running with options', opts);
const { agoricNames, readLatestHead } = await makeRpcUtils({ fetch });

const previousOfferId = await lookupOfferIdForVault(
opts.vaultId,
Expand Down

0 comments on commit c548d92

Please sign in to comment.