Skip to content

Commit

Permalink
feat(cli): use new wallet.current node
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 29, 2022
1 parent e47ae2c commit c911acb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
21 changes: 15 additions & 6 deletions packages/agoric-cli/src/commands/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
fetchSwingsetParams,
normalizeAddress,
} from '../lib/chain.js';
import { getCurrent } from '../lib/wallet.js';

const SLEEP_SECONDS = 3;

Expand Down Expand Up @@ -132,7 +133,7 @@ export const makeWalletCommand = async () => {
.action(async function () {
const opts = this.opts();

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

Expand All @@ -148,14 +149,22 @@ export const makeWalletCommand = async () => {
},
);

const state = await coalesceWalletState(follower);
const coalesced = await coalesceWalletState(follower);

const current = await getCurrent(opts.from, fromBoard, {
vstorage,
});

console.warn(
'got state',
util.inspect(state, { depth: 10, colors: true }),
'got coalesced',
util.inspect(coalesced, { depth: 10, colors: true }),
);
const summary = summarize(state, agoricNames);
process.stdout.write(fmtRecordOfLines(summary));
try {
const summary = summarize(current, coalesced, agoricNames);
process.stdout.write(fmtRecordOfLines(summary));
} catch (e) {
console.error('CAUGHT HERE', e);
}
});

wallet
Expand Down
40 changes: 23 additions & 17 deletions packages/agoric-cli/src/lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const makeAmountFormatter = assets => amt => {
/** @type {[petname: string, qty: number]} */
return [name, Number(value) / 10 ** decimalPlaces];
case 'set':
if (value[0].handle.iface.includes('InvitationHandle')) {
return [name, value.map(v => v.description)];
}
return [name, value];
default:
return [name, ['?']];
Expand All @@ -66,12 +69,12 @@ export const asPercent = ratio => {
/**
* Summarize the balances array as user-facing informative tuples
* @param {Array<Amount>} balances
* @param {import('@agoric/smart-wallet/src/smartWallet').CurrentWalletRecord['purses']} purses
* @param {AssetDescriptor[]} assets
*/
export const purseBalanceTuples = (balances, assets) => {
export const purseBalanceTuples = (purses, assets) => {
const fmt = makeAmountFormatter(assets);
return balances.map(b => fmt(b));
return purses.map(b => fmt(b.balance));
};

/**
Expand Down Expand Up @@ -154,25 +157,28 @@ export const offerStatusTuples = (state, agoricNames) => {

/**
*
* @param {ReturnType<import('@agoric/smart-wallet/src/utils.js').makeWalletStateCoalescer>['state']} state
* @param {import('@agoric/smart-wallet/src/smartWallet').CurrentWalletRecord} current
* @param {ReturnType<import('@agoric/smart-wallet/src/utils.js').makeWalletStateCoalescer>['state']} coalesced
* @param {Awaited<ReturnType<typeof makeAgoricNames>>} agoricNames
*/
export const summarize = (state, agoricNames) => {
const { balances, brands, invitationsReceived } = state;
const invitations = Array.from(invitationsReceived.values()).map(v => [
agoricNames.reverse[v.instance.boardId],
v.description,
v.acceptedIn || 'not yet accepted',
]);
export const summarize = (current, coalesced, agoricNames) => {
return {
invitations,
balances: purseBalanceTuples(
lastOfferId: [current.lastOfferId],
purses: purseBalanceTuples(
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -- https://github.com/Agoric/agoric-sdk/issues/4620 */
// @ts-ignore xxx RpcRemote
[...balances.values()],
// @ts-expect-error xxx RpcRemote
[...brands.values()],
[...current.purses.values()],
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -- https://github.com/Agoric/agoric-sdk/issues/4620 */
// @ts-ignore xxx RpcRemote
[...current.brands.values()],
),
usedInvitations: Object.entries(current.offerToUsedInvitation).map(
([offerId, invitationAmt]) => [
agoricNames.reverse[invitationAmt.value[0].instance.boardId],
invitationAmt.value[0].description,
Number(offerId),
],
),
offers: offerStatusTuples(state, agoricNames),
offers: offerStatusTuples(coalesced, agoricNames),
};
};
6 changes: 5 additions & 1 deletion packages/agoric-cli/src/lib/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const makeVStorage = powers => {
/**
*
* @param {string} path
* @returns {Promise<unknown>} latest vstorage value at path
* @returns {Promise<string>} latest vstorage value at path
*/
async readLatest(path = 'published') {
const raw = await getJSON(this.url(path, { kind: 'data' }));
Expand Down Expand Up @@ -210,6 +210,10 @@ export const storageHelper = {
const capDatas = storageHelper.parseMany(values);
return { blockHeight, capDatas };
},
/**
* @param {string} txt
* @param {IdMap} ctx
*/
unserializeTxt: (txt, ctx) => {
const { capDatas } = storageHelper.parseCapData(txt);
return capDatas.map(capData =>
Expand Down
20 changes: 20 additions & 0 deletions packages/agoric-cli/src/lib/wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check

import { storageHelper } from './rpc.js';

/**
* @param {string} addr
* @param {import('./rpc').IdMap} ctx
* @param {object} io
* @param {import('./rpc.js').VStorage} io.vstorage
* @returns {Promise<import('@agoric/smart-wallet/src/smartWallet').CurrentWalletRecord>}
*/
export const getCurrent = async (addr, ctx, { vstorage }) => {
const capDataStr = await vstorage.readLatest(
`published.wallet.${addr}.current`,
);

const capDatas = storageHelper.unserializeTxt(capDataStr, ctx);

return capDatas[0];
};
2 changes: 1 addition & 1 deletion packages/smart-wallet/scripts/start-local-chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ agd query bank balances "$WALLET_BECH32" | grep ubld || exit 1
echo "Provisioning your smart wallet..."
agoric wallet provision --spend --account "$WALLET"
echo "waiting for blocks"
sleep 5
sleep 10
# verify
agoric wallet list
agoric wallet show --from "$WALLET"
2 changes: 1 addition & 1 deletion packages/smart-wallet/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const getFirstHeight = async follower => {
};

/**
*
* @deprecated use `.current` node for current state
* @param {import('@agoric/casting').Follower<import('@agoric/casting').ValueFollowerElement<import('./smartWallet').UpdateRecord>>} follower
*/
export const coalesceWalletState = async follower => {
Expand Down

0 comments on commit c911acb

Please sign in to comment.