Skip to content

Commit

Permalink
refactor(scaledPriceAuthority): factor out mintQuote()
Browse files Browse the repository at this point in the history
 - clarify priceOutPerIn
 - clarify mintCurrentQuote
  • Loading branch information
dckc committed Feb 17, 2023
1 parent fdc8582 commit b5820be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
46 changes: 20 additions & 26 deletions packages/zoe/src/contractSupport/priceAuthorityInitial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { makeNotifier } from '@agoric/notifier';
import { AmountMath } from '@agoric/ertp';

import { multiplyBy } from './ratio.js';
import { mintQuote } from './priceAuthorityTransform.js';

/** @template T @typedef {import('@endo/eventual-send').EOnly<T>} EOnly */

Expand All @@ -15,22 +16,22 @@ import { multiplyBy } from './ratio.js';
*
* Mainly for testing vaults without waiting for oracle operators to PushPrice.
*
* @param {Ratio} initialPrice
* @param {Ratio} priceOutPerIn
* @param {PriceAuthority} priceAuthority
* @param {ERef<Mint<'set'>>} quoteMint
* @param {Brand<'nat'>} brandIn
* @param {Brand<'nat'>} brandOut
* @returns {PriceAuthority}
*/
export const makeInitialTransform = (
initialPrice,
priceOutPerIn,
priceAuthority,
quoteMint,
brandIn,
brandOut,
) => {
assert.equal(initialPrice.numerator.brand, brandOut);
assert.equal(initialPrice.denominator.brand, brandIn);
assert.equal(priceOutPerIn.numerator.brand, brandOut);
assert.equal(priceOutPerIn.denominator.brand, brandIn);
let initialMode = true;

const quoteBrandP = E(E(quoteMint).getIssuer()).getBrand();
Expand All @@ -41,34 +42,22 @@ export const makeInitialTransform = (
* @param {Amount<'nat'>} amountOut
* @returns {Promise<PriceQuote>}
*/
const oneQuote = async (amountIn, amountOut) => {
const mintCurrentQuote = async (amountIn, amountOut) => {
const [quoteBrand, timer, timestamp] = await Promise.all([
quoteBrandP,
timerP,
E(timerP).getCurrentTimestamp(),
]);

const quoteAmount = harden({
brand: quoteBrand,
value: [
{
amountIn,
amountOut,
timer,
timestamp,
},
],
});
const quotePayment = await E(quoteMint).mintPayment({
brand: quoteBrand,
value: [quoteAmount],
});
return harden({ quoteAmount, quotePayment });
return mintQuote(
quoteBrand,
amountIn,
amountOut,
timer,
timestamp,
quoteMint,
);
};
const initialUpdateP = oneQuote(
initialPrice.denominator,
initialPrice.numerator,
).then(value => harden({ value, updateCount: 0n }));

/** @type {PriceAuthority['makeQuoteNotifier']} */
const makeQuoteNotifier = (amountIn, bOut) => {
Expand All @@ -77,6 +66,11 @@ export const makeInitialTransform = (

const notifier = E(priceAuthority).makeQuoteNotifier(amountIn, brandOut);

const initialUpdateP = mintCurrentQuote(
priceOutPerIn.denominator,
priceOutPerIn.numerator,
).then(value => harden({ value, updateCount: 0n }));

// Wrap our underlying notifier.
const prefixedNotifier = harden({
async getUpdateSince(updateCount = -1n) {
Expand Down Expand Up @@ -106,7 +100,7 @@ export const makeInitialTransform = (
const quoteP = E(priceAuthority).quoteGiven(amountIn, brandOut);
quoteP.then(() => (initialMode = false));
return initialMode
? oneQuote(amountIn, multiplyBy(amountIn, initialPrice))
? mintCurrentQuote(amountIn, multiplyBy(amountIn, priceOutPerIn))
: quoteP;
};

Expand Down
46 changes: 37 additions & 9 deletions packages/zoe/src/contractSupport/priceAuthorityTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ import { makeNotifier } from '@agoric/notifier';

/** @template T @typedef {import('@endo/eventual-send').EOnly<T>} EOnly */

/**
*
* @param {Brand<'set'>} quoteBrand
* @param {Amount<'nat'>} amountIn
* @param {Amount<'nat'>} amountOut
* @param {ERef<import('@agoric/time/src/types').TimerService>} timer
* @param {import('@agoric/time').Timestamp} timestamp
* @param {ERef<Mint<'set'>>} quoteMint
* @returns {Promise<PriceQuote>}
*/
export const mintQuote = async (
quoteBrand,
amountIn,
amountOut,
timer,
timestamp,
quoteMint,
) => {
const quoteAmount = {
brand: quoteBrand,
value: [{ amountIn, amountOut, timer, timestamp }],
};
const quotePayment = await E(quoteMint).mintPayment({
brand: quoteBrand,
value: [quoteAmount],
});
return harden({ quoteAmount, quotePayment });
};

/**
* @param {object} opts
* @param {ERef<Mint<'set'>>} [opts.quoteMint]
Expand Down Expand Up @@ -84,15 +113,14 @@ export const makePriceAuthorityTransform = async ({
const amountIn = transformSourceAmountIn(sourceAmountIn);
const amountOut = transformSourceAmountOut(sourceAmountOut);

const quoteAmount = {
brand: quoteBrand,
value: [{ amountIn, amountOut, timer, timestamp }],
};
const quotePayment = await E(quoteMint).mintPayment({
brand: quoteBrand,
value: [quoteAmount],
});
return harden({ quoteAmount, quotePayment });
return mintQuote(
quoteBrand,
amountIn,
amountOut,
timer,
timestamp,
quoteMint,
);
};

/**
Expand Down

0 comments on commit b5820be

Please sign in to comment.