Skip to content

Commit

Permalink
feat: add more collateral types, pivot to BLD/RUN and decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Apr 1, 2021
1 parent ac9049c commit 7cbce9f
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 76 deletions.
76 changes: 51 additions & 25 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import { amountMath } from '@agoric/ertp';
import { GCI } from './gci';
import { makeBridgeManager } from './bridge';
import { makeNameHubKit } from './nameHub';
import { CENTRAL_ISSUER_NAME, fakeIssuerEntries } from './issuers';
import {
CENTRAL_ISSUER_NAME,
fakeIssuerEntries,
fromCosmosIssuerEntries,
fromPegasusIssuerEntries,
} from './issuers';

const NUM_IBC_PORTS = 3;
const QUOTE_INTERVAL = 30;

const PERCENT_DENOM = 100n;
const BASIS_POINTS_DENOM = 10000n;

console.debug(`loading bootstrap.js`);
Expand Down Expand Up @@ -115,28 +119,34 @@ 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 => E(agoricNames).lookup(hub, 'MOE')),
['issuer', 'brand'].map(hub => E(agoricNames).lookup(hub, 'RUN')),
);

// [string, import('./issuers').IssuerInitializationRecord]
const CENTRAL_ISSUER_ENTRY = [
CENTRAL_ISSUER_NAME,
{
issuer: centralIssuer,
mintValue: 0,
pursePetname: 'Local currency',
fakeTradesGivenCentral: [[1, 1]],
defaultPurses: [['Agoric local currency', 0]],
tradesGivenCentral: [[1, 1]],
},
];

/** @type {Store<string, import('./issuers').IssuerInitializationRecord>} */
const issuerNameToRecord = makeStore();
/** @type {Array<[string, import('./issuers').IssuerInitializationRecord]>} */
const issuerEntries = [
CENTRAL_ISSUER_ENTRY,
...fromCosmosIssuerEntries,
...fromPegasusIssuerEntries,
];
if (!noFakeCurrencies) {
fakeIssuerEntries.map(entry => issuerNameToRecord.init(...entry));
issuerEntries.push(...fakeIssuerEntries);
}
issuerNameToRecord.init(...CENTRAL_ISSUER_ENTRY);
issuerEntries.forEach(entry => issuerNameToRecord.init(...entry));

const issuerNames = [...issuerNameToRecord.keys()];
const issuers = await Promise.all(
await Promise.all(
issuerNames.map(async issuerName => {
const record = issuerNameToRecord.get(issuerName);
if (record.issuer !== undefined) {
Expand Down Expand Up @@ -188,20 +198,20 @@ export function buildRootObject(vatPowers, vatParameters) {
console.debug(`Creating ${issuerName}-${CENTRAL_ISSUER_NAME}`);
const record = issuerNameToRecord.get(issuerName);
assert(record);
const { fakeTradesGivenCentral, issuer } = record;
if (!fakeTradesGivenCentral) {
const { tradesGivenCentral, issuer } = record;
if (!tradesGivenCentral) {
return;
}
const fakeTradesGivenOther =
const tradesGivenOther =
centralIssuer !== issuer &&
fakeTradesGivenCentral.map(([valueCentral, valueOther]) => [
tradesGivenCentral.map(([valueCentral, valueOther]) => [
valueOther,
valueCentral,
]);
await Promise.all([
makeFakePriceAuthority(centralIssuer, issuer, fakeTradesGivenCentral),
fakeTradesGivenOther &&
makeFakePriceAuthority(issuer, centralIssuer, fakeTradesGivenOther),
makeFakePriceAuthority(centralIssuer, issuer, tradesGivenCentral),
tradesGivenOther &&
makeFakePriceAuthority(issuer, centralIssuer, tradesGivenOther),
]);
}),
);
Expand All @@ -218,11 +228,14 @@ export function buildRootObject(vatPowers, vatParameters) {
if (!config) {
return undefined;
}
assert(record.tradesGivenCentral);
const initialPrice = record.tradesGivenCentral[0];
assert(initialPrice);
const rates = {
initialPrice: makeRatio(
config.initialPricePercent,
initialPrice[0],
centralBrand,
PERCENT_DENOM,
initialPrice[1],
record.brand,
),
initialMargin: makeRatio(config.initialMarginPercent, centralBrand),
Expand Down Expand Up @@ -304,18 +317,30 @@ export function buildRootObject(vatPowers, vatParameters) {
additionalPowers.treasuryCreator = treasuryCreator;
}

const mintNames = [];
const mintPurses = [];
const mintValues = [];
issuerNames.forEach(issuerName => {
const record = issuerNameToRecord.get(issuerName);
if (!record.defaultPurses) {
return;
}
record.defaultPurses.forEach(([purseName, value]) => {
mintNames.push(issuerName);
mintPurses.push(purseName);
mintValues.push(value);
});
});
const payments = await E(vats.mints).mintInitialPayments(
issuerNames,
issuerNames.map(
issuerName => issuerNameToRecord.get(issuerName).mintValue,
),
mintNames,
mintValues,
);

const paymentInfo = issuerNames.map((issuerName, i) => ({
issuer: issuers[i],
const paymentInfo = mintNames.map((issuerName, i) => ({
issuer: issuerNameToRecord.get(issuerName).issuer,
issuerPetname: issuerName,
payment: payments[i],
pursePetname: issuerNameToRecord.get(issuerName).pursePetname,
pursePetname: mintPurses[i],
}));

const faucet = Far('faucet', {
Expand Down Expand Up @@ -600,6 +625,7 @@ export function buildRootObject(vatPowers, vatParameters) {
return chainBundler.createUserBundle(nickname, 'demo', [
'agoric.agoricNamesAdmin',
'agoric.priceAuthorityAdmin',
'agoric.treasuryCreator',
'agoric.vattp',
]);
}
Expand Down
171 changes: 127 additions & 44 deletions packages/cosmic-swingset/lib/ag-solo/vats/issuers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @ts-check

export const CENTRAL_ISSUER_NAME = 'MOE';
export const CENTRAL_ISSUER_NAME = 'RUN';

/** @typedef {number | bigint} Bigish */

/**
* @typedef {Object} CollateralConfig
* @property {string} keyword
* @property {Bigish} collateralValue
* @property {bigint} initialPricePercent
* @property {bigint} initialMarginPercent
* @property {bigint} liquidationMarginPercent
* @property {bigint} interestRateBasis
Expand All @@ -21,76 +20,160 @@ export const CENTRAL_ISSUER_NAME = 'MOE';
* @property {Brand} [brand]
* @property {Array<any>} [issuerArgs]
* @property {CollateralConfig} [collateralConfig]
* @property {string} pursePetname
* @property {Bigish} mintValue
* @property {Array<[Bigish, Bigish]>} [fakeTradesGivenCentral]
* @property {Array<[string, Bigish]>} [defaultPurses]
* @property {Array<[Bigish, Bigish]>} [tradesGivenCentral]
*/

export const makeScaler = toDecimals => (n, fromDecimals = 0) => {
if (typeof n === 'bigint') {
return n * 10n ** BigInt(toDecimals);
}
return (
BigInt(Math.floor(n * 10 ** fromDecimals)) *
10n ** BigInt(toDecimals - fromDecimals)
);
};
export const scaleMills = makeScaler(4);
export const scaleMicro = makeScaler(6);
export const scaleEth = makeScaler(18);
export const scaleCentral = scaleMicro;

/** @type {Array<[string, IssuerInitializationRecord]>} */
const fakeIssuerEntries = [
const fromCosmosIssuerEntries = [
[
'BLD',
{
issuerArgs: [undefined, { decimalPlaces: 6 }],
defaultPurses: [['Agoric staking token', scaleMicro(73)]],
collateralConfig: {
keyword: 'BLD',
collateralValue: scaleMicro(1000000n),
initialMarginPercent: 150n,
liquidationMarginPercent: 125n,
interestRateBasis: 250n,
loanFeeBasis: 50n,
},
tradesGivenCentral: [
[scaleCentral(27.9, 1), scaleMicro(1)],
[scaleCentral(25.7, 1), scaleMicro(1)],
[scaleCentral(26.8, 1), scaleMicro(1)],
],
},
],
];

harden(fromCosmosIssuerEntries);
export { fromCosmosIssuerEntries };

/** @type {Array<[string, IssuerInitializationRecord]>} */
const fromPegasusIssuerEntries = [
[
'ATOM',
{
issuerArgs: [undefined, { decimalPlaces: 6 }],
defaultPurses: [['Cosmos Staking', scaleMicro(68)]],
collateralConfig: {
keyword: 'ATOM',
collateralValue: scaleMicro(1000000n),
initialMarginPercent: 150n,
liquidationMarginPercent: 125n,
interestRateBasis: 250n,
loanFeeBasis: 50n,
},
tradesGivenCentral: [
[scaleCentral(18.61, 2), scaleMicro(1)],
[scaleCentral(19.97, 2), scaleMicro(1)],
[scaleCentral(19.17, 2), scaleMicro(1)],
],
},
],
[
'ETH',
{
issuerArgs: [undefined, { decimalPlaces: 18 }],
collateralConfig: {
keyword: 'ETH',
collateralValue: scaleEth(1000000n),
initialMarginPercent: 150n,
liquidationMarginPercent: 125n,
interestRateBasis: 250n,
loanFeeBasis: 50n,
},
tradesGivenCentral: [
[scaleCentral(1914.86, 2), scaleEth(1)],
[scaleCentral(1489.87, 2), scaleEth(1)],
[scaleCentral(1924.4, 2), scaleEth(1)],
],
},
],
// FIXME: Either of these entries (LINK or USDC) screw up the bootstrap process.
// I'm not sure why.
/*
[
'LINK',
{
issuerArgs: [undefined, { decimalPlaces: 18 }],
mintValue: 51n * 10n ** 18n,
defaultPurses: [['Oracle fee', scaleEth(51n)]],
collateralConfig: {
keyword: 'LINK',
collateralValue: 1000000n * 10n ** 18n,
initialPricePercent: 125n,
collateralValue: scaleEth(1000000n),
initialMarginPercent: 150n,
liquidationMarginPercent: 125n,
interestRateBasis: 250n,
loanFeeBasis: 50n,
},
pursePetname: 'Oracle fee',
fakeTradesGivenCentral: [
[279000n, 10n ** 18n],
[257000n, 10n ** 18n],
[268000n, 10n ** 18n],
tradesGivenCentral: [
[scaleCentral(27.9, 2), scaleEth(1)],
[scaleCentral(25.7, 2), scaleEth(1)],
[scaleCentral(26.8, 2), scaleEth(1)],
],
},
],
[
'moola',
'USDC',
{
mintValue: 1900,
issuerArgs: [undefined, { decimalPlaces: 18 }],
defaultPurses: [['USD Coin', scaleEth(1323n)]],
collateralConfig: {
collateralValue: 7400000,
keyword: 'Moola',
initialPricePercent: 150n,
keyword: 'USDC',
collateralValue: scaleEth(1000000n),
initialMarginPercent: 150n,
liquidationMarginPercent: 120n,
interestRateBasis: 200n,
loanFeeBasis: 150n,
liquidationMarginPercent: 125n,
interestRateBasis: 250n,
loanFeeBasis: 50n,
},
pursePetname: 'Fun budget',
fakeTradesGivenCentral: [
[10000, 1],
[13000, 1],
[12000, 1],
[18000, 1],
[15000, 1],
tradesGivenCentral: [[scaleCentral(1), scaleEth(1)]],
},
],
*/
];

harden(fromPegasusIssuerEntries);
export { fromPegasusIssuerEntries };

/** @type {Array<[string, IssuerInitializationRecord]>} */
const fakeIssuerEntries = [
[
'moola',
{
defaultPurses: [['Fun budget', 1900]],
tradesGivenCentral: [
[scaleCentral(1), 1],
[scaleCentral(1.3, 1), 1],
[scaleCentral(1.2, 1), 1],
[scaleCentral(1.8, 1), 1],
[scaleCentral(1.5, 1), 1],
],
},
],
[
'simolean',
{
mintValue: 970,
collateralConfig: {
collateralValue: 968000,
keyword: 'Simolean',
initialPricePercent: 110n,
initialMarginPercent: 120n,
liquidationMarginPercent: 105n,
interestRateBasis: 100n,
loanFeeBasis: 225n,
},
pursePetname: 'Nest egg',
fakeTradesGivenCentral: [
[213500, 1],
[217200, 1],
[212400, 1],
defaultPurses: [['Nest egg', 970]],
tradesGivenCentral: [
[scaleCentral(21.35, 2), 1],
[scaleCentral(21.72, 2), 1],
[scaleCentral(21.24, 2), 1],
],
},
],
Expand Down
Loading

0 comments on commit 7cbce9f

Please sign in to comment.