@cashu/coco-expo-sqlite 2.0.0-rc.2 → 2.0.0

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 (2) hide show
  1. package/dist/index.js +158 -69
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { DEFAULT_UNIT, QuoteIdentityConflictError, deserializeAmount, deserializeToken, getMintQuoteAmount, getMintQuoteRemoteState, isMintQuotePending, isStatefulMintQuote, normalizeMeltMethodData, normalizeMintUrl, normalizeUnit, operationHistoryId, parseHistoryEntryId, projectLegacyHistoryRow, serializeAmount, stringifyJson } from "@cashu/coco-core/adapter";
1
+ import { DEFAULT_UNIT, QuoteIdentityConflictError, deriveBolt11MintQuoteState, deserializeAmount, deserializeToken, getMintQuoteAmount, getMintQuoteRemoteState, isMintQuotePending, isStatefulMintQuote, normalizeMeltMethodData, normalizeMintUrl, normalizeUnit, operationHistoryId, parseHistoryEntryId, projectLegacyHistoryRow, serializeAmount, stringifyJson } from "@cashu/coco-core/adapter";
2
+ import { Amount } from "@cashu/cashu-ts";
2
3
 
3
4
  //#region ../sql-storage/src/schema.ts
4
5
  const SEND_OPERATION_METHOD_MIGRATION_IDS = ["012_send_operations_method", "013_send_operations_method"];
@@ -1323,6 +1324,50 @@ const MIGRATIONS = [
1323
1324
  ON coco_cashu_canonical_mint_quotes(mintUrl, quoteId);
1324
1325
  CREATE UNIQUE INDEX IF NOT EXISTS ux_coco_cashu_melt_quotes_identity
1325
1326
  ON coco_cashu_melt_quotes(mintUrl, quoteId);
1327
+ `
1328
+ },
1329
+ {
1330
+ id: "037_mint_quote_accounting",
1331
+ sql: `
1332
+ ALTER TABLE coco_cashu_canonical_mint_quotes
1333
+ ADD COLUMN amountPaid TEXT NOT NULL DEFAULT '0';
1334
+ ALTER TABLE coco_cashu_canonical_mint_quotes
1335
+ ADD COLUMN amountIssued TEXT NOT NULL DEFAULT '0';
1336
+ ALTER TABLE coco_cashu_canonical_mint_quotes
1337
+ ADD COLUMN remoteUpdatedAt INTEGER;
1338
+
1339
+ UPDATE coco_cashu_canonical_mint_quotes
1340
+ SET amountPaid = CASE
1341
+ WHEN reusable = 1
1342
+ THEN COALESCE(
1343
+ CAST(json_extract(
1344
+ CASE WHEN json_valid(quoteDataJson) THEN quoteDataJson ELSE '{}' END,
1345
+ '$.amountPaid'
1346
+ ) AS TEXT),
1347
+ '0'
1348
+ )
1349
+ WHEN state IN ('PAID', 'ISSUED')
1350
+ THEN COALESCE(CAST(amount AS TEXT), '0')
1351
+ ELSE '0'
1352
+ END,
1353
+ amountIssued = CASE
1354
+ WHEN reusable = 1
1355
+ THEN COALESCE(
1356
+ CAST(json_extract(
1357
+ CASE WHEN json_valid(quoteDataJson) THEN quoteDataJson ELSE '{}' END,
1358
+ '$.amountIssued'
1359
+ ) AS TEXT),
1360
+ '0'
1361
+ )
1362
+ WHEN state = 'ISSUED'
1363
+ THEN COALESCE(CAST(amount AS TEXT), '0')
1364
+ ELSE '0'
1365
+ END,
1366
+ remoteUpdatedAt = NULL,
1367
+ quoteDataJson = CASE
1368
+ WHEN json_valid(quoteDataJson) THEN quoteDataJson
1369
+ ELSE '{}'
1370
+ END;
1326
1371
  `
1327
1372
  }
1328
1373
  ];
@@ -1914,7 +1959,29 @@ var SqliteMeltQuoteRepository = class {
1914
1959
  quoteId: quote.quoteId
1915
1960
  });
1916
1961
  if (identityOwner && identityOwner.method !== quote.method) throw new QuoteIdentityConflictError("melt", normalizedMintUrl, quote.quoteId, [identityOwner.method, quote.method], `Melt quote ${quote.quoteId} at ${normalizedMintUrl} already exists for method ${identityOwner.method}`);
1917
- await this.db.run(`INSERT INTO coco_cashu_melt_quotes
1962
+ const row = {
1963
+ mintUrl: normalizedMintUrl,
1964
+ method: quote.method,
1965
+ quoteId: quote.quoteId,
1966
+ state: quote.state,
1967
+ request: quote.request,
1968
+ amount: serializeAmount(quote.amount),
1969
+ unit: quote.unit,
1970
+ fee_reserve: quote.method === "onchain" ? null : serializeAmount(quote.fee_reserve),
1971
+ expiry: quote.expiry,
1972
+ payment_preimage: quote.method === "onchain" ? null : quote.payment_preimage ?? null,
1973
+ fee_options_json: quote.method === "onchain" ? stringifyJson(quote.fee_options.map((option) => ({
1974
+ ...option,
1975
+ fee_reserve: serializeAmount(option.fee_reserve)
1976
+ }))) : null,
1977
+ outpoint: quote.method === "onchain" ? quote.outpoint ?? null : null,
1978
+ changeJson: quote.change ? stringifyJson(quote.change) : null,
1979
+ lastObservedRemoteState: quote.lastObservedRemoteState ?? quote.state,
1980
+ lastObservedRemoteStateAt: quote.lastObservedRemoteStateAt ?? now,
1981
+ createdAt: quote.createdAt,
1982
+ updatedAt: quote.updatedAt || now
1983
+ };
1984
+ const persisted = await this.db.get(`INSERT INTO coco_cashu_melt_quotes
1918
1985
  (mintUrl, method, quoteId, state, request, amount, unit, fee_reserve, expiry,
1919
1986
  payment_preimage, fee_options_json, outpoint, changeJson, lastObservedRemoteState,
1920
1987
  lastObservedRemoteStateAt, createdAt, updatedAt)
@@ -1932,28 +1999,30 @@ var SqliteMeltQuoteRepository = class {
1932
1999
  changeJson=excluded.changeJson,
1933
2000
  lastObservedRemoteState=excluded.lastObservedRemoteState,
1934
2001
  lastObservedRemoteStateAt=excluded.lastObservedRemoteStateAt,
1935
- updatedAt=excluded.updatedAt`, [
1936
- normalizedMintUrl,
1937
- quote.method,
1938
- quote.quoteId,
1939
- quote.state,
1940
- quote.request,
1941
- serializeAmount(quote.amount),
1942
- quote.unit,
1943
- quote.method === "onchain" ? null : serializeAmount(quote.fee_reserve),
1944
- quote.expiry,
1945
- quote.method === "onchain" ? null : quote.payment_preimage ?? null,
1946
- quote.method === "onchain" ? stringifyJson(quote.fee_options.map((option) => ({
1947
- ...option,
1948
- fee_reserve: serializeAmount(option.fee_reserve)
1949
- }))) : null,
1950
- quote.method === "onchain" ? quote.outpoint ?? null : null,
1951
- quote.change ? stringifyJson(quote.change) : null,
1952
- quote.lastObservedRemoteState ?? quote.state,
1953
- quote.lastObservedRemoteStateAt ?? now,
1954
- quote.createdAt,
1955
- quote.updatedAt || now
2002
+ updatedAt=excluded.updatedAt
2003
+ RETURNING mintUrl, method, quoteId, state, request, amount, unit, fee_reserve, expiry,
2004
+ payment_preimage, fee_options_json, outpoint, changeJson,
2005
+ lastObservedRemoteState, lastObservedRemoteStateAt, createdAt, updatedAt`, [
2006
+ row.mintUrl,
2007
+ row.method,
2008
+ row.quoteId,
2009
+ row.state,
2010
+ row.request,
2011
+ row.amount,
2012
+ row.unit,
2013
+ row.fee_reserve ?? null,
2014
+ row.expiry,
2015
+ row.payment_preimage ?? null,
2016
+ row.fee_options_json ?? null,
2017
+ row.outpoint ?? null,
2018
+ row.changeJson ?? null,
2019
+ row.lastObservedRemoteState ?? null,
2020
+ row.lastObservedRemoteStateAt ?? null,
2021
+ row.createdAt,
2022
+ row.updatedAt
1956
2023
  ]);
2024
+ if (!persisted) throw new Error(`Melt quote ${quote.quoteId} for ${quote.method} at ${normalizedMintUrl} was not returned after persistence`);
2025
+ return rowToQuote(persisted);
1957
2026
  }
1958
2027
  async getPendingMeltQuotes(method) {
1959
2028
  return (await this.db.all(`SELECT mintUrl, method, quoteId, state, request, amount, unit, fee_reserve, expiry,
@@ -1988,19 +2057,21 @@ function rowToMintQuote(row) {
1988
2057
  expiry: row.expiry,
1989
2058
  pubkey,
1990
2059
  reusable: true,
2060
+ amountPaid: deserializeAmount(row.amountPaid),
2061
+ amountIssued: deserializeAmount(row.amountIssued),
2062
+ remoteUpdatedAt: row.remoteUpdatedAt,
1991
2063
  quoteData: {
1992
2064
  pubkey,
1993
- ...amount !== void 0 ? { amount } : {},
1994
- amountPaid: deserializeAmount(quoteData.amountPaid ?? 0),
1995
- amountIssued: deserializeAmount(quoteData.amountIssued ?? 0)
2065
+ ...amount !== void 0 ? { amount } : {}
1996
2066
  },
1997
- lastObservedRemoteStateAt: row.lastObservedRemoteStateAt ?? void 0,
1998
2067
  createdAt: row.createdAt,
1999
2068
  updatedAt: row.updatedAt
2000
2069
  };
2001
2070
  }
2002
2071
  const amount = deserializeAmount(quoteData.amount ?? row.amount ?? 0);
2003
- const state = row.state ?? row.lastObservedRemoteState ?? "UNPAID";
2072
+ const amountPaid = deserializeAmount(row.amountPaid);
2073
+ const amountIssued = deserializeAmount(row.amountIssued);
2074
+ const state = deriveBolt11MintQuoteState(amountPaid, amountIssued);
2004
2075
  return {
2005
2076
  mintUrl: row.mintUrl,
2006
2077
  method: "bolt11",
@@ -2012,9 +2083,10 @@ function rowToMintQuote(row) {
2012
2083
  unit: row.unit,
2013
2084
  expiry: row.expiry,
2014
2085
  pubkey: row.pubkey ?? void 0,
2015
- lastObservedRemoteState: row.lastObservedRemoteState ?? state,
2016
- lastObservedRemoteStateAt: row.lastObservedRemoteStateAt ?? void 0,
2017
2086
  reusable: false,
2087
+ amountPaid,
2088
+ amountIssued,
2089
+ remoteUpdatedAt: row.remoteUpdatedAt,
2018
2090
  quoteData: { amount },
2019
2091
  createdAt: row.createdAt,
2020
2092
  updatedAt: row.updatedAt
@@ -2026,16 +2098,10 @@ function serializeQuoteData(quote) {
2026
2098
  const amount = quote.quoteData.amount ?? quote.amount;
2027
2099
  return stringifyJson({
2028
2100
  pubkey: quote.quoteData.pubkey,
2029
- ...amount !== void 0 ? { amount: serializeAmount(amount) } : {},
2030
- amountPaid: serializeAmount(quote.quoteData.amountPaid),
2031
- amountIssued: serializeAmount(quote.quoteData.amountIssued)
2101
+ ...amount !== void 0 ? { amount: serializeAmount(amount) } : {}
2032
2102
  });
2033
2103
  }
2034
- return stringifyJson({
2035
- pubkey: quote.quoteData.pubkey,
2036
- amountPaid: serializeAmount(quote.quoteData.amountPaid),
2037
- amountIssued: serializeAmount(quote.quoteData.amountIssued)
2038
- });
2104
+ return stringifyJson({ pubkey: quote.quoteData.pubkey });
2039
2105
  }
2040
2106
  var SqliteMintQuoteRepository = class {
2041
2107
  db;
@@ -2045,7 +2111,7 @@ var SqliteMintQuoteRepository = class {
2045
2111
  async getMintQuoteById(identity) {
2046
2112
  const normalizedMintUrl = normalizeMintUrl(identity.mintUrl);
2047
2113
  const rows = await this.db.all(`SELECT mintUrl, method, quoteId, state, request, amount, unit, expiry, pubkey,
2048
- quoteDataJson, lastObservedRemoteState, lastObservedRemoteStateAt, reusable,
2114
+ quoteDataJson, amountPaid, amountIssued, remoteUpdatedAt, reusable,
2049
2115
  createdAt, updatedAt
2050
2116
  FROM coco_cashu_canonical_mint_quotes
2051
2117
  WHERE mintUrl = ? AND quoteId = ?`, [normalizedMintUrl, identity.quoteId]);
@@ -2054,7 +2120,7 @@ var SqliteMintQuoteRepository = class {
2054
2120
  }
2055
2121
  async getMintQuote(mintUrl, method, quoteId) {
2056
2122
  const row = await this.db.get(`SELECT mintUrl, method, quoteId, state, request, amount, unit, expiry, pubkey,
2057
- quoteDataJson, lastObservedRemoteState, lastObservedRemoteStateAt, reusable,
2123
+ quoteDataJson, amountPaid, amountIssued, remoteUpdatedAt, reusable,
2058
2124
  createdAt, updatedAt
2059
2125
  FROM coco_cashu_canonical_mint_quotes
2060
2126
  WHERE mintUrl = ? AND method = ? AND quoteId = ? LIMIT 1`, [
@@ -2076,8 +2142,8 @@ var SqliteMintQuoteRepository = class {
2076
2142
  const amount = getMintQuoteAmount(quote);
2077
2143
  await this.db.run(`INSERT INTO coco_cashu_canonical_mint_quotes
2078
2144
  (mintUrl, method, quoteId, state, request, amount, unit, expiry, pubkey, quoteDataJson,
2079
- lastObservedRemoteState, lastObservedRemoteStateAt, reusable, createdAt, updatedAt)
2080
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2145
+ amountPaid, amountIssued, remoteUpdatedAt, reusable, createdAt, updatedAt)
2146
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2081
2147
  ON CONFLICT(mintUrl, method, quoteId) DO UPDATE SET
2082
2148
  state=excluded.state,
2083
2149
  request=excluded.request,
@@ -2086,8 +2152,9 @@ var SqliteMintQuoteRepository = class {
2086
2152
  expiry=excluded.expiry,
2087
2153
  pubkey=excluded.pubkey,
2088
2154
  quoteDataJson=excluded.quoteDataJson,
2089
- lastObservedRemoteState=excluded.lastObservedRemoteState,
2090
- lastObservedRemoteStateAt=excluded.lastObservedRemoteStateAt,
2155
+ amountPaid=excluded.amountPaid,
2156
+ amountIssued=excluded.amountIssued,
2157
+ remoteUpdatedAt=excluded.remoteUpdatedAt,
2091
2158
  reusable=excluded.reusable,
2092
2159
  updatedAt=excluded.updatedAt`, [
2093
2160
  normalizedMintUrl,
@@ -2100,8 +2167,9 @@ var SqliteMintQuoteRepository = class {
2100
2167
  quote.expiry,
2101
2168
  quote.pubkey ?? null,
2102
2169
  serializeQuoteData(quote),
2103
- getMintQuoteRemoteState(quote) ?? null,
2104
- quote.lastObservedRemoteStateAt ?? now,
2170
+ serializeAmount(quote.amountPaid),
2171
+ serializeAmount(quote.amountIssued),
2172
+ quote.remoteUpdatedAt,
2105
2173
  quote.reusable ? 1 : 0,
2106
2174
  quote.createdAt,
2107
2175
  quote.updatedAt || now
@@ -2109,8 +2177,25 @@ var SqliteMintQuoteRepository = class {
2109
2177
  }
2110
2178
  async setMintQuoteState(mintUrl, method, quoteId, state, observedAt = Date.now()) {
2111
2179
  await this.db.run(`UPDATE coco_cashu_canonical_mint_quotes
2112
- SET state = ?, lastObservedRemoteState = ?, lastObservedRemoteStateAt = ?, updatedAt = ?
2113
- WHERE mintUrl = ? AND method = ? AND quoteId = ?`, [
2180
+ SET state = CASE
2181
+ WHEN ? = 'ISSUED' OR amountIssued = amount THEN 'ISSUED'
2182
+ WHEN ? IN ('PAID', 'ISSUED') OR amountPaid = amount THEN 'PAID'
2183
+ ELSE 'UNPAID'
2184
+ END,
2185
+ amountPaid = CASE WHEN ? IN ('PAID', 'ISSUED') THEN amount ELSE amountPaid END,
2186
+ amountIssued = CASE WHEN ? = 'ISSUED' THEN amount ELSE amountIssued END,
2187
+ updatedAt = CASE WHEN updatedAt > ? THEN updatedAt ELSE ? END
2188
+ WHERE mintUrl = ? AND method = ? AND quoteId = ?
2189
+ AND method = 'bolt11'
2190
+ AND amount IS NOT NULL
2191
+ AND remoteUpdatedAt IS NULL
2192
+ AND (
2193
+ (amountPaid = '0' AND amountIssued = '0') OR
2194
+ (amountPaid = amount AND amountIssued = '0') OR
2195
+ (amountPaid = amount AND amountIssued = amount)
2196
+ )`, [
2197
+ state,
2198
+ state,
2114
2199
  state,
2115
2200
  state,
2116
2201
  observedAt,
@@ -2122,10 +2207,10 @@ var SqliteMintQuoteRepository = class {
2122
2207
  }
2123
2208
  async getPendingMintQuotes(method) {
2124
2209
  return (await this.db.all(`SELECT mintUrl, method, quoteId, state, request, amount, unit, expiry, pubkey,
2125
- quoteDataJson, lastObservedRemoteState, lastObservedRemoteStateAt, reusable,
2210
+ quoteDataJson, amountPaid, amountIssued, remoteUpdatedAt, reusable,
2126
2211
  createdAt, updatedAt
2127
2212
  FROM coco_cashu_canonical_mint_quotes
2128
- WHERE (state IS NULL OR state != 'ISSUED') ${method ? "AND method = ?" : ""}`, method ? [method] : [])).map(rowToMintQuote).filter(isMintQuotePending);
2213
+ ${method ? "WHERE method = ?" : ""}`, method ? [method] : [])).map(rowToMintQuote).filter(isMintQuotePending);
2129
2214
  }
2130
2215
  };
2131
2216
 
@@ -2141,24 +2226,28 @@ var SqliteLegacyMintQuoteRepository = class {
2141
2226
  FROM coco_cashu_mint_quotes
2142
2227
  WHERE state != 'ISSUED' ${normalizedMintUrl ? "AND mintUrl = ?" : ""}`, normalizedMintUrl ? [normalizedMintUrl] : []);
2143
2228
  const now = Date.now();
2144
- return rows.map((row) => ({
2145
- mintUrl: row.mintUrl,
2146
- method: "bolt11",
2147
- quoteId: row.quote,
2148
- quote: row.quote,
2149
- state: row.state,
2150
- request: row.request,
2151
- amount: deserializeAmount(row.amount),
2152
- unit: row.unit,
2153
- expiry: row.expiry,
2154
- pubkey: row.pubkey ?? void 0,
2155
- lastObservedRemoteState: row.state,
2156
- lastObservedRemoteStateAt: now,
2157
- reusable: false,
2158
- quoteData: { amount: deserializeAmount(row.amount) },
2159
- createdAt: now,
2160
- updatedAt: now
2161
- }));
2229
+ return rows.map((row) => {
2230
+ const amount = deserializeAmount(row.amount);
2231
+ return {
2232
+ mintUrl: row.mintUrl,
2233
+ method: "bolt11",
2234
+ quoteId: row.quote,
2235
+ quote: row.quote,
2236
+ state: row.state,
2237
+ request: row.request,
2238
+ amount,
2239
+ unit: row.unit,
2240
+ expiry: row.expiry,
2241
+ pubkey: row.pubkey ?? void 0,
2242
+ reusable: false,
2243
+ amountPaid: row.state === "PAID" ? amount : Amount.zero(),
2244
+ amountIssued: Amount.zero(),
2245
+ remoteUpdatedAt: null,
2246
+ quoteData: { amount },
2247
+ createdAt: now,
2248
+ updatedAt: now
2249
+ };
2250
+ });
2162
2251
  }
2163
2252
  };
2164
2253
 
@@ -2232,7 +2321,7 @@ const projectionSelect = `
2232
2321
  NULL AS inputProofsJson,
2233
2322
  NULL AS metadata,
2234
2323
  op.id AS operationId,
2235
- q.lastObservedRemoteState AS remoteState,
2324
+ q.state AS remoteState,
2236
2325
  op.error
2237
2326
  FROM coco_cashu_mint_operations op
2238
2327
  LEFT JOIN coco_cashu_canonical_mint_quotes q
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashu/coco-expo-sqlite",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,8 +15,8 @@
15
15
  "README.md"
16
16
  ],
17
17
  "devDependencies": {
18
- "@cashu/coco-adapter-tests": "2.0.0-rc.2",
19
- "@cashu/coco-sql-storage": "2.0.0-rc.2"
18
+ "@cashu/coco-adapter-tests": "2.0.0",
19
+ "@cashu/coco-sql-storage": "2.0.0"
20
20
  },
21
21
  "exports": {
22
22
  ".": {
@@ -31,9 +31,9 @@
31
31
  "test": "bun test"
32
32
  },
33
33
  "peerDependencies": {
34
- "@cashu/cashu-ts": "4.5",
34
+ "@cashu/cashu-ts": "5.0.0-rc.4",
35
35
  "typescript": "^5",
36
36
  "expo-sqlite": "*",
37
- "@cashu/coco-core": "2.0.0-rc.2"
37
+ "@cashu/coco-core": "2.0.0"
38
38
  }
39
39
  }