aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/wallet/ExchangeSelection/state.ts
blob: f077f45a456c188bd3d0bacb81a687dbee1f45bc (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 This file is part of GNU Taler
 (C) 2022 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/>
 */


import { AbsoluteTime, AmountJson, Amounts, DenominationInfo, TalerProtocolTimestamp } from "@gnu-taler/taler-util";
import { useState } from "preact/hooks";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import * as wxApi from "../../wxApi.js";
import { FeeDescription, FeeDescriptionPair, OperationMap, Props, State } from "./index.js";

export function useComponentState(
  { onCancel, onSelection, currency }: Props,
  api: typeof wxApi,
): State {
  const hook = useAsyncAsHook(api.listExchanges);

  const initialValue = 0
  const [value, setValue] = useState(String(initialValue));

  if (!hook) {
    return {
      status: "loading",
      error: undefined,
    }
  }
  if (hook.hasError) {
    return {
      status: "loading-uri",
      error: hook,
    };
  }

  const exchanges = hook.response.exchanges;

  if (exchanges.length === 0) {
    return {
      status: "no-exchanges",
      error: undefined
    }
  }

  const original = exchanges[initialValue];
  const selected = exchanges[Number(value)];

  let nextFeeUpdate = TalerProtocolTimestamp.never();

  nextFeeUpdate = Object.values(selected.wireInfo.feesForType).reduce(
    (prev, cur) => {
      return cur.reduce((p, c) => nearestTimestamp(p, c.endStamp), prev);
    },
    nextFeeUpdate,
  );

  nextFeeUpdate = selected.denominations.reduce((prev, cur) => {
    return [
      cur.stampExpireWithdraw,
      cur.stampExpireLegal,
      cur.stampExpireDeposit,
    ].reduce(nearestTimestamp, prev);
  }, nextFeeUpdate);

  const timeline: OperationMap<FeeDescription[]> = {
    deposit: createDenominationTimeline(
      selected.denominations,
      "stampExpireDeposit",
      "feeDeposit",
    ),
    refresh: createDenominationTimeline(
      selected.denominations,
      "stampExpireWithdraw",
      "feeRefresh",
    ),
    refund: createDenominationTimeline(
      selected.denominations,
      "stampExpireWithdraw",
      "feeRefund",
    ),
    withdraw: createDenominationTimeline(
      selected.denominations,
      "stampExpireWithdraw",
      "feeWithdraw",
    ),
  };

  const exchangeMap = exchanges.reduce((prev, cur, idx) => ({ ...prev, [cur.exchangeBaseUrl]: String(idx) }), {} as Record<string, string>)

  if (original === selected) {
    return {
      status: "ready",
      exchanges: {
        list: exchangeMap,
        value: value,
        onChange: async (v) => {
          setValue(v)
        }
      },
      error: undefined,
      nextFeeUpdate: AbsoluteTime.fromTimestamp(nextFeeUpdate),
      onClose: {
        onClick: onCancel
      },
      selected,
      timeline
    }
  }

  const originalTimeline: OperationMap<FeeDescription[]> = {
    deposit: createDenominationTimeline(
      original.denominations,
      "stampExpireDeposit",
      "feeDeposit",
    ),
    refresh: createDenominationTimeline(
      original.denominations,
      "stampExpireWithdraw",
      "feeRefresh",
    ),
    refund: createDenominationTimeline(
      original.denominations,
      "stampExpireWithdraw",
      "feeRefund",
    ),
    withdraw: createDenominationTimeline(
      original.denominations,
      "stampExpireWithdraw",
      "feeWithdraw",
    ),
  };
  const pairTimeline: OperationMap<FeeDescription[]> = {
    deposit: createDenominationPairTimeline(timeline.deposit, originalTimeline.deposit),
    refresh: createDenominationPairTimeline(timeline.refresh, originalTimeline.refresh),
    refund: createDenominationPairTimeline(timeline.refund, originalTimeline.refund),
    withdraw: createDenominationPairTimeline(timeline.withdraw, originalTimeline.withdraw),
  }

  return {
    status: "comparing",
    exchanges: {
      list: exchangeMap,
      value: value,
      onChange: async (v) => {
        setValue(v)
      }
    },
    error: undefined,
    nextFeeUpdate: AbsoluteTime.fromTimestamp(nextFeeUpdate),
    onReset: {
      onClick: async () => {
        setValue(String(initialValue))
      }
    },
    onSelect: {
      onClick: async () => {
        onSelection(selected.exchangeBaseUrl)
      }
    },
    selected,
    pairTimeline,
  }

}

function nearestTimestamp(
  first: TalerProtocolTimestamp,
  second: TalerProtocolTimestamp,
): TalerProtocolTimestamp {
  const f = AbsoluteTime.fromTimestamp(first);
  const s = AbsoluteTime.fromTimestamp(second);
  const a = AbsoluteTime.min(f, s);
  return AbsoluteTime.toTimestamp(a);
}



export interface TimePoint {
  type: "start" | "end";
  moment: AbsoluteTime;
  denom: DenominationInfo;
}

/**
 * Given a list of denominations with the same value and same period of time:
 * return the one that will be used.
 * The best denomination is the one that will minimize the fee cost.
 *
 * @param list denominations of same value
 * @returns
 */
function selectBestForOverlappingDenominations(
  list: DenominationInfo[],
): DenominationInfo | undefined {
  let minDeposit: DenominationInfo | undefined = undefined;
  list.forEach((e) => {
    if (minDeposit === undefined) {
      minDeposit = e;
      return;
    }
    if (Amounts.cmp(minDeposit.feeDeposit, e.feeDeposit) > -1) {
      minDeposit = e;
    }
  });
  return minDeposit;
}

type PropsWithReturnType<T extends object, F> = Exclude<
  {
    [K in keyof T]: T[K] extends F ? K : never;
  }[keyof T],
  undefined
>;

/**
 * Takes two list and create one with one timeline.
 * For any element in the position "p" on the left or right "list", then
 * list[p].until should be equal to list[p+1].from
 * 
 * @see {createDenominationTimeline}
 * 
 * @param left list denominations @type {FeeDescription}
 * @param right list denominations @type {FeeDescription}
 * @returns list of pairs for the same time
 */
export function createDenominationPairTimeline(left: FeeDescription[], right: FeeDescription[]): FeeDescriptionPair[] {
  //both list empty, discarded
  if (left.length === 0 && right.length === 0) return [];

  const pairList: FeeDescriptionPair[] = [];

  let li = 0;
  let ri = 0;

  while (li < left.length && ri < right.length) {
    const currentValue = Amounts.cmp(left[li].value, right[ri].value) < 0 ? left[li].value : right[ri].value;

    let ll = 0 //left length (until next value)
    while (li + ll < left.length && Amounts.cmp(left[li + ll].value, currentValue) === 0) {
      ll++
    }
    let rl = 0 //right length (until next value)
    while (ri + rl < right.length && Amounts.cmp(right[ri + rl].value, currentValue) === 0) {
      rl++
    }
    const leftIsEmpty = ll === 0
    const rightIsEmpty = rl === 0
    //check which start after, add gap so both list starts at the same time
    // one list may be empty
    const leftStarts: AbsoluteTime =
      leftIsEmpty ? { t_ms: "never" } : left[li].from;
    const rightStarts: AbsoluteTime =
      rightIsEmpty ? { t_ms: "never" } : right[ri].from;

    //first time cut is the smallest time
    let timeCut: AbsoluteTime = leftStarts;

    if (AbsoluteTime.cmp(leftStarts, rightStarts) < 0) {
      const ends =
        rightIsEmpty ? left[li + ll - 1].until : right[0].from;

      right.splice(ri, 0, {
        from: leftStarts,
        until: ends,
        value: left[li].value,
      });
      rl++;

      timeCut = leftStarts
    }
    if (AbsoluteTime.cmp(leftStarts, rightStarts) > 0) {
      const ends =
        leftIsEmpty ? right[ri + rl - 1].until : left[0].from;

      left.splice(li, 0, {
        from: rightStarts,
        until: ends,
        value: right[ri].value,
      });
      ll++;

      timeCut = rightStarts
    }

    //check which ends sooner, add gap so both list ends at the same time
    // here both list are non empty
    const leftEnds: AbsoluteTime = left[li + ll - 1].until;
    const rightEnds: AbsoluteTime = right[ri + rl - 1].until;

    if (AbsoluteTime.cmp(leftEnds, rightEnds) > 0) {
      right.splice(ri + rl, 0, {
        from: rightEnds,
        until: leftEnds,
        value: left[0].value,
      });
      rl++;

    }
    if (AbsoluteTime.cmp(leftEnds, rightEnds) < 0) {
      left.splice(li + ll, 0, {
        from: leftEnds,
        until: rightEnds,
        value: right[0].value,
      });
      ll++;
    }

    //now both lists are non empty and (starts,ends) at the same time
    while (li < left.length && ri < right.length && Amounts.cmp(left[li].value, right[ri].value) === 0) {
      // console.log('start', li, ri, left[li], right[ri])

      if (AbsoluteTime.cmp(left[li].from, timeCut) !== 0 && AbsoluteTime.cmp(right[ri].from, timeCut) !== 0) {
        // timeCut comes from the latest "until" (expiration from the previous)
        // and this value comes from the latest left or right
        // it should be the same as the "from" from one of the latest left or right
        // otherwise it means that there is missing a gap object in the middle
        // the list is not complete and the behavior is undefined
        console.log(li, ri, timeCut)
        throw Error('one of the list is not completed: list[i].until !== list[i+1].from')
      }

      pairList.push({
        left: left[li].fee,
        right: right[ri].fee,
        from: timeCut,
        until: AbsoluteTime.never(),
        value: currentValue,
      });

      if (left[li].until.t_ms === right[ri].until.t_ms) {
        timeCut = left[li].until;
        ri++;
        li++;
      } else if (left[li].until.t_ms < right[ri].until.t_ms) {
        timeCut = left[li].until;
        li++;
      } else if (left[li].until.t_ms > right[ri].until.t_ms) {
        timeCut = right[ri].until;
        ri++;
      }
      pairList[pairList.length - 1].until = timeCut

      if (li < left.length && Amounts.cmp(left[li].value, pairList[pairList.length - 1].value) !== 0) {
        //value changed, should break
        //this if will catch when both (left and right) change at the same time
        //if just one side changed it will catch in the while condition
        break;
      }

    }

  }
  //one of the list left or right can still have elements
  if (li < left.length) {
    let timeCut = pairList.length > 0 && Amounts.cmp(pairList[pairList.length - 1].value, left[li].value) === 0 ? pairList[pairList.length - 1].until : left[li].from;
    while (li < left.length) {
      pairList.push({
        left: left[li].fee,
        right: undefined,
        from: timeCut,
        until: left[li].until,
        value: left[li].value,
      })
      timeCut = left[li].until
      li++;
    }
  }
  if (ri < right.length) {
    let timeCut = pairList.length > 0 && Amounts.cmp(pairList[pairList.length - 1].value, right[ri].value) === 0 ? pairList[pairList.length - 1].until : right[ri].from;
    while (ri < right.length) {
      pairList.push({
        right: right[ri].fee,
        left: undefined,
        from: timeCut,
        until: right[ri].until,
        value: right[ri].value,
      })
      timeCut = right[ri].until
      ri++;
    }
  }
  return pairList
}

/**
 * Create a usage timeline with the denominations given.
 *
 * If there are multiple denominations that can be used, the list will
 * contain the one that minimize the fee cost. @see selectBestForOverlappingDenominations
 *
 * @param list list of denominations
 * @param periodProp property of element of the list that will be used as end of the usage period
 * @param feeProp property of the element of the list that will be used as fee reference
 * @returns  list of @type {FeeDescription} sorted by usage period
 */
export function createDenominationTimeline(
  list: DenominationInfo[],
  periodProp: PropsWithReturnType<DenominationInfo, TalerProtocolTimestamp>,
  feeProp: PropsWithReturnType<DenominationInfo, AmountJson>,
): FeeDescription[] {
  const points = list
    .reduce((ps, denom) => {
      //exclude denoms with bad configuration
      if (denom.stampStart.t_s >= denom[periodProp].t_s) {
        throw Error(`denom ${denom.denomPubHash} has start after the end`);
        // return ps;
      }
      ps.push({
        type: "start",
        moment: AbsoluteTime.fromTimestamp(denom.stampStart),
        denom,
      });
      ps.push({
        type: "end",
        moment: AbsoluteTime.fromTimestamp(denom[periodProp]),
        denom,
      });
      return ps;
    }, [] as TimePoint[])
    .sort((a, b) => {
      const v = Amounts.cmp(a.denom.value, b.denom.value);
      if (v != 0) return v;
      const t = AbsoluteTime.cmp(a.moment, b.moment);
      if (t != 0) return t;
      if (a.type === b.type) return 0;
      return a.type === "start" ? 1 : -1;
    });

  const activeAtTheSameTime: DenominationInfo[] = [];
  return points.reduce((result, cursor, idx) => {
    const hash = cursor.denom.denomPubHash;
    if (!hash)
      throw Error(
        `denomination without hash ${JSON.stringify(
          cursor.denom,
          undefined,
          2,
        )}`,
      );

    let prev = result.length > 0 ? result[result.length - 1] : undefined;
    const prevHasSameValue =
      prev && Amounts.cmp(prev.value, cursor.denom.value) === 0;
    if (prev) {
      if (prevHasSameValue) {
        prev.until = cursor.moment;

        if (prev.from.t_ms === prev.until.t_ms) {
          result.pop();
          prev = result[result.length - 1];
        }
      } else {
        // the last end adds a gap that we have to remove
        result.pop();
      }
    }

    //update the activeAtTheSameTime list
    if (cursor.type === "end") {
      const loc = activeAtTheSameTime.findIndex((v) => v.denomPubHash === hash);
      if (loc === -1) {
        throw Error(`denomination ${hash} has an end but no start`);
      }
      activeAtTheSameTime.splice(loc, 1);
    } else if (cursor.type === "start") {
      activeAtTheSameTime.push(cursor.denom);
    } else {
      const exhaustiveCheck: never = cursor.type;
      throw new Error(`not TimePoint defined for type: ${exhaustiveCheck}`);
    }

    if (idx == points.length - 1) {
      //this is the last element in the list, prevent adding
      //a gap in the end
      if (cursor.type !== "end") {
        throw Error(
          `denomination ${hash} starts after ending or doesn't have an ending`,
        );
      }
      if (activeAtTheSameTime.length > 0) {
        throw Error(
          `there are ${activeAtTheSameTime.length} denominations without ending`,
        );
      }
      return result;
    }

    const current = selectBestForOverlappingDenominations(activeAtTheSameTime);

    if (current) {
      if (
        prev === undefined || //is the first
        !prev.fee || //is a gap
        Amounts.cmp(prev.fee, current[feeProp]) !== 0 // prev has the same fee
      ) {
        result.push({
          value: cursor.denom.value,
          from: cursor.moment,
          until: AbsoluteTime.never(), //not yet known
          fee: current[feeProp],
        });
      } else {
        prev.until = cursor.moment;
      }
    } else {
      result.push({
        value: cursor.denom.value,
        from: cursor.moment,
        until: AbsoluteTime.never(), //not yet known
      });
    }

    return result;
  }, [] as FeeDescription[]);
}