diff options
author | Florian Dold <florian@dold.me> | 2024-03-25 20:31:56 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-03-25 20:31:56 +0100 |
commit | bd70ccfddfb9f993a5951a31be5bdc982fe1a58f (patch) | |
tree | bc631a948ec7ce5e52619c850622e8f91df4d7e7 /packages/taler-util/src | |
parent | 31b7ce31a3d34149d2397f999a86c14100bd72ac (diff) | |
download | wallet-core-bd70ccfddfb9f993a5951a31be5bdc982fe1a58f.tar.xz |
wallet-core: re-denomination of withdrawal groups
Diffstat (limited to 'packages/taler-util/src')
-rw-r--r-- | packages/taler-util/src/amounts.ts | 50 | ||||
-rw-r--r-- | packages/taler-util/src/transactions-types.ts | 8 | ||||
-rw-r--r-- | packages/taler-util/src/wallet-types.ts | 15 |
3 files changed, 67 insertions, 6 deletions
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts index 4b7063fd2..82a3d3b68 100644 --- a/packages/taler-util/src/amounts.ts +++ b/packages/taler-util/src/amounts.ts @@ -76,6 +76,48 @@ export interface AmountJson { readonly currency: string; } +/** + * Immutable amount. + */ +export class Amount { + static from(a: AmountLike): Amount { + return new Amount(Amounts.parseOrThrow(a), 0); + } + + static zeroOfCurrency(currency: string): Amount { + return new Amount(Amounts.zeroOfCurrency(currency), 0); + } + + add(...a: AmountLike[]): Amount { + if (this.saturated) { + return this; + } + const r = Amounts.add(this.val, ...a); + return new Amount(r.amount, r.saturated ? 1 : 0); + } + + mult(n: number): Amount { + if (this.saturated) { + return this; + } + const r = Amounts.mult(this, n); + return new Amount(r.amount, r.saturated ? 1 : 0); + } + + toJson(): AmountJson { + return { ...this.val }; + } + + toString(): AmountString { + return Amounts.stringify(this.val); + } + + private constructor( + private val: AmountJson, + private saturated: number, + ) {} +} + export const codecForAmountJson = (): Codec<AmountJson> => buildCodecForObject<AmountJson>() .property("currency", codecForString()) @@ -118,7 +160,7 @@ export interface Result { /** * Type for things that are treated like amounts. */ -export type AmountLike = string | AmountString | AmountJson; +export type AmountLike = string | AmountString | AmountJson | Amount; export interface DivmodResult { quotient: number; @@ -162,6 +204,9 @@ export class Amounts { if (typeof amt === "string") { return Amounts.parseOrThrow(amt); } + if (amt instanceof Amount) { + return amt.toJson(); + } return amt; } @@ -406,6 +451,9 @@ export class Amounts { * throw if the input is not a valid amount. */ static parseOrThrow(s: AmountLike): AmountJson { + if (s instanceof Amount) { + return s.toJson(); + } if (typeof s === "object") { if (typeof s.currency !== "string") { throw Error("invalid amount object"); diff --git a/packages/taler-util/src/transactions-types.ts b/packages/taler-util/src/transactions-types.ts index c5d838809..8c4c2c7ed 100644 --- a/packages/taler-util/src/transactions-types.ts +++ b/packages/taler-util/src/transactions-types.ts @@ -77,8 +77,13 @@ export interface TransactionsRequest { * Sort order of the transaction items. * By default, items are sorted ascending by their * main timestamp. + * + * ascending: ascending by timestamp, but pending transactions first + * descending: ascending by timestamp, but pending transactions first + * stable-ascending: ascending by timestamp, with pending transactions amidst other transactions + * (stable in the sense of: pending transactions don't jump around) */ - sort?: "ascending" | "descending"; + sort?: "ascending" | "descending" | "stable-ascending"; /** * If true, include all refreshes in the transactions list. @@ -747,6 +752,7 @@ export const codecForTransactionsRequest = (): Codec<TransactionsRequest> => codecForEither( codecForConstString("ascending"), codecForConstString("descending"), + codecForConstString("stable-ascending"), ), ), ) diff --git a/packages/taler-util/src/wallet-types.ts b/packages/taler-util/src/wallet-types.ts index 0b09b0dbf..723e5a282 100644 --- a/packages/taler-util/src/wallet-types.ts +++ b/packages/taler-util/src/wallet-types.ts @@ -1548,16 +1548,23 @@ export interface WithdrawalDetailsForAmount { scopeInfo: ScopeInfo; } +export interface DenomSelItem { + denomPubHash: string; + count: number; + /** + * Number of denoms/planchets to skip, because + * a re-denomination effectively deleted them. + */ + skip?: number; +} + /** * Selected denominations withn some extra info. */ export interface DenomSelectionState { totalCoinValue: AmountString; totalWithdrawCost: AmountString; - selectedDenoms: { - denomPubHash: string; - count: number; - }[]; + selectedDenoms: DenomSelItem[]; } /** |