feat: ERTP mints, purses, payments, and a shop, in Loam

makeIssuerKit(name) answers a mint; its issuer makes purses, which
withdraw payments and take deposits of their own brand. makeShop(price,
proceeds, stock) sells one unit of stock for exactly price, moving
payment, proceeds, and stock in one Loam transaction. A public board,
a home every caller shares, is where Alice and Bob find each other's
issuers and the shop.

Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
dckc committed 2026-11-18 06:35:59 -06:00
1 parent b717ccaedf
commit ed7d662d56
4 files changed
+489 -28

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
//! Helpers for Loam behaviors that write more than their own payload: make
//! records, change other records, answer with what they made.
const std = @import("std");
const glowcap = @import("glowcap");
const core = glowcap.loam.core;
const hosting = glowcap.loam.hosting;
const driver = glowcap.driver.core;
/// Have `plan.stage(call, store)` run in Loam's batch for `call`, and answer
/// what it returns. Records and capabilities can be made only there, so an
/// invocation that makes them returns `void` and this answers for it.
pub fn stageWith(comptime Plan: type, call: *hosting.Call, plan: Plan) !driver.Value {
const Holder = struct {
call: *hosting.Call,
plan: Plan,
answer: ?driver.Value = null,
fn stage(context: *anyopaque, store: *core.Store) !void {
const self: *@This() = @ptrCast(@alignCast(context));
self.answer = try self.plan.stage(self.call, store);
}
fn result(c: *hosting.Call) !driver.Value {
const self: *@This() = @ptrCast(@alignCast(c.context.?));
return self.answer orelse error.NotStaged;
}
};
const holder = try call.arena().create(Holder);
holder.* = .{ .call = call, .plan = plan };
call.transaction = .{ .context = holder, .stage = Holder.stage };
call.result = Holder.result;
call.context = holder;
return .{ .void = {} };
}
/// A preparation that leases the records with these ids before invocation.
pub const Leases = struct {
pub const max = 4;
store: *core.Store,
ids: [max]core.ObjectId = undefined,
leases: [max]?core.Lease = @splat(null),
count: usize = 0,
/// Lease `ids` for `call`; `view(call, i)` then reads the `i`th.
pub fn prepare(call: *hosting.Call, ids: []const core.ObjectId) !hosting.Preparation {
if (ids.len > max) return error.TooManyRecords;
const self = try call.arena().create(Leases);
self.* = .{ .store = call.attachment.store, .count = ids.len };
@memcpy(self.ids[0..ids.len], ids);
return .{ .context = self, .advanceFn = advance, .completeFn = complete, .deinitFn = deinit };
}
/// The current view of the `index`th record `prepare` leased.
pub fn view(call: *hosting.Call, index: usize) !core.RecordView {
const self: *Leases = @ptrCast(@alignCast((call.preparation orelse return error.Unprepared).context));
return self.store.currentView(self.leases[index] orelse return error.Unprepared);
}
fn advance(context: *anyopaque) !hosting.Preparation.Step {
const self: *Leases = @ptrCast(@alignCast(context));
for (self.ids[0..self.count], self.leases[0..self.count]) |id, *lease| if (lease.* == null) {
switch (try hosting.Preparation.acquire(self.store, id)) {
.ready => |ready| lease.* = ready,
.read => |read| return .{ .read = read },
.waiting => return .waiting,
}
};
return .ready;
}
fn complete(context: *anyopaque, ticket: core.Ticket, result: core.ReadResult) !void {
const self: *Leases = @ptrCast(@alignCast(context));
try self.store.completeRead(ticket, result);
}
fn deinit(context: *anyopaque) void {
const self: *Leases = @ptrCast(@alignCast(context));
for (&self.leases) |*slot| if (slot.*) |lease| {
self.store.release(lease) catch {};
slot.* = null;
};
}
};
/// The record a durable capability argument designates.
pub fn target(call: *hosting.Call, arg: driver.Value) !core.ObjectId {
if (arg != .capability) return error.InvalidArguments;
return (try call.attachment.capture(arg.capability)).target;
}
/// A record's payload read as one little-endian i64.
pub fn amountOf(view: core.RecordView) !i64 {
if (view.payload.len != 8) return error.InvalidRecord;
return std.mem.readInt(i64, view.payload[0..8], .little);
}
pub fn amountBytes(arena: std.mem.Allocator, amount: i64) ![]const u8 {
const bytes = try arena.alloc(u8, 8);
std.mem.writeInt(i64, bytes[0..8], amount, .little);
return bytes;
}
/// `view` with a new payload, keeping its schema, edges, and rooting.
pub fn rewrite(store: *core.Store, arena: std.mem.Allocator, view: core.RecordView, payload: []const u8) !void {
const edges = try arena.alloc(core.Edge, view.edge_count);
for (edges, 0..) |*edge, i| edge.* = try view.edgeAt(i);
try store.putCopying(.{ .id = view.id, .revision = 0, .schema = view.schema, .schema_version = view.schema_version, .payload = payload, .edges = edges, .rooted = view.rooted }, view.revision);
}