Skip to content

Instantly share code, notes, and snippets.

@tgrecojs
Created May 31, 2023 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgrecojs/bd1ddf7b1bb8a302c9685ba570237c20 to your computer and use it in GitHub Desktop.
Save tgrecojs/bd1ddf7b1bb8a302c9685ba570237c20 to your computer and use it in GitHub Desktop.
mintMaker exo with Interface guard for prepareIssuerKit
import { Nat } from '@endo/nat';
import { M } from '@agoric/store';
// reproducing purseShape found in ERTP
// https://github.com/Agoric/agoric-sdk/blob/master/packages/ERTP/src/typeGuards.js#L8
const purseShape = M.remotable('Purse');
const IssuerI = M.interface('Issuer', {
makeEmptyPurse: M.call().returns(purseShape),
});
const MintI = M.interface('Mint', {
makePurse: M.call(M.bigint()).returns(purseShape),
});
/**
* Mint maker, transformed to an exo class kit.
*
* @param {import('@agoric/zone').Zone} zone
*/
export const prepareIssuerKit = zone => {
const makePurse = zone.exoClass(
'Purse',
undefined,
(issuer, ledger) => ({ issuer, ledger }),
{
getIssuer() {
return this.state.issuer;
},
getBalance() {
const { self } = this;
return this.state.ledger.get(self);
},
/**
* @param {bigint} amount
* @param {unknown} src
*/
deposit(amount, src) {
const { self } = this;
const { ledger } = this.state;
Nat(ledger.get(self) + Nat(amount));
ledger.set(src, Nat(ledger.get(src) - amount));
ledger.set(self, ledger.get(self) + amount);
},
withdraw(amount) {
const { self } = this;
const { issuer } = this.state;
const newPurse = issuer.makeEmptyPurse();
newPurse.deposit(amount, self);
return newPurse;
},
},
);
const makeIssuerKit = zone.exoClassKit(
'Mint',
{ issuer: IssuerI, mint: MintI },
() => ({
/** @type {WeakMapStore<unknown, bigint>} */
ledger: zone.weakMapStore('ledger'),
}),
{
issuer: {
makeEmptyPurse() {
const { mint } = this.facets;
return mint.makePurse(0n);
},
},
mint: {
/** @param {bigint} initialBalance */
makePurse(initialBalance) {
const { issuer } = this.facets;
const { ledger } = this.state;
const purse = makePurse(issuer, ledger);
ledger.init(purse, initialBalance);
return purse;
},
},
},
);
return makeIssuerKit;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment