@vllnt/convex-wallet 0.1.0-canary.3a56ec7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -0
  3. package/dist/client/index.d.ts +109 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +105 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +45 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +3 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +42 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +105 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +121 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +78 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/crons.d.ts +12 -0
  32. package/dist/component/crons.d.ts.map +1 -0
  33. package/dist/component/crons.js +19 -0
  34. package/dist/component/crons.js.map +1 -0
  35. package/dist/component/internal_mutations.d.ts +20 -0
  36. package/dist/component/internal_mutations.d.ts.map +1 -0
  37. package/dist/component/internal_mutations.js +52 -0
  38. package/dist/component/internal_mutations.js.map +1 -0
  39. package/dist/component/mutations.d.ts +79 -0
  40. package/dist/component/mutations.d.ts.map +1 -0
  41. package/dist/component/mutations.js +243 -0
  42. package/dist/component/mutations.js.map +1 -0
  43. package/dist/component/queries.d.ts +43 -0
  44. package/dist/component/queries.d.ts.map +1 -0
  45. package/dist/component/queries.js +70 -0
  46. package/dist/component/queries.js.map +1 -0
  47. package/dist/component/schema.d.ts +45 -0
  48. package/dist/component/schema.d.ts.map +1 -0
  49. package/dist/component/schema.js +33 -0
  50. package/dist/component/schema.js.map +1 -0
  51. package/dist/component/validators.d.ts +73 -0
  52. package/dist/component/validators.d.ts.map +1 -0
  53. package/dist/component/validators.js +45 -0
  54. package/dist/component/validators.js.map +1 -0
  55. package/dist/react/index.d.ts +37 -0
  56. package/dist/react/index.d.ts.map +1 -0
  57. package/dist/react/index.js +30 -0
  58. package/dist/react/index.js.map +1 -0
  59. package/dist/shared.d.ts +84 -0
  60. package/dist/shared.d.ts.map +1 -0
  61. package/dist/shared.js +91 -0
  62. package/dist/shared.js.map +1 -0
  63. package/package.json +112 -0
  64. package/src/client/index.ts +257 -0
  65. package/src/client/types.ts +51 -0
  66. package/src/component/_generated/api.ts +58 -0
  67. package/src/component/_generated/component.ts +118 -0
  68. package/src/component/_generated/dataModel.ts +60 -0
  69. package/src/component/_generated/server.ts +156 -0
  70. package/src/component/convex.config.ts +9 -0
  71. package/src/component/crons.ts +29 -0
  72. package/src/component/internal_mutations.ts +58 -0
  73. package/src/component/mutations.ts +279 -0
  74. package/src/component/queries.ts +76 -0
  75. package/src/component/schema.ts +33 -0
  76. package/src/component/validators.ts +58 -0
  77. package/src/react/index.test.tsx +61 -0
  78. package/src/react/index.tsx +50 -0
  79. package/src/shared.ts +123 -0
  80. package/src/test.ts +12 -0
@@ -0,0 +1,243 @@
1
+ import { v } from "convex/values";
2
+ import { mutation } from "./_generated/server";
3
+ import { applyRegen, assertPositiveAmount } from "../shared";
4
+ import { maxArg, regenArg, spendResult } from "./validators";
5
+ const balanceObject = v.object({ balance: v.number() });
6
+ /**
7
+ * Find the prior ledger row for a replayed `idempotencyKey`, scoped to this
8
+ * exact `(subjectRef, currency)`. A key reused across different subjects or
9
+ * currencies is independent — so a legitimate grant is never dropped.
10
+ */
11
+ async function findIdempotent(ctx, subjectRef, currency, idempotencyKey) {
12
+ return ctx.db
13
+ .query("ledger")
14
+ .withIndex("by_idem", (q) => q
15
+ .eq("subjectRef", subjectRef)
16
+ .eq("currency", currency)
17
+ .eq("idempotencyKey", idempotencyKey))
18
+ .first();
19
+ }
20
+ /** Clamp a post-credit balance to an optional per-currency `max` ceiling. */
21
+ function clampToMax(balance, max) {
22
+ return max === undefined ? balance : Math.min(balance, max);
23
+ }
24
+ /**
25
+ * Credit `amount` of `currency` to `subjectRef`. When `idempotencyKey` is
26
+ * supplied and already recorded for this `(subjectRef, currency)`, this is a
27
+ * no-op replay — the current (regen-aware) balance is returned without
28
+ * double-crediting. A credit that would exceed `max` is clamped to `max`.
29
+ */
30
+ export const earn = mutation({
31
+ args: {
32
+ subjectRef: v.string(),
33
+ currency: v.string(),
34
+ amount: v.number(),
35
+ reason: v.string(),
36
+ idempotencyKey: v.optional(v.string()),
37
+ regen: regenArg,
38
+ max: maxArg,
39
+ },
40
+ returns: balanceObject,
41
+ handler: async (ctx, args) => {
42
+ assertPositiveAmount(args.amount);
43
+ const now = Date.now();
44
+ const row = await ctx.db
45
+ .query("balances")
46
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.subjectRef).eq("currency", args.currency))
47
+ .unique();
48
+ let balance;
49
+ let delta;
50
+ if (row === null) {
51
+ // No balance row → no recorded key for it can exist, so the idempotency
52
+ // check is unnecessary: this is always a first credit.
53
+ balance = clampToMax(args.amount, args.max);
54
+ delta = balance;
55
+ await ctx.db.insert("balances", {
56
+ subjectRef: args.subjectRef,
57
+ currency: args.currency,
58
+ amount: balance,
59
+ lastRegenAt: now,
60
+ lifetimeEarned: balance,
61
+ lifetimeSpent: 0,
62
+ });
63
+ }
64
+ else {
65
+ if (args.idempotencyKey !== undefined) {
66
+ const seen = await findIdempotent(ctx, args.subjectRef, args.currency, args.idempotencyKey);
67
+ if (seen !== null) {
68
+ // Replay: return the current (regen-aware) balance, no double-credit.
69
+ return {
70
+ balance: applyRegen(row.amount, row.lastRegenAt, now, args.regen).amount,
71
+ };
72
+ }
73
+ }
74
+ const r = applyRegen(row.amount, row.lastRegenAt, now, args.regen);
75
+ balance = clampToMax(r.amount + args.amount, args.max);
76
+ delta = balance - r.amount;
77
+ await ctx.db.patch(row._id, {
78
+ amount: balance,
79
+ lastRegenAt: r.lastRegenAt,
80
+ lifetimeEarned: row.lifetimeEarned + delta,
81
+ });
82
+ }
83
+ await ctx.db.insert("ledger", {
84
+ subjectRef: args.subjectRef,
85
+ currency: args.currency,
86
+ delta,
87
+ reason: args.reason,
88
+ idempotencyKey: args.idempotencyKey,
89
+ createdAt: now,
90
+ });
91
+ return { balance };
92
+ },
93
+ });
94
+ /**
95
+ * Debit `amount` of `currency` from `subjectRef`. Never goes negative — when
96
+ * the (regen-aware) balance is short, returns `{ ok: false, code:
97
+ * "INSUFFICIENT" }` while still persisting any regen that accrued.
98
+ */
99
+ export const spend = mutation({
100
+ args: {
101
+ subjectRef: v.string(),
102
+ currency: v.string(),
103
+ amount: v.number(),
104
+ reason: v.string(),
105
+ idempotencyKey: v.optional(v.string()),
106
+ regen: regenArg,
107
+ },
108
+ returns: spendResult,
109
+ handler: async (ctx, args) => {
110
+ assertPositiveAmount(args.amount);
111
+ const now = Date.now();
112
+ const row = await ctx.db
113
+ .query("balances")
114
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.subjectRef).eq("currency", args.currency))
115
+ .unique();
116
+ if (row === null) {
117
+ return { ok: false, balance: 0, code: "INSUFFICIENT" };
118
+ }
119
+ if (args.idempotencyKey !== undefined) {
120
+ const seen = await findIdempotent(ctx, args.subjectRef, args.currency, args.idempotencyKey);
121
+ if (seen !== null) {
122
+ // Replay: return the current (regen-aware) balance, no double-debit.
123
+ return {
124
+ ok: true,
125
+ balance: applyRegen(row.amount, row.lastRegenAt, now, args.regen).amount,
126
+ };
127
+ }
128
+ }
129
+ const r = applyRegen(row.amount, row.lastRegenAt, now, args.regen);
130
+ if (r.amount < args.amount) {
131
+ await ctx.db.patch(row._id, { amount: r.amount, lastRegenAt: r.lastRegenAt });
132
+ return { ok: false, balance: r.amount, code: "INSUFFICIENT" };
133
+ }
134
+ const balance = r.amount - args.amount;
135
+ await ctx.db.patch(row._id, {
136
+ amount: balance,
137
+ lastRegenAt: r.lastRegenAt,
138
+ lifetimeSpent: row.lifetimeSpent + args.amount,
139
+ });
140
+ await ctx.db.insert("ledger", {
141
+ subjectRef: args.subjectRef,
142
+ currency: args.currency,
143
+ delta: -args.amount,
144
+ reason: args.reason,
145
+ idempotencyKey: args.idempotencyKey,
146
+ createdAt: now,
147
+ });
148
+ return { ok: true, balance };
149
+ },
150
+ });
151
+ /**
152
+ * Move `amount` of `currency` from `fromRef` to `toRef` in one transaction.
153
+ * Debits the sender (with the same insufficiency guard as {@link spend}); on
154
+ * success credits the receiver (clamped to `max`) and ledgers both legs.
155
+ * `fromRef === toRef` is rejected with `SELF_TRANSFER` (never double-touches one
156
+ * row). Returns the sender's new balance.
157
+ */
158
+ export const transfer = mutation({
159
+ args: {
160
+ fromRef: v.string(),
161
+ toRef: v.string(),
162
+ currency: v.string(),
163
+ amount: v.number(),
164
+ reason: v.string(),
165
+ regen: regenArg,
166
+ max: maxArg,
167
+ },
168
+ returns: spendResult,
169
+ handler: async (ctx, args) => {
170
+ assertPositiveAmount(args.amount);
171
+ const now = Date.now();
172
+ const fromRow = await ctx.db
173
+ .query("balances")
174
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.fromRef).eq("currency", args.currency))
175
+ .unique();
176
+ if (args.fromRef === args.toRef) {
177
+ const current = fromRow === null
178
+ ? 0
179
+ : applyRegen(fromRow.amount, fromRow.lastRegenAt, now, args.regen).amount;
180
+ return { ok: false, balance: current, code: "SELF_TRANSFER" };
181
+ }
182
+ if (fromRow === null) {
183
+ return { ok: false, balance: 0, code: "INSUFFICIENT" };
184
+ }
185
+ const fromR = applyRegen(fromRow.amount, fromRow.lastRegenAt, now, args.regen);
186
+ if (fromR.amount < args.amount) {
187
+ await ctx.db.patch(fromRow._id, {
188
+ amount: fromR.amount,
189
+ lastRegenAt: fromR.lastRegenAt,
190
+ });
191
+ return { ok: false, balance: fromR.amount, code: "INSUFFICIENT" };
192
+ }
193
+ const fromBalance = fromR.amount - args.amount;
194
+ await ctx.db.patch(fromRow._id, {
195
+ amount: fromBalance,
196
+ lastRegenAt: fromR.lastRegenAt,
197
+ lifetimeSpent: fromRow.lifetimeSpent + args.amount,
198
+ });
199
+ await ctx.db.insert("ledger", {
200
+ subjectRef: args.fromRef,
201
+ currency: args.currency,
202
+ delta: -args.amount,
203
+ reason: args.reason,
204
+ createdAt: now,
205
+ });
206
+ const toRow = await ctx.db
207
+ .query("balances")
208
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.toRef).eq("currency", args.currency))
209
+ .unique();
210
+ let receiverDelta;
211
+ if (toRow === null) {
212
+ const credited = clampToMax(args.amount, args.max);
213
+ receiverDelta = credited;
214
+ await ctx.db.insert("balances", {
215
+ subjectRef: args.toRef,
216
+ currency: args.currency,
217
+ amount: credited,
218
+ lastRegenAt: now,
219
+ lifetimeEarned: credited,
220
+ lifetimeSpent: 0,
221
+ });
222
+ }
223
+ else {
224
+ const toR = applyRegen(toRow.amount, toRow.lastRegenAt, now, args.regen);
225
+ const credited = clampToMax(toR.amount + args.amount, args.max);
226
+ receiverDelta = credited - toR.amount;
227
+ await ctx.db.patch(toRow._id, {
228
+ amount: credited,
229
+ lastRegenAt: toR.lastRegenAt,
230
+ lifetimeEarned: toRow.lifetimeEarned + receiverDelta,
231
+ });
232
+ }
233
+ await ctx.db.insert("ledger", {
234
+ subjectRef: args.toRef,
235
+ currency: args.currency,
236
+ delta: receiverDelta,
237
+ reason: args.reason,
238
+ createdAt: now,
239
+ });
240
+ return { ok: true, balance: fromBalance };
241
+ },
242
+ });
243
+ //# sourceMappingURL=mutations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAGlC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE7D,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAIxD;;;;GAIG;AACH,KAAK,UAAU,cAAc,CAC3B,GAAW,EACX,UAAkB,EAClB,QAAgB,EAChB,cAAsB;IAEtB,OAAO,GAAG,CAAC,EAAE;SACV,KAAK,CAAC,QAAQ,CAAC;SACf,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE,CAC1B,CAAC;SACE,EAAE,CAAC,YAAY,EAAE,UAAU,CAAC;SAC5B,EAAE,CAAC,UAAU,EAAE,QAAQ,CAAC;SACxB,EAAE,CAAC,gBAAgB,EAAE,cAAc,CAAC,CACxC;SACA,KAAK,EAAE,CAAC;AACb,CAAC;AAED,6EAA6E;AAC7E,SAAS,UAAU,CAAC,OAAe,EAAE,GAAuB;IAC1D,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,QAAQ,CAAC;IAC3B,IAAI,EAAE;QACJ,UAAU,EAAE,CAAC,CAAC,MAAM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CAAC,WAAW;gBAC5B,cAAc,EAAE,KAAK,CAAC,cAAc,GAAG,aAAa;aACrD,CAAC,CAAC;QACL,CAAC;QACD,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,EAAE;YAC5B,UAAU,EAAE,IAAI,CAAC,KAAK;YACtB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,KAAK,EAAE,aAAa;YACpB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,SAAS,EAAE,GAAG;SACf,CAAC,CAAC;QACH,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAC5C,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Regen-aware single-currency read. Projects the stored balance forward to the
3
+ * server clock under the per-currency `regen` (compute-only — queries cannot
4
+ * write, so the regen is reflected in the result but not persisted). Missing → 0.
5
+ */
6
+ export declare const balance: import("convex/server").RegisteredQuery<"public", {
7
+ regen?: {
8
+ amount: number;
9
+ intervalMs: number;
10
+ cap: number;
11
+ } | undefined;
12
+ subjectRef: string;
13
+ currency: string;
14
+ }, Promise<number>>;
15
+ /**
16
+ * Multi-currency overview for `subjectRef`, regen-aware. The client passes its
17
+ * per-currency `regen` map so each row is projected exactly as {@link balance}
18
+ * would — no stale divergence. A currency absent from the map is static.
19
+ */
20
+ export declare const balances: import("convex/server").RegisteredQuery<"public", {
21
+ regen?: Record<string, {
22
+ amount: number;
23
+ intervalMs: number;
24
+ cap: number;
25
+ }> | undefined;
26
+ subjectRef: string;
27
+ }, Promise<{
28
+ currency: string;
29
+ amount: number;
30
+ }[]>>;
31
+ /** Newest-first ledger entries for one currency, capped at `limit`. */
32
+ export declare const history: import("convex/server").RegisteredQuery<"public", {
33
+ subjectRef: string;
34
+ currency: string;
35
+ limit: number;
36
+ }, Promise<{
37
+ currency: string;
38
+ delta: number;
39
+ reason: string;
40
+ idempotencyKey: string | undefined;
41
+ createdAt: number;
42
+ }[]>>;
43
+ //# sourceMappingURL=queries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,OAAO;;;;;;;;mBAoBlB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;KAgBnB,CAAC;AAEH,uEAAuE;AACvE,eAAO,MAAM,OAAO;;;;;;;;;;KAmBlB,CAAC"}
@@ -0,0 +1,70 @@
1
+ import { v } from "convex/values";
2
+ import { query } from "./_generated/server";
3
+ import { applyRegen } from "../shared";
4
+ import { balanceEntry, ledgerDoc, regenArg, regenMapArg } from "./validators";
5
+ /**
6
+ * Regen-aware single-currency read. Projects the stored balance forward to the
7
+ * server clock under the per-currency `regen` (compute-only — queries cannot
8
+ * write, so the regen is reflected in the result but not persisted). Missing → 0.
9
+ */
10
+ export const balance = query({
11
+ args: {
12
+ subjectRef: v.string(),
13
+ currency: v.string(),
14
+ regen: regenArg,
15
+ },
16
+ returns: v.number(),
17
+ handler: async (ctx, args) => {
18
+ const now = Date.now();
19
+ const row = await ctx.db
20
+ .query("balances")
21
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.subjectRef).eq("currency", args.currency))
22
+ .unique();
23
+ if (row === null) {
24
+ return 0;
25
+ }
26
+ return applyRegen(row.amount, row.lastRegenAt, now, args.regen).amount;
27
+ },
28
+ });
29
+ /**
30
+ * Multi-currency overview for `subjectRef`, regen-aware. The client passes its
31
+ * per-currency `regen` map so each row is projected exactly as {@link balance}
32
+ * would — no stale divergence. A currency absent from the map is static.
33
+ */
34
+ export const balances = query({
35
+ args: { subjectRef: v.string(), regen: regenMapArg },
36
+ returns: v.array(balanceEntry),
37
+ handler: async (ctx, args) => {
38
+ const now = Date.now();
39
+ const regenMap = args.regen ?? {};
40
+ const rows = await ctx.db
41
+ .query("balances")
42
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.subjectRef))
43
+ .collect();
44
+ return rows.map((row) => ({
45
+ currency: row.currency,
46
+ amount: applyRegen(row.amount, row.lastRegenAt, now, regenMap[row.currency])
47
+ .amount,
48
+ }));
49
+ },
50
+ });
51
+ /** Newest-first ledger entries for one currency, capped at `limit`. */
52
+ export const history = query({
53
+ args: { subjectRef: v.string(), currency: v.string(), limit: v.number() },
54
+ returns: v.array(ledgerDoc),
55
+ handler: async (ctx, args) => {
56
+ const rows = await ctx.db
57
+ .query("ledger")
58
+ .withIndex("by_subject_currency", (q) => q.eq("subjectRef", args.subjectRef).eq("currency", args.currency))
59
+ .order("desc")
60
+ .take(args.limit);
61
+ return rows.map((row) => ({
62
+ currency: row.currency,
63
+ delta: row.delta,
64
+ reason: row.reason,
65
+ idempotencyKey: row.idempotencyKey,
66
+ createdAt: row.createdAt,
67
+ }));
68
+ },
69
+ });
70
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE9E;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,CAAC;IAC3B,IAAI,EAAE;QACJ,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,KAAK,EAAE,QAAQ;KAChB;IACD,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;IACnB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC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+ /**
2
+ * Sandboxed tables — the wallet's own concern. `subjectRef` is an opaque
3
+ * host-owned reference (never assume it is a user id or its shape); `currency`
4
+ * is an opaque host-chosen string (coins, energy, gems, credits).
5
+ */
6
+ declare const _default: import("convex/server").SchemaDefinition<{
7
+ balances: import("convex/server").TableDefinition<import("convex/values").VObject<{
8
+ subjectRef: string;
9
+ currency: string;
10
+ amount: number;
11
+ lastRegenAt: number;
12
+ lifetimeEarned: number;
13
+ lifetimeSpent: number;
14
+ }, {
15
+ subjectRef: import("convex/values").VString<string, "required">;
16
+ currency: import("convex/values").VString<string, "required">;
17
+ amount: import("convex/values").VFloat64<number, "required">;
18
+ lastRegenAt: import("convex/values").VFloat64<number, "required">;
19
+ lifetimeEarned: import("convex/values").VFloat64<number, "required">;
20
+ lifetimeSpent: import("convex/values").VFloat64<number, "required">;
21
+ }, "required", "subjectRef" | "currency" | "amount" | "lastRegenAt" | "lifetimeEarned" | "lifetimeSpent">, {
22
+ by_subject_currency: ["subjectRef", "currency", "_creationTime"];
23
+ }, {}, {}>;
24
+ ledger: import("convex/server").TableDefinition<import("convex/values").VObject<{
25
+ idempotencyKey?: string | undefined;
26
+ subjectRef: string;
27
+ currency: string;
28
+ reason: string;
29
+ delta: number;
30
+ createdAt: number;
31
+ }, {
32
+ subjectRef: import("convex/values").VString<string, "required">;
33
+ currency: import("convex/values").VString<string, "required">;
34
+ delta: import("convex/values").VFloat64<number, "required">;
35
+ reason: import("convex/values").VString<string, "required">;
36
+ idempotencyKey: import("convex/values").VString<string | undefined, "optional">;
37
+ createdAt: import("convex/values").VFloat64<number, "required">;
38
+ }, "required", "subjectRef" | "currency" | "reason" | "idempotencyKey" | "delta" | "createdAt">, {
39
+ by_subject_currency: ["subjectRef", "currency", "createdAt", "_creationTime"];
40
+ by_idem: ["subjectRef", "currency", "idempotencyKey", "_creationTime"];
41
+ by_created: ["createdAt", "_creationTime"];
42
+ }, {}, {}>;
43
+ }, true>;
44
+ export default _default;
45
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAGA;;;;GAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACH,wBAwBG"}
@@ -0,0 +1,33 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ /**
4
+ * Sandboxed tables — the wallet's own concern. `subjectRef` is an opaque
5
+ * host-owned reference (never assume it is a user id or its shape); `currency`
6
+ * is an opaque host-chosen string (coins, energy, gems, credits).
7
+ */
8
+ export default defineSchema({
9
+ balances: defineTable({
10
+ subjectRef: v.string(),
11
+ currency: v.string(),
12
+ amount: v.number(),
13
+ lastRegenAt: v.number(),
14
+ lifetimeEarned: v.number(),
15
+ lifetimeSpent: v.number(),
16
+ }).index("by_subject_currency", ["subjectRef", "currency"]),
17
+ ledger: defineTable({
18
+ subjectRef: v.string(),
19
+ currency: v.string(),
20
+ delta: v.number(),
21
+ reason: v.string(),
22
+ idempotencyKey: v.optional(v.string()),
23
+ createdAt: v.number(),
24
+ })
25
+ .index("by_subject_currency", ["subjectRef", "currency", "createdAt"])
26
+ // Idempotency is scoped per (subject, currency): the same key reused across
27
+ // different subjects/currencies is independent, so a legitimate grant is
28
+ // never silently dropped by a key collision.
29
+ .index("by_idem", ["subjectRef", "currency", "idempotencyKey"])
30
+ // Drives the retention/idempotency prune cron (oldest-first by createdAt).
31
+ .index("by_created", ["createdAt"]),
32
+ });
33
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;GAIG;AACH,eAAe,YAAY,CAAC;IAC1B,QAAQ,EAAE,WAAW,CAAC;QACpB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;QAClB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;QACvB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;QAC1B,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;KAC1B,CAAC,CAAC,KAAK,CAAC,qBAAqB,EAAE,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IAC3D,MAAM,EAAE,WAAW,CAAC;QAClB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;QAClB,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QACtC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC;SACC,KAAK,CAAC,qBAAqB,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;QACtE,4EAA4E;QAC5E,yEAAyE;QACzE,6CAA6C;SAC5C,KAAK,CAAC,SAAS,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC;QAC/D,2EAA2E;SAC1E,KAAK,CAAC,YAAY,EAAE,CAAC,WAAW,CAAC,CAAC;CACtC,CAAC,CAAC"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Optional per-call time-regen rule, injected by the client from its
3
+ * per-currency config. The component itself is config-less. `max`, when set,
4
+ * is the hard ceiling on stored balance for the currency — credits are clamped
5
+ * to it (see {@link mutations}).
6
+ */
7
+ export declare const regenArg: import("convex/values").VObject<{
8
+ amount: number;
9
+ intervalMs: number;
10
+ cap: number;
11
+ } | undefined, {
12
+ amount: import("convex/values").VFloat64<number, "required">;
13
+ intervalMs: import("convex/values").VFloat64<number, "required">;
14
+ cap: import("convex/values").VFloat64<number, "required">;
15
+ }, "optional", "amount" | "intervalMs" | "cap">;
16
+ /**
17
+ * Optional per-currency hard ceiling on the stored balance. A credit that would
18
+ * carry the balance above `max` is clamped to `max`; an `earn`/`grant`/receive
19
+ * leg that is ALREADY at or above `max` is a no-op credit (idempotent ceiling).
20
+ */
21
+ export declare const maxArg: import("convex/values").VFloat64<number | undefined, "optional">;
22
+ /** Per-currency regen map for the multi-currency {@link queries.balances} read. */
23
+ export declare const regenMapArg: import("convex/values").VRecord<Record<string, {
24
+ amount: number;
25
+ intervalMs: number;
26
+ cap: number;
27
+ }> | undefined, import("convex/values").VString<string, "required">, import("convex/values").VObject<{
28
+ amount: number;
29
+ intervalMs: number;
30
+ cap: number;
31
+ }, {
32
+ amount: import("convex/values").VFloat64<number, "required">;
33
+ intervalMs: import("convex/values").VFloat64<number, "required">;
34
+ cap: import("convex/values").VFloat64<number, "required">;
35
+ }, "required", "amount" | "intervalMs" | "cap">, "optional", string>;
36
+ /** Stable, code-tagged failure reasons. `reason` (free text) is the ledger note. */
37
+ export declare const errorCode: import("convex/values").VUnion<"INSUFFICIENT" | "SELF_TRANSFER", [import("convex/values").VLiteral<"INSUFFICIENT", "required">, import("convex/values").VLiteral<"SELF_TRANSFER", "required">], "required", never>;
38
+ /**
39
+ * Result of a spend/transfer that may be rejected. `code` carries a stable
40
+ * machine-readable tag when `ok` is false; it is absent on success.
41
+ */
42
+ export declare const spendResult: import("convex/values").VObject<{
43
+ code?: "INSUFFICIENT" | "SELF_TRANSFER" | undefined;
44
+ ok: boolean;
45
+ balance: number;
46
+ }, {
47
+ ok: import("convex/values").VBoolean<boolean, "required">;
48
+ balance: import("convex/values").VFloat64<number, "required">;
49
+ code: import("convex/values").VUnion<"INSUFFICIENT" | "SELF_TRANSFER" | undefined, [import("convex/values").VLiteral<"INSUFFICIENT", "required">, import("convex/values").VLiteral<"SELF_TRANSFER", "required">], "optional", never>;
50
+ }, "required", "ok" | "balance" | "code">;
51
+ /** A single currency balance in the multi-currency overview. */
52
+ export declare const balanceEntry: import("convex/values").VObject<{
53
+ currency: string;
54
+ amount: number;
55
+ }, {
56
+ currency: import("convex/values").VString<string, "required">;
57
+ amount: import("convex/values").VFloat64<number, "required">;
58
+ }, "required", "currency" | "amount">;
59
+ /** A ledger row projected for history reads (sandbox fields omitted). */
60
+ export declare const ledgerDoc: import("convex/values").VObject<{
61
+ idempotencyKey?: string | undefined;
62
+ currency: string;
63
+ reason: string;
64
+ delta: number;
65
+ createdAt: number;
66
+ }, {
67
+ currency: import("convex/values").VString<string, "required">;
68
+ delta: import("convex/values").VFloat64<number, "required">;
69
+ reason: import("convex/values").VString<string, "required">;
70
+ idempotencyKey: import("convex/values").VString<string | undefined, "optional">;
71
+ createdAt: import("convex/values").VFloat64<number, "required">;
72
+ }, "required", "currency" | "reason" | "idempotencyKey" | "delta" | "createdAt">;
73
+ //# sourceMappingURL=validators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,QAAQ;;;;;;;;+CAMpB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,MAAM,kEAAyB,CAAC;AAE7C,mFAAmF;AACnF,eAAO,MAAM,WAAW;;;;;;;;;;;;oEAKvB,CAAC;AAEF,oFAAoF;AACpF,eAAO,MAAM,SAAS,oNAAiE,CAAC;AAExF;;;GAGG;AACH,eAAO,MAAM,WAAW;;;;;;;;yCAItB,CAAC;AAEH,gEAAgE;AAChE,eAAO,MAAM,YAAY;;;;;;qCAGvB,CAAC;AAEH,yEAAyE;AACzE,eAAO,MAAM,SAAS;;;;;;;;;;;;gFAMpB,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { v } from "convex/values";
2
+ /**
3
+ * Optional per-call time-regen rule, injected by the client from its
4
+ * per-currency config. The component itself is config-less. `max`, when set,
5
+ * is the hard ceiling on stored balance for the currency — credits are clamped
6
+ * to it (see {@link mutations}).
7
+ */
8
+ export const regenArg = v.optional(v.object({
9
+ amount: v.number(),
10
+ intervalMs: v.number(),
11
+ cap: v.number(),
12
+ }));
13
+ /**
14
+ * Optional per-currency hard ceiling on the stored balance. A credit that would
15
+ * carry the balance above `max` is clamped to `max`; an `earn`/`grant`/receive
16
+ * leg that is ALREADY at or above `max` is a no-op credit (idempotent ceiling).
17
+ */
18
+ export const maxArg = v.optional(v.number());
19
+ /** Per-currency regen map for the multi-currency {@link queries.balances} read. */
20
+ export const regenMapArg = v.optional(v.record(v.string(), v.object({ amount: v.number(), intervalMs: v.number(), cap: v.number() })));
21
+ /** Stable, code-tagged failure reasons. `reason` (free text) is the ledger note. */
22
+ export const errorCode = v.union(v.literal("INSUFFICIENT"), v.literal("SELF_TRANSFER"));
23
+ /**
24
+ * Result of a spend/transfer that may be rejected. `code` carries a stable
25
+ * machine-readable tag when `ok` is false; it is absent on success.
26
+ */
27
+ export const spendResult = v.object({
28
+ ok: v.boolean(),
29
+ balance: v.number(),
30
+ code: v.optional(errorCode),
31
+ });
32
+ /** A single currency balance in the multi-currency overview. */
33
+ export const balanceEntry = v.object({
34
+ currency: v.string(),
35
+ amount: v.number(),
36
+ });
37
+ /** A ledger row projected for history reads (sandbox fields omitted). */
38
+ export const ledgerDoc = v.object({
39
+ currency: v.string(),
40
+ delta: v.number(),
41
+ reason: v.string(),
42
+ idempotencyKey: v.optional(v.string()),
43
+ createdAt: v.number(),
44
+ });
45
+ //# sourceMappingURL=validators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAChC,CAAC,CAAC,MAAM,CA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,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;IACjB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;IAClB,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACtC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Optional, tree-shakeable React front-tooling for `@vllnt/convex-wallet`.
3
+ *
4
+ * Thin reactive hooks over `useQuery` from `convex/react`. Each hook takes the
5
+ * HOST's re-exported wallet function reference plus its args — the component
6
+ * never imports the host `api`. `react` and `convex/react` are optional peer
7
+ * deps: a backend-only consumer pulls none of this code.
8
+ */
9
+ import type { FunctionReference } from "convex/server";
10
+ import type { BalanceEntry } from "../client/types.js";
11
+ /**
12
+ * Reactive single-currency balance for `subjectRef`.
13
+ *
14
+ * @param balanceRef - The host's re-exported `wallet.balance` query reference.
15
+ * @param args - `{ subjectRef, currency }` opaque identity + currency string.
16
+ * @returns The current balance, or `undefined` while the query loads.
17
+ */
18
+ export declare function useBalance(balanceRef: FunctionReference<"query", "public", {
19
+ subjectRef: string;
20
+ currency: string;
21
+ }, number>, args: {
22
+ subjectRef: string;
23
+ currency: string;
24
+ }): number | undefined;
25
+ /**
26
+ * Reactive multi-currency overview for `subjectRef`.
27
+ *
28
+ * @param balancesRef - The host's re-exported `wallet.balances` query reference.
29
+ * @param args - `{ subjectRef }` opaque identity.
30
+ * @returns The balances array, or `undefined` while the query loads.
31
+ */
32
+ export declare function useBalances(balancesRef: FunctionReference<"query", "public", {
33
+ subjectRef: string;
34
+ }, BalanceEntry[]>, args: {
35
+ subjectRef: string;
36
+ }): BalanceEntry[] | undefined;
37
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,UAAU,EAAE,iBAAiB,CAC3B,OAAO,EACP,QAAQ,EACR;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,EACxC,MAAM,CACP,EACD,IAAI,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC7C,MAAM,GAAG,SAAS,CAEpB;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,WAAW,EAAE,iBAAiB,CAC5B,OAAO,EACP,QAAQ,EACR;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,EACtB,YAAY,EAAE,CACf,EACD,IAAI,EAAE;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,GAC3B,YAAY,EAAE,GAAG,SAAS,CAE5B"}