Skip to content

Commit

Permalink
chore: address PR comments. make a pureCopy but test if empty recor…
Browse files Browse the repository at this point in the history
…d is being miscategorized (for this purpose) as a presence
  • Loading branch information
katelynsills committed Nov 4, 2020
1 parent 1418149 commit 65952d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/ERTP/src/displayInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, details, q } from '@agoric/assert';
import { pureCopy, passStyleOf, REMOTE_STYLE } from '@agoric/marshal';

// TODO: assertSubset and assertKeysAllowed are copied from Zoe. Move
// this code to a location where it can be used by ERTP and Zoe
Expand Down Expand Up @@ -40,3 +41,12 @@ export const assertDisplayInfo = allegedDisplayInfo => {
const displayInfoKeys = harden(['decimalPlaces']);
assertKeysAllowed(displayInfoKeys, allegedDisplayInfo);
};

export const coerceDisplayInfo = allegedDisplayInfo => {
if (passStyleOf(allegedDisplayInfo) === REMOTE_STYLE) {
return harden({});
}
allegedDisplayInfo = pureCopy(allegedDisplayInfo);
assertDisplayInfo(allegedDisplayInfo);
return allegedDisplayInfo;
};
4 changes: 2 additions & 2 deletions packages/ERTP/src/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isPromise } from '@agoric/promise-kit';

import { makeAmountMath, MathKind } from './amountMath';
import { makeInterface, ERTPKind } from './interfaces';
import { assertDisplayInfo } from './displayInfo';
import { coerceDisplayInfo } from './displayInfo';

import './types';

Expand All @@ -23,7 +23,7 @@ function makeIssuerKit(
displayInfo = undefined,
) {
assert.typeof(allegedName, 'string');
assertDisplayInfo(displayInfo);
displayInfo = coerceDisplayInfo(displayInfo);

const brand = Remotable(
makeInterface(allegedName, ERTPKind.BRAND),
Expand Down
5 changes: 3 additions & 2 deletions packages/ERTP/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@

/**
* @typedef {Object} DisplayInfo
* @property {number} decimalPlaces
* @property {number=} decimalPlaces
* Tells the display software how many decimal places to move the
* decimal over to the left, or in other words, which position corresponds to whole
* numbers. We require fungible digital assets to be represented in
* integers, in the smallest unit (i.e. USD might be represented in mill,
* a thousandth of a dollar. In that case, `decimalPlaces` would be 3.)
* For non-fungible digital assets, this should be left as undefined.
* This property is optional, and for non-fungible digital assets,
* should not be specified.
* The decimalPlaces property should be used for *display purposes only*. Any
* other use is an anti-pattern.
*/
Expand Down
9 changes: 8 additions & 1 deletion packages/ERTP/test/unitTests/test-issuerObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('issuer.getBrand, brand.isMyIssuer', t => {
test('brand.getDisplayInfo()', t => {
const displayInfo = harden({ decimalPlaces: 3 });
const { brand } = makeIssuerKit('fungible', MathKind.NAT, displayInfo);
t.is(brand.getDisplayInfo(), displayInfo);
t.deepEqual(brand.getDisplayInfo(), displayInfo);
const display = amount => {
const { brand: myBrand, value } = amount;
const { decimalPlaces } = myBrand.getDisplayInfo();
Expand All @@ -37,12 +37,19 @@ test('brand.getDisplayInfo()', t => {

test('bad display info', t => {
const displayInfo = harden({ somethingUnexpected: 3 });
// @ts-ignore
t.throws(() => makeIssuerKit('fungible', MathKind.NAT, displayInfo), {
message:
'key "somethingUnexpected" was not one of the expected keys ["decimalPlaces"]',
});
});

test('empty display info', t => {
const displayInfo = harden({});
const { brand } = makeIssuerKit('fungible', MathKind.NAT, displayInfo);
t.deepEqual(brand.getDisplayInfo(), displayInfo);
});

test('amountMath from makeIssuerKit', async t => {
const { issuer, amountMath, brand } = makeIssuerKit('fungible');
const ibrand = await E(issuer).getBrand();
Expand Down

0 comments on commit 65952d4

Please sign in to comment.