Skip to content

Commit

Permalink
refactor(clientSupport): inline parseAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 22, 2023
1 parent 9020d5f commit 327e88f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
23 changes: 3 additions & 20 deletions packages/agoric-cli/src/commands/inter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import { CommanderError, InvalidArgumentError } from 'commander';
// TODO: should get M from endo https://github.com/Agoric/agoric-sdk/issues/7090
import { makeOfferSpecShape } from '@agoric/inter-protocol/src/auction/auctionBook.js';
import {
Offers,
makeParseAmount,
} from '@agoric/inter-protocol/src/clientSupport.js';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { objectMap } from '@agoric/internal';
import { M, matches } from '@agoric/store';

Expand Down Expand Up @@ -418,14 +415,7 @@ inter auction status
async ({ generateOnly, dryRun, ...opts }) => {
const tools = await tryMakeUtils();

const parseAmount = makeParseAmount(
tools.agoricNames,
msg => new InvalidArgumentError(msg),
);
const offer = Offers.auction.Bid(tools.agoricNames.brand, {
...opts,
parseAmount,
});
const offer = Offers.auction.Bid(tools.agoricNames, opts);

if (generateOnly) {
outputActionAndHint(
Expand Down Expand Up @@ -466,14 +456,7 @@ inter auction status
async ({ generateOnly, ...opts }) => {
const tools = await tryMakeUtils();

const parseAmount = makeParseAmount(
tools.agoricNames,
msg => new InvalidArgumentError(msg),
);
const offer = Offers.auction.Bid(tools.agoricNames.brand, {
...opts,
parseAmount,
});
const offer = Offers.auction.Bid(tools.agoricNames, opts);
if (generateOnly) {
outputActionAndHint(
{ method: 'executeOffer', offer },
Expand Down
8 changes: 3 additions & 5 deletions packages/inter-protocol/src/clientSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,26 @@ export const makeParseAmount =
};

/**
* @param {Record<string, Brand>} _brands
* @param {Pick<import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes, 'brand' | 'vbankAsset'>} agoricNames
* @param {{
* offerId: string,
* give: string,
* maxBuy: string,
* wantMinimum?: string,
* parseAmount: (x: string) => Amount<'nat'>,
* } & ({
* price: number,
* } | {
* discount: number, // -1 to 1. e.g. 0.10 for 10% discount, -0.05 for 5% markup
* })} opts
* @returns {import('@agoric/smart-wallet/src/offers.js').OfferSpec}
*/
const makeBidOffer = (_brands, opts) => {
assert.typeof(opts.parseAmount, 'function');
const makeBidOffer = (agoricNames, opts) => {
assertAllDefined({
offerId: opts.offerId,
give: opts.give,
maxBuy: opts.maxBuy,
});
const { parseAmount } = opts;
const parseAmount = makeParseAmount(agoricNames);
const proposal = {
give: { Bid: parseAmount(opts.give) },
...(opts.wantMinimum
Expand Down
19 changes: 8 additions & 11 deletions packages/inter-protocol/test/test-clientSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { makeIssuerKit } from '@agoric/ertp';
import { makeRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { Offers } from '../src/clientSupport.js';
import { withAmountUtils } from './supports.js';
import { Offers, makeParseAmount } from '../src/clientSupport.js';

const ist = withAmountUtils(makeIssuerKit('IST'));
const atom = withAmountUtils(makeIssuerKit('ATOM'));
Expand All @@ -13,7 +13,9 @@ const brands = {
ATOM: atom.brand,
};

const agoricNames = /** @type {const} */ ({
// XXX use @satisfies
/** @type {import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes} */
const agoricNames = /** @type {any} */ ({
brand: brands,
vbankAsset: {
uist: {
Expand Down Expand Up @@ -43,16 +45,14 @@ test('Offers.auction.Bid', async t => {
{ cliArg: -0.1, offerBidScaling: makeRatio(110n, ist.brand, 100n) },
];

const parseAmount = makeParseAmount(agoricNames);
discounts.forEach(({ cliArg, offerBidScaling }) => {
t.log('discount', cliArg * 100, '%');
t.deepEqual(
Offers.auction.Bid(brands, {
Offers.auction.Bid(agoricNames, {
offerId: 'foo1',
give: '4.56IST',
discount: cliArg,
maxBuy: '10_000ATOM',
parseAmount,
}),
{
id: 'foo1',
Expand All @@ -75,12 +75,11 @@ test('Offers.auction.Bid', async t => {
const price = 7;
const offerPrice = makeRatio(7n, ist.brand, 1n, atom.brand);
t.deepEqual(
Offers.auction.Bid(brands, {
Offers.auction.Bid(agoricNames, {
offerId: 'by-price2',
give: '4.56IST',
price,
maxBuy: '10_000ATOM',
parseAmount,
}),
{
id: 'by-price2',
Expand All @@ -98,13 +97,12 @@ test('Offers.auction.Bid', async t => {
);

t.deepEqual(
Offers.auction.Bid(brands, {
Offers.auction.Bid(agoricNames, {
offerId: 'by-price2',
maxBuy: '10_000ATOM',
wantMinimum: '1.23ATOM',
give: '4.56IST',
price,
parseAmount,
}),
{
id: 'by-price2',
Expand All @@ -128,12 +126,11 @@ test('Offers.auction.Bid', async t => {
t.throws(
() =>
// @ts-expect-error error checking test
Offers.auction.Bid(brands, {
Offers.auction.Bid(agoricNames, {
offerId: 'by-price2',
wantMinimum: '1.23ATOM',
give: '4.56IST',
price,
parseAmount,
}),
{ message: 'missing ["maxBuy"]' },
);
Expand Down

0 comments on commit 327e88f

Please sign in to comment.