Skip to content

Commit

Permalink
chore: map old brand board IDs to newly created brands
Browse files Browse the repository at this point in the history
 - compute total fees
  • Loading branch information
dckc committed Apr 21, 2023
1 parent 381c743 commit 1ff8891
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 39 deletions.
72 changes: 47 additions & 25 deletions packages/inter-protocol/src/proposals/startPSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,55 +548,77 @@ export const restorePSM = async powers => {
vatPowers: {
argv: { vstorage: vsEntries, anchorAssets },
},
consume: { zoe },
} = powers;

const mkThing = entries => {
const board = boardSlottingMarshaller();
const mkThing = (entries, slotToVal = undefined) => {
const board = boardSlottingMarshaller(slotToVal);
const vsMap = new Map(entries);

const getMapItem = key => {
const raw = vsMap.get(key) || Fail`no ${key}`;
const capData = decodeToCapData(raw);
return new Map(board.fromCapData(capData));
return new Map(harden(board.fromCapData(capData)));
};
const getRecordItem = key => {
const raw = vsMap.get(key) || Fail`no ${key}`;
const capData = decodeToCapData(raw);
return board.fromCapData(capData);
return harden(board.fromCapData(capData));
};
const getKeysByPrefix = prefix =>
[...vsMap.keys()].filter(k => k.startsWith(prefix));
return { getMapItem, getRecordItem, getKeysByPrefix };
const children = prefix => [
...new Set(
entries
.map(([k, _]) => k)
.filter(k => k.length > prefix.length && k.startsWith(prefix))
.map(k => k.slice(prefix.length).split('.')[0]),
),
];
return { getMapItem, getRecordItem, children };
};
const thing = mkThing(vsEntries);

const brand = thing.getMapItem('published.agoricNames.brand');
const psmNames = thing.children('published.psm.IST.');
// old board IDs
const brandIdByName = new Map(
psmNames.map(name => {
const metrics = thing.getRecordItem(`published.psm.IST.${name}.metrics`);
return [name, metrics.anchorPoolBalance.brand];
}),
);

const metricsKeys = thing
.getKeysByPrefix('published.psm.IST.')
.filter(key => key.endsWith('.metrics'));
const publishedAssets = metricsKeys.map(metricsKey => {
const metrics = thing.getRecordItem(metricsKey);
const governance = thing.getRecordItem(
metricsKey.replace(/.metrics$/, '.governance'),
);
const name = metricsKey.split('.')[3];
return { name, metrics, governance };
});
const brand = thing.getMapItem('published.agoricNames.brand');
const stableBrand = await E(E(zoe).getFeeIssuer()).getBrand();

const kits = [];
for await (const asset of publishedAssets) {
const slotToVal = new Map([[brand.get('IST'), stableBrand]]);
for await (const name of psmNames) {
/** @type {AnchorOptions} */
const anchorOptions =
anchorAssets.find(a => a.keyword === asset.name) ||
Fail`no anchor options for ${asset.name}`;
anchorAssets.find(a => a.keyword === name) ||
Fail`no anchor options for ${name}`;
const anchorKit = await makeAnchorAsset(powers, {
options: { anchorOptions },
});
kits.push(anchorKit);
const boardId = brandIdByName.get(name);
assert.typeof(boardId, 'string');
slotToVal.set(boardId, anchorKit.brand);
}

return { brand, publishedAssets, kits };
const thing2 = mkThing(vsEntries, s => slotToVal.get(s));
const psmStateByName = new Map(
psmNames.map(name => {
const m = thing2.getRecordItem(`published.psm.IST.${name}.metrics`);
const g = thing2.getRecordItem(`published.psm.IST.${name}.governance`);
return [name, { metrics: m, governance: g }];
}),
);

const feeAmounts = [...psmStateByName.values()].map(
({ metrics }) => metrics.feePoolBalance,
);

const feeTotal = amountSum(feeAmounts);

return { brand, publishedAssets: psmStateByName, feeTotal };
};

const restorePSM_TODO = async powers => {
Expand Down
44 changes: 30 additions & 14 deletions packages/inter-protocol/test/psm/test-psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ const vstorage = [
'{"blockHeight":"9555449","values":["{\\"body\\":\\"{\\\\\\"anchorPoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: USDC_axl brand\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"487464281410\\\\\\"}},\\\\\\"feePoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: IST brand\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"0\\\\\\"}},\\\\\\"mintedPoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"487464281410\\\\\\"}},\\\\\\"totalAnchorProvided\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"4327825824427\\\\\\"}},\\\\\\"totalMintedProvided\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"4815290105837\\\\\\"}}}\\",\\"slots\\":[\\"board0223\\",\\"board02314\\"]}"]}',
],

[
'published.psm.IST.USDT_axl.governance',
'{"blockHeight":"9174468","values":["{\\"body\\":\\"{\\\\\\"current\\\\\\":{\\\\\\"Electorate\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"invitation\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: Zoe Invitation brand\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":[{\\\\\\"description\\\\\\":\\\\\\"questionPoser\\\\\\",\\\\\\"handle\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: InvitationHandle\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"installation\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: BundleInstallation\\\\\\",\\\\\\"index\\\\\\":2},\\\\\\"instance\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: InstanceHandle\\\\\\",\\\\\\"index\\\\\\":3}}]}},\\\\\\"GiveMintedFee\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"ratio\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"denominator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: IST brand\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"10000\\\\\\"}},\\\\\\"numerator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"0\\\\\\"}}}},\\\\\\"MintLimit\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"amount\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"650000000000\\\\\\"}}},\\\\\\"WantMintedFee\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"ratio\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"denominator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"10000\\\\\\"}},\\\\\\"numerator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"0\\\\\\"}}}}}}\\",\\"slots\\":[\\"board04016\\",\\"board06120\\",\\"board00218\\",\\"board0074\\",\\"board02314\\"]}"]}',
],
[
'published.psm.IST.USDT_axl.metrics',
'{"blockHeight":"9554534","values":["{\\"body\\":\\"{\\\\\\"anchorPoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: USDT_axl brand\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"12155727701\\\\\\"}},\\\\\\"feePoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: IST brand\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"58732736\\\\\\"}},\\\\\\"mintedPoolBalance\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"12155727701\\\\\\"}},\\\\\\"totalAnchorProvided\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"301812236296\\\\\\"}},\\\\\\"totalMintedProvided\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"313967963997\\\\\\"}}}\\",\\"slots\\":[\\"board0188\\",\\"board02314\\"]}"]}',
],

[
'published.psm.IST.DAI_axl.governance',
'{"blockHeight":"7739600","values":["{\\"body\\":\\"{\\\\\\"current\\\\\\":{\\\\\\"Electorate\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"invitation\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: Zoe Invitation brand\\\\\\",\\\\\\"index\\\\\\":0},\\\\\\"value\\\\\\":[{\\\\\\"description\\\\\\":\\\\\\"questionPoser\\\\\\",\\\\\\"handle\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: InvitationHandle\\\\\\",\\\\\\"index\\\\\\":1},\\\\\\"installation\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: BundleInstallation\\\\\\",\\\\\\"index\\\\\\":2},\\\\\\"instance\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: InstanceHandle\\\\\\",\\\\\\"index\\\\\\":3}}]}},\\\\\\"GiveMintedFee\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"ratio\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"denominator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"iface\\\\\\":\\\\\\"Alleged: IST brand\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"10000\\\\\\"}},\\\\\\"numerator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"0\\\\\\"}}}},\\\\\\"MintLimit\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"amount\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"1100000000000\\\\\\"}}},\\\\\\"WantMintedFee\\\\\\":{\\\\\\"type\\\\\\":\\\\\\"ratio\\\\\\",\\\\\\"value\\\\\\":{\\\\\\"denominator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"10000\\\\\\"}},\\\\\\"numerator\\\\\\":{\\\\\\"brand\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"slot\\\\\\",\\\\\\"index\\\\\\":4},\\\\\\"value\\\\\\":{\\\\\\"@qclass\\\\\\":\\\\\\"bigint\\\\\\",\\\\\\"digits\\\\\\":\\\\\\"0\\\\\\"}}}}}}\\",\\"slots\\":[\\"board04016\\",\\"board01759\\",\\"board00218\\",\\"board0074\\",\\"board02314\\"]}"]}',
Expand All @@ -743,6 +752,13 @@ const anchorAssets = [
decimalPlaces: 6,
denom: 'ibc/toyusdc',
},
{
keyword: 'USDT_axl',
proposedName: 'Tether USD',
decimalPlaces: 6,
denom:
'ibc/F2331645B9683116188EF36FC04A809C28BD36B54555E8705A37146D0182F045',
},
{
keyword: 'DAI_axl',
proposedName: 'DAI',
Expand Down Expand Up @@ -773,7 +789,7 @@ const makeMockBankManager = t => {
test('restore PSM: decode brand entries, metrics, governance', async t => {
const { installs, zoe } = t.context;
const { produce, consume } = makePromiseSpace();
const { agoricNamesAdmin, spaces } = makeAgoricNamesAccess();
const { agoricNames, agoricNamesAdmin, spaces } = makeAgoricNamesAccess();

produce.agoricNamesAdmin.resolve(agoricNamesAdmin);
produce.zoe.resolve(zoe);
Expand All @@ -787,28 +803,28 @@ test('restore PSM: decode brand entries, metrics, governance', async t => {
...spaces,
};

const { brand, publishedAssets } = await restorePSM(powers);
const {
brand: brandByName,
publishedAssets,
feeTotal,
} = await restorePSM(powers);

t.is(brand.get('IST'), 'board02314', 'IST brand board ID is correct');
t.is(brandByName.get('IST'), 'board02314', 'IST brand board ID is correct');

t.true(Array.isArray(publishedAssets), 'publishedAssets is an array');
const [a0] = publishedAssets;
const a0 = publishedAssets.get('USDC_axl') || assert.fail();

t.is(a0.name, 'USDC_axl');
const stableBrand = await E(E(zoe).getFeeIssuer()).getBrand();
const anchorBrand = await agoricNames.lookup('brand', 'USDC_axl');
t.deepEqual(
a0.metrics.anchorPoolBalance,
{
brand: 'board0223',
value: 487464281410n,
},
{ brand: anchorBrand, value: 487_464_281_410n },
'metrics.anchorPoolBalance',
);
t.deepEqual(
a0.governance.current.MintLimit.value,
{
brand: 'board02314',
value: 1000000000000n,
},
{ brand: stableBrand, value: 1_000_000_000_000n },
'governance.MintLimit',
);

t.deepEqual(feeTotal, { brand: stableBrand, value: 58_732_736n }, 'feeTotal');
});

0 comments on commit 1ff8891

Please sign in to comment.