Skip to content

Commit

Permalink
fix(ERTP): use makeExternalStore for ledgers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 9, 2020
1 parent e520e79 commit 20667ce
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/ERTP/src/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @ts-check

import { assert, details } from '@agoric/assert';
import makeStore from '@agoric/weak-store';
import { makeExternalStore } from '@agoric/store';
import { E } from '@agoric/eventual-send';
import { Remotable } from '@agoric/marshal';
import { isPromise } from '@agoric/promise-kit';
Expand Down Expand Up @@ -37,8 +37,20 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {

const amountMath = makeAmountMath(brand, amountMathKind);

const paymentLedger = makeStore('payment');
const purseLedger = makeStore('purse');
const {
makeInstance: makePayment,
makeWeakStore: makePaymentWeakStore,
} = makeExternalStore('payment', () =>
Remotable(makeInterface(allegedName, ERTPKind.PAYMENT), undefined, {
getAllegedBrand: () => brand,
}),
);

/** @type {WeakStore<Payment, Amount>} */
const paymentLedger = makePaymentWeakStore();

/** @type {WeakStore<Purse, Amount>} */
let purseLedger;

function assertKnownPayment(payment) {
assert(
Expand All @@ -47,11 +59,6 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
);
}

const makePayment = () =>
Remotable(makeInterface(allegedName, ERTPKind.PAYMENT), undefined, {
getAllegedBrand: () => brand,
});

// Methods like deposit() have an optional second parameter `amount`
// which, if present, is supposed to be equal to the balance of the
// payment. This helper function does that check.
Expand All @@ -67,7 +74,10 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
/**
* @returns {Purse}
*/
const makePurse = () => {
const {
makeInstance: makePurse,
makeWeakStore: makePurseWeakStore,
} = makeExternalStore('purse', () => {
/** @type {Purse} */
const purse = Remotable(
makeInterface(allegedName, ERTPKind.PURSE),
Expand Down Expand Up @@ -129,7 +139,9 @@ function makeIssuerKit(allegedName, amountMathKind = MathKind.NAT) {
);

return purse;
};
});

purseLedger = makePurseWeakStore();

const { add } = amountMath;
const empty = amountMath.getEmpty();
Expand Down

0 comments on commit 20667ce

Please sign in to comment.