Skip to content

Commit

Permalink
refactor: withAmountUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 17, 2024
1 parent 4cdea40 commit 2235c6a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 46 deletions.
6 changes: 4 additions & 2 deletions packages/inter-protocol/test/auction/auctionContract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ import { setUpInstallations } from './tools.js';
/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;

/** @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js'; */

/**
* @typedef {Record<string, any> & {
* bid: IssuerKit & import('../supports.js').AmountUtils;
* collateral: IssuerKit & import('../supports.js').AmountUtils;
* bid: IssuerKit & AmountUtils;
* collateral: IssuerKit & AmountUtils;
* zoe: ZoeService;
* }} Context
*/
Expand Down
36 changes: 8 additions & 28 deletions packages/inter-protocol/test/supports.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { AmountMath } from '@agoric/ertp';
import binaryVoteCounterBundle from '@agoric/governance/bundles/bundle-binaryVoteCounter.js';
import committeeBundle from '@agoric/governance/bundles/bundle-committee.js';
import contractGovernorBundle from '@agoric/governance/bundles/bundle-contractGovernor.js';
import puppetContractGovernorBundle from '@agoric/governance/bundles/bundle-puppetContractGovernor.js';
import * as utils from '@agoric/vats/src/core/utils.js';
import { makePromiseSpace, makeAgoricNamesAccess } from '@agoric/vats';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import { makeTracer } from '@agoric/internal';
import { makeMockChainStorageRoot } from '@agoric/internal/src/storage-test-utils.js';
import { setUpZoeForTest as generalSetUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { E } from '@endo/far';
import { makeTracer } from '@agoric/internal';
import {
makeNotifierFromAsyncIterable,
observeIteration,
subscribeEach,
} from '@agoric/notifier';
import { makeAgoricNamesAccess, makePromiseSpace } from '@agoric/vats';
import { produceDiagnostics } from '@agoric/vats/src/core/basic-behaviors.js';
import * as utils from '@agoric/vats/src/core/utils.js';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { setUpZoeForTest as generalSetUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { E } from '@endo/far';

/** @import {EconomyBootstrapPowers as Space} from '../src/proposals/econ-behaviors.js' */

Expand Down Expand Up @@ -152,25 +150,7 @@ export const produceInstallations = (space, installations) => {

export const scale6 = x => BigInt(Math.round(x * 1_000_000));

/** @param {Pick<IssuerKit<'nat'>, 'brand' | 'issuer' | 'mint'>} kit */
export const withAmountUtils = kit => {
const decimalPlaces = kit.issuer.getDisplayInfo?.()?.decimalPlaces ?? 6;
return {
...kit,
/** @param {NatValue} v */
make: v => AmountMath.make(kit.brand, v),
makeEmpty: () => AmountMath.makeEmpty(kit.brand),
/**
* @param {NatValue} n
* @param {NatValue} [d]
*/
makeRatio: (n, d) => makeRatio(n, kit.brand, d),
/** @param {number} n */
units: n =>
AmountMath.make(kit.brand, BigInt(Math.round(n * 10 ** decimalPlaces))),
};
};
/** @typedef {ReturnType<typeof withAmountUtils>} AmountUtils */
export { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';

/** @param {ERef<StoredSubscription<unknown> | StoredSubscriber<unknown>>} subscription */
export const subscriptionKey = subscription => {
Expand Down
15 changes: 9 additions & 6 deletions packages/inter-protocol/test/vaultFactory/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
withAmountUtils,
} from '../supports.js';

/** @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js' */
/**
* @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js';
* @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js';
*/

const trace = makeTracer('VFDriver');

Expand All @@ -60,7 +63,7 @@ const contractRoots = {
/**
* dL: 1M, lM: 105%, lP: 10%, iR: 100, lF: 500, lP: 0%
*
* @param {import('../supports.js').AmountUtils} debt
* @param {AmountUtils} debt
*/
const defaultParamValues = debt =>
harden({
Expand All @@ -78,7 +81,7 @@ const defaultParamValues = debt =>

/**
* @typedef {{
* aeth: IssuerKit & import('../supports.js').AmountUtils;
* aeth: IssuerKit & AmountUtils;
* aethInitialLiquidity: Amount<'nat'>;
* consume: import('../../src/proposals/econ-behaviors.js').EconomyBootstrapPowers['consume'];
* puppetGovernors: {
Expand All @@ -95,7 +98,7 @@ const defaultParamValues = debt =>
* minInitialDebt: bigint;
* reserveCreatorFacet: ERef<AssetReserveLimitedCreatorFacet>;
* rates: any;
* run: IssuerKit & import('../supports.js').AmountUtils;
* run: IssuerKit & AmountUtils;
* stableInitialLiquidity: Amount<'nat'>;
* timer: ReturnType<typeof buildManualTimer>;
* zoe: ZoeService;
Expand Down Expand Up @@ -370,7 +373,7 @@ export const makeManagerDriver = async (
notification: () => notification,
/**
* @param {bigint} collValue
* @param {import('../supports.js').AmountUtils} collUtils
* @param {AmountUtils} collUtils
* @param {bigint} [mintedValue]
*/
giveCollateral: async (collValue, collUtils, mintedValue = 0n) => {
Expand All @@ -391,7 +394,7 @@ export const makeManagerDriver = async (
},
/**
* @param {bigint} mintedValue
* @param {import('../supports.js').AmountUtils} collUtils
* @param {AmountUtils} collUtils
* @param {bigint} [collValue]
*/
giveMinted: async (mintedValue, collUtils, collValue = 0n) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ import {
import '../../src/vaultFactory/types-ambient.js';
import { defaultParamValues } from './vaultFactoryUtils.js';

/** @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js' */
/**
* @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js';
* @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js';
*/

/**
* @typedef {Record<string, any> & {
* aeth: IssuerKit & import('../supports.js').AmountUtils;
* run: IssuerKit & import('../supports.js').AmountUtils;
* aeth: IssuerKit & AmountUtils;
* run: IssuerKit & AmountUtils;
* bundleCache: Awaited<ReturnType<typeof unsafeMakeBundleCache>>;
* rates: VaultManagerParamValues;
* interestTiming: InterestTiming;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ import {
/**
* @import {PriceAuthority, PriceDescription, PriceQuote, PriceQuoteValue, PriceQuery,} from '@agoric/zoe/tools/types.js';
* @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js'
* @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js';
*/

/**
* @typedef {Record<string, any> & {
* aeth: IssuerKit & import('../supports.js').AmountUtils;
* run: IssuerKit & import('../supports.js').AmountUtils;
* aeth: IssuerKit & AmountUtils;
* run: IssuerKit & AmountUtils;
* bundleCache: Awaited<ReturnType<typeof unsafeMakeBundleCache>>;
* rates: VaultManagerParamValues;
* interestTiming: InterestTiming;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import { startEconomicCommittee } from '../../src/proposals/startEconCommittee.j

export const BASIS_POINTS = 10000n;

/** @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js'; */

/**
* @typedef {Record<string, any> & {
* aeth: IssuerKit & import('../supports.js').AmountUtils;
* run: IssuerKit & import('../supports.js').AmountUtils;
* aeth: IssuerKit & AmountUtils;
* run: IssuerKit & AmountUtils;
* bundleCache: Awaited<
* ReturnType<
* typeof import('@agoric/swingset-vat/tools/bundleTool.js').unsafeMakeBundleCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ import {
setupElectorateReserveAndAuction,
} from './vaultFactoryUtils.js';

/** @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js' */
/**
* @import {VaultFactoryContract as VFC} from '../../src/vaultFactory/vaultFactory.js';
* @import {AmountUtils} from '@agoric/zoe/tools/test-utils.js';
*/

/**
* @typedef {Record<string, any> & {
* aeth: IssuerKit & import('../supports.js').AmountUtils;
* run: IssuerKit & import('../supports.js').AmountUtils;
* aeth: IssuerKit & AmountUtils;
* run: IssuerKit & AmountUtils;
* bundleCache: Awaited<ReturnType<typeof unsafeMakeBundleCache>>;
* rates: VaultManagerParamValues;
* interestTiming: InterestTiming;
Expand Down
24 changes: 24 additions & 0 deletions packages/zoe/tools/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AmountMath } from '@agoric/ertp';
import { makeRatio } from '../src/contractSupport/ratio.js';

/** @param {Pick<IssuerKit<'nat'>, 'brand' | 'issuer' | 'mint'>} kit */
export const withAmountUtils = kit => {
const decimalPlaces = kit.issuer.getDisplayInfo?.()?.decimalPlaces ?? 6;
return {
...kit,
/** @param {NatValue} v */
make: v => AmountMath.make(kit.brand, v),
makeEmpty: () => AmountMath.makeEmpty(kit.brand),
/**
* @param {NatValue} n
* @param {NatValue} [d]
*/
makeRatio: (n, d) => makeRatio(n, kit.brand, d),
/** @param {number} n */
units: n =>
AmountMath.make(kit.brand, BigInt(Math.round(n * 10 ** decimalPlaces))),
};
};
/** @typedef {ReturnType<typeof withAmountUtils>} AmountUtils */

export { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';

0 comments on commit 2235c6a

Please sign in to comment.