1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
This file is part of GNU Taler
(C) 2021-2024 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Selection of denominations for withdrawals.
*
* @author Florian Dold
*/
/**
* Imports.
*/
import {
AbsoluteTime,
AmountJson,
Amounts,
DenomSelectionState,
ForcedDenomSel,
Logger,
} from "@gnu-taler/taler-util";
import { DenominationRecord, timestampAbsoluteFromDb } from "./db.js";
import { isWithdrawableDenom } from "./denominations.js";
const logger = new Logger("denomSelection.ts");
/**
* Get a list of denominations (with repetitions possible)
* whose total value is as close as possible to the available
* amount, but never larger.
*/
export function selectWithdrawalDenominations(
amountAvailable: AmountJson,
denoms: DenominationRecord[],
denomselAllowLate: boolean = false,
): DenomSelectionState {
let remaining = Amounts.copy(amountAvailable);
const selectedDenoms: {
count: number;
denomPubHash: string;
}[] = [];
let totalCoinValue = Amounts.zeroOfCurrency(amountAvailable.currency);
let totalWithdrawCost = Amounts.zeroOfCurrency(amountAvailable.currency);
let earliestDepositExpiration: AbsoluteTime | undefined;
let hasDenomWithAgeRestriction = false;
denoms = denoms.filter((d) => isWithdrawableDenom(d, denomselAllowLate));
denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value));
if (logger.shouldLogTrace()) {
logger.trace(
`selecting withdrawal denoms for ${Amounts.stringify(amountAvailable)}`,
);
}
for (const d of denoms) {
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(d.value, count).amount,
).amount;
totalWithdrawCost = Amounts.add(
totalWithdrawCost,
Amounts.mult(cost, count).amount,
).amount;
selectedDenoms.push({
count,
denomPubHash: d.denomPubHash,
});
hasDenomWithAgeRestriction =
hasDenomWithAgeRestriction || d.denomPub.age_mask > 0;
const expireDeposit = timestampAbsoluteFromDb(d.stampExpireDeposit);
if (!earliestDepositExpiration) {
earliestDepositExpiration = expireDeposit;
} else {
earliestDepositExpiration = AbsoluteTime.min(
expireDeposit,
earliestDepositExpiration,
);
}
}
if (logger.shouldLogTrace()) {
logger.trace(
`denom_pub_hash=${
d.denomPubHash
}, count=${count}, val=${Amounts.stringify(
d.value,
)}, wdfee=${Amounts.stringify(d.fees.feeWithdraw)}`,
);
}
if (Amounts.isZero(remaining)) {
break;
}
}
if (logger.shouldLogTrace()) {
logger.trace("(end of denom selection)");
}
earliestDepositExpiration ??= AbsoluteTime.never();
return {
selectedDenoms,
totalCoinValue: Amounts.stringify(totalCoinValue),
totalWithdrawCost: Amounts.stringify(totalWithdrawCost),
hasDenomWithAgeRestriction,
};
}
export function selectForcedWithdrawalDenominations(
amountAvailable: AmountJson,
denoms: DenominationRecord[],
forcedDenomSel: ForcedDenomSel,
denomselAllowLate: boolean,
): DenomSelectionState {
const selectedDenoms: {
count: number;
denomPubHash: string;
}[] = [];
let totalCoinValue = Amounts.zeroOfCurrency(amountAvailable.currency);
let totalWithdrawCost = Amounts.zeroOfCurrency(amountAvailable.currency);
let earliestDepositExpiration: AbsoluteTime | undefined;
let hasDenomWithAgeRestriction = false;
denoms = denoms.filter((d) => isWithdrawableDenom(d, denomselAllowLate));
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(x.value, fds.value) == 0;
});
if (!denom) {
throw Error(
`unable to find denom for forced selection (value ${fds.value})`,
);
}
const cost = Amounts.add(denom.value, denom.fees.feeWithdraw).amount;
totalCoinValue = Amounts.add(
totalCoinValue,
Amounts.mult(denom.value, count).amount,
).amount;
totalWithdrawCost = Amounts.add(
totalWithdrawCost,
Amounts.mult(cost, count).amount,
).amount;
selectedDenoms.push({
count,
denomPubHash: denom.denomPubHash,
});
hasDenomWithAgeRestriction =
hasDenomWithAgeRestriction || denom.denomPub.age_mask > 0;
const expireDeposit = timestampAbsoluteFromDb(denom.stampExpireDeposit);
if (!earliestDepositExpiration) {
earliestDepositExpiration = expireDeposit;
} else {
earliestDepositExpiration = AbsoluteTime.min(
expireDeposit,
earliestDepositExpiration,
);
}
}
earliestDepositExpiration ??= AbsoluteTime.never();
return {
selectedDenoms,
totalCoinValue: Amounts.stringify(totalCoinValue),
totalWithdrawCost: Amounts.stringify(totalWithdrawCost),
hasDenomWithAgeRestriction,
};
}
|