Skip to content

Commit

Permalink
feat: implement makeQuoteNotifier(amountIn, brandOut)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 10, 2020
1 parent 570c92c commit 3035203
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
32 changes: 20 additions & 12 deletions packages/zoe/tools/fakePriceAuthority.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// @ts-check
import { makeIssuerKit, MathKind, makeLocalAmountMath } from '@agoric/ertp';
import { makePromiseKit } from '@agoric/promise-kit';
import { makeNotifierKit } from '@agoric/notifier';
import {
makeNotifierKit,
makeNotifierFromAsyncIterable,
} from '@agoric/notifier';
import { E } from '@agoric/eventual-send';
import { assert, details } from '@agoric/assert';

Expand Down Expand Up @@ -79,7 +82,7 @@ export async function makeFakePriceAuthority(options) {
const quoteIssuer = E(quoteMint).getIssuer();
const quoteMath = await makeLocalAmountMath(quoteIssuer);

/** @type {NotifierRecord<PriceQuote>} */
/** @type {NotifierRecord<undefined>} */
const { notifier, updater } = makeNotifierKit();

/**
Expand Down Expand Up @@ -140,13 +143,7 @@ export async function makeFakePriceAuthority(options) {
} else {
currentPriceIndex += 1;
}
const [tradeValueIn] = currentTrade();
const rawQuote = priceInQuote(
mathIn.make(tradeValueIn),
mathOut.getBrand(),
t,
);
updater.updateState(rawQuote);
updater.updateState(undefined);
for (const req of comparisonQueue) {
// eslint-disable-next-line no-await-in-loop
const priceQuote = priceInQuote(req.amountIn, req.brandOut, t);
Expand Down Expand Up @@ -176,6 +173,17 @@ export async function makeFakePriceAuthority(options) {
return promiseKit.promise;
}

async function* generateQuotes(amountIn, brandOut) {
let record = await notifier.getUpdateSince();
while (record.updateCount) {
// eslint-disable-next-line no-await-in-loop
const timestamp = await E(timer).getCurrentTimestamp();
yield priceInQuote(amountIn, brandOut, timestamp);
// eslint-disable-next-line no-await-in-loop
record = await notifier.getUpdateSince(record.updateCount);
}
}

/** @type {PriceAuthority} */
const priceAuthority = {
getQuoteIssuer: (brandIn, brandOut) => {
Expand All @@ -186,9 +194,9 @@ export async function makeFakePriceAuthority(options) {
assertBrands(brandIn, brandOut);
return timer;
},
getQuoteNotifier: async (brandIn, brandOut) => {
assertBrands(brandIn, brandOut);
return notifier;
makeQuoteNotifier: async (amountIn, brandOut) => {
assertBrands(amountIn.brand, brandOut);
return makeNotifierFromAsyncIterable(generateQuotes(amountIn, brandOut));
},
quoteAtTime: (timeStamp, amountIn, brandOut) => {
assertBrands(amountIn.brand, brandOut);
Expand Down
7 changes: 5 additions & 2 deletions packages/zoe/tools/priceAuthorityRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ export const makePriceAuthorityRegistry = () => {
async quoteWanted(brandIn, amountOut) {
return E(paFor(brandIn, amountOut.brand)).quoteWanted(brandIn, amountOut);
},
async getQuoteNotifier(brandIn, brandOut) {
return E(paFor(brandIn, brandOut)).getQuoteNotifier(brandIn, brandOut);
async makeQuoteNotifier(amountIn, brandOut) {
return E(paFor(amountIn.brand, brandOut)).makeQuoteNotifier(
amountIn,
brandOut,
);
},
async quoteAtTime(deadline, amountIn, brandOut) {
return E(paFor(amountIn.brand, brandOut)).quoteAtTime(
Expand Down
10 changes: 4 additions & 6 deletions packages/zoe/tools/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@
* getTimerService get the timer used in PriceQuotes for a given
* brandIn/brandOut pair
*
* @property {(brandIn: Brand, brandOut: Brand) => ERef<Notifier<PriceQuote>>}
* getQuoteNotifier be notified of the latest PriceQuotes for a given
* brandIn/brandOut pair. Note that these are not necessarily all for a
* constant amountIn (or amountOut), though some authorities may do that. The
* fact that they are raw quotes means that a PriceAuthority can implement
* quotes for both fungible and non-fungible brands.
* @property {(amountIn: Amount, brandOut: Brand) => ERef<Notifier<PriceQuote>>}
* makeQuoteNotifier Be notified of the latest PriceQuotes for a given
* `amountIn`. The rate at which these are issued may be very different between
* `priceAuthorities`.
*
* @property {(deadline: Timestamp, amountIn: Amount, brandOut: Brand) =>
* Promise<PriceQuote>} quoteAtTime Resolves after `deadline` passes on the
Expand Down

0 comments on commit 3035203

Please sign in to comment.