Skip to content

Commit

Permalink
fix: eventual auxdata
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Mar 20, 2023
1 parent a66f8f2 commit 9c90ec5
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 158 deletions.
10 changes: 10 additions & 0 deletions packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export const preparePaymentLedger = (
getAmountShape() {
return amountShape;
},
aux() {
return brandAuxData;
},
});

const amountShape = amountShapeFromElementShape(
Expand All @@ -114,6 +117,13 @@ export const preparePaymentLedger = (
elementShape,
);

const brandAuxData = harden({
name,
assetKind,
displayInfo,
amountShape,
});

const { IssuerI, MintI, PaymentI, PurseIKit } = makeIssuerInterfaces(
brand,
assetKind,
Expand Down
8 changes: 8 additions & 0 deletions packages/ERTP/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,19 @@ export const IssuerKitShape = harden({

// //////////////////////// Interfaces /////////////////////////////////////////

export const BrandAuxDataShape = harden({
name: M.string(),
assetKind: AssetKindShape,
displayInfo: DisplayInfoShape,
amountShape: M.pattern(),
});

export const BrandI = M.interface('Brand', {
isMyIssuer: M.callWhen(M.await(IssuerShape)).returns(M.boolean()),
getAllegedName: M.call().returns(M.string()),
getDisplayInfo: M.call().returns(DisplayInfoShape),
getAmountShape: M.call().returns(M.pattern()),
aux: M.call().returns(BrandAuxDataShape),
});

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/ERTP/src/types-ambient.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
* AssetKind.SET or AssetKind.COPY_SET (non-fungible)
*/

/**
* @typedef {object} BrandAuxData
* @property {string} name
* @property {AssetKind} assetKind
* @property {DisplayInfo} displayInfo
* @property {Pattern} amountShape
*/

/**
* @template {AssetKind} [K=AssetKind]
* @typedef {object} Brand
Expand All @@ -102,6 +110,7 @@
* @property {() => DisplayInfo<K>} getDisplayInfo
* Give information to UI on how to display the amount.
* @property {() => Pattern} getAmountShape
* @property {() => BrandAuxData} aux
*/

// /////////////////////////// Issuer //////////////////////////////////////////
Expand Down
20 changes: 13 additions & 7 deletions packages/ERTP/test/unitTests/mathHelpers/mockBrand.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Far } from '@endo/marshal';
import { M } from '@agoric/store';
import { AssetKind } from '../../../src/index.js';

const mockAuxData = harden({
name: 'mock',
assetKind: AssetKind.NAT,
displayInfo: { assetKind: AssetKind.NAT },
amountShape: M.any(),
});

/** @type {Brand<AssetKind>} */
export const mockBrand = Far('brand', {
// eslint-disable-next-line no-unused-vars
isMyIssuer: async allegedIssuer => false,
getAllegedName: () => 'mock',
getAmountShape: () => {},
getDisplayInfo: () => ({
assetKind: AssetKind.NAT,
}),
getAllegedName: () => mockAuxData.name,
isMyIssuer: async _allegedIssuer => false,
getDisplayInfo: () => mockAuxData.displayInfo,
getAmountShape: () => mockAuxData.amountShape,
aux: () => mockAuxData,
});
154 changes: 32 additions & 122 deletions packages/ERTP/test/unitTests/mathHelpers/test-natMathHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import { mockBrand } from './mockBrand.js';
// AmountMath so that we can test that any duplication is handled
// correctly.

const otherAuxData = harden({
name: 'somename',
assetKind: AssetKind.NAT,
displayInfo: { assetKind: AssetKind.NAT },
amountShape: M.any(),
});

const otherBrand = Far('otherBrand', {
getAllegedName: () => otherAuxData.name,
isMyIssuer: async _allegedIssuer => false,
getDisplayInfo: () => otherAuxData.displayInfo,
getAmountShape: () => otherAuxData.amountShape,
aux: () => otherAuxData,
});

test('natMathHelpers make', t => {
t.deepEqual(m.make(mockBrand, 4n), { brand: mockBrand, value: 4n });
// @ts-expect-error deliberate invalid arguments for testing
Expand Down Expand Up @@ -62,12 +77,7 @@ test('natMathHelpers coerce', t => {
m.coerce(
mockBrand,
harden({
brand: Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
brand: otherBrand,
value: 4n,
}),
),
Expand Down Expand Up @@ -186,39 +196,14 @@ test('natMathHelpers isGTE', t => {
});

test('natMathHelpers isGTE mixed brands', t => {
t.throws(
() =>
m.isGTE(
m.make(
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
5n,
),
m.make(mockBrand, 3n),
),
{
message: /Brands in left .* and right .* should match but do not/,
},
);
t.throws(() => m.isGTE(m.make(otherBrand, 5n), m.make(mockBrand, 3n)), {
message: /Brands in left .* and right .* should match but do not/,
});
});

test(`natMathHelpers isGTE - brands don't match objective brand`, t => {
t.throws(
() =>
m.isGTE(
m.make(mockBrand, 5n),
m.make(mockBrand, 3n),
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
),
() => m.isGTE(m.make(mockBrand, 5n), m.make(mockBrand, 3n), otherBrand),
{
message: /amount's brand .* did not match expected brand .*/,
},
Expand All @@ -237,39 +222,14 @@ test('natMathHelpers isEqual', t => {
});

test('natMathHelpers isEqual mixed brands', t => {
t.throws(
() =>
m.isEqual(
m.make(
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
4n,
),
m.make(mockBrand, 4n),
),
{
message: /Brands in left .* and right .* should match but do not/,
},
);
t.throws(() => m.isEqual(m.make(otherBrand, 4n), m.make(mockBrand, 4n)), {
message: /Brands in left .* and right .* should match but do not/,
});
});

test(`natMathHelpers isEqual - brands don't match objective brand`, t => {
t.throws(
() =>
m.isEqual(
m.make(mockBrand, 4n),
m.make(mockBrand, 4n),
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
),
() => m.isEqual(m.make(mockBrand, 4n), m.make(mockBrand, 4n), otherBrand),
{
message: /amount's brand .* did not match expected brand .*/,
},
Expand All @@ -285,39 +245,14 @@ test('natMathHelpers add', t => {
});

test('natMathHelpers add mixed brands', t => {
t.throws(
() =>
m.add(
m.make(
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
5n,
),
m.make(mockBrand, 9n),
),
{
message: /Brands in left .* and right .* should match but do not/,
},
);
t.throws(() => m.add(m.make(otherBrand, 5n), m.make(mockBrand, 9n)), {
message: /Brands in left .* and right .* should match but do not/,
});
});

test(`natMathHelpers add - brands don't match objective brand`, t => {
t.throws(
() =>
m.add(
m.make(mockBrand, 5n),
m.make(mockBrand, 9n),
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
),
() => m.add(m.make(mockBrand, 5n), m.make(mockBrand, 9n), otherBrand),
{
message: /amount's brand .* did not match expected brand .*/,
},
Expand All @@ -333,39 +268,14 @@ test('natMathHelpers subtract', t => {
});

test('natMathHelpers subtract mixed brands', t => {
t.throws(
() =>
m.subtract(
m.make(
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
6n,
),
m.make(mockBrand, 1n),
),
{
message: /Brands in left .* and right .* should match but do not/,
},
);
t.throws(() => m.subtract(m.make(otherBrand, 6n), m.make(mockBrand, 1n)), {
message: /Brands in left .* and right .* should match but do not/,
});
});

test(`natMathHelpers subtract brands don't match brand`, t => {
t.throws(
() =>
m.subtract(
m.make(mockBrand, 6n),
m.make(mockBrand, 1n),
Far('otherBrand', {
getAllegedName: () => 'somename',
isMyIssuer: async () => false,
getDisplayInfo: () => ({ assetKind: AssetKind.NAT }),
getAmountShape: () => M.any(),
}),
),
() => m.subtract(m.make(mockBrand, 6n), m.make(mockBrand, 1n), otherBrand),
{
message: /amount's brand .* did not match expected brand .*/,
},
Expand Down
29 changes: 23 additions & 6 deletions packages/store/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,10 @@
// TODO parameterize this to match the behavior object it guards
/**
* @typedef {{
* klass: 'Interface',
* interfaceName: string,
* methodGuards: Record<string | symbol, MethodGuard>
* sloppy?: boolean
* klass: 'Interface',
* interfaceName: string,
* methodGuards: Record<string | symbol, MethodGuard>
* sloppy?: boolean
* }} InterfaceGuard
*/

Expand All @@ -766,8 +766,25 @@
* ```
*/

/** @typedef {{ klass: 'methodGuard', callKind: 'sync' | 'async', returnGuard: unknown }} MethodGuard */
/** @typedef {any} ArgGuard */
/**
* @typedef {{
* klass: 'methodGuard',
* callKind: 'sync' | 'async',
* argGuards: ArgGuard[],
* optionalArgGuards: ArgGuard[],
* restArgGuard: Pattern,
* returnGuard: Pattern
* }} MethodGuard
*/

/**
* @typedef {{
* klass: 'awaitArg',
* argGuard: ArgGuard,
* }} AwaitArgGuard
*/

/** @typedef {Pattern | AwaitArgGuard } ArgGuard */

/**
* @typedef {object} PatternKit
Expand Down
Loading

0 comments on commit 9c90ec5

Please sign in to comment.