@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.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/client/index.d.ts +109 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +105 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +45 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +42 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +105 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +12 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +19 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/internal_mutations.d.ts +20 -0
- package/dist/component/internal_mutations.d.ts.map +1 -0
- package/dist/component/internal_mutations.js +52 -0
- package/dist/component/internal_mutations.js.map +1 -0
- package/dist/component/mutations.d.ts +79 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +243 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +43 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +70 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +45 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +33 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +73 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +45 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +37 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +30 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +84 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +91 -0
- package/dist/shared.js.map +1 -0
- package/package.json +112 -0
- package/src/client/index.ts +257 -0
- package/src/client/types.ts +51 -0
- package/src/component/_generated/api.ts +58 -0
- package/src/component/_generated/component.ts +118 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/crons.ts +29 -0
- package/src/component/internal_mutations.ts +58 -0
- package/src/component/mutations.ts +279 -0
- package/src/component/queries.ts +76 -0
- package/src/component/schema.ts +33 -0
- package/src/component/validators.ts +58 -0
- package/src/react/index.test.tsx +61 -0
- package/src/react/index.tsx +50 -0
- package/src/shared.ts +123 -0
- package/src/test.ts +12 -0
|
@@ -0,0 +1,30 @@
|
|
|
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 { useQuery } from "convex/react";
|
|
10
|
+
/**
|
|
11
|
+
* Reactive single-currency balance for `subjectRef`.
|
|
12
|
+
*
|
|
13
|
+
* @param balanceRef - The host's re-exported `wallet.balance` query reference.
|
|
14
|
+
* @param args - `{ subjectRef, currency }` opaque identity + currency string.
|
|
15
|
+
* @returns The current balance, or `undefined` while the query loads.
|
|
16
|
+
*/
|
|
17
|
+
export function useBalance(balanceRef, args) {
|
|
18
|
+
return useQuery(balanceRef, args);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reactive multi-currency overview for `subjectRef`.
|
|
22
|
+
*
|
|
23
|
+
* @param balancesRef - The host's re-exported `wallet.balances` query reference.
|
|
24
|
+
* @param args - `{ subjectRef }` opaque identity.
|
|
25
|
+
* @returns The balances array, or `undefined` while the query loads.
|
|
26
|
+
*/
|
|
27
|
+
export function useBalances(balancesRef, args) {
|
|
28
|
+
return useQuery(balancesRef, args);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxC;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACxB,UAKC,EACD,IAA8C;IAE9C,OAAO,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CACzB,WAKC,EACD,IAA4B;IAE5B,OAAO,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;AACrC,CAAC"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
|
+
export declare const COMPONENT_NAME = "wallet";
|
|
3
|
+
/** Default newest-first page size for {@link history} when the caller omits one. */
|
|
4
|
+
export declare const DEFAULT_HISTORY_LIMIT = 50;
|
|
5
|
+
/**
|
|
6
|
+
* Default ledger retention: rows older than this are pruned by the internal
|
|
7
|
+
* cron. 90 days. Override per mount via {@link WalletOptions.ledgerRetentionMs}.
|
|
8
|
+
*/
|
|
9
|
+
export declare const DEFAULT_LEDGER_RETENTION_MS: number;
|
|
10
|
+
/**
|
|
11
|
+
* Default idempotency window: a recorded `idempotencyKey` is honored (replays
|
|
12
|
+
* are no-ops) for this long; afterwards the cron forgets it so the key can be
|
|
13
|
+
* reused. 7 days. Override via {@link WalletOptions.idempotencyTtlMs}.
|
|
14
|
+
*/
|
|
15
|
+
export declare const DEFAULT_IDEMPOTENCY_TTL_MS: number;
|
|
16
|
+
/** Default cron cadence for the ledger/idempotency prune sweep. 1 hour. */
|
|
17
|
+
export declare const DEFAULT_PRUNE_INTERVAL_MS: number;
|
|
18
|
+
/** Stable, code-tagged failure reasons returned on the `code` field of a result. */
|
|
19
|
+
export declare const ERROR_CODES: {
|
|
20
|
+
/** A spend/transfer was short of funds. */
|
|
21
|
+
readonly INSUFFICIENT: "INSUFFICIENT";
|
|
22
|
+
/** A transfer named the same subject as sender and receiver. */
|
|
23
|
+
readonly SELF_TRANSFER: "SELF_TRANSFER";
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Thrown by {@link applyRegen} when a regen config has a non-finite or
|
|
27
|
+
* non-positive field. Carries the stable `INVALID_REGEN` code so hosts can
|
|
28
|
+
* branch without string-matching prose.
|
|
29
|
+
*/
|
|
30
|
+
export declare const INVALID_REGEN = "INVALID_REGEN";
|
|
31
|
+
/** A non-throwing result failure tag. */
|
|
32
|
+
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
33
|
+
/**
|
|
34
|
+
* Thrown by {@link assertPositiveAmount} when an amount is non-finite or not
|
|
35
|
+
* strictly positive. Carries the stable `INVALID_AMOUNT` code so hosts can
|
|
36
|
+
* branch on `error.message` without string-matching prose.
|
|
37
|
+
*/
|
|
38
|
+
export declare const INVALID_AMOUNT = "INVALID_AMOUNT";
|
|
39
|
+
/** Opaque host-supplied subject reference. Never assume its shape or source. */
|
|
40
|
+
export type SubjectRef = string;
|
|
41
|
+
/**
|
|
42
|
+
* Time-regen rule for an energy-style currency: gain `amount` units every
|
|
43
|
+
* `intervalMs`, never exceeding `cap`. Coin/gem-style currencies omit it.
|
|
44
|
+
*/
|
|
45
|
+
export interface RegenConfig {
|
|
46
|
+
amount: number;
|
|
47
|
+
intervalMs: number;
|
|
48
|
+
cap: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Guard every value-moving entry point. Rejects non-finite (`NaN`, `Infinity`)
|
|
52
|
+
* and non-positive (`0`, negative) amounts — a negative `spend` would otherwise
|
|
53
|
+
* MINT funds, and `NaN` would corrupt a balance. Throws `INVALID_AMOUNT`.
|
|
54
|
+
*
|
|
55
|
+
* @param amount - the caller-supplied amount to credit or debit.
|
|
56
|
+
* @throws when `amount` is not a finite number `> 0`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function assertPositiveAmount(amount: number): void;
|
|
59
|
+
/**
|
|
60
|
+
* Pure, deterministic lazy regen. Given a `stored` balance last touched at
|
|
61
|
+
* `lastRegenAt`, project it forward to `now` under `regen`.
|
|
62
|
+
*
|
|
63
|
+
* - No `regen` → unchanged `{ amount: stored, lastRegenAt }`.
|
|
64
|
+
* - Backwards/equal clock (`now <= lastRegenAt`) → unchanged: the pointer is
|
|
65
|
+
* never moved backward and never advanced past `now`, so a host that supplies
|
|
66
|
+
* a stale/rewound clock can never over-regen or rewind accrual.
|
|
67
|
+
* - `ticks = floor((now - lastRegenAt) / intervalMs)`; zero ticks → unchanged
|
|
68
|
+
* (the clock pointer does not advance until a whole tick elapses).
|
|
69
|
+
* - Otherwise grant `ticks * amount`, clamped to `cap` (never above `cap`,
|
|
70
|
+
* never below `stored`), and advance `lastRegenAt` by exactly the ticks
|
|
71
|
+
* consumed so partial-interval progress is preserved. Because
|
|
72
|
+
* `lastRegenAt + ticks * intervalMs <= now`, the pointer never passes `now`.
|
|
73
|
+
*
|
|
74
|
+
* @param stored - the persisted balance.
|
|
75
|
+
* @param lastRegenAt - epoch ms the balance was last reconciled.
|
|
76
|
+
* @param now - current epoch ms (read server-side via `Date.now()`).
|
|
77
|
+
* @param regen - optional regen rule; when absent the balance is static.
|
|
78
|
+
* @returns the projected `{ amount, lastRegenAt }`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function applyRegen(stored: number, lastRegenAt: number, now: number, regen?: RegenConfig): {
|
|
81
|
+
amount: number;
|
|
82
|
+
lastRegenAt: number;
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,eAAO,MAAM,cAAc,WAAW,CAAC;AAEvC,oFAAoF;AACpF,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC;;;GAGG;AACH,eAAO,MAAM,2BAA2B,QAA2B,CAAC;AAEpE;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,QAA0B,CAAC;AAElE,2EAA2E;AAC3E,eAAO,MAAM,yBAAyB,QAAiB,CAAC;AAExD,oFAAoF;AACpF,eAAO,MAAM,WAAW;IACtB,2CAA2C;;IAE3C,gEAAgE;;CAExD,CAAC;AAEX;;;;GAIG;AACH,eAAO,MAAM,aAAa,kBAAkB,CAAC;AAE7C,yCAAyC;AACzC,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AAEvE;;;;GAIG;AACH,eAAO,MAAM,cAAc,mBAAmB,CAAC;AAE/C,gFAAgF;AAChF,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAEhC;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;GAOG;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
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
|
+
export const COMPONENT_NAME = "wallet";
|
|
3
|
+
/** Default newest-first page size for {@link history} when the caller omits one. */
|
|
4
|
+
export const DEFAULT_HISTORY_LIMIT = 50;
|
|
5
|
+
/**
|
|
6
|
+
* Default ledger retention: rows older than this are pruned by the internal
|
|
7
|
+
* cron. 90 days. Override per mount via {@link WalletOptions.ledgerRetentionMs}.
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_LEDGER_RETENTION_MS = 90 * 24 * 60 * 60 * 1000;
|
|
10
|
+
/**
|
|
11
|
+
* Default idempotency window: a recorded `idempotencyKey` is honored (replays
|
|
12
|
+
* are no-ops) for this long; afterwards the cron forgets it so the key can be
|
|
13
|
+
* reused. 7 days. Override via {@link WalletOptions.idempotencyTtlMs}.
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_IDEMPOTENCY_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
16
|
+
/** Default cron cadence for the ledger/idempotency prune sweep. 1 hour. */
|
|
17
|
+
export const DEFAULT_PRUNE_INTERVAL_MS = 60 * 60 * 1000;
|
|
18
|
+
/** Stable, code-tagged failure reasons returned on the `code` field of a result. */
|
|
19
|
+
export const ERROR_CODES = {
|
|
20
|
+
/** A spend/transfer was short of funds. */
|
|
21
|
+
INSUFFICIENT: "INSUFFICIENT",
|
|
22
|
+
/** A transfer named the same subject as sender and receiver. */
|
|
23
|
+
SELF_TRANSFER: "SELF_TRANSFER",
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Thrown by {@link applyRegen} when a regen config has a non-finite or
|
|
27
|
+
* non-positive field. Carries the stable `INVALID_REGEN` code so hosts can
|
|
28
|
+
* branch without string-matching prose.
|
|
29
|
+
*/
|
|
30
|
+
export const INVALID_REGEN = "INVALID_REGEN";
|
|
31
|
+
/**
|
|
32
|
+
* Thrown by {@link assertPositiveAmount} when an amount is non-finite or not
|
|
33
|
+
* strictly positive. Carries the stable `INVALID_AMOUNT` code so hosts can
|
|
34
|
+
* branch on `error.message` without string-matching prose.
|
|
35
|
+
*/
|
|
36
|
+
export const INVALID_AMOUNT = "INVALID_AMOUNT";
|
|
37
|
+
/**
|
|
38
|
+
* Guard every value-moving entry point. Rejects non-finite (`NaN`, `Infinity`)
|
|
39
|
+
* and non-positive (`0`, negative) amounts — a negative `spend` would otherwise
|
|
40
|
+
* MINT funds, and `NaN` would corrupt a balance. Throws `INVALID_AMOUNT`.
|
|
41
|
+
*
|
|
42
|
+
* @param amount - the caller-supplied amount to credit or debit.
|
|
43
|
+
* @throws when `amount` is not a finite number `> 0`.
|
|
44
|
+
*/
|
|
45
|
+
export function assertPositiveAmount(amount) {
|
|
46
|
+
if (!Number.isFinite(amount) || amount <= 0) {
|
|
47
|
+
throw new Error(INVALID_AMOUNT);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Pure, deterministic lazy regen. Given a `stored` balance last touched at
|
|
52
|
+
* `lastRegenAt`, project it forward to `now` under `regen`.
|
|
53
|
+
*
|
|
54
|
+
* - No `regen` → unchanged `{ amount: stored, lastRegenAt }`.
|
|
55
|
+
* - Backwards/equal clock (`now <= lastRegenAt`) → unchanged: the pointer is
|
|
56
|
+
* never moved backward and never advanced past `now`, so a host that supplies
|
|
57
|
+
* a stale/rewound clock can never over-regen or rewind accrual.
|
|
58
|
+
* - `ticks = floor((now - lastRegenAt) / intervalMs)`; zero ticks → unchanged
|
|
59
|
+
* (the clock pointer does not advance until a whole tick elapses).
|
|
60
|
+
* - Otherwise grant `ticks * amount`, clamped to `cap` (never above `cap`,
|
|
61
|
+
* never below `stored`), and advance `lastRegenAt` by exactly the ticks
|
|
62
|
+
* consumed so partial-interval progress is preserved. Because
|
|
63
|
+
* `lastRegenAt + ticks * intervalMs <= now`, the pointer never passes `now`.
|
|
64
|
+
*
|
|
65
|
+
* @param stored - the persisted balance.
|
|
66
|
+
* @param lastRegenAt - epoch ms the balance was last reconciled.
|
|
67
|
+
* @param now - current epoch ms (read server-side via `Date.now()`).
|
|
68
|
+
* @param regen - optional regen rule; when absent the balance is static.
|
|
69
|
+
* @returns the projected `{ amount, lastRegenAt }`.
|
|
70
|
+
*/
|
|
71
|
+
export function applyRegen(stored, lastRegenAt, now, regen) {
|
|
72
|
+
if (regen === undefined || now <= lastRegenAt) {
|
|
73
|
+
return { amount: stored, lastRegenAt };
|
|
74
|
+
}
|
|
75
|
+
if (!Number.isFinite(regen.intervalMs) ||
|
|
76
|
+
regen.intervalMs <= 0 ||
|
|
77
|
+
!Number.isFinite(regen.amount) ||
|
|
78
|
+
regen.amount <= 0 ||
|
|
79
|
+
!Number.isFinite(regen.cap) ||
|
|
80
|
+
regen.cap <= 0) {
|
|
81
|
+
throw new Error(INVALID_REGEN);
|
|
82
|
+
}
|
|
83
|
+
const ticks = Math.floor((now - lastRegenAt) / regen.intervalMs);
|
|
84
|
+
return ticks === 0
|
|
85
|
+
? { amount: stored, lastRegenAt }
|
|
86
|
+
: {
|
|
87
|
+
amount: Math.max(stored, Math.min(regen.cap, stored + ticks * regen.amount)),
|
|
88
|
+
lastRegenAt: lastRegenAt + ticks * regen.intervalMs,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +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;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAAC;AAK7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAe/C;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;IAClC,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,UAAU,CACxB,MAAc,EACd,WAAmB,EACnB,GAAW,EACX,KAAmB;IAEnB,IAAI,KAAK,KAAK,SAAS,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC;QAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,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
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vllnt/convex-wallet",
|
|
3
|
+
"version": "0.1.0-canary.3a56ec7",
|
|
4
|
+
"description": "Consumable balances and economy ledger with atomic spend and lazy regen, as a Convex component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@9.15.4",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/client/index.d.ts",
|
|
16
|
+
"default": "./dist/client/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./react": {
|
|
19
|
+
"types": "./dist/react/index.d.ts",
|
|
20
|
+
"default": "./dist/react/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./test": "./src/test.ts",
|
|
23
|
+
"./_generated/component.js": {
|
|
24
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./_generated/component": {
|
|
27
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./convex.config": {
|
|
30
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
31
|
+
"default": "./dist/component/convex.config.js"
|
|
32
|
+
},
|
|
33
|
+
"./convex.config.js": {
|
|
34
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
35
|
+
"default": "./dist/component/convex.config.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"types": "./dist/client/index.d.ts",
|
|
39
|
+
"module": "./dist/client/index.js",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
42
|
+
"build:codegen": "pnpm convex codegen --component-dir ./src/component && pnpm build",
|
|
43
|
+
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
+
"lint": "eslint .",
|
|
47
|
+
"test": "vitest run --passWithNoTests",
|
|
48
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
50
|
+
"generate:llms": "node scripts/generate-llms.mjs",
|
|
51
|
+
"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
|
+
},
|
|
56
|
+
"dependencies": {},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"convex": "^1.41.0",
|
|
59
|
+
"react": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"react": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@edge-runtime/vm": "^5.0.0",
|
|
68
|
+
"@testing-library/react": "^16.3.2",
|
|
69
|
+
"@types/react": "^19.2.17",
|
|
70
|
+
"@types/react-dom": "^19.2.3",
|
|
71
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
72
|
+
"@vllnt/eslint-config": "^1.0.0",
|
|
73
|
+
"@vllnt/typescript": "^1.0.0",
|
|
74
|
+
"convex": "^1.41.0",
|
|
75
|
+
"convex-test": "^0.0.53",
|
|
76
|
+
"eslint": "^9.0.0",
|
|
77
|
+
"jsdom": "^29.1.1",
|
|
78
|
+
"prettier": "^3.4.0",
|
|
79
|
+
"react": "^19.2.0",
|
|
80
|
+
"react-dom": "^19.2.7",
|
|
81
|
+
"typescript": "^5.7.0",
|
|
82
|
+
"vitest": "^4.1.8"
|
|
83
|
+
},
|
|
84
|
+
"homepage": "https://github.com/vllnt/convex-wallet#readme",
|
|
85
|
+
"bugs": {
|
|
86
|
+
"url": "https://github.com/vllnt/convex-wallet/issues"
|
|
87
|
+
},
|
|
88
|
+
"author": {
|
|
89
|
+
"name": "bntvllnt",
|
|
90
|
+
"url": "https://bntvllnt.com"
|
|
91
|
+
},
|
|
92
|
+
"funding": {
|
|
93
|
+
"type": "github",
|
|
94
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
95
|
+
},
|
|
96
|
+
"repository": {
|
|
97
|
+
"type": "git",
|
|
98
|
+
"url": "https://github.com/vllnt/convex-wallet"
|
|
99
|
+
},
|
|
100
|
+
"engines": {
|
|
101
|
+
"node": ">=18"
|
|
102
|
+
},
|
|
103
|
+
"publishConfig": {
|
|
104
|
+
"access": "public"
|
|
105
|
+
},
|
|
106
|
+
"sideEffects": false,
|
|
107
|
+
"keywords": [
|
|
108
|
+
"convex",
|
|
109
|
+
"convex-component",
|
|
110
|
+
"wallet"
|
|
111
|
+
]
|
|
112
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FunctionArgs,
|
|
3
|
+
FunctionReference,
|
|
4
|
+
FunctionReturnType,
|
|
5
|
+
} from "convex/server";
|
|
6
|
+
import type {
|
|
7
|
+
BalanceEntry,
|
|
8
|
+
CurrencyConfig,
|
|
9
|
+
ErrorCode,
|
|
10
|
+
LedgerEntry,
|
|
11
|
+
RegenConfig,
|
|
12
|
+
SpendResult,
|
|
13
|
+
WalletOptions,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
import { DEFAULT_HISTORY_LIMIT } from "../shared.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The wallet component's function references, as exposed on the host via
|
|
19
|
+
* `components.wallet`.
|
|
20
|
+
*/
|
|
21
|
+
export interface WalletComponent {
|
|
22
|
+
mutations: {
|
|
23
|
+
earn: FunctionReference<
|
|
24
|
+
"mutation",
|
|
25
|
+
"internal",
|
|
26
|
+
{
|
|
27
|
+
subjectRef: string;
|
|
28
|
+
currency: string;
|
|
29
|
+
amount: number;
|
|
30
|
+
reason: string;
|
|
31
|
+
idempotencyKey?: string;
|
|
32
|
+
regen?: RegenConfig;
|
|
33
|
+
max?: number;
|
|
34
|
+
},
|
|
35
|
+
{ balance: number }
|
|
36
|
+
>;
|
|
37
|
+
spend: FunctionReference<
|
|
38
|
+
"mutation",
|
|
39
|
+
"internal",
|
|
40
|
+
{
|
|
41
|
+
subjectRef: string;
|
|
42
|
+
currency: string;
|
|
43
|
+
amount: number;
|
|
44
|
+
reason: string;
|
|
45
|
+
idempotencyKey?: string;
|
|
46
|
+
regen?: RegenConfig;
|
|
47
|
+
},
|
|
48
|
+
SpendResult
|
|
49
|
+
>;
|
|
50
|
+
transfer: FunctionReference<
|
|
51
|
+
"mutation",
|
|
52
|
+
"internal",
|
|
53
|
+
{
|
|
54
|
+
fromRef: string;
|
|
55
|
+
toRef: string;
|
|
56
|
+
currency: string;
|
|
57
|
+
amount: number;
|
|
58
|
+
reason: string;
|
|
59
|
+
regen?: RegenConfig;
|
|
60
|
+
max?: number;
|
|
61
|
+
},
|
|
62
|
+
SpendResult
|
|
63
|
+
>;
|
|
64
|
+
};
|
|
65
|
+
queries: {
|
|
66
|
+
balance: FunctionReference<
|
|
67
|
+
"query",
|
|
68
|
+
"internal",
|
|
69
|
+
{ subjectRef: string; currency: string; regen?: RegenConfig },
|
|
70
|
+
number
|
|
71
|
+
>;
|
|
72
|
+
balances: FunctionReference<
|
|
73
|
+
"query",
|
|
74
|
+
"internal",
|
|
75
|
+
{ subjectRef: string; regen?: Record<string, RegenConfig> },
|
|
76
|
+
BalanceEntry[]
|
|
77
|
+
>;
|
|
78
|
+
history: FunctionReference<
|
|
79
|
+
"query",
|
|
80
|
+
"internal",
|
|
81
|
+
{ subjectRef: string; currency: string; limit: number },
|
|
82
|
+
LedgerEntry[]
|
|
83
|
+
>;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface RunQueryCtx {
|
|
88
|
+
runQuery<Q extends FunctionReference<"query", "internal">>(
|
|
89
|
+
reference: Q,
|
|
90
|
+
args: FunctionArgs<Q>,
|
|
91
|
+
): Promise<FunctionReturnType<Q>>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface RunMutationCtx {
|
|
95
|
+
runMutation<M extends FunctionReference<"mutation", "internal">>(
|
|
96
|
+
reference: M,
|
|
97
|
+
args: FunctionArgs<M>,
|
|
98
|
+
): Promise<FunctionReturnType<M>>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Consumer-facing client for the consumable-balances wallet. The host owns auth,
|
|
103
|
+
* payment confirmation, and meaning: it resolves identity, verifies any IAP /
|
|
104
|
+
* Stripe purchase, then passes an opaque `subjectRef` and a `currency` string
|
|
105
|
+
* in. The client injects each currency's `regen` rule and `max` ceiling per
|
|
106
|
+
* call; the component reads the server clock (`Date.now()`) itself.
|
|
107
|
+
*/
|
|
108
|
+
export class Wallet {
|
|
109
|
+
private readonly currencies: Record<string, CurrencyConfig>;
|
|
110
|
+
private readonly defaultHistoryLimit: number;
|
|
111
|
+
|
|
112
|
+
constructor(
|
|
113
|
+
private readonly component: WalletComponent,
|
|
114
|
+
options: WalletOptions = {},
|
|
115
|
+
) {
|
|
116
|
+
this.currencies = options.currencies ?? {};
|
|
117
|
+
this.defaultHistoryLimit = options.defaultHistoryLimit ?? DEFAULT_HISTORY_LIMIT;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private regenFor(currency: string): RegenConfig | undefined {
|
|
121
|
+
return this.currencies[currency]?.regen;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private maxFor(currency: string): number | undefined {
|
|
125
|
+
return this.currencies[currency]?.max;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The per-currency regen map for the multi-currency {@link balances} read. */
|
|
129
|
+
private regenMap(): Record<string, RegenConfig> | undefined {
|
|
130
|
+
const out: Record<string, RegenConfig> = {};
|
|
131
|
+
for (const [currency, config] of Object.entries(this.currencies)) {
|
|
132
|
+
if (config.regen !== undefined) {
|
|
133
|
+
out[currency] = config.regen;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return Object.keys(out).length === 0 ? undefined : out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Credit `amount` of `currency` to `subjectRef`. Returns the new balance. */
|
|
140
|
+
earn(
|
|
141
|
+
ctx: RunMutationCtx,
|
|
142
|
+
subjectRef: string,
|
|
143
|
+
currency: string,
|
|
144
|
+
amount: number,
|
|
145
|
+
reason: string,
|
|
146
|
+
opts: { idempotencyKey?: string } = {},
|
|
147
|
+
): Promise<{ balance: number }> {
|
|
148
|
+
return ctx.runMutation(this.component.mutations.earn, {
|
|
149
|
+
subjectRef,
|
|
150
|
+
currency,
|
|
151
|
+
amount,
|
|
152
|
+
reason,
|
|
153
|
+
idempotencyKey: opts.idempotencyKey,
|
|
154
|
+
regen: this.regenFor(currency),
|
|
155
|
+
max: this.maxFor(currency),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Idempotent grant — credit only once per `idempotencyKey`. Use after the
|
|
161
|
+
* host has verified an IAP / Stripe purchase. Returns the new balance.
|
|
162
|
+
*/
|
|
163
|
+
grant(
|
|
164
|
+
ctx: RunMutationCtx,
|
|
165
|
+
subjectRef: string,
|
|
166
|
+
currency: string,
|
|
167
|
+
amount: number,
|
|
168
|
+
reason: string,
|
|
169
|
+
idempotencyKey: string,
|
|
170
|
+
): Promise<{ balance: number }> {
|
|
171
|
+
return this.earn(ctx, subjectRef, currency, amount, reason, { idempotencyKey });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Debit `amount` of `currency` from `subjectRef`. Never goes negative.
|
|
176
|
+
* When `opts.idempotencyKey` is supplied and already recorded for this
|
|
177
|
+
* `(subjectRef, currency)`, this is a no-op replay — the current
|
|
178
|
+
* (regen-aware) balance is returned without a second debit or ledger row.
|
|
179
|
+
*/
|
|
180
|
+
spend(
|
|
181
|
+
ctx: RunMutationCtx,
|
|
182
|
+
subjectRef: string,
|
|
183
|
+
currency: string,
|
|
184
|
+
amount: number,
|
|
185
|
+
reason: string,
|
|
186
|
+
opts: { idempotencyKey?: string } = {},
|
|
187
|
+
): Promise<SpendResult> {
|
|
188
|
+
return ctx.runMutation(this.component.mutations.spend, {
|
|
189
|
+
subjectRef,
|
|
190
|
+
currency,
|
|
191
|
+
amount,
|
|
192
|
+
reason,
|
|
193
|
+
idempotencyKey: opts.idempotencyKey,
|
|
194
|
+
regen: this.regenFor(currency),
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Move `amount` of `currency` from `fromRef` to `toRef` in one transaction. */
|
|
199
|
+
transfer(
|
|
200
|
+
ctx: RunMutationCtx,
|
|
201
|
+
fromRef: string,
|
|
202
|
+
toRef: string,
|
|
203
|
+
currency: string,
|
|
204
|
+
amount: number,
|
|
205
|
+
reason: string,
|
|
206
|
+
): Promise<SpendResult> {
|
|
207
|
+
return ctx.runMutation(this.component.mutations.transfer, {
|
|
208
|
+
fromRef,
|
|
209
|
+
toRef,
|
|
210
|
+
currency,
|
|
211
|
+
amount,
|
|
212
|
+
reason,
|
|
213
|
+
regen: this.regenFor(currency),
|
|
214
|
+
max: this.maxFor(currency),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Regen-aware single-currency balance for `subjectRef`. Missing → 0. */
|
|
219
|
+
balance(ctx: RunQueryCtx, subjectRef: string, currency: string): Promise<number> {
|
|
220
|
+
return ctx.runQuery(this.component.queries.balance, {
|
|
221
|
+
subjectRef,
|
|
222
|
+
currency,
|
|
223
|
+
regen: this.regenFor(currency),
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** All currency balances held by `subjectRef`, regen-aware (matches {@link balance}). */
|
|
228
|
+
balances(ctx: RunQueryCtx, subjectRef: string): Promise<BalanceEntry[]> {
|
|
229
|
+
return ctx.runQuery(this.component.queries.balances, {
|
|
230
|
+
subjectRef,
|
|
231
|
+
regen: this.regenMap(),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Newest-first ledger history for one currency. */
|
|
236
|
+
history(
|
|
237
|
+
ctx: RunQueryCtx,
|
|
238
|
+
subjectRef: string,
|
|
239
|
+
currency: string,
|
|
240
|
+
limit?: number,
|
|
241
|
+
): Promise<LedgerEntry[]> {
|
|
242
|
+
return ctx.runQuery(this.component.queries.history, {
|
|
243
|
+
subjectRef,
|
|
244
|
+
currency,
|
|
245
|
+
limit: limit ?? this.defaultHistoryLimit,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export type {
|
|
251
|
+
BalanceEntry,
|
|
252
|
+
ErrorCode,
|
|
253
|
+
LedgerEntry,
|
|
254
|
+
RegenConfig,
|
|
255
|
+
SpendResult,
|
|
256
|
+
WalletOptions,
|
|
257
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Public TypeScript surface for the wallet client. */
|
|
2
|
+
|
|
3
|
+
import type { ErrorCode, RegenConfig } from "../shared.js";
|
|
4
|
+
|
|
5
|
+
export type { ErrorCode, RegenConfig };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Result of a spend/transfer that may be rejected. `code` is a stable,
|
|
9
|
+
* machine-readable failure tag, present only when `ok` is false.
|
|
10
|
+
*/
|
|
11
|
+
export interface SpendResult {
|
|
12
|
+
ok: boolean;
|
|
13
|
+
balance: number;
|
|
14
|
+
/** Stable failure tag when `ok` is false: `INSUFFICIENT` | `SELF_TRANSFER`. */
|
|
15
|
+
code?: ErrorCode;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A single currency balance in the multi-currency overview. */
|
|
19
|
+
export interface BalanceEntry {
|
|
20
|
+
currency: string;
|
|
21
|
+
amount: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A ledger row as returned by {@link Wallet.history}. */
|
|
25
|
+
export interface LedgerEntry {
|
|
26
|
+
currency: string;
|
|
27
|
+
/** Signed change: positive on earn/receive, negative on spend/send. */
|
|
28
|
+
delta: number;
|
|
29
|
+
reason: string;
|
|
30
|
+
idempotencyKey?: string;
|
|
31
|
+
createdAt: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Per-currency configuration. Omit `regen` for static (coin/gem) currencies.
|
|
36
|
+
* `max` is an optional hard ceiling on the stored balance for this currency —
|
|
37
|
+
* credits are clamped to it.
|
|
38
|
+
*/
|
|
39
|
+
export interface CurrencyConfig {
|
|
40
|
+
regen?: RegenConfig;
|
|
41
|
+
/** Hard cap on the stored balance for this currency. Credits clamp to it. */
|
|
42
|
+
max?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Construction options for the {@link Wallet} client. */
|
|
46
|
+
export interface WalletOptions {
|
|
47
|
+
/** Per-currency rules, keyed by the opaque currency string. */
|
|
48
|
+
currencies?: Record<string, CurrencyConfig>;
|
|
49
|
+
/** Default page size for {@link Wallet.history}. Default `50`. */
|
|
50
|
+
defaultHistoryLimit?: number;
|
|
51
|
+
}
|