Skip to content

Commit

Permalink
fixup! feat: repair vaultUpgrade proposal; register scaledPriceAuthority
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Jul 31, 2024
1 parent 0aa860e commit 1eab8fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 111 deletions.
22 changes: 20 additions & 2 deletions packages/inter-protocol/src/proposals/addAssetToVault.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ export const publishInterchainAssetFromBank = async (
* @param {object} config.options
* @param {InterchainAssetOptions} config.options.interchainAssetOptions
*/
export const registerScaledPriceAuthority = async (
export const startScaledPriceAuthority = async (
{
consume: {
agoricNamesAdmin,
startUpgradable,
priceAuthorityAdmin,
priceAuthority,
},
instance: { produce: produceInstance },
},
{ options: { interchainAssetOptions } },
) => {
Expand Down Expand Up @@ -230,6 +229,25 @@ export const registerScaledPriceAuthority = async (
true, // force
);

return spaKit;
};

/**
* @param {BootstrapPowers} powers
* @param {object} config
* @param {object} config.options
*/
export const registerScaledPriceAuthority = async (powers, { options }) => {
const {
instance: { produce: produceInstance },
} = powers;

const { keyword, issuerName = keyword } = options.interchainAssetOptions;

const spaKit = await startScaledPriceAuthority(powers, { options });

const label = scaledPriceFeedName(issuerName);

// publish into agoricNames so that others can await its presence.
// This must stay after registerPriceAuthority above so it's evidence of registration.
// eslint-disable-next-line no-restricted-syntax -- computed property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,129 +1,29 @@
import { makeTracer, deeplyFulfilledObject } from '@agoric/internal';
import { makeTracer } from '@agoric/internal';
import { E } from '@endo/far';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { parseRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { startScaledPriceAuthority } from './addAssetToVault.js';

import { reserveThenGetNames, scaledPriceFeedName } from './utils.js';
import { scaledPriceFeedName } from './utils.js';

const trace = makeTracer('replaceScaledPA', true);

/**
* Copied with minor modification from addAssetToVault.js because a previous
* scaledPriceAuthority is being replaced.
*
* @param {BootstrapPowers} powers
* @param {object} config
* @param {object} config.options
* @param {import('./addAssetToVault.js').InterchainAssetOptions} config.options.interchainAssetOptions
*/
export const replaceScaledPriceAuthority = async (
{
consume: {
agoricNamesAdmin,
startUpgradable,
priceAuthorityAdmin,
priceAuthority,
},
instance: { produce: produceInstance },
},
{ options: { interchainAssetOptions } },
) => {
export const replaceScaledPriceAuthority = async (powers, { options }) => {
const {
keyword,
issuerName = keyword,
oracleBrand = issuerName,
initialPrice: initialPriceRaw,
} = interchainAssetOptions;
assert.typeof(issuerName, 'string');
assert.typeof(oracleBrand, 'string');

const [
sourcePriceAuthority,
[interchainBrand, stableBrand],
[interchainOracleBrand, usdBrand],
[scaledPriceAuthority],
] = await Promise.all([
priceAuthority,
reserveThenGetNames(E(agoricNamesAdmin).lookupAdmin('brand'), [
issuerName,
'IST',
]),
reserveThenGetNames(E(agoricNamesAdmin).lookupAdmin('oracleBrand'), [
oracleBrand,
'USD',
]),
reserveThenGetNames(E(agoricNamesAdmin).lookupAdmin('installation'), [
'scaledPriceAuthority',
]),
]);

// We need "unit amounts" of each brand in order to get the ratios right. You
// can ignore decimalPlaces when adding and subtracting a brand with itself,
// but not when creating ratios.
const getDecimalP = async brand => {
const displayInfo = E(brand).getDisplayInfo();
return E.get(displayInfo).decimalPlaces;
};
const [
decimalPlacesInterchainOracle = 0,
decimalPlacesInterchain = 0,
decimalPlacesUsd = 0,
decimalPlacesRun = 0,
] = await Promise.all([
getDecimalP(interchainOracleBrand),
getDecimalP(interchainBrand),
getDecimalP(usdBrand),
getDecimalP(stableBrand),
]);

const scaleIn = makeRatio(
10n ** BigInt(decimalPlacesInterchainOracle),
interchainOracleBrand,
10n ** BigInt(decimalPlacesInterchain),
interchainBrand,
);
const scaleOut = makeRatio(
10n ** BigInt(decimalPlacesUsd),
usdBrand,
10n ** BigInt(decimalPlacesRun),
stableBrand,
);
const initialPrice = initialPriceRaw
? parseRatio(initialPriceRaw, stableBrand, interchainBrand)
: undefined;
instance: { produce: produceInstance },
} = powers;
const { keyword, issuerName = keyword } = options.interchainAssetOptions;

const terms = await deeplyFulfilledObject(
harden({
sourcePriceAuthority,
scaleIn,
scaleOut,
initialPrice,
}),
);
const spaKit = await startScaledPriceAuthority(powers, { options });

const label = scaledPriceFeedName(issuerName);

const spaKit = await E(startUpgradable)({
installation: scaledPriceAuthority,
label,
terms,
});

// @ts-expect-error The public facet should have getPriceAuthority
const pa = await E(spaKit.publicFacet).getPriceAuthority();
trace(pa);

await E(priceAuthorityAdmin).registerPriceAuthority(
pa,
interchainBrand,
stableBrand,
true, // force
);

produceInstance[label].reset();

// publish into agoricNames so that others can await its presence.
// This must stay after registerPriceAuthority above so it's evidence of registration.

produceInstance[label].resolve(spaKit.instance);
};

Expand Down

0 comments on commit 1eab8fc

Please sign in to comment.