Ending a session breaks every reference and pending answer that lives only in it, so it takes its own update method rather than happening as a side effect of start. A second start traps again; the clients take --abandon-all to end the session a previous run left open. Generated-by: Claude Code (claude-opus-5-5)
107 lines
4.0 KiB
JavaScript
107 lines
4.0 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* @file Alice buys a widget from Bob, over ERTP in the canister.
|
|
*/
|
|
const HELP = `usage: shop.mjs HOST CANISTER_ID KEY_FILE STEP [--abandon-all]
|
|
|
|
Each STEP runs as the principal whose key is in KEY_FILE (made if missing):
|
|
|
|
mint issue moola, mint 10 into a purse kept at home as "wallet",
|
|
and post the moola issuer on the board
|
|
open issue widgets, stock one, and post a shop selling it for 3
|
|
moola, paid into a purse kept at home as "proceeds"
|
|
buy pay 3 moola from "wallet" for the widget, and keep it in a
|
|
purse at home as "closet"
|
|
balances show each purse kept at home
|
|
|
|
Run mint and buy as Alice, open as Bob. A principal has one session at a
|
|
time; --abandon-all first ends the one a previous run left open, breaking
|
|
everything that lived only in it.`;
|
|
|
|
import { connect } from './connect.js';
|
|
|
|
const steps = {
|
|
async mint(peer, home, board) {
|
|
const mint = peer.send(peer.fetch('makeIssuerKit'), 'make', 'moola');
|
|
const moola = peer.send(mint, 'getIssuer');
|
|
const wallet = peer.send(moola, 'makeEmptyPurse');
|
|
const minted = peer.send(mint, 'mintPayment', 10n);
|
|
await peer.flush();
|
|
const deposited = peer.send(wallet, 'deposit', minted);
|
|
peer.send(home, 'init', 'wallet', wallet);
|
|
peer.send(board, 'init', 'moola', moola);
|
|
await peer.flush();
|
|
console.log('minted', peer.valueOf(deposited), 'moola into wallet');
|
|
},
|
|
async open(peer, home, board) {
|
|
const mint = peer.send(peer.fetch('makeIssuerKit'), 'make', 'widget');
|
|
const widgets = peer.send(mint, 'getIssuer');
|
|
const stock = peer.send(widgets, 'makeEmptyPurse');
|
|
const widget = peer.send(mint, 'mintPayment', 1n);
|
|
const proceeds = peer.send(peer.send(board, 'get', 'moola'), 'makeEmptyPurse');
|
|
await peer.flush();
|
|
const stocked = peer.send(stock, 'deposit', widget);
|
|
const shop = peer.send(peer.fetch('makeShop'), 'make', 3n, proceeds, stock);
|
|
peer.send(home, 'init', 'proceeds', proceeds);
|
|
peer.send(board, 'init', 'widget', widgets);
|
|
await peer.flush();
|
|
peer.send(board, 'init', 'bobs-shop', shop);
|
|
await peer.flush();
|
|
console.log('stocked', peer.valueOf(stocked), 'widget; shop open at 3 moola');
|
|
},
|
|
async buy(peer, home, board) {
|
|
const wallet = peer.send(home, 'get', 'wallet');
|
|
const pay = peer.send(wallet, 'withdraw', 3n);
|
|
const shop = peer.send(board, 'get', 'bobs-shop');
|
|
await peer.flush();
|
|
const goods = peer.send(shop, 'buy', pay);
|
|
await peer.flush();
|
|
try {
|
|
peer.valueOf(goods);
|
|
} catch (err) {
|
|
// The shop refused; the payment still holds what was withdrawn.
|
|
const back = peer.send(wallet, 'deposit', pay);
|
|
await peer.flush();
|
|
console.log('refunded', peer.valueOf(back), 'moola to wallet');
|
|
throw err;
|
|
}
|
|
const closet = peer.send(peer.send(board, 'get', 'widget'), 'makeEmptyPurse');
|
|
await peer.flush();
|
|
const got = peer.send(closet, 'deposit', goods);
|
|
peer.send(home, 'init', 'closet', closet);
|
|
await peer.flush();
|
|
console.log('bought', peer.valueOf(got), 'widget into closet');
|
|
},
|
|
async balances(peer, home) {
|
|
const names = ['wallet', 'closet', 'proceeds'];
|
|
const amounts = names.map(name => peer.send(peer.send(home, 'get', name), 'getCurrentAmount'));
|
|
await peer.flush();
|
|
names.forEach((name, i) => {
|
|
let amount;
|
|
try {
|
|
amount = peer.valueOf(amounts[i]);
|
|
} catch (err) {
|
|
amount = `none (${err.message})`;
|
|
}
|
|
console.log(name, amount);
|
|
});
|
|
},
|
|
};
|
|
|
|
const main = async argv => {
|
|
const abandonAll = argv.includes('--abandon-all');
|
|
const [host, canisterId, keyFile, step] = argv.filter(arg => arg !== '--abandon-all');
|
|
if (!Object.hasOwn(steps, step ?? '')) {
|
|
console.error(HELP);
|
|
process.exitCode = 2;
|
|
return;
|
|
}
|
|
const peer = await connect(host, canisterId, keyFile, { abandonAll });
|
|
await steps[step](peer, peer.fetch('home'), peer.fetch('board'));
|
|
};
|
|
|
|
main(process.argv.slice(2)).catch(err => {
|
|
console.error(err);
|
|
process.exitCode = 1;
|
|
});
|