Skip to content

Commit

Permalink
fix: update types and implementation now that Far preserves them
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Apr 28, 2021
1 parent fcc0cc4 commit a4695c4
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/vats/network/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @typedef {Object} ListenHandler A handler for incoming connections
* @property {(port: Port, l: ListenHandler) => Promise<void>} [onListen] The listener has been registered
* @property {(port: Port, localAddr: Endpoint, remoteAddr: Endpoint, l: ListenHandler) => Promise<ConnectionHandler>} onAccept A new connection is incoming
* @property {(port: Port, localAddr: Endpoint, remoteAddr: Endpoint, l: ListenHandler) => Promise<void>} onReject The connection was rejected
* @property {(port: Port, localAddr: Endpoint, remoteAddr: Endpoint, l: ListenHandler) => Promise<void>} [onReject] The connection was rejected
* @property {(port: Port, rej: any, l: ListenHandler) => Promise<void>} [onError] There was an error while listening
* @property {(port: Port, l: ListenHandler) => Promise<void>} [onRemove] The listener has been removed
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-svelte-wallet/api/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
/**
* @typedef {Object} PurseActions
* @property {(receiverP: ERef<{ receive: (payment: Payment) => void }>, valueToSend: Value) => Promise<void>} send
* @property {(payment: Payment) => Promise<Value>} receive
* @property {(payment: Payment, amount: Amount=) => Promise<Value>} deposit
* @property {(payment: Payment) => Promise<Amount>} receive
* @property {(payment: Payment, amount: Amount=) => Promise<Amount>} deposit
*/

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/notifier/src/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {

import './types';

/**
* @template T
* @param {BaseNotifier<T>} baseNotifierP
* @returns {AsyncIterable<T> & SharableNotifier}
*/
export const makeNotifier = baseNotifierP => {
const asyncIterable = makeAsyncIterableFromNotifier(baseNotifierP);

Expand Down
14 changes: 10 additions & 4 deletions packages/notifier/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@

/**
* @template T
* @typedef {BaseNotifier<T> & AsyncIterable<T>} Notifier<T> an object that can
* @typedef {BaseNotifier<T> & AsyncIterable<T> & SharableNotifier} Notifier<T> an object that can
* be used to get the current state or updates
*
*/

/**
* @typedef {Object} SharableNotifier
* @property {() => NotifierInternals} getSharableNotifierInternals
* Used to replicate the multicast values at other sites. To manually create a
* local representative of a Notification, do
Expand Down Expand Up @@ -103,9 +106,12 @@

/**
* @template T
* @typedef {BaseSubscription<T> & AsyncIterable<T>} Subscription<T>
* @typedef {BaseSubscription<T> & AsyncIterable<T> & SharableSubscription} Subscription<T>
* A form of AsyncIterable supporting distributed and multicast usage.
*
*/

/**
* @typedef {Object} SharableSubscription
* @property {() => SubscriptionInternals} getSharableSubscriptionInternals
* Used to replicate the multicast values at other sites. To manually create a
* local representative of a Subscription, do
Expand Down
2 changes: 1 addition & 1 deletion packages/vats/src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export function buildRootObject(vatPowers, vatParameters) {
return harden(makeEchoConnectionHandler());
},
async onListen(port, _listenHandler) {
console.debug(`listening on port: ${port}`);
console.debug(`listening on echo port: ${port}`);
},
}),
);
Expand Down
44 changes: 39 additions & 5 deletions packages/zoe/src/contractSupport/priceAuthority.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Far } from '@agoric/marshal';
import { assert, details as X } from '@agoric/assert';
import { makePromiseKit } from '@agoric/promise-kit';
import { amountMath } from '@agoric/ertp';
import { makeNotifier } from '@agoric/notifier';

import '../../exported';

Expand Down Expand Up @@ -236,26 +237,57 @@ export function makeOnewayPriceAuthorityKit(opts) {
assertBrands(brandIn, brandOut);
return timer;
},
getPriceNotifier(brandIn, brandOut) {
assertBrands(brandIn, brandOut);
return notifier;
makeQuoteNotifier(amountIn, brandOut) {
amountMath.coerce(amountIn, actualBrandIn);
assertBrands(amountIn.brand, brandOut);

// Wrap our underlying notifier with specific quotes.
const specificBaseNotifier = harden({
async getUpdateSince(updateCount = NaN) {
// We use the same updateCount as our underlying notifier.
const record = await E(notifier).getUpdateSince(updateCount);

// We create a quote inline.
const quote = createQuote(calcAmountOut => ({
amountIn,
amountOut: calcAmountOut(amountIn),
}));
assert(quote);

const value = await quote;
return harden({
value,
updateCount: record.updateCount,
});
},
});

/** @type {Notifier<PriceQuote>} */
const specificNotifier = Far('QuoteNotifier', {
...makeNotifier(specificBaseNotifier),
// TODO stop exposing baseNotifier methods directly.
...specificBaseNotifier,
});
return specificNotifier;
},
async quoteGiven(amountIn, brandOut) {
amountMath.coerce(amountIn, actualBrandIn);
assertBrands(amountIn.brand, brandOut);

await E(notifier).getUpdateSince();
return createQuote(calcAmountOut => ({
const quote = createQuote(calcAmountOut => ({
amountIn,
amountOut: calcAmountOut(amountIn),
}));
assert(quote);
return quote;
},
async quoteWanted(brandIn, amountOut) {
amountMath.coerce(amountOut, actualBrandOut);
assertBrands(brandIn, amountOut.brand);

await E(notifier).getUpdateSince();
return createQuote((calcAmountOut, calcAmountIn) => {
const quote = createQuote((calcAmountOut, calcAmountIn) => {
// We need to determine an amountIn that guarantees at least the amountOut.
const amountIn = calcAmountIn(amountOut);
const actualAmountOut = calcAmountOut(amountIn);
Expand All @@ -266,6 +298,8 @@ export function makeOnewayPriceAuthorityKit(opts) {
);
return { amountIn, amountOut };
});
assert(quote);
return quote;
},
async quoteAtTime(deadline, amountIn, brandOut) {
assert.typeof(deadline, 'bigint');
Expand Down
4 changes: 1 addition & 3 deletions packages/zoe/src/contracts/multipoolAutoswap/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* using makeSwapOutInvitation at the current price
* @property {() => Record<string, Amount>} getPoolAllocation get an
* AmountKeywordRecord showing the current balances in the pool.
* @property {() => Array<Brand>} getAllPoolBrands get a list of all the brands
* that have associated pools.
*/

/**
Expand Down Expand Up @@ -88,7 +86,7 @@
* AmountKeywordRecord showing the current balances in the pool for brand.
* @property {() => Issuer} getQuoteIssuer - get the Issuer that attests to
* the prices in the priceQuotes issued by the PriceAuthorities
* @property {(brand: Brand) => {toCentral: PriceAuthority, fromCentral: PriceAuthority}}} getPriceAuthorities
* @property {(brand: Brand) => {toCentral: PriceAuthority, fromCentral: PriceAuthority}} getPriceAuthorities
* get a pair of PriceAuthorities { toCentral, fromCentral } for requesting
* Prices and notifications about changing prices.
*/
9 changes: 6 additions & 3 deletions packages/zoe/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,17 @@
* @typedef {Object} InstanceAdmin
* @property {() => void} assertAcceptingOffers
* @property {(invitationHandle: InvitationHandle,
initialAllocation: Allocation,
proposal: ProposalRecord) => UserSeat } makeUserSeat
* initialAllocation: Allocation,
* proposal: ProposalRecord) => Promise<UserSeat> } makeUserSeat
* @property {(initialAllocation: Allocation,
* proposal: ProposalRecord,
* exitObj: ExitObj,
* seatHandle: SeatHandle) => foo } makeNoEscrowSeat
* @property {() => Instance} getInstance
* @property {() => Object} getPublicFacet
* @property {() => IssuerKeywordRecord} getIssuers
* @property {() => BrandKeywordRecord} getBrands
* @property {() => Object} getTerms
* @property {() => boolean} acceptingOffers
* @property {(completion: Completion) => void} exitAllSeats
* @property {(reason: TerminationReason) => void} failAllSeats
* @property {() => void} stopAcceptingOffers
Expand Down
3 changes: 2 additions & 1 deletion packages/zoe/src/zoeService/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function makeZoe(vatAdminSvc, zcfBundleName = undefined) {
const hasExited = zoeSeatAdmin => !zoeSeatAdmins.has(zoeSeatAdmin);

/** @type {InstanceAdmin} */
return Far('instanceAdmin', {
const instanceAdmin = Far('instanceAdmin', {
getPublicFacet: () => publicFacetPromiseKit.promise,
getTerms,
getIssuers,
Expand Down Expand Up @@ -278,6 +278,7 @@ function makeZoe(vatAdminSvc, zcfBundleName = undefined) {
return { userSeat, notifier, zoeSeatAdmin };
},
});
return instanceAdmin;
};

const instanceAdmin = makeInstanceAdmin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ test('median aggregator', /** @param {ExecutionContext} t */ async t => {

// TODO: Port this to makeQuoteNotifier(amountIn, brandOut)
// @ts-ignore fix needed
const notifier = E(pa).getPriceNotifier(brandIn, brandOut);
const notifier = E(pa).makeQuoteNotifier(
amountMath.make(brandIn, 1n),
brandOut,
);
await E(aggregator.creatorFacet).initOracle(price1000.instance, {
increment: 10n,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/tools/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@

/**
* @typedef {Object} MutableQuote
* @property {() => void} cancel
* @property {() => void} updateLevel
* @property {(reason?: any) => void} cancel
* @property {(amountIn: Amount, amountOut: Amount) => void} updateLevel
* @property {() => ERef<PriceQuote>} getPromise
*/

Expand Down

0 comments on commit a4695c4

Please sign in to comment.