feat: Alice buys a widget from Bob, from the JS client
shop.mjs runs the ERTP story step by step as the principal of a key file; connect.js holds the session setup it shares with counter.mjs. The client stages any number of string, integer, and reference arguments, and reports why an answer broke. A new start from a caller closes that caller's old session, so a client can come back with the same key. Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
1 parent
ed7d662d56
commit
9db91b1b9c
8 files changed
+287
-86
No files matched your search
+102
@@ -0,0 +1,102 @@
|
||||
#!/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
|
||||
|
||||
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.`;
|
||||
|
||||
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 ([host, canisterId, keyFile, step]) => {
|
||||
if (!Object.hasOwn(steps, step ?? '')) {
|
||||
console.error(HELP);
|
||||
process.exitCode = 2;
|
||||
return;
|
||||
}
|
||||
const peer = await connect(host, canisterId, keyFile);
|
||||
await steps[step](peer, peer.fetch('home'), peer.fetch('board'));
|
||||
};
|
||||
|
||||
main(process.argv.slice(2)).catch(err => {
|
||||
console.error(err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in new issue
Block a user