Skip to content

Commit

Permalink
feat: use multipoolAutoswap as the treasury priceAuthority
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Apr 2, 2021
1 parent 13c0e85 commit a37c795
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 55 deletions.
154 changes: 101 additions & 53 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ export function buildRootObject(vatPowers, vatParameters) {

// Now we can bootstrap the economy!
const treasuryCreator = await installEconomy();
const [centralIssuer, centralBrand] = await Promise.all(
['issuer', 'brand'].map(hub =>
const [centralIssuer, centralBrand, ammInstance] = await Promise.all([
...['issuer', 'brand'].map(hub =>
E(agoricNames).lookup(hub, CENTRAL_ISSUER_NAME),
),
);
E(agoricNames).lookup('instance', 'autoswap'),
]);

// [string, import('./issuers').IssuerInitializationRecord]
const CENTRAL_ISSUER_ENTRY = [
Expand Down Expand Up @@ -169,56 +170,6 @@ export function buildRootObject(vatPowers, vatParameters) {
}),
);

/**
* @param {ERef<Issuer>} issuerIn
* @param {ERef<Issuer>} issuerOut
* @param {Array<[number, number]>} tradeList
*/
const makeFakePriceAuthority = async (issuerIn, issuerOut, tradeList) => {
const [brandIn, brandOut] = await Promise.all([
E(issuerIn).getBrand(),
E(issuerOut).getBrand(),
]);
const pa = await E(vats.priceAuthority).makeFakePriceAuthority({
issuerIn,
issuerOut,
actualBrandIn: brandIn,
actualBrandOut: brandOut,
tradeList,
timer: chainTimerService,
quoteInterval: QUOTE_INTERVAL,
});
return E(priceAuthorityAdmin).registerPriceAuthority(
pa,
brandIn,
brandOut,
);
};
await Promise.all(
issuerNames.map(async issuerName => {
// Create priceAuthority pairs for centralIssuer based on the
// FakePriceAuthority.
console.debug(`Creating ${issuerName}-${CENTRAL_ISSUER_NAME}`);
const record = issuerNameToRecord.get(issuerName);
assert(record);
const { tradesGivenCentral, issuer } = record;
if (!tradesGivenCentral) {
return;
}
const tradesGivenOther =
centralIssuer !== issuer &&
tradesGivenCentral.map(([valueCentral, valueOther]) => [
valueOther,
valueCentral,
]);
await Promise.all([
makeFakePriceAuthority(centralIssuer, issuer, tradesGivenCentral),
tradesGivenOther &&
makeFakePriceAuthority(issuer, centralIssuer, tradesGivenOther),
]);
}),
);

async function addAllCollateral() {
const govBrand = await E(agoricNames).lookup(
'brand',
Expand Down Expand Up @@ -293,8 +244,105 @@ export function buildRootObject(vatPowers, vatParameters) {
}),
);
}
// await addAllCollateral();

/**
* @param {ERef<Issuer>} issuerIn
* @param {ERef<Issuer>} issuerOut
* @param {ERef<Brand>} brandIn
* @param {ERef<Brand>} brandOut
* @param {Array<[number, number]>} tradeList
*/
const makeFakePriceAuthority = (
issuerIn,
issuerOut,
brandIn,
brandOut,
tradeList,
) =>
E(vats.priceAuthority).makeFakePriceAuthority({
issuerIn,
issuerOut,
actualBrandIn: brandIn,
actualBrandOut: brandOut,
tradeList,
timer: chainTimerService,
quoteInterval: QUOTE_INTERVAL,
});

const ammPublicFacet = E(zoe).getPublicFacet(ammInstance);
await addAllCollateral();

await Promise.all(
issuerNames.map(async issuerName => {
// Create priceAuthority pairs for centralIssuer based on the
// AMM or FakePriceAuthority.
console.debug(`Creating ${issuerName}-${CENTRAL_ISSUER_NAME}`);
const record = issuerNameToRecord.get(issuerName);
assert(record);
const { tradesGivenCentral, issuer } = record;

const brand = await E(issuer).getBrand();
let { toCentral, fromCentral } = await E(ammPublicFacet)
.getPriceAuthorities(brand)
.catch(_e => {
// console.warn('could not get AMM priceAuthorities', _e);
return {};
});

if (!fromCentral && tradesGivenCentral) {
// We have no amm from-central price authority, make one from trades.
if (issuerName !== CENTRAL_ISSUER_NAME) {
console.log(
`Making fake price authority for ${CENTRAL_ISSUER_NAME}-${issuerName}`,
);
}
fromCentral = makeFakePriceAuthority(
centralIssuer,
issuer,
centralBrand,
brand,
tradesGivenCentral,
);
}

if (!toCentral && centralIssuer !== issuer && tradesGivenCentral) {
// We have no amm to-central price authority, make one from trades.
console.log(
`Making fake price authority for ${issuerName}-${CENTRAL_ISSUER_NAME}`,
);
const tradesGivenOther = tradesGivenCentral.map(
([valueCentral, valueOther]) => [valueOther, valueCentral],
);
toCentral = makeFakePriceAuthority(
issuer,
centralIssuer,
brand,
centralBrand,
tradesGivenOther,
);
}

// Register the price pairs.
await Promise.all(
[
[fromCentral, centralBrand, brand],
[toCentral, brand, centralBrand],
].map(async ([pa, fromBrand, toBrand]) => {
const paPresence = await pa;
if (!paPresence) {
return;
}
await E(priceAuthorityAdmin).registerPriceAuthority(
paPresence,
fromBrand,
toBrand,
);
}),
);
}),
);

return Far('chainBundler', {
async createUserBundle(_nickname, address, powerFlags = []) {
// Bind to some fresh ports (unspecified name) on the IBC implementation
Expand Down
6 changes: 5 additions & 1 deletion packages/treasury/src/stablecoinMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export async function start(zcf) {
/** @type {{ publicFacet: MultipoolAutoswapPublicFacet, instance: Instance}} */
const { publicFacet: autoswapAPI, instance: autoswapInstance } = await E(
zoe,
).startInstance(autoswapInstall, { Central: sconeIssuer });
).startInstance(
autoswapInstall,
{ Central: sconeIssuer },
{ timer: timerService },
);

// We process only one offer per collateralType. They must tell us the
// dollar value of their collateral, and we create that many Scones.
Expand Down
2 changes: 2 additions & 0 deletions packages/zoe/src/contracts/multipoolAutoswap/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export const makeAddPool = (
// getPriceGivenAvailableInput() throws.
const toCentralPriceAuthority = makePriceAuthority(
pool.getPriceGivenAvailableInput,
pool.getPriceGivenRequiredOutput,
secondaryBrand,
centralBrand,
timer,
Expand All @@ -271,6 +272,7 @@ export const makeAddPool = (
);
const fromCentralPriceAuthority = makePriceAuthority(
pool.getPriceGivenAvailableInput,
pool.getPriceGivenRequiredOutput,
centralBrand,
secondaryBrand,
timer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { makeOnewayPriceAuthorityKit } from '../../contractSupport';

export const makePriceAuthority = (
getOutputForGivenInput,
getInputForWantedOutput,
actualBrandIn,
actualBrandOut,
timer,
Expand All @@ -30,7 +31,7 @@ export const makePriceAuthority = (
const calcAmountOut = amountIn =>
getOutputForGivenInput(amountIn, actualBrandOut).amountOut;
const calcAmountIn = amountOut =>
getOutputForGivenInput(amountOut, actualBrandIn).amountIn;
getInputForWantedOutput(actualBrandIn, amountOut).amountIn;

/**
* @param {PriceQuery} priceQuery
Expand Down

0 comments on commit a37c795

Please sign in to comment.