Skip to content

Commit

Permalink
feat: integrate the economy's central token in chain bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 29, 2021
1 parent 90a28c9 commit 2288e60
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 46 deletions.
83 changes: 62 additions & 21 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from '@agoric/swingset-vat/src/vats/network';
import { E } from '@agoric/eventual-send';
import { Far } from '@agoric/marshal';
import { makeStore } from '@agoric/store';
import { installOnChain as installEconomyOnChain } from '@agoric/treasury/bundles/install-on-chain';

// this will return { undefined } until `ag-solo set-gci-ingress`
// has been run to update gci.js
Expand Down Expand Up @@ -66,25 +68,73 @@ export function buildRootObject(vatPowers, vatParameters) {
E(vats.priceAuthority).makePriceAuthority(),
]);

const {
nameHub: agoricNames,
nameAdmin: agoricNamesAdmin,
} = makeNameHubKit();
const {
nameHub: namesByAddress,
nameAdmin: namesByAddressAdmin,
} = makeNameHubKit();

async function installEconomy() {
// Create a mapping from all the nameHubs we create to their corresponding
// nameAdmin.
const nameAdmins = makeStore();
await Promise.all(
['brand', 'installation', 'issuer', 'instance', 'uiConfig'].map(
async nm => {
const { nameHub, nameAdmin } = makeNameHubKit();
await E(agoricNamesAdmin).update(nm, nameHub);
nameAdmins.init(nameHub, nameAdmin);
},
),
);

// Install the economy, giving it access to the name admins we made.
await installEconomyOnChain({
agoricNames,
board,
chainTimerService,
nameAdmins,
priceAuthority,
zoe,
});
}

// Now we can bootstrap the economy!
await installEconomy();
const centralIssuer = await E(agoricNames).lookup('issuer', 'Moe');

const CENTRAL_ISSUER_ENTRY = [
CENTRAL_ISSUER_NAME,
{
issuer: centralIssuer,
mintValue: 0,
pursePetname: 'Local currency',
fakeTradesGivenCentral: [[1, 1]],
},
];

const issuerNameToRecord = noFakeCurrencies
? new Map()
: fakeIssuerNameToRecord;
issuerNameToRecord.set(...CENTRAL_ISSUER_ENTRY);

const issuerNames = [...issuerNameToRecord.keys()];
const centralIssuerIndex = issuerNames.findIndex(
issuerName => issuerName === CENTRAL_ISSUER_NAME,
);
// Make the other demo mints
const issuers = await Promise.all(
issuerNames.map(issuerName =>
E(vats.mints).makeMintAndIssuer(
issuerNames.map(issuerName => {
const record = issuerNameToRecord.get(issuerName);
if (record.issuer !== undefined) {
return record.issuer;
}
return E(vats.mints).makeMintAndIssuer(
issuerName,
...(issuerNameToRecord.get(issuerName).issuerArgs || []),
),
),
...(record.issuerArgs || []),
);
}),
);

const centralIssuer = issuers[centralIssuerIndex];

/**
* @param {ERef<Issuer>} issuerIn
* @param {ERef<Issuer>} issuerOut
Expand Down Expand Up @@ -112,7 +162,7 @@ export function buildRootObject(vatPowers, vatParameters) {
};
await Promise.all(
issuers.map(async (issuer, i) => {
// Create priceAuthority pairs for centralIssuerIndex based on the
// Create priceAuthority pairs for centralIssuer based on the
// FakePriceAuthority.
console.debug(`Creating ${issuerNames[i]}-${CENTRAL_ISSUER_NAME}`);
const { fakeTradesGivenCentral } = issuerNameToRecord.get(
Expand All @@ -135,15 +185,6 @@ export function buildRootObject(vatPowers, vatParameters) {
}),
);

const {
nameHub: agoricNames,
nameAdmin: agoricNamesAdmin,
} = makeNameHubKit();
const {
nameHub: namesByAddress,
nameAdmin: namesByAddressAdmin,
} = makeNameHubKit();

return Far('chainBundler', {
async createUserBundle(_nickname, address, powerFlags = []) {
// Bind to some fresh ports (unspecified name) on the IBC implementation
Expand Down
28 changes: 3 additions & 25 deletions packages/cosmic-swingset/lib/ag-solo/vats/issuers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

export const CENTRAL_ISSUER_NAME = 'Testnet.$USD';
export const CENTRAL_ISSUER_NAME = 'MOE';

/**
* @typedef {Object} IssuerInitializationRecord
* @property {Issuer} [issuer]
* @property {Array<any>} [issuerArgs]
* @property {string} pursePetname
* @property {number} mintValue
Expand All @@ -14,16 +15,7 @@ export const CENTRAL_ISSUER_NAME = 'Testnet.$USD';
export const fakeIssuerNameToRecord = new Map(
harden([
[
CENTRAL_ISSUER_NAME,
{
issuerArgs: [undefined, { decimalPlaces: 3 }],
mintValue: 20000,
pursePetname: 'Local currency',
fakeTradesGivenCentral: [[1, 1]],
},
],
[
'Testnet.$LINK',
'$LINK',
{
issuerArgs: [undefined, { decimalPlaces: 6 }],
mintValue: 7 * 10 ** 6,
Expand All @@ -49,20 +41,6 @@ export const fakeIssuerNameToRecord = new Map(
],
},
],
[
'MOE',
{
mintValue: 0,
pursePetname: 'MOE funds',
fakeTradesGivenCentral: [
[10, 15],
[13, 9],
[12, 13],
[18, 15],
[15, 17],
],
},
],
[
'simolean',
{
Expand Down
3 changes: 3 additions & 0 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-mints.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export function buildRootObject(_vatPowers) {
return issuer;
},
mintInitialPayment: (issuerName, value) => {
if (!mintsAndMath.has(issuerName)) {
return undefined;
}
const { mint, amountMath } = mintsAndMath.get(issuerName);
const amount = amountMath.make(value);
return mint.mintPayment(amount);
Expand Down
1 change: 1 addition & 0 deletions packages/cosmic-swingset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@agoric/swing-store-simple": "^0.3.4",
"@agoric/swingset-vat": "^0.14.0",
"@agoric/transform-eventual-send": "^1.4.4",
"@agoric/treasury": "^0.1.1",
"@agoric/zoe": "^0.13.1",
"@babel/generator": "^7.6.4",
"@iarna/toml": "^2.2.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/dapp-svelte-wallet/api/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export default async function deployWallet(
),
);
}
if (!payment) {
return;
}
pursePetname = await issuerToPursePetnameP.get(issuer);

// Deposit payment.
Expand Down

0 comments on commit 2288e60

Please sign in to comment.