Skip to content

Commit

Permalink
fix: improve factoring and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Apr 1, 2021
1 parent 42642ec commit e7b356d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
21 changes: 12 additions & 9 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export function buildRootObject(vatPowers, vatParameters) {
return installEconomyOnChain({
agoricNames,
board,
centralName: CENTRAL_ISSUER_NAME,
chainTimerService,
nameAdmins,
priceAuthority,
Expand All @@ -119,15 +120,17 @@ 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, 'RUN')),
['issuer', 'brand'].map(hub =>
E(agoricNames).lookup(hub, CENTRAL_ISSUER_NAME),
),
);

// [string, import('./issuers').IssuerInitializationRecord]
const CENTRAL_ISSUER_ENTRY = [
CENTRAL_ISSUER_NAME,
{
issuer: centralIssuer,
defaultPurses: [['Agoric local currency', 0]],
defaultPurses: [['Agoric RUN currency', 0]],
tradesGivenCentral: [[1, 1]],
},
];
Expand Down Expand Up @@ -317,30 +320,30 @@ export function buildRootObject(vatPowers, vatParameters) {
additionalPowers.treasuryCreator = treasuryCreator;
}

const mintNames = [];
const mintPurses = [];
const mintIssuerNames = [];
const mintPurseNames = [];
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);
mintIssuerNames.push(issuerName);
mintPurseNames.push(purseName);
mintValues.push(value);
});
});
const payments = await E(vats.mints).mintInitialPayments(
mintNames,
mintIssuerNames,
mintValues,
);

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

const faucet = Far('faucet', {
Expand Down
54 changes: 40 additions & 14 deletions packages/cosmic-swingset/lib/ag-solo/vats/issuers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-check
import { assert } from '@agoric/assert';
import { Nat } from '@agoric/nat';

export const CENTRAL_ISSUER_NAME = 'RUN';

Expand All @@ -7,7 +9,8 @@ export const CENTRAL_ISSUER_NAME = 'RUN';
/**
* @typedef {Object} CollateralConfig
* @property {string} keyword
* @property {Bigish} collateralValue
* @property {Bigish} collateralValue the initial price of this collateral is
* provided by tradesGivenCentral[0]
* @property {bigint} initialMarginPercent
* @property {bigint} liquidationMarginPercent
* @property {bigint} interestRateBasis
Expand All @@ -24,14 +27,37 @@ export const CENTRAL_ISSUER_NAME = 'RUN';
* @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)
);
/**
* @callback Scaler Scale a number from a (potentially fractional) input to a
* fixed-precision bigint
* @param {Bigish} n the input number to scale
* @param {number} [fromDecimalPlaces=0] number of decimal places to keep from the input
* @returns {bigint} the scaled integer
*/

/**
* Create a decimal scaler.
*
* @param {number} toDecimalPlaces number of decimal places in the scaled value
* @returns {Scaler}
*/
export const makeScaler = toDecimalPlaces => {
assert.typeof(toDecimalPlaces, 'number');
Nat(toDecimalPlaces);
return (n, fromDecimalPlaces = 0) => {
assert.typeof(fromDecimalPlaces, 'number');
Nat(fromDecimalPlaces);
if (typeof n === 'bigint') {
// Bigints never preserve decimal places.
return Nat(n) * 10n ** Nat(toDecimalPlaces);
}
// Fractional scaling needs a number, not a bigint.
assert.typeof(n, 'number');
return (
Nat(Math.floor(n * 10 ** fromDecimalPlaces)) *
10n ** Nat(toDecimalPlaces - fromDecimalPlaces)
);
};
};
export const scaleMills = makeScaler(4);
export const scaleMicro = makeScaler(6);
Expand Down Expand Up @@ -140,7 +166,7 @@ Nested error under Error#1
at Function.applyMethod (packages/tame-metering/src/tame.js:184:20)
at meteredConstructor.deliver (packages/SwingSet/src/kernel/liveSlots.js:522:28)
at eval (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
at eval* (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
2021-04-01T19:23:41.028Z SwingSet: ls: v11: Logging sent error stack (Error#3)
Error#3: already have remote (a string)
Expand All @@ -149,7 +175,7 @@ Error#3: already have remote (a string)
at meteredConstructor.unserialize (packages/marshal/src/marshal.js:953:19)
at notifyOnePromise (packages/SwingSet/src/kernel/liveSlots.js:594:19)
at meteredConstructor.notify (packages/SwingSet/src/kernel/liveSlots.js:607:7)
at eval (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
at eval* (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
Error#3 ERROR_NOTE: Received as error:liveSlots:v14#1
Error#3 ERROR_NOTE: Rejection from: (Error#4) : 1404 . 0
Expand All @@ -159,8 +185,8 @@ Nested 2 errors under Error#3
Error#4: Event: 1403.1
at Function.applyMethod (packages/tame-metering/src/tame.js:184:20)
at Proxy.eval (packages/eventual-send/src/E.js:37:49)
at eval (packages/cosmic-swingset/t3/vats/bootstrap.js:639:15)
at Proxy.eval* (packages/eventual-send/src/E.js:37:49)
at eval* (packages/cosmic-swingset/t3/vats/bootstrap.js:639:15)
at Array.map (<anonymous>)
at Array.map (packages/tame-metering/src/tame.js:184:20)
at Alleged: root.bootstrap (packages/cosmic-swingset/t3/vats/bootstrap.js:636:38)
Expand All @@ -169,7 +195,7 @@ Nested 2 errors under Error#3
at Function.applyMethod (packages/tame-metering/src/tame.js:184:20)
at meteredConstructor.deliver (packages/SwingSet/src/kernel/liveSlots.js:522:28)
at eval (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
at eval* (packages/SwingSet/src/kernel/vatManager/deliver.js:51:48)
2021-04-01T19:23:41.029Z SwingSet: kernel: ##### KERNEL PANIC: kp40.policy panic: rejected {"body":"{\"@qclass\":\"error\",\"errorId\":\"error:liveSlots:v11#1\",\"message\":\"already have remote (a string)\",\"name\":\"Error\"}","slots":[]} #####
2021-04-01T19:23:41.030Z fake-chain: error fake processing (Error#6)
Expand Down
7 changes: 4 additions & 3 deletions packages/treasury/bundles/install-on-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
* @param {Object} param0
* @param {ERef<NameHub>} param0.agoricNames
* @param {ERef<Board>} param0.board
* @param {string} param0.centralName
* @param {ERef<TimerService>} param0.chainTimerService
* @param {Store<NameHub, NameAdmin>} param0.nameAdmins
* @param {ERef<PriceAuthority>} param0.priceAuthority
* @param {ERef<ZoeService>} param0.zoe
*/
export async function installOnChain({ agoricNames, board, chainTimerService, nameAdmins, priceAuthority, zoe }) {
export async function installOnChain({ agoricNames, board, centralName, chainTimerService, nameAdmins, priceAuthority, zoe }) {
// Fetch the nameAdmins we need.
const [brandAdmin, installAdmin, instanceAdmin, issuerAdmin, uiConfigAdmin] = await Promise.all(
['brand', 'installation', 'instance', 'issuer', 'uiConfig'].map(async edge => {
Expand Down Expand Up @@ -100,8 +101,8 @@ export async function installOnChain({ agoricNames, board, chainTimerService, na
[instanceAdmin, treasuryUiDefaults.AMM_NAME, ammInstance],
[brandAdmin, 'TreasuryGovernance', govBrand],
[issuerAdmin, 'TreasuryGovernance', govIssuer],
[brandAdmin, 'RUN', centralBrand],
[issuerAdmin, 'RUN', centralIssuer],
[brandAdmin, centralName, centralBrand],
[issuerAdmin, centralName, centralIssuer],
];
await Promise.all(
nameAdminUpdates.map(([nameAdmin, name, value]) => E(nameAdmin).update(name, value)),
Expand Down

0 comments on commit e7b356d

Please sign in to comment.