aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r--packages/taler-wallet-core/src/util/coinSelection.ts62
-rw-r--r--packages/taler-wallet-core/src/util/instructedAmountConversion.ts2
-rw-r--r--packages/taler-wallet-core/src/util/query.ts109
3 files changed, 105 insertions, 68 deletions
diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts
index daba2ead5..ef2f85789 100644
--- a/packages/taler-wallet-core/src/util/coinSelection.ts
+++ b/packages/taler-wallet-core/src/util/coinSelection.ts
@@ -588,22 +588,22 @@ async function selectPayMerchantCandidates(
for (const acc of exchangeDetails.wireInfo.accounts) {
const pp = parsePaytoUri(acc.payto_uri);
checkLogicInvariant(!!pp);
- if (pp.targetType === req.wireMethod) {
- // also check that wire method is supported now
- const wireFeeStr = exchangeDetails.wireInfo.feesForType[
- req.wireMethod
- ]?.find((x) => {
- return AbsoluteTime.isBetween(
- AbsoluteTime.now(),
- AbsoluteTime.fromProtocolTimestamp(x.startStamp),
- AbsoluteTime.fromProtocolTimestamp(x.endStamp),
- );
- })?.wireFee;
- if (wireFeeStr) {
- wireMethodFee = wireFeeStr;
- }
- break;
+ if (pp.targetType !== req.wireMethod) {
+ continue;
+ }
+ const wireFeeStr = exchangeDetails.wireInfo.feesForType[
+ req.wireMethod
+ ]?.find((x) => {
+ return AbsoluteTime.isBetween(
+ AbsoluteTime.now(),
+ AbsoluteTime.fromProtocolTimestamp(x.startStamp),
+ AbsoluteTime.fromProtocolTimestamp(x.endStamp),
+ );
+ })?.wireFee;
+ if (wireFeeStr) {
+ wireMethodFee = wireFeeStr;
}
+ break;
}
if (!wireMethodFee) {
break;
@@ -699,25 +699,17 @@ export function selectWithdrawalDenominations(
let totalWithdrawCost = Amounts.zeroOfCurrency(amountAvailable.currency);
denoms = denoms.filter((d) => isWithdrawableDenom(d, denomselAllowLate));
- denoms.sort((d1, d2) =>
- Amounts.cmp(
- DenominationRecord.getValue(d2),
- DenominationRecord.getValue(d1),
- ),
- );
+ denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value));
for (const d of denoms) {
- const cost = Amounts.add(
- DenominationRecord.getValue(d),
- d.fees.feeWithdraw,
- ).amount;
+ const cost = Amounts.add(d.value, d.fees.feeWithdraw).amount;
const res = Amounts.divmod(remaining, cost);
const count = res.quotient;
remaining = Amounts.sub(remaining, Amounts.mult(cost, count).amount).amount;
if (count > 0) {
totalCoinValue = Amounts.add(
totalCoinValue,
- Amounts.mult(DenominationRecord.getValue(d), count).amount,
+ Amounts.mult(d.value, count).amount,
).amount;
totalWithdrawCost = Amounts.add(
totalWithdrawCost,
@@ -766,30 +758,22 @@ export function selectForcedWithdrawalDenominations(
let totalWithdrawCost = Amounts.zeroOfCurrency(amountAvailable.currency);
denoms = denoms.filter((d) => isWithdrawableDenom(d, denomselAllowLate));
- denoms.sort((d1, d2) =>
- Amounts.cmp(
- DenominationRecord.getValue(d2),
- DenominationRecord.getValue(d1),
- ),
- );
+ denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value));
for (const fds of forcedDenomSel.denoms) {
const count = fds.count;
const denom = denoms.find((x) => {
- return Amounts.cmp(DenominationRecord.getValue(x), fds.value) == 0;
+ return Amounts.cmp(x.value, fds.value) == 0;
});
if (!denom) {
throw Error(
`unable to find denom for forced selection (value ${fds.value})`,
);
}
- const cost = Amounts.add(
- DenominationRecord.getValue(denom),
- denom.fees.feeWithdraw,
- ).amount;
+ const cost = Amounts.add(denom.value, denom.fees.feeWithdraw).amount;
totalCoinValue = Amounts.add(
totalCoinValue,
- Amounts.mult(DenominationRecord.getValue(denom), count).amount,
+ Amounts.mult(denom.value, count).amount,
).amount;
totalWithdrawCost = Amounts.add(
totalWithdrawCost,
@@ -1002,7 +986,7 @@ export async function selectPeerCoins(
x.coinAvailability,
x.denominations,
x.refreshGroups,
- x.peerPushPaymentInitiations,
+ x.peerPushDebit,
])
.runReadWrite(async (tx) => {
const exchanges = await tx.exchanges.iter().toArray();
diff --git a/packages/taler-wallet-core/src/util/instructedAmountConversion.ts b/packages/taler-wallet-core/src/util/instructedAmountConversion.ts
index bd02e7b22..54c08eee4 100644
--- a/packages/taler-wallet-core/src/util/instructedAmountConversion.ts
+++ b/packages/taler-wallet-core/src/util/instructedAmountConversion.ts
@@ -321,7 +321,7 @@ function buildCoinInfoFromDenom(
AbsoluteTime.fromProtocolTimestamp(denom.stampExpireDeposit),
),
totalAvailable: total,
- value: DenominationRecord.getValue(denom),
+ value: Amounts.parseOrThrow(denom.value),
maxAge,
};
}
diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts
index 1c3ff6a2a..309c17a43 100644
--- a/packages/taler-wallet-core/src/util/query.ts
+++ b/packages/taler-wallet-core/src/util/query.ts
@@ -35,7 +35,7 @@ import {
IDBKeyPath,
IDBKeyRange,
} from "@gnu-taler/idb-bridge";
-import { Logger, j2s } from "@gnu-taler/taler-util";
+import { Codec, Logger, j2s } from "@gnu-taler/taler-util";
const logger = new Logger("query.ts");
@@ -387,11 +387,11 @@ export interface StoreReadWriteAccessor<RecordType, IndexMap> {
export interface StoreWithIndexes<
StoreName extends string,
- SD extends StoreDescriptor<unknown>,
+ RecordType,
IndexMap,
> {
storeName: StoreName;
- store: SD;
+ store: StoreDescriptor<RecordType>;
indexMap: IndexMap;
/**
@@ -401,19 +401,13 @@ export interface StoreWithIndexes<
mark: Symbol;
}
-export type GetRecordType<T> = T extends StoreDescriptor<infer X> ? X : unknown;
-
const storeWithIndexesSymbol = Symbol("StoreWithIndexesMark");
-export function describeStore<
- StoreName extends string,
- SD extends StoreDescriptor<unknown>,
- IndexMap,
->(
+export function describeStore<StoreName extends string, RecordType, IndexMap>(
name: StoreName,
- s: SD,
+ s: StoreDescriptor<RecordType>,
m: IndexMap,
-): StoreWithIndexes<StoreName, SD, IndexMap> {
+): StoreWithIndexes<StoreName, RecordType, IndexMap> {
return {
storeName: name,
store: s,
@@ -422,13 +416,72 @@ export function describeStore<
};
}
+export function describeStoreV2<
+ StoreName extends string,
+ RecordType,
+ IndexMap extends { [x: string]: IndexDescriptor } = {},
+>(args: {
+ storeName: StoreName;
+ recordCodec: Codec<RecordType>;
+ keyPath?: IDBKeyPath | IDBKeyPath[];
+ autoIncrement?: boolean;
+ /**
+ * Database version that this store was added in, or
+ * undefined if added in the first version.
+ */
+ versionAdded?: number;
+ indexes?: IndexMap;
+}): StoreWithIndexes<StoreName, RecordType, IndexMap> {
+ return {
+ storeName: args.storeName,
+ store: {
+ _dummy: undefined as any,
+ autoIncrement: args.autoIncrement,
+ keyPath: args.keyPath,
+ versionAdded: args.versionAdded,
+ },
+ indexMap: args.indexes ?? ({} as IndexMap),
+ mark: storeWithIndexesSymbol,
+ };
+}
+
+type KeyPathComponents = string | number;
+
+/**
+ * Follow a key path (dot-separated) in an object.
+ */
+type DerefKeyPath<T, P> = P extends `${infer PX extends keyof T &
+ KeyPathComponents}`
+ ? T[PX]
+ : P extends `${infer P0 extends keyof T & KeyPathComponents}.${infer Rest}`
+ ? DerefKeyPath<T[P0], Rest>
+ : unknown;
+
+/**
+ * Return a path if it is a valid dot-separate path to an object.
+ * Otherwise, return "never".
+ */
+type ValidateKeyPath<T, P> = P extends `${infer PX extends keyof T &
+ KeyPathComponents}`
+ ? PX
+ : P extends `${infer P0 extends keyof T & KeyPathComponents}.${infer Rest}`
+ ? `${P0}.${ValidateKeyPath<T[P0], Rest>}`
+ : never;
+
+// function foo<T, P>(
+// x: T,
+// p: P extends ValidateKeyPath<T, P> ? P : never,
+// ): void {}
+
+// foo({x: [0,1,2]}, "x.0");
+
export type GetReadOnlyAccess<BoundStores> = {
[P in keyof BoundStores]: BoundStores[P] extends StoreWithIndexes<
- infer SN,
- infer SD,
- infer IM
+ infer StoreName,
+ infer RecordType,
+ infer IndexMap
>
- ? StoreReadOnlyAccessor<GetRecordType<SD>, IM>
+ ? StoreReadOnlyAccessor<RecordType, IndexMap>
: unknown;
};
@@ -446,11 +499,11 @@ export type DbReadOnlyTransaction<
}
? {
[P in Stores]: StoreMap[P] extends StoreWithIndexes<
- infer SN,
- infer SD,
- infer IM
+ infer StoreName,
+ infer RecordType,
+ infer IndexMap
>
- ? StoreReadOnlyAccessor<GetRecordType<SD>, IM>
+ ? StoreReadOnlyAccessor<RecordType, IndexMap>
: unknown;
}
: unknown;
@@ -463,22 +516,22 @@ export type DbReadWriteTransaction<
}
? {
[P in Stores]: StoreMap[P] extends StoreWithIndexes<
- infer SN,
- infer SD,
- infer IM
+ infer StoreName,
+ infer RecordType,
+ infer IndexMap
>
- ? StoreReadWriteAccessor<GetRecordType<SD>, IM>
+ ? StoreReadWriteAccessor<RecordType, IndexMap>
: unknown;
}
: unknown;
export type GetReadWriteAccess<BoundStores> = {
[P in keyof BoundStores]: BoundStores[P] extends StoreWithIndexes<
- infer SN,
- infer SD,
- infer IM
+ infer StoreName,
+ infer RecordType,
+ infer IndexMap
>
- ? StoreReadWriteAccessor<GetRecordType<SD>, IM>
+ ? StoreReadWriteAccessor<RecordType, IndexMap>
: unknown;
};