Skip to content

Commit

Permalink
fix: allow priceRegistry to force-override an entry
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 6, 2020
1 parent af76585 commit dceafd6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
66 changes: 41 additions & 25 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,45 @@ export function buildRootObject(vatPowers, vatParameters) {
issuerArgs: [undefined, { decimalPlaces: 6 }],
mintValue: 7 * 10 ** 6,
pursePetname: 'Oracle fee',
tradesGivenCentral: [[1000, 3000000], [1000, 2500000], [1000, 2750000]],
tradesGivenCentral: [
[1000, 3000000],
[1000, 2500000],
[1000, 2750000],
],
},
],
[
'moola',
{
mintValue: 1900,
pursePetname: 'Fun budget',
tradesGivenCentral: [[10, 1], [13, 1], [12, 1], [18, 1], [15, 1]],
tradesGivenCentral: [
[10, 1],
[13, 1],
[12, 1],
[18, 1],
[15, 1],
],
},
],
[
'simolean',
{
mintValue: 1900,
pursePetname: 'Nest egg',
tradesGivenCentral: [[2135, 50], [2172, 50], [2124, 50]],
tradesGivenCentral: [
[2135, 50],
[2172, 50],
[2124, 50],
],
},
],
]),
);
const issuerNames = [...issuerNameToRecord.keys()];
const centralIssuerIndex = issuerNames.findIndex(issuerName => issuerName === CENTRAL_ISSUER_NAME);
const centralIssuerIndex = issuerNames.findIndex(
issuerName => issuerName === CENTRAL_ISSUER_NAME,
);
if (centralIssuerIndex < 0) {
throw Error(`Cannot find issuer ${CENTRAL_ISSUER_NAME}`);
}
Expand All @@ -120,10 +136,10 @@ export function buildRootObject(vatPowers, vatParameters) {
);

const centralIssuer = issuers[centralIssuerIndex];

/**
* @param {ERef<Issuer>} issuerIn
* @param {ERef<Issuer>} issuerOut
* @param {ERef<Issuer>} issuerIn
* @param {ERef<Issuer>} issuerOut
* @param {Array<[number, number]>} tradeList
*/
const makeFakePriceAuthority = async (issuerIn, issuerOut, tradeList) => {
Expand All @@ -138,25 +154,25 @@ export function buildRootObject(vatPowers, vatParameters) {
quoteInterval: QUOTE_INTERVAL,
}),
]);
return E(priceAuthorityAdmin).registerPriceAuthority(pa, brandIn, brandOut);
return E(priceAuthorityAdmin).registerPriceAuthority(
pa,
brandIn,
brandOut,
);
};
await Promise.all(issuers.map(async (issuer, i) => {
// Create priceAuthority pairs for centralIssuerIndex based on the
// FakePriceAuthority.
if (issuer === centralIssuer) {
return;
}
console.error(`Creating ${issuerNames[i]}-${issuerNames[centralIssuerIndex]}`);
const { tradesGivenCentral } = issuerNameToRecord.get(issuerNames[i]);
const tradesGivenThis = tradesGivenCentral.map(([a, b]) => [b, a]);
return Promise.all([
makeFakePriceAuthority(centralIssuer, issuer, tradesGivenCentral),
makeFakePriceAuthority(issuer, centralIssuer, tradesGivenThis),
]);
}));

for (const issuerpetname of issuerNameToRecord)

await Promise.all(
issuers.map(async (issuer, i) => {
// Create priceAuthority pairs for centralIssuerIndex based on the
// FakePriceAuthority.
console.error(`Creating ${issuerNames[i]}-${CENTRAL_ISSUER_NAME}`);
const { tradesGivenCentral } = issuerNameToRecord.get(issuerNames[i]);
const tradesGivenThis = tradesGivenCentral.map(([a, b]) => [b, a]);
return Promise.all([
makeFakePriceAuthority(centralIssuer, issuer, tradesGivenCentral),
makeFakePriceAuthority(issuer, centralIssuer, tradesGivenThis),
]);
}),
);

return harden({
async createUserBundle(_nickname, powerFlags = []) {
Expand Down
10 changes: 7 additions & 3 deletions packages/zoe/tools/priceAuthorityRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import '../exported';

/**
* @typedef {Object} PriceAuthorityRegistryAdmin
* @property {(pa: ERef<PriceAuthority>, brandIn: Brand, brandOut: Brand)
* @property {(pa: ERef<PriceAuthority>, brandIn: Brand, brandOut: Brand, force?: boolean)
* => Deleter} registerPriceAuthority Add a unique price authority for a given
* pair
*/
Expand Down Expand Up @@ -109,7 +109,7 @@ export const makePriceAuthorityRegistry = () => {

/** @type {PriceAuthorityRegistryAdmin} */
const adminFacet = {
registerPriceAuthority(pa, brandIn, brandOut) {
registerPriceAuthority(pa, brandIn, brandOut, force = false) {
/** @type {Store<Brand, PriceAuthorityRecord>} */
let priceStore;
if (assetToPriceStore.has(brandIn)) {
Expand All @@ -126,7 +126,11 @@ export const makePriceAuthorityRegistry = () => {
};

// Set up the record.
priceStore.init(brandOut, harden(record));
if (force && priceStore.has(brandOut)) {
priceStore.set(brandOut, harden(record));
} else {
priceStore.init(brandOut, harden(record));
}

return harden({
delete() {
Expand Down

0 comments on commit dceafd6

Please sign in to comment.