-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathpurse.js
45 lines (39 loc) · 1.33 KB
/
purse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { makeNotifierKit } from '@agoric/notifier';
import { Far } from '@agoric/marshal';
import { AmountMath } from './amountMath';
export const makePurse = (allegedName, assetKind, brand, purseMethods) => {
let currentBalance = AmountMath.makeEmpty(brand, assetKind);
/** @type {NotifierRecord<Amount>} */
const {
notifier: balanceNotifier,
updater: balanceUpdater,
} = makeNotifierKit(currentBalance);
const updatePurseBalance = newPurseBalance => {
currentBalance = newPurseBalance;
balanceUpdater.updateState(currentBalance);
};
/** @type {Purse} */
const purse = Far(`${allegedName} purse`, {
deposit: (srcPayment, optAmount = undefined) => {
// Note COMMIT POINT within deposit.
return purseMethods.deposit(
currentBalance,
updatePurseBalance,
srcPayment,
optAmount,
);
},
withdraw: amount =>
// Note COMMIT POINT within withdraw.
purseMethods.withdraw(currentBalance, updatePurseBalance, amount),
getCurrentAmount: () => currentBalance,
getCurrentAmountNotifier: () => balanceNotifier,
getAllegedBrand: () => brand,
// eslint-disable-next-line no-use-before-define
getDepositFacet: () => depositFacet,
});
const depositFacet = Far(`${allegedName} depositFacet`, {
receive: purse.deposit,
});
return purse;
};