Skip to content

Commit

Permalink
test: base makeAgdWalletUtils on client-utils WalletUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Dec 11, 2024
1 parent 4dd8cb6 commit a9b385a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 79 deletions.
11 changes: 6 additions & 5 deletions a3p-integration/proposals/s:stake-bld/stakeBld.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GOV1ADDR } from '@agoric/synthetic-chain';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import assert from 'node:assert';
import process from 'node:process';
import { networkConfig, walletUtils } from './test-lib/index.js';
import { networkConfig, agdWalletUtils } from './test-lib/index.js';

// XXX not the same as VALIDATOR_ADDRESS, which is actually the delegator
const VALIDATOR_ADDRESS = process.env.VALIDATOR_ADDRESS;
Expand All @@ -26,14 +26,15 @@ const currentDelegation = async () => {
test('basic', async t => {
assert(GOV1ADDR);

const { brand } = walletUtils.agoricNames;
const { brand } = agdWalletUtils.agoricNames;

t.is((await currentDelegation()).length, 1, 'just the initial delegation');

/** @type {import('@agoric/ertp').Brand} */
// @ts-expect-error actually a BoardRemote
const BLDBrand = brand.BLD;

await walletUtils.broadcastBridgeAction(GOV1ADDR, {
await agdWalletUtils.broadcastBridgeAction(GOV1ADDR, {
method: 'executeOffer',
offer: {
id: 'request-stake',
Expand All @@ -50,7 +51,7 @@ test('basic', async t => {
},
});

await walletUtils.broadcastBridgeAction(GOV1ADDR, {
await agdWalletUtils.broadcastBridgeAction(GOV1ADDR, {
method: 'executeOffer',
offer: {
id: 'request-delegate-6',
Expand All @@ -75,7 +76,7 @@ test('basic', async t => {
// omit 'delegation' because it has 'delegatorAddress' which is different every test run
});

await walletUtils.broadcastBridgeAction(GOV1ADDR, {
await agdWalletUtils.broadcastBridgeAction(GOV1ADDR, {
method: 'executeOffer',
offer: {
id: 'request-undelegate',
Expand Down
13 changes: 9 additions & 4 deletions a3p-integration/proposals/s:stake-bld/test-lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env node */
import { makeWalletUtils, LOCAL_CONFIG } from '@agoric/client-utils';
import { execFileSync } from 'child_process';
import { LOCAL_CONFIG as networkConfig } from '@agoric/client-utils';
import { makeWalletUtils } from './wallet.js';
import { makeAgdWalletUtils } from './wallet.js';

export { networkConfig };
export const networkConfig = LOCAL_CONFIG;

/**
* Resolve after a delay in milliseconds.
Expand All @@ -14,6 +14,11 @@ export { networkConfig };
const delay = ms => new Promise(resolve => setTimeout(() => resolve(), ms));

export const walletUtils = await makeWalletUtils(
{ execFileSync, delay, fetch },
{ delay, fetch },
networkConfig,
);

export const agdWalletUtils = await makeAgdWalletUtils(
{ execFileSync, walletUtils, delay },
networkConfig,
);
24 changes: 12 additions & 12 deletions a3p-integration/proposals/s:stake-bld/test-lib/wallet.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// @ts-check

import { makeVstorageKit } from '@agoric/client-utils';
import { sendAction } from 'agoric/src/lib/index.js';
import { inspect } from 'util';

export const makeWalletUtils = async (
{ delay, execFileSync, fetch },
/**
* Stop-gap using execFileSync until we have a pure JS signing client.
*
* @param {object} root0
* @param {import('child_process')['execFileSync']} root0.execFileSync
* @param {import('@agoric/client-utils').WalletUtils} root0.walletUtils
* @param {any} root0.delay
* @param {import('@agoric/client-utils').MinimalNetworkConfig} networkConfig
*/
export const makeAgdWalletUtils = async (
{ execFileSync, walletUtils, delay },
networkConfig,
) => {
const { agoricNames, fromBoard, marshaller, readLatestHead, vstorage } =
await makeVstorageKit({ fetch }, networkConfig);

/**
*
* @param {string} from
Expand All @@ -23,17 +28,12 @@ export const makeWalletUtils = async (
delay,
execFileSync,
from,
marshaller,
keyring: { backend: 'test' },
});
};

return {
agoricNames,
...walletUtils,
broadcastBridgeAction,
fromBoard,
networkConfig,
readLatestHead,
vstorage,
};
};
2 changes: 1 addition & 1 deletion a3p-integration/proposals/z:acceptance/test-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export const walletUtils = await makeWalletUtils(
);

export const agdWalletUtils = await makeAgdWalletUtils(
{ execFileSync, setTimeout, walletUtils },
{ execFileSync, walletUtils, delay },
networkConfig,
);
62 changes: 5 additions & 57 deletions a3p-integration/proposals/z:acceptance/test-lib/wallet.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,21 @@
// TODO DRY in https://github.com/Agoric/agoric-sdk/issues/9109
// @ts-check
/* global */

import { sendAction } from 'agoric/src/lib/index.js';
import { inspect } from 'util';
import { execSwingsetTransaction, pollTx } from './chain.js';
import { makeTimerUtils } from './utils.js';

/**
* Sign and broadcast a wallet-action.
*
* @throws { Error & { code: number } } if transaction fails
* @param {import('@agoric/smart-wallet/src/smartWallet.js').BridgeAction} bridgeAction
* @param {import('@agoric/client-utils').MinimalNetworkConfig & {
* from: string,
* marshaller: Pick<import('@endo/marshal').Marshal<string | null>, 'toCapData'>,
* fees?: string,
* verbose?: boolean,
* keyring?: {home?: string, backend: string},
* stdout?: Pick<import('stream').Writable, 'write'>,
* execFileSync: typeof import('child_process').execFileSync,
* delay: (ms: number) => Promise<void>,
* dryRun?: boolean,
* }} opts
*/
export const sendAction = async (bridgeAction, opts) => {
const { marshaller } = opts;
const offerBody = JSON.stringify(marshaller.toCapData(harden(bridgeAction)));

// tryExit should not require --allow-spend
// https://github.com/Agoric/agoric-sdk/issues/7291
const spendMethods = ['executeOffer', 'tryExitOffer'];
const spendArg = spendMethods.includes(bridgeAction.method)
? ['--allow-spend']
: [];

const act = ['wallet-action', ...spendArg, offerBody];
const out = execSwingsetTransaction([...act, '--output', 'json'], opts);
if (opts.dryRun) {
return;
}

assert(out); // not dry run
const tx = JSON.parse(out);
if (tx.code !== 0) {
const err = Error(`failed to send tx: ${tx.raw_log} code: ${tx.code}`);
// @ts-expect-error XXX how to add properties to an error?
err.code = tx.code;
throw err;
}

return pollTx(tx.txhash, opts);
};

/**
* Stop-gap using execFileSync until we have a pure JS signing client.
*
* @param {object} root0
* @param {import('@agoric/client-utils').WalletUtils} root0.walletUtils
* @param {import('child_process')['execFileSync']} root0.execFileSync
* @param {typeof setTimeout} root0.setTimeout
* @param {import('@agoric/client-utils').WalletUtils} root0.walletUtils
* @param {any} root0.delay
* @param {import('@agoric/client-utils').MinimalNetworkConfig} networkConfig
*/
export const makeAgdWalletUtils = async (
{ execFileSync, walletUtils, setTimeout },
{ execFileSync, walletUtils, delay },
networkConfig,
) => {
const { marshaller } = walletUtils;

const { delay } = await makeTimerUtils({ setTimeout });
/**
*
* @param {string} from
Expand All @@ -80,12 +28,12 @@ export const makeAgdWalletUtils = async (
delay,
execFileSync,
from,
marshaller,
keyring: { backend: 'test' },
});
};

return {
...walletUtils,
broadcastBridgeAction,
};
};

0 comments on commit a9b385a

Please sign in to comment.