@vllnt/convex-wallet 0.1.0 → 0.1.1-canary.3b72732
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.
- package/README.md +112 -33
- package/dist/component/crons.js +2 -2
- package/dist/component/crons.js.map +1 -1
- package/dist/component/internal_mutations.js +1 -1
- package/dist/component/internal_mutations.js.map +1 -1
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +44 -10
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.js +3 -3
- package/dist/component/queries.js.map +1 -1
- package/dist/shared.d.ts +32 -4
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +48 -5
- package/dist/shared.js.map +1 -1
- package/package.json +32 -21
- package/src/component/crons.ts +2 -2
- package/src/component/internal_mutations.ts +1 -1
- package/src/component/mutations.ts +65 -11
- package/src/component/queries.ts +3 -3
- package/src/shared.ts +57 -5
- package/src/test.ts +2 -1
- package/src/component/component.test.ts +0 -112
- package/src/react/index.test.tsx +0 -61
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<!-- Badges -->
|
|
2
|
+
|
|
2
3
|
[](https://www.convex.dev/components)
|
|
3
4
|
[](https://www.npmjs.com/package/@vllnt/convex-wallet)
|
|
4
5
|
[](https://github.com/vllnt/convex-wallet/actions/workflows/ci.yml)
|
|
@@ -6,11 +7,15 @@
|
|
|
6
7
|
|
|
7
8
|
# @vllnt/convex-wallet
|
|
8
9
|
|
|
9
|
-
Consumable balances and economy ledger with atomic spend and lazy regen, as a
|
|
10
|
+
Consumable balances and economy ledger with atomic spend and lazy regen, as a
|
|
11
|
+
Convex component — hold per-subject balances of any opaque currency (coins,
|
|
12
|
+
energy, gems, credits).
|
|
10
13
|
|
|
11
14
|
```ts
|
|
12
15
|
const wallet = new Wallet(components.wallet, {
|
|
13
|
-
currencies: {
|
|
16
|
+
currencies: {
|
|
17
|
+
energy: { regen: { amount: 1, intervalMs: 10_000, cap: 100 } },
|
|
18
|
+
},
|
|
14
19
|
});
|
|
15
20
|
await wallet.spend(ctx, subjectRef, "coins", 5, "purchase"); // atomic, never negative
|
|
16
21
|
const energy = await wallet.balance(ctx, subjectRef, "energy"); // regen-aware
|
|
@@ -18,12 +23,20 @@ const energy = await wallet.balance(ctx, subjectRef, "energy"); // regen-aware
|
|
|
18
23
|
|
|
19
24
|
## Features
|
|
20
25
|
|
|
21
|
-
- **Atomic spend** — debits ride the Convex mutation transaction; never goes
|
|
22
|
-
|
|
23
|
-
- **
|
|
24
|
-
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
- **Atomic spend** — debits ride the Convex mutation transaction; never goes
|
|
27
|
+
negative, never double-spends.
|
|
28
|
+
- **Abuse-guarded amounts** — every value-moving call rejects non-finite,
|
|
29
|
+
non-positive or above-2^53 amounts (`INVALID_AMOUNT`), overflowing balances
|
|
30
|
+
(`BALANCE_OVERFLOW`), empty identifiers (`INVALID_ARGUMENT`) and a reused
|
|
31
|
+
idempotency key across credit and debit (`IDEMPOTENCY_KEY_REUSED`).
|
|
32
|
+
- **Typed ledger** — every earn / spend / transfer records a signed `delta` (the
|
|
33
|
+
actual change), `reason`, and timestamp.
|
|
34
|
+
- **Idempotent grants & spend** — credit/debit exactly once per
|
|
35
|
+
`idempotencyKey`, scoped per `(subjectRef, currency)`.
|
|
36
|
+
- **Lazy time-regen** — energy-style currencies regenerate on read from the
|
|
37
|
+
server clock, no cron.
|
|
38
|
+
- **Per-currency `max` ceiling, multi-currency, transfers** — with a
|
|
39
|
+
`SELF_TRANSFER` guard.
|
|
27
40
|
- **Typed error codes, self-pruning ledger cron, opaque host refs.**
|
|
28
41
|
|
|
29
42
|
## Installation
|
|
@@ -32,7 +45,9 @@ const energy = await wallet.balance(ctx, subjectRef, "energy"); // regen-aware
|
|
|
32
45
|
pnpm add @vllnt/convex-wallet
|
|
33
46
|
```
|
|
34
47
|
|
|
35
|
-
|
|
48
|
+
Node.js >=18; required peer: `convex@^1.45.0`. React >=18 is optional. The
|
|
49
|
+
default npm channel is stable; use `@vllnt/convex-wallet@canary` for the current
|
|
50
|
+
prerelease surface documented here.
|
|
36
51
|
|
|
37
52
|
## Usage
|
|
38
53
|
|
|
@@ -63,7 +78,8 @@ const wallet = new Wallet(components.wallet, {
|
|
|
63
78
|
|
|
64
79
|
export const buy = mutation({
|
|
65
80
|
args: { userId: v.string(), cost: v.number() },
|
|
66
|
-
handler: (ctx, { userId, cost }) =>
|
|
81
|
+
handler: (ctx, { userId, cost }) =>
|
|
82
|
+
wallet.spend(ctx, userId, "coins", cost, "purchase"),
|
|
67
83
|
});
|
|
68
84
|
|
|
69
85
|
// Call AFTER the host has verified the IAP — idempotent on the receipt id.
|
|
@@ -79,42 +95,85 @@ export const energy = query({
|
|
|
79
95
|
});
|
|
80
96
|
```
|
|
81
97
|
|
|
98
|
+
The wrappers above illustrate calls only: authenticate the caller, authorize the
|
|
99
|
+
subject, and derive costs and credit amounts from trusted host data before use.
|
|
100
|
+
A comment about payment verification is not an authorization check. After
|
|
101
|
+
mounting, run `pnpm convex dev` to generate the host's `components` references.
|
|
102
|
+
|
|
103
|
+
## Configuration and limits
|
|
104
|
+
|
|
105
|
+
`WalletOptions` supports only `currencies` (default `{}`: static, uncapped) and
|
|
106
|
+
`defaultHistoryLimit` (default `50`, integer 1–1000). Currency rules accept
|
|
107
|
+
optional `regen: { amount, intervalMs, cap }` and `max`. There are no client
|
|
108
|
+
retention or idempotency-TTL options. The internal hourly sweep uses 90-day
|
|
109
|
+
ledger retention and a 7-day key-expiry threshold; bounded cleanup may lag those
|
|
110
|
+
thresholds. Replay protection ends when the key or ledger row is removed.
|
|
111
|
+
Earn/grant and spend share keys per subject/currency; transfer has no replay
|
|
112
|
+
key.
|
|
113
|
+
|
|
114
|
+
Regen is projected when a query executes, not pushed by a clock-driven
|
|
115
|
+
subscription. Transfers clamp the receiver's credit to `max`, so a capped
|
|
116
|
+
transfer need not preserve the combined balance. This is a consumable wallet,
|
|
117
|
+
not payment processing.
|
|
118
|
+
|
|
119
|
+
## Multiple mounts
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
app.use(wallet, { name: "webWallet" });
|
|
123
|
+
app.use(wallet, { name: "gameWallet" });
|
|
124
|
+
// new Wallet(components.webWallet), new Wallet(components.gameWallet)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Each mount has isolated balances, ledger rows and maintenance.
|
|
128
|
+
|
|
82
129
|
## API Reference
|
|
83
130
|
|
|
84
|
-
| Method
|
|
85
|
-
|
|
86
|
-
| `earn(ctx, subjectRef, currency, amount, reason, opts?)`
|
|
87
|
-
| `grant(ctx, subjectRef, currency, amount, reason, idempotencyKey)` | mutation | `{ balance }` (credited once per key)
|
|
88
|
-
| `spend(ctx, subjectRef, currency, amount, reason, opts?)`
|
|
89
|
-
| `transfer(ctx, fromRef, toRef, currency, amount, reason)`
|
|
90
|
-
| `balance(ctx, subjectRef, currency)`
|
|
91
|
-
| `balances(ctx, subjectRef)`
|
|
92
|
-
| `history(ctx, subjectRef, currency, limit?)`
|
|
131
|
+
| Method | Kind | Result |
|
|
132
|
+
| ------------------------------------------------------------------ | -------- | ------------------------------------------------------------------------------ |
|
|
133
|
+
| `earn(ctx, subjectRef, currency, amount, reason, opts?)` | mutation | `{ balance }` (clamped to `max`) |
|
|
134
|
+
| `grant(ctx, subjectRef, currency, amount, reason, idempotencyKey)` | mutation | `{ balance }` (credited once per key) |
|
|
135
|
+
| `spend(ctx, subjectRef, currency, amount, reason, opts?)` | mutation | `{ ok, balance, code? }` |
|
|
136
|
+
| `transfer(ctx, fromRef, toRef, currency, amount, reason)` | mutation | `{ ok, balance, code? }` |
|
|
137
|
+
| `balance(ctx, subjectRef, currency)` | query | `number` (regen-aware) |
|
|
138
|
+
| `balances(ctx, subjectRef)` | query | `{ currency, amount }[]` |
|
|
139
|
+
| `history(ctx, subjectRef, currency, limit?)` | query | `LedgerEntry[]` (newest-first; `limit` must be an integer from 1 through 1000) |
|
|
93
140
|
|
|
94
|
-
Full reference: [docs/API.md](docs/API.md) — including per-currency config,
|
|
141
|
+
Full reference: [docs/API.md](docs/API.md) — including per-currency config,
|
|
142
|
+
error codes (`INVALID_AMOUNT`, `INVALID_REGEN`, `INVALID_LIMIT`, `INSUFFICIENT`,
|
|
143
|
+
`SELF_TRANSFER`), and the regen model.
|
|
95
144
|
|
|
96
145
|
## React
|
|
97
146
|
|
|
98
|
-
Optional, tree-shakeable hooks at `@vllnt/convex-wallet/react`; `react` is an
|
|
147
|
+
Optional, tree-shakeable hooks at `@vllnt/convex-wallet/react`; `react` is an
|
|
148
|
+
optional peer dep. Pass the host's own re-exported query refs — the component
|
|
149
|
+
never imports your `api`.
|
|
99
150
|
|
|
100
151
|
```tsx
|
|
101
152
|
import { useBalance, useBalances } from "@vllnt/convex-wallet/react";
|
|
102
153
|
import { api } from "../convex/_generated/api";
|
|
103
154
|
|
|
104
|
-
|
|
105
|
-
|
|
155
|
+
// Host queries must accept these exact arguments; these are separate wrappers
|
|
156
|
+
// from the userId-based query above, and must authorize subjectRef.
|
|
157
|
+
const energy = useBalance(api.wallet.balance, {
|
|
158
|
+
subjectRef: userId,
|
|
159
|
+
currency: "energy",
|
|
160
|
+
});
|
|
161
|
+
const all = useBalances(api.wallet.balances, { subjectRef: userId });
|
|
106
162
|
```
|
|
107
163
|
|
|
108
|
-
| Hook
|
|
109
|
-
|
|
110
|
-
| `useBalance(balanceRef, { subjectRef, currency })` | host's `balance` query ref
|
|
111
|
-
| `useBalances(balancesRef, { subjectRef })`
|
|
164
|
+
| Hook | Args | Result |
|
|
165
|
+
| -------------------------------------------------- | --------------------------- | ----------------------------- |
|
|
166
|
+
| `useBalance(balanceRef, { subjectRef, currency })` | host's `balance` query ref | `number \| undefined` |
|
|
167
|
+
| `useBalances(balancesRef, { subjectRef })` | host's `balances` query ref | `BalanceEntry[] \| undefined` |
|
|
112
168
|
|
|
113
169
|
## Security
|
|
114
170
|
|
|
115
|
-
- Auth- and payment-agnostic — the host gates access and verifies any IAP /
|
|
116
|
-
|
|
117
|
-
-
|
|
171
|
+
- Auth- and payment-agnostic — the host gates access and verifies any IAP /
|
|
172
|
+
Stripe purchase before calling `grant`.
|
|
173
|
+
- Tables are sandboxed; spend and transfer are atomic, so balances never go
|
|
174
|
+
negative and grants never double-credit.
|
|
175
|
+
- Amounts and regen configs are validated, and time is server-sourced — a forged
|
|
176
|
+
or replayed clock can't drive extra regen.
|
|
118
177
|
|
|
119
178
|
See [docs/API.md](docs/API.md).
|
|
120
179
|
|
|
@@ -125,7 +184,25 @@ pnpm test # single run
|
|
|
125
184
|
pnpm test:coverage # enforced 100% on covered files
|
|
126
185
|
```
|
|
127
186
|
|
|
128
|
-
Tests
|
|
187
|
+
Tests use the simulated `convex-test` backend (`@edge-runtime/vm`), not a real
|
|
188
|
+
Convex deployment; they do not prove production concurrency or scheduler
|
|
189
|
+
delivery.
|
|
190
|
+
|
|
191
|
+
### Testing your app
|
|
192
|
+
|
|
193
|
+
`@vllnt/convex-wallet/test` exports `register(t, name = "wallet")` to mount the
|
|
194
|
+
component in a `convex-test` instance. It needs `convex-test` and `vitest` (with
|
|
195
|
+
Vite's `import.meta.glob`) in the consuming app:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
/// <reference types="vite/client" />
|
|
199
|
+
import { convexTest } from "convex-test";
|
|
200
|
+
import { register } from "@vllnt/convex-wallet/test";
|
|
201
|
+
import schema from "./schema";
|
|
202
|
+
|
|
203
|
+
const t = convexTest(schema, import.meta.glob("./**/*.ts"));
|
|
204
|
+
register(t);
|
|
205
|
+
```
|
|
129
206
|
|
|
130
207
|
## Contributing
|
|
131
208
|
|
|
@@ -133,9 +210,11 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
|
133
210
|
|
|
134
211
|
## Author
|
|
135
212
|
|
|
136
|
-
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
213
|
+
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
214
|
+
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
137
215
|
|
|
138
|
-
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
216
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
217
|
+
[vllnt.com](https://vllnt.com)
|
|
139
218
|
|
|
140
219
|
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
141
220
|
|
package/dist/component/crons.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cronJobs } from "convex/server";
|
|
2
|
-
import { internal } from "./_generated/api";
|
|
3
|
-
import { DEFAULT_IDEMPOTENCY_TTL_MS, DEFAULT_LEDGER_RETENTION_MS, } from "../shared";
|
|
2
|
+
import { internal } from "./_generated/api.js";
|
|
3
|
+
import { DEFAULT_IDEMPOTENCY_TTL_MS, DEFAULT_LEDGER_RETENTION_MS, } from "../shared.js";
|
|
4
4
|
/**
|
|
5
5
|
* The wallet's own scheduled maintenance. Scheduling lives INSIDE the component
|
|
6
6
|
* (never the host) and the swept mutation is idempotent (at-least-once safe).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crons.js","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"crons.js","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EACL,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,cAAc,CAAC;AAEtB;;;;;;;;GAQG;AACH,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;AAEzB,KAAK,CAAC,QAAQ,CACZ,qBAAqB,EACrB,EAAE,KAAK,EAAE,CAAC,EAAE,EACZ,QAAQ,CAAC,kBAAkB,CAAC,WAAW,EACvC;IACE,WAAW,EAAE,2BAA2B;IACxC,gBAAgB,EAAE,0BAA0B;CAC7C,CACF,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal_mutations.js","sourceRoot":"","sources":["../../src/component/internal_mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"internal_mutations.js","sourceRoot":"","sources":["../../src/component/internal_mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,8EAA8E;AAC9E,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,gBAAgB,CAAC;IAC1C,IAAI,EAAE;QACJ,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;QACvB,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE;KAC7B;IACD,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC;IAC/D,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,YAAY,GAAG,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC;QAC5C,MAAM,YAAY,GAAG,GAAG,GAAG,IAAI,CAAC,gBAAgB,CAAC;QAEjD,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,EAAE;aACxB,KAAK,C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oot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAmFA;;;;;GAKG;AACH,eAAO,MAAM,IAAI;;;;;;;;;;;;;;GA2Ef,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;;UAqB0B,cAAc;;;;;GA4CxD,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;UA2B6B,eAAe;;;;UAGrB,cAAc;;;;;GAmExD,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
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";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
import { applyRegen, assertNonEmpty, assertPositiveAmount, assertSafeBalance, assertValidMax, fail, IDEMPOTENCY_KEY_REUSED, } from "../shared.js";
|
|
4
|
+
import { maxArg, regenArg, spendResult } from "./validators.js";
|
|
5
5
|
const balanceObject = v.object({ balance: v.number() });
|
|
6
6
|
/**
|
|
7
7
|
* Find the prior ledger row for a replayed `idempotencyKey`, scoped to this
|
|
@@ -17,9 +17,34 @@ async function findIdempotent(ctx, subjectRef, currency, idempotencyKey) {
|
|
|
17
17
|
.eq("idempotencyKey", idempotencyKey))
|
|
18
18
|
.first();
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Clamp a post-credit balance to an optional per-currency `max` ceiling. A
|
|
22
|
+
* credit never lowers the balance: when `before` is already above `max` (the
|
|
23
|
+
* host lowered it), the balance is left as is and the credit is a no-op.
|
|
24
|
+
* Throws `BALANCE_OVERFLOW` past 2^53 - 1.
|
|
25
|
+
*/
|
|
26
|
+
function clampToMax(before, credit, max) {
|
|
27
|
+
const after = before + credit;
|
|
28
|
+
const clamped = max === undefined ? after : Math.max(before, Math.min(after, max));
|
|
29
|
+
assertSafeBalance(clamped);
|
|
30
|
+
return clamped;
|
|
31
|
+
}
|
|
32
|
+
/** Validate the string identifiers shared by every value-moving mutation. */
|
|
33
|
+
function assertIdentifiers(args, subjects) {
|
|
34
|
+
for (const [name, value] of Object.entries(subjects)) {
|
|
35
|
+
assertNonEmpty(name, value);
|
|
36
|
+
}
|
|
37
|
+
assertNonEmpty("currency", args.currency);
|
|
38
|
+
assertNonEmpty("reason", args.reason);
|
|
39
|
+
if (args.idempotencyKey !== undefined) {
|
|
40
|
+
assertNonEmpty("idempotencyKey", args.idempotencyKey);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** A recorded key may only be replayed by the same direction (credit vs debit). */
|
|
44
|
+
function assertSameDirection(seen, debit) {
|
|
45
|
+
if (seen.delta < 0 !== debit) {
|
|
46
|
+
fail(IDEMPOTENCY_KEY_REUSED, "idempotencyKey was already used by a " + (debit ? "credit" : "debit"));
|
|
47
|
+
}
|
|
23
48
|
}
|
|
24
49
|
/**
|
|
25
50
|
* Credit `amount` of `currency` to `subjectRef`. When `idempotencyKey` is
|
|
@@ -39,7 +64,9 @@ export const earn = mutation({
|
|
|
39
64
|
},
|
|
40
65
|
returns: balanceObject,
|
|
41
66
|
handler: async (ctx, args) => {
|
|
67
|
+
assertIdentifiers(args, { subjectRef: args.subjectRef });
|
|
42
68
|
assertPositiveAmount(args.amount);
|
|
69
|
+
assertValidMax(args.max);
|
|
43
70
|
const now = Date.now();
|
|
44
71
|
const row = await ctx.db
|
|
45
72
|
.query("balances")
|
|
@@ -50,7 +77,7 @@ export const earn = mutation({
|
|
|
50
77
|
if (row === null) {
|
|
51
78
|
// No balance row → no recorded key for it can exist, so the idempotency
|
|
52
79
|
// check is unnecessary: this is always a first credit.
|
|
53
|
-
balance = clampToMax(args.amount, args.max);
|
|
80
|
+
balance = clampToMax(0, args.amount, args.max);
|
|
54
81
|
delta = balance;
|
|
55
82
|
await ctx.db.insert("balances", {
|
|
56
83
|
subjectRef: args.subjectRef,
|
|
@@ -65,6 +92,7 @@ export const earn = mutation({
|
|
|
65
92
|
if (args.idempotencyKey !== undefined) {
|
|
66
93
|
const seen = await findIdempotent(ctx, args.subjectRef, args.currency, args.idempotencyKey);
|
|
67
94
|
if (seen !== null) {
|
|
95
|
+
assertSameDirection(seen, false);
|
|
68
96
|
// Replay: return the current (regen-aware) balance, no double-credit.
|
|
69
97
|
return {
|
|
70
98
|
balance: applyRegen(row.amount, row.lastRegenAt, now, args.regen).amount,
|
|
@@ -72,8 +100,9 @@ export const earn = mutation({
|
|
|
72
100
|
}
|
|
73
101
|
}
|
|
74
102
|
const r = applyRegen(row.amount, row.lastRegenAt, now, args.regen);
|
|
75
|
-
balance = clampToMax(r.amount
|
|
103
|
+
balance = clampToMax(r.amount, args.amount, args.max);
|
|
76
104
|
delta = balance - r.amount;
|
|
105
|
+
assertSafeBalance(row.lifetimeEarned + delta);
|
|
77
106
|
await ctx.db.patch("balances", row._id, {
|
|
78
107
|
amount: balance,
|
|
79
108
|
lastRegenAt: r.lastRegenAt,
|
|
@@ -107,6 +136,7 @@ export const spend = mutation({
|
|
|
107
136
|
},
|
|
108
137
|
returns: spendResult,
|
|
109
138
|
handler: async (ctx, args) => {
|
|
139
|
+
assertIdentifiers(args, { subjectRef: args.subjectRef });
|
|
110
140
|
assertPositiveAmount(args.amount);
|
|
111
141
|
const now = Date.now();
|
|
112
142
|
const row = await ctx.db
|
|
@@ -119,6 +149,7 @@ export const spend = mutation({
|
|
|
119
149
|
if (args.idempotencyKey !== undefined) {
|
|
120
150
|
const seen = await findIdempotent(ctx, args.subjectRef, args.currency, args.idempotencyKey);
|
|
121
151
|
if (seen !== null) {
|
|
152
|
+
assertSameDirection(seen, true);
|
|
122
153
|
// Replay: return the current (regen-aware) balance, no double-debit.
|
|
123
154
|
return {
|
|
124
155
|
ok: true,
|
|
@@ -172,7 +203,9 @@ export const transfer = mutation({
|
|
|
172
203
|
},
|
|
173
204
|
returns: spendResult,
|
|
174
205
|
handler: async (ctx, args) => {
|
|
206
|
+
assertIdentifiers(args, { fromRef: args.fromRef, toRef: args.toRef });
|
|
175
207
|
assertPositiveAmount(args.amount);
|
|
208
|
+
assertValidMax(args.max);
|
|
176
209
|
const now = Date.now();
|
|
177
210
|
const fromRow = await ctx.db
|
|
178
211
|
.query("balances")
|
|
@@ -217,7 +250,7 @@ export const transfer = mutation({
|
|
|
217
250
|
.unique();
|
|
218
251
|
let receiverDelta;
|
|
219
252
|
if (toRow === null) {
|
|
220
|
-
const credited = clampToMax(args.amount, args.max);
|
|
253
|
+
const credited = clampToMax(0, args.amount, args.max);
|
|
221
254
|
receiverDelta = credited;
|
|
222
255
|
await ctx.db.insert("balances", {
|
|
223
256
|
subjectRef: args.toRef,
|
|
@@ -230,8 +263,9 @@ export const transfer = mutation({
|
|
|
230
263
|
}
|
|
231
264
|
else {
|
|
232
265
|
const toR = applyRegen(toRow.amount, toRow.lastRegenAt, now, args.regen);
|
|
233
|
-
const credited = clampToMax(toR.amount
|
|
266
|
+
const credited = clampToMax(toR.amount, args.amount, args.max);
|
|
234
267
|
receiverDelta = credited - toR.amount;
|
|
268
|
+
assertSafeBalance(toRow.lifetimeEarned + receiverDelta);
|
|
235
269
|
await ctx.db.patch("balances", toRow._id, {
|
|
236
270
|
amount: credited,
|
|
237
271
|
lastRegenAt: toR.lastRegenAt,
|
|
@@ -1 +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,EA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,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"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
-
import { query } from "./_generated/server";
|
|
3
|
-
import { applyRegen } from "../shared";
|
|
4
|
-
import { balanceEntry, ledgerDoc, regenArg, regenMapArg } from "./validators";
|
|
2
|
+
import { query } from "./_generated/server.js";
|
|
3
|
+
import { applyRegen } from "../shared.js";
|
|
4
|
+
import { balanceEntry, ledgerDoc, regenArg, regenMapArg } from "./validators.js";
|
|
5
5
|
const MAX_HISTORY_LIMIT = 1000;
|
|
6
6
|
function validateHistoryLimit(limit) {
|
|
7
7
|
if (!Number.isFinite(limit) ||
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEjF,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAE/B,SAAS,oBAAoB,CAAC,KAAa;IACzC,IACE,CAAC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jC,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,EAAE;aACtB,KAAK,CAAC,QAAQ,CAAC;aACf,SAAS,CAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,EAAE,CACtC,CAAC,CAAC,EAAE,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAClE;aACA,KAAK,CAAC,MAAM,CAAC;aACb,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACxB,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,cAAc,EAAE,GAAG,CAAC,cAAc;YAClC,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC,CAAC,CAAC;IACN,CAAC;CACF,CAAC,CAAC"}
|
package/dist/shared.d.ts
CHANGED
|
@@ -48,12 +48,40 @@ export interface RegenConfig {
|
|
|
48
48
|
cap: number;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
|
|
51
|
+
* Thrown (as a `ConvexError` with `data.code`) when a required string argument
|
|
52
|
+
* (`subjectRef`, `currency`, `reason`, `idempotencyKey`) is empty.
|
|
53
|
+
*/
|
|
54
|
+
export declare const INVALID_ARGUMENT = "INVALID_ARGUMENT";
|
|
55
|
+
/** Thrown when a per-currency `max` ceiling is `NaN` or negative. */
|
|
56
|
+
export declare const INVALID_MAX = "INVALID_MAX";
|
|
57
|
+
/**
|
|
58
|
+
* Thrown when a credit would push a balance (or lifetime counter) past
|
|
59
|
+
* `Number.MAX_SAFE_INTEGER` (2^53 - 1), where doubles stop being exact.
|
|
60
|
+
*/
|
|
61
|
+
export declare const BALANCE_OVERFLOW = "BALANCE_OVERFLOW";
|
|
62
|
+
/**
|
|
63
|
+
* Thrown when an `idempotencyKey` already recorded for a `(subjectRef,
|
|
64
|
+
* currency)` by a credit is reused on a debit, or the reverse. Silently
|
|
65
|
+
* treating it as a replay would drop the second operation.
|
|
66
|
+
*/
|
|
67
|
+
export declare const IDEMPOTENCY_KEY_REUSED = "IDEMPOTENCY_KEY_REUSED";
|
|
68
|
+
/** Throw a `ConvexError` carrying a stable `code` (same shape as `INVALID_LIMIT`). */
|
|
69
|
+
export declare function fail(code: string, message: string): never;
|
|
70
|
+
/** Reject an empty string for a required argument. Throws `INVALID_ARGUMENT`. */
|
|
71
|
+
export declare function assertNonEmpty(name: string, value: string): void;
|
|
72
|
+
/** Reject a `NaN` or negative per-currency `max`. Throws `INVALID_MAX`. */
|
|
73
|
+
export declare function assertValidMax(max: number | undefined): void;
|
|
74
|
+
/** Reject a balance/counter above 2^53 - 1. Throws `BALANCE_OVERFLOW`. */
|
|
75
|
+
export declare function assertSafeBalance(value: number): void;
|
|
76
|
+
/**
|
|
77
|
+
* Guard every value-moving entry point. Rejects non-finite (`NaN`, `Infinity`),
|
|
78
|
+
* non-positive (`0`, negative) and above-`Number.MAX_SAFE_INTEGER` amounts - a
|
|
79
|
+
* negative `spend` would otherwise MINT funds, `NaN` would corrupt a balance,
|
|
80
|
+
* and past 2^53 doubles silently lose precision. Throws `INVALID_AMOUNT`.
|
|
81
|
+
* Fractional amounts are accepted; hosts should use integer minor units.
|
|
54
82
|
*
|
|
55
83
|
* @param amount - the caller-supplied amount to credit or debit.
|
|
56
|
-
* @throws when `amount` is not a finite number
|
|
84
|
+
* @throws when `amount` is not a finite number in `(0, 2^53 - 1]`.
|
|
57
85
|
*/
|
|
58
86
|
export declare function assertPositiveAmount(amount: number): void;
|
|
59
87
|
/**
|
package/dist/shared.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"
|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,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAIhE;AAED,2EAA2E;AAC3E,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAI5D;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAIrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAIzD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,WAAW,GAClB;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAqBzC"}
|
package/dist/shared.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ConvexError } from "convex/values";
|
|
1
2
|
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
3
|
export const COMPONENT_NAME = "wallet";
|
|
3
4
|
/** Default newest-first page size for {@link history} when the caller omits one. */
|
|
@@ -35,15 +36,57 @@ export const INVALID_REGEN = "INVALID_REGEN";
|
|
|
35
36
|
*/
|
|
36
37
|
export const INVALID_AMOUNT = "INVALID_AMOUNT";
|
|
37
38
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
39
|
+
* Thrown (as a `ConvexError` with `data.code`) when a required string argument
|
|
40
|
+
* (`subjectRef`, `currency`, `reason`, `idempotencyKey`) is empty.
|
|
41
|
+
*/
|
|
42
|
+
export const INVALID_ARGUMENT = "INVALID_ARGUMENT";
|
|
43
|
+
/** Thrown when a per-currency `max` ceiling is `NaN` or negative. */
|
|
44
|
+
export const INVALID_MAX = "INVALID_MAX";
|
|
45
|
+
/**
|
|
46
|
+
* Thrown when a credit would push a balance (or lifetime counter) past
|
|
47
|
+
* `Number.MAX_SAFE_INTEGER` (2^53 - 1), where doubles stop being exact.
|
|
48
|
+
*/
|
|
49
|
+
export const BALANCE_OVERFLOW = "BALANCE_OVERFLOW";
|
|
50
|
+
/**
|
|
51
|
+
* Thrown when an `idempotencyKey` already recorded for a `(subjectRef,
|
|
52
|
+
* currency)` by a credit is reused on a debit, or the reverse. Silently
|
|
53
|
+
* treating it as a replay would drop the second operation.
|
|
54
|
+
*/
|
|
55
|
+
export const IDEMPOTENCY_KEY_REUSED = "IDEMPOTENCY_KEY_REUSED";
|
|
56
|
+
/** Throw a `ConvexError` carrying a stable `code` (same shape as `INVALID_LIMIT`). */
|
|
57
|
+
export function fail(code, message) {
|
|
58
|
+
throw new ConvexError({ code, message });
|
|
59
|
+
}
|
|
60
|
+
/** Reject an empty string for a required argument. Throws `INVALID_ARGUMENT`. */
|
|
61
|
+
export function assertNonEmpty(name, value) {
|
|
62
|
+
if (value === "") {
|
|
63
|
+
fail(INVALID_ARGUMENT, `${name} must not be empty`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Reject a `NaN` or negative per-currency `max`. Throws `INVALID_MAX`. */
|
|
67
|
+
export function assertValidMax(max) {
|
|
68
|
+
if (max !== undefined && (Number.isNaN(max) || max < 0)) {
|
|
69
|
+
fail(INVALID_MAX, "max must be a number >= 0");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Reject a balance/counter above 2^53 - 1. Throws `BALANCE_OVERFLOW`. */
|
|
73
|
+
export function assertSafeBalance(value) {
|
|
74
|
+
if (value > Number.MAX_SAFE_INTEGER) {
|
|
75
|
+
fail(BALANCE_OVERFLOW, "balance would exceed Number.MAX_SAFE_INTEGER");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Guard every value-moving entry point. Rejects non-finite (`NaN`, `Infinity`),
|
|
80
|
+
* non-positive (`0`, negative) and above-`Number.MAX_SAFE_INTEGER` amounts - a
|
|
81
|
+
* negative `spend` would otherwise MINT funds, `NaN` would corrupt a balance,
|
|
82
|
+
* and past 2^53 doubles silently lose precision. Throws `INVALID_AMOUNT`.
|
|
83
|
+
* Fractional amounts are accepted; hosts should use integer minor units.
|
|
41
84
|
*
|
|
42
85
|
* @param amount - the caller-supplied amount to credit or debit.
|
|
43
|
-
* @throws when `amount` is not a finite number
|
|
86
|
+
* @throws when `amount` is not a finite number in `(0, 2^53 - 1]`.
|
|
44
87
|
*/
|
|
45
88
|
export function assertPositiveAmount(amount) {
|
|
46
|
-
if (!Number.isFinite(amount) || amount <= 0) {
|
|
89
|
+
if (!Number.isFinite(amount) || amount <= 0 || amount > Number.MAX_SAFE_INTEGER) {
|
|
47
90
|
throw new Error(INVALID_AMOUNT);
|
|
48
91
|
}
|
|
49
92
|
}
|
package/dist/shared.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,MAAM,CAAC,MAAM,cAAc,GAAG,QAAQ,CAAC;AAEvC,oFAAoF;AACpF,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAExC;;;GAGG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEpE;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAElE,2EAA2E;AAC3E,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAExD,oFAAoF;AACpF,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,2CAA2C;IAC3C,YAAY,EAAE,cAAc;IAC5B,gEAAgE;IAChE,aAAa,EAAE,eAAe;CACtB,CAAC;AAEX;;;;GAIG;A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,EAAE,WAAW,EAAE,CAAC;IACzC,CAAC;IACD,IACE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC;QAClC,KAAK,CAAC,UAAU,IAAI,CAAC;QACrB,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;QAC9B,KAAK,CAAC,MAAM,IAAI,CAAC;QACjB,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC;QAC3B,KAAK,CAAC,GAAG,IAAI,CAAC,EACd,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IACjC,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,WAAW,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;IACjE,OAAO,KAAK,KAAK,CAAC;QAChB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE;QACjC,CAAC,CAAC;YACE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;YAC5E,WAAW,EAAE,WAAW,GAAG,KAAK,GAAG,KAAK,CAAC,UAAU;SACpD,CAAC;AACR,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-wallet",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-canary.3b72732",
|
|
4
4
|
"description": "Consumable balances and economy ledger with atomic spend and lazy regen, as a Convex component",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"!src/**/*.test.*"
|
|
11
12
|
],
|
|
12
13
|
"exports": {
|
|
13
14
|
"./package.json": "./package.json",
|
|
@@ -41,17 +42,14 @@
|
|
|
41
42
|
"build": "tsc --project ./tsconfig.build.json",
|
|
42
43
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
43
44
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
-
"lint": "pnpm check:agents && eslint .",
|
|
45
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
46
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
47
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
47
48
|
"test": "vitest run --passWithNoTests",
|
|
48
49
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
50
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
50
51
|
"generate:llms": "node scripts/generate-llms.mjs",
|
|
51
52
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
52
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
53
|
-
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
54
|
-
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
55
53
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
56
54
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
57
55
|
},
|
|
@@ -67,22 +65,22 @@
|
|
|
67
65
|
"devDependencies": {
|
|
68
66
|
"@edge-runtime/vm": "^5.0.0",
|
|
69
67
|
"@testing-library/react": "^16.3.3",
|
|
70
|
-
"@types/react": "^19.
|
|
71
|
-
"@types/react-dom": "^19.
|
|
68
|
+
"@types/react": "^19.3.0",
|
|
69
|
+
"@types/react-dom": "^19.3.0",
|
|
72
70
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
73
|
-
"@vitest/coverage-v8": "^
|
|
71
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
74
72
|
"@vllnt/eslint-config": "2.0.0",
|
|
75
73
|
"@vllnt/typescript": "2.0.0",
|
|
76
|
-
"convex": "^1.
|
|
77
|
-
"convex-test": "^0.0.
|
|
74
|
+
"convex": "^1.48.0",
|
|
75
|
+
"convex-test": "^0.0.65",
|
|
78
76
|
"eslint": "^9.39.5",
|
|
79
|
-
"jsdom": "^
|
|
80
|
-
"prettier": "^3.9.
|
|
81
|
-
"react": "^19.
|
|
82
|
-
"react-dom": "^19.
|
|
77
|
+
"jsdom": "^30.1.3",
|
|
78
|
+
"prettier": "^3.9.10",
|
|
79
|
+
"react": "^19.3.1",
|
|
80
|
+
"react-dom": "^19.3.1",
|
|
83
81
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
84
|
-
"vite": "^8.
|
|
85
|
-
"vitest": "^
|
|
82
|
+
"vite": "^8.3.5",
|
|
83
|
+
"vitest": "^5.0.5"
|
|
86
84
|
},
|
|
87
85
|
"homepage": "https://github.com/vllnt/convex-wallet#readme",
|
|
88
86
|
"bugs": {
|
|
@@ -111,5 +109,18 @@
|
|
|
111
109
|
"convex",
|
|
112
110
|
"convex-component",
|
|
113
111
|
"wallet"
|
|
114
|
-
]
|
|
112
|
+
],
|
|
113
|
+
"pnpm": {
|
|
114
|
+
"overrides": {
|
|
115
|
+
"deepmerge-ts@<8.0.0": "8.0.2",
|
|
116
|
+
"handlebars@<4.7.10": "^4.7.10"
|
|
117
|
+
},
|
|
118
|
+
"minimumReleaseAge": 10080,
|
|
119
|
+
"minimumReleaseAgeExclude": [
|
|
120
|
+
"@vllnt/*"
|
|
121
|
+
],
|
|
122
|
+
"ignoredBuiltDependencies": [
|
|
123
|
+
"esbuild"
|
|
124
|
+
]
|
|
125
|
+
}
|
|
115
126
|
}
|
package/src/component/crons.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { cronJobs } from "convex/server";
|
|
2
|
-
import { internal } from "./_generated/api";
|
|
2
|
+
import { internal } from "./_generated/api.js";
|
|
3
3
|
import {
|
|
4
4
|
DEFAULT_IDEMPOTENCY_TTL_MS,
|
|
5
5
|
DEFAULT_LEDGER_RETENTION_MS,
|
|
6
|
-
} from "../shared";
|
|
6
|
+
} from "../shared.js";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* The wallet's own scheduled maintenance. Scheduling lives INSIDE the component
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
|
-
import type { DataModel } from "./_generated/dataModel";
|
|
2
|
+
import type { DataModel } from "./_generated/dataModel.js";
|
|
3
3
|
import type { GenericMutationCtx } from "convex/server";
|
|
4
|
-
import { mutation } from "./_generated/server";
|
|
5
|
-
import {
|
|
6
|
-
|
|
4
|
+
import { mutation } from "./_generated/server.js";
|
|
5
|
+
import {
|
|
6
|
+
applyRegen,
|
|
7
|
+
assertNonEmpty,
|
|
8
|
+
assertPositiveAmount,
|
|
9
|
+
assertSafeBalance,
|
|
10
|
+
assertValidMax,
|
|
11
|
+
fail,
|
|
12
|
+
IDEMPOTENCY_KEY_REUSED,
|
|
13
|
+
} from "../shared.js";
|
|
14
|
+
import { maxArg, regenArg, spendResult } from "./validators.js";
|
|
7
15
|
|
|
8
16
|
const balanceObject = v.object({ balance: v.number() });
|
|
9
17
|
|
|
@@ -31,9 +39,46 @@ async function findIdempotent(
|
|
|
31
39
|
.first();
|
|
32
40
|
}
|
|
33
41
|
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Clamp a post-credit balance to an optional per-currency `max` ceiling. A
|
|
44
|
+
* credit never lowers the balance: when `before` is already above `max` (the
|
|
45
|
+
* host lowered it), the balance is left as is and the credit is a no-op.
|
|
46
|
+
* Throws `BALANCE_OVERFLOW` past 2^53 - 1.
|
|
47
|
+
*/
|
|
48
|
+
function clampToMax(
|
|
49
|
+
before: number,
|
|
50
|
+
credit: number,
|
|
51
|
+
max: number | undefined,
|
|
52
|
+
): number {
|
|
53
|
+
const after = before + credit;
|
|
54
|
+
const clamped = max === undefined ? after : Math.max(before, Math.min(after, max));
|
|
55
|
+
assertSafeBalance(clamped);
|
|
56
|
+
return clamped;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Validate the string identifiers shared by every value-moving mutation. */
|
|
60
|
+
function assertIdentifiers(
|
|
61
|
+
args: { currency: string; reason: string; idempotencyKey?: string },
|
|
62
|
+
subjects: Record<string, string>,
|
|
63
|
+
): void {
|
|
64
|
+
for (const [name, value] of Object.entries(subjects)) {
|
|
65
|
+
assertNonEmpty(name, value);
|
|
66
|
+
}
|
|
67
|
+
assertNonEmpty("currency", args.currency);
|
|
68
|
+
assertNonEmpty("reason", args.reason);
|
|
69
|
+
if (args.idempotencyKey !== undefined) {
|
|
70
|
+
assertNonEmpty("idempotencyKey", args.idempotencyKey);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A recorded key may only be replayed by the same direction (credit vs debit). */
|
|
75
|
+
function assertSameDirection(seen: { delta: number }, debit: boolean): void {
|
|
76
|
+
if (seen.delta < 0 !== debit) {
|
|
77
|
+
fail(
|
|
78
|
+
IDEMPOTENCY_KEY_REUSED,
|
|
79
|
+
"idempotencyKey was already used by a " + (debit ? "credit" : "debit"),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
37
82
|
}
|
|
38
83
|
|
|
39
84
|
/**
|
|
@@ -54,7 +99,9 @@ export const earn = mutation({
|
|
|
54
99
|
},
|
|
55
100
|
returns: balanceObject,
|
|
56
101
|
handler: async (ctx, args) => {
|
|
102
|
+
assertIdentifiers(args, { subjectRef: args.subjectRef });
|
|
57
103
|
assertPositiveAmount(args.amount);
|
|
104
|
+
assertValidMax(args.max);
|
|
58
105
|
const now = Date.now();
|
|
59
106
|
const row = await ctx.db
|
|
60
107
|
.query("balances")
|
|
@@ -68,7 +115,7 @@ export const earn = mutation({
|
|
|
68
115
|
if (row === null) {
|
|
69
116
|
// No balance row → no recorded key for it can exist, so the idempotency
|
|
70
117
|
// check is unnecessary: this is always a first credit.
|
|
71
|
-
balance = clampToMax(args.amount, args.max);
|
|
118
|
+
balance = clampToMax(0, args.amount, args.max);
|
|
72
119
|
delta = balance;
|
|
73
120
|
await ctx.db.insert("balances", {
|
|
74
121
|
subjectRef: args.subjectRef,
|
|
@@ -87,6 +134,7 @@ export const earn = mutation({
|
|
|
87
134
|
args.idempotencyKey,
|
|
88
135
|
);
|
|
89
136
|
if (seen !== null) {
|
|
137
|
+
assertSameDirection(seen, false);
|
|
90
138
|
// Replay: return the current (regen-aware) balance, no double-credit.
|
|
91
139
|
return {
|
|
92
140
|
balance: applyRegen(row.amount, row.lastRegenAt, now, args.regen).amount,
|
|
@@ -94,8 +142,9 @@ export const earn = mutation({
|
|
|
94
142
|
}
|
|
95
143
|
}
|
|
96
144
|
const r = applyRegen(row.amount, row.lastRegenAt, now, args.regen);
|
|
97
|
-
balance = clampToMax(r.amount
|
|
145
|
+
balance = clampToMax(r.amount, args.amount, args.max);
|
|
98
146
|
delta = balance - r.amount;
|
|
147
|
+
assertSafeBalance(row.lifetimeEarned + delta);
|
|
99
148
|
await ctx.db.patch("balances", row._id, {
|
|
100
149
|
amount: balance,
|
|
101
150
|
lastRegenAt: r.lastRegenAt,
|
|
@@ -131,6 +180,7 @@ export const spend = mutation({
|
|
|
131
180
|
},
|
|
132
181
|
returns: spendResult,
|
|
133
182
|
handler: async (ctx, args) => {
|
|
183
|
+
assertIdentifiers(args, { subjectRef: args.subjectRef });
|
|
134
184
|
assertPositiveAmount(args.amount);
|
|
135
185
|
const now = Date.now();
|
|
136
186
|
const row = await ctx.db
|
|
@@ -150,6 +200,7 @@ export const spend = mutation({
|
|
|
150
200
|
args.idempotencyKey,
|
|
151
201
|
);
|
|
152
202
|
if (seen !== null) {
|
|
203
|
+
assertSameDirection(seen, true);
|
|
153
204
|
// Replay: return the current (regen-aware) balance, no double-debit.
|
|
154
205
|
return {
|
|
155
206
|
ok: true,
|
|
@@ -204,7 +255,9 @@ export const transfer = mutation({
|
|
|
204
255
|
},
|
|
205
256
|
returns: spendResult,
|
|
206
257
|
handler: async (ctx, args) => {
|
|
258
|
+
assertIdentifiers(args, { fromRef: args.fromRef, toRef: args.toRef });
|
|
207
259
|
assertPositiveAmount(args.amount);
|
|
260
|
+
assertValidMax(args.max);
|
|
208
261
|
const now = Date.now();
|
|
209
262
|
const fromRow = await ctx.db
|
|
210
263
|
.query("balances")
|
|
@@ -257,7 +310,7 @@ export const transfer = mutation({
|
|
|
257
310
|
.unique();
|
|
258
311
|
let receiverDelta: number;
|
|
259
312
|
if (toRow === null) {
|
|
260
|
-
const credited = clampToMax(args.amount, args.max);
|
|
313
|
+
const credited = clampToMax(0, args.amount, args.max);
|
|
261
314
|
receiverDelta = credited;
|
|
262
315
|
await ctx.db.insert("balances", {
|
|
263
316
|
subjectRef: args.toRef,
|
|
@@ -269,8 +322,9 @@ export const transfer = mutation({
|
|
|
269
322
|
});
|
|
270
323
|
} else {
|
|
271
324
|
const toR = applyRegen(toRow.amount, toRow.lastRegenAt, now, args.regen);
|
|
272
|
-
const credited = clampToMax(toR.amount
|
|
325
|
+
const credited = clampToMax(toR.amount, args.amount, args.max);
|
|
273
326
|
receiverDelta = credited - toR.amount;
|
|
327
|
+
assertSafeBalance(toRow.lifetimeEarned + receiverDelta);
|
|
274
328
|
await ctx.db.patch("balances", toRow._id, {
|
|
275
329
|
amount: credited,
|
|
276
330
|
lastRegenAt: toR.lastRegenAt,
|
package/src/component/queries.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
-
import { query } from "./_generated/server";
|
|
3
|
-
import { applyRegen } from "../shared";
|
|
4
|
-
import { balanceEntry, ledgerDoc, regenArg, regenMapArg } from "./validators";
|
|
2
|
+
import { query } from "./_generated/server.js";
|
|
3
|
+
import { applyRegen } from "../shared.js";
|
|
4
|
+
import { balanceEntry, ledgerDoc, regenArg, regenMapArg } from "./validators.js";
|
|
5
5
|
|
|
6
6
|
const MAX_HISTORY_LIMIT = 1000;
|
|
7
7
|
|
package/src/shared.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ConvexError } from "convex/values";
|
|
2
|
+
|
|
1
3
|
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
4
|
|
|
3
5
|
export const COMPONENT_NAME = "wallet";
|
|
@@ -60,15 +62,65 @@ export interface RegenConfig {
|
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
65
|
+
* Thrown (as a `ConvexError` with `data.code`) when a required string argument
|
|
66
|
+
* (`subjectRef`, `currency`, `reason`, `idempotencyKey`) is empty.
|
|
67
|
+
*/
|
|
68
|
+
export const INVALID_ARGUMENT = "INVALID_ARGUMENT";
|
|
69
|
+
|
|
70
|
+
/** Thrown when a per-currency `max` ceiling is `NaN` or negative. */
|
|
71
|
+
export const INVALID_MAX = "INVALID_MAX";
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Thrown when a credit would push a balance (or lifetime counter) past
|
|
75
|
+
* `Number.MAX_SAFE_INTEGER` (2^53 - 1), where doubles stop being exact.
|
|
76
|
+
*/
|
|
77
|
+
export const BALANCE_OVERFLOW = "BALANCE_OVERFLOW";
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Thrown when an `idempotencyKey` already recorded for a `(subjectRef,
|
|
81
|
+
* currency)` by a credit is reused on a debit, or the reverse. Silently
|
|
82
|
+
* treating it as a replay would drop the second operation.
|
|
83
|
+
*/
|
|
84
|
+
export const IDEMPOTENCY_KEY_REUSED = "IDEMPOTENCY_KEY_REUSED";
|
|
85
|
+
|
|
86
|
+
/** Throw a `ConvexError` carrying a stable `code` (same shape as `INVALID_LIMIT`). */
|
|
87
|
+
export function fail(code: string, message: string): never {
|
|
88
|
+
throw new ConvexError({ code, message });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Reject an empty string for a required argument. Throws `INVALID_ARGUMENT`. */
|
|
92
|
+
export function assertNonEmpty(name: string, value: string): void {
|
|
93
|
+
if (value === "") {
|
|
94
|
+
fail(INVALID_ARGUMENT, `${name} must not be empty`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Reject a `NaN` or negative per-currency `max`. Throws `INVALID_MAX`. */
|
|
99
|
+
export function assertValidMax(max: number | undefined): void {
|
|
100
|
+
if (max !== undefined && (Number.isNaN(max) || max < 0)) {
|
|
101
|
+
fail(INVALID_MAX, "max must be a number >= 0");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Reject a balance/counter above 2^53 - 1. Throws `BALANCE_OVERFLOW`. */
|
|
106
|
+
export function assertSafeBalance(value: number): void {
|
|
107
|
+
if (value > Number.MAX_SAFE_INTEGER) {
|
|
108
|
+
fail(BALANCE_OVERFLOW, "balance would exceed Number.MAX_SAFE_INTEGER");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Guard every value-moving entry point. Rejects non-finite (`NaN`, `Infinity`),
|
|
114
|
+
* non-positive (`0`, negative) and above-`Number.MAX_SAFE_INTEGER` amounts - a
|
|
115
|
+
* negative `spend` would otherwise MINT funds, `NaN` would corrupt a balance,
|
|
116
|
+
* and past 2^53 doubles silently lose precision. Throws `INVALID_AMOUNT`.
|
|
117
|
+
* Fractional amounts are accepted; hosts should use integer minor units.
|
|
66
118
|
*
|
|
67
119
|
* @param amount - the caller-supplied amount to credit or debit.
|
|
68
|
-
* @throws when `amount` is not a finite number
|
|
120
|
+
* @throws when `amount` is not a finite number in `(0, 2^53 - 1]`.
|
|
69
121
|
*/
|
|
70
122
|
export function assertPositiveAmount(amount: number): void {
|
|
71
|
-
if (!Number.isFinite(amount) || amount <= 0) {
|
|
123
|
+
if (!Number.isFinite(amount) || amount <= 0 || amount > Number.MAX_SAFE_INTEGER) {
|
|
72
124
|
throw new Error(INVALID_AMOUNT);
|
|
73
125
|
}
|
|
74
126
|
}
|
package/src/test.ts
CHANGED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { convexTest } from "convex-test";
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
3
|
-
import { api, internal } from "./_generated/api";
|
|
4
|
-
import schema from "./schema";
|
|
5
|
-
|
|
6
|
-
const modules = import.meta.glob("./**/*.ts");
|
|
7
|
-
|
|
8
|
-
function setup() {
|
|
9
|
-
return convexTest(schema, modules);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function setClock(ms: number) {
|
|
13
|
-
vi.setSystemTime(new Date(ms));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async function earn(
|
|
17
|
-
t: ReturnType<typeof setup>,
|
|
18
|
-
reason: string,
|
|
19
|
-
idempotencyKey?: string,
|
|
20
|
-
) {
|
|
21
|
-
return await t.mutation(api.mutations.earn, {
|
|
22
|
-
subjectRef: "u",
|
|
23
|
-
currency: "coins",
|
|
24
|
-
amount: 1,
|
|
25
|
-
reason,
|
|
26
|
-
idempotencyKey,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async function prune(
|
|
31
|
-
t: ReturnType<typeof setup>,
|
|
32
|
-
retentionMs: number,
|
|
33
|
-
idempotencyTtlMs: number,
|
|
34
|
-
) {
|
|
35
|
-
return await t.mutation(internal.internal_mutations.pruneLedger, {
|
|
36
|
-
retentionMs,
|
|
37
|
-
idempotencyTtlMs,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
describe("wallet component — retention and idempotency sweep", () => {
|
|
42
|
-
beforeEach(() => {
|
|
43
|
-
vi.useFakeTimers();
|
|
44
|
-
setClock(0);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
afterEach(() => {
|
|
48
|
-
vi.useRealTimers();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("prunes old ledger rows and leaves fresh ones", async () => {
|
|
52
|
-
const t = setup();
|
|
53
|
-
await earn(t, "old");
|
|
54
|
-
setClock(10_000);
|
|
55
|
-
await earn(t, "new");
|
|
56
|
-
expect(await prune(t, 5_000, 1_000_000)).toEqual({
|
|
57
|
-
deleted: 1,
|
|
58
|
-
expired: 0,
|
|
59
|
-
});
|
|
60
|
-
const history = await t.query(api.queries.history, {
|
|
61
|
-
subjectRef: "u",
|
|
62
|
-
currency: "coins",
|
|
63
|
-
limit: 100,
|
|
64
|
-
});
|
|
65
|
-
expect(history).toHaveLength(1);
|
|
66
|
-
expect(history[0]).toMatchObject({ reason: "new" });
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("expires an idempotency key so it can be reused", async () => {
|
|
70
|
-
const t = setup();
|
|
71
|
-
await earn(t, "iap", "receipt");
|
|
72
|
-
setClock(10_000);
|
|
73
|
-
expect(await prune(t, 1_000_000, 5_000)).toEqual({
|
|
74
|
-
deleted: 0,
|
|
75
|
-
expired: 1,
|
|
76
|
-
});
|
|
77
|
-
expect(await earn(t, "iap", "receipt")).toEqual({ balance: 2 });
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("is idempotent and converges to a no-op", async () => {
|
|
81
|
-
const t = setup();
|
|
82
|
-
await earn(t, "old");
|
|
83
|
-
await earn(t, "iap", "k");
|
|
84
|
-
setClock(10_000);
|
|
85
|
-
expect((await prune(t, 1_000_000, 5_000)).expired).toBe(1);
|
|
86
|
-
expect(await prune(t, 1_000_000, 5_000)).toEqual({
|
|
87
|
-
deleted: 0,
|
|
88
|
-
expired: 0,
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test("does nothing when no rows are stale", async () => {
|
|
93
|
-
const t = setup();
|
|
94
|
-
await earn(t, "iap", "k");
|
|
95
|
-
expect(await prune(t, 1_000_000, 1_000_000)).toEqual({
|
|
96
|
-
deleted: 0,
|
|
97
|
-
expired: 0,
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test("caps a full prune batch and skips idempotency expiry", async () => {
|
|
102
|
-
const t = setup();
|
|
103
|
-
for (let i = 0; i < 256; i++) {
|
|
104
|
-
await earn(t, `old-${i}`);
|
|
105
|
-
}
|
|
106
|
-
setClock(10_000);
|
|
107
|
-
expect(await prune(t, 5_000, 1)).toEqual({
|
|
108
|
-
deleted: 256,
|
|
109
|
-
expired: 0,
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
});
|
package/src/react/index.test.tsx
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { renderHook } from "@testing-library/react";
|
|
3
|
-
import { expect, test, vi } from "vitest";
|
|
4
|
-
import * as cr from "convex/react";
|
|
5
|
-
import type { FunctionReference } from "convex/server";
|
|
6
|
-
import type { BalanceEntry } from "../client/types.js";
|
|
7
|
-
import { useBalance, useBalances } from "./index.js";
|
|
8
|
-
|
|
9
|
-
vi.mock("convex/react", () => ({
|
|
10
|
-
useQuery: vi.fn(),
|
|
11
|
-
usePaginatedQuery: vi.fn(),
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
const mockedUseQuery = cr.useQuery as unknown as ReturnType<typeof vi.fn>;
|
|
15
|
-
|
|
16
|
-
const balanceRef = {} as FunctionReference<
|
|
17
|
-
"query",
|
|
18
|
-
"public",
|
|
19
|
-
{ subjectRef: string; currency: string },
|
|
20
|
-
number
|
|
21
|
-
>;
|
|
22
|
-
|
|
23
|
-
const balancesRef = {} as FunctionReference<
|
|
24
|
-
"query",
|
|
25
|
-
"public",
|
|
26
|
-
{ subjectRef: string },
|
|
27
|
-
BalanceEntry[]
|
|
28
|
-
>;
|
|
29
|
-
|
|
30
|
-
test("useBalance passes ref + args and returns the loaded value", () => {
|
|
31
|
-
mockedUseQuery.mockReturnValue(42);
|
|
32
|
-
const args = { subjectRef: "s", currency: "coins" };
|
|
33
|
-
const { result } = renderHook(() => useBalance(balanceRef, args));
|
|
34
|
-
expect(mockedUseQuery).toHaveBeenCalledWith(balanceRef, args);
|
|
35
|
-
expect(result.current).toBe(42);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("useBalance returns undefined while loading", () => {
|
|
39
|
-
mockedUseQuery.mockReturnValue(undefined);
|
|
40
|
-
const { result } = renderHook(() =>
|
|
41
|
-
useBalance(balanceRef, { subjectRef: "s", currency: "coins" }),
|
|
42
|
-
);
|
|
43
|
-
expect(result.current).toBeUndefined();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("useBalances passes ref + args and returns the loaded value", () => {
|
|
47
|
-
const balances: BalanceEntry[] = [{ currency: "coins", amount: 7 }];
|
|
48
|
-
mockedUseQuery.mockReturnValue(balances);
|
|
49
|
-
const args = { subjectRef: "s" };
|
|
50
|
-
const { result } = renderHook(() => useBalances(balancesRef, args));
|
|
51
|
-
expect(mockedUseQuery).toHaveBeenCalledWith(balancesRef, args);
|
|
52
|
-
expect(result.current).toBe(balances);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("useBalances returns undefined while loading", () => {
|
|
56
|
-
mockedUseQuery.mockReturnValue(undefined);
|
|
57
|
-
const { result } = renderHook(() =>
|
|
58
|
-
useBalances(balancesRef, { subjectRef: "s" }),
|
|
59
|
-
);
|
|
60
|
-
expect(result.current).toBeUndefined();
|
|
61
|
-
});
|