summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/instructedAmountConversion.ts
blob: 1f7d959599463614167fda3acfbf1508fea813d2 (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
 This file is part of GNU Taler
 (C) 2023 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 { GlobalIDB } from "@gnu-taler/idb-bridge";
import {
  AbsoluteTime,
  AgeRestriction,
  AmountJson,
  AmountResponse,
  Amounts,
  ConvertAmountRequest,
  Duration,
  GetAmountRequest,
  GetPlanForOperationRequest,
  TransactionAmountMode,
  TransactionType,
  checkDbInvariant,
  parsePaytoUri,
  strcmp,
} from "@gnu-taler/taler-util";
import { DenominationRecord, timestampProtocolFromDb } from "./db.js";
import { getExchangeWireDetailsInTx } from "./exchanges.js";
import { WalletExecutionContext } from "./wallet.js";

export interface CoinInfo {
  id: string;
  value: AmountJson;
  denomDeposit: AmountJson;
  denomWithdraw: AmountJson;
  denomRefresh: AmountJson;
  totalAvailable: number | undefined;
  exchangeWire: AmountJson | undefined;
  exchangePurse: AmountJson | undefined;
  duration: Duration;
  exchangeBaseUrl: string;
  maxAge: number;
}

/**
 * If the operation going to be plan subtracts
 * or adds amount in the wallet db
 */
export enum OperationType {
  Credit = "credit",
  Debit = "debit",
}

// FIXME: Name conflict ...
interface ExchangeInfo {
  wireFee: AmountJson | undefined;
  purseFee: AmountJson | undefined;
  creditDeadline: AbsoluteTime;
  debitDeadline: AbsoluteTime;
}

function getOperationType(txType: TransactionType): OperationType {
  const operationType =
    txType === TransactionType.Withdrawal
      ? OperationType.Credit
      : txType === TransactionType.Deposit
        ? OperationType.Debit
        : undefined;
  if (!operationType) {
    throw Error(`operation type ${txType} not yet supported`);
  }
  return operationType;
}

interface SelectedCoins {
  totalValue: AmountJson;
  coins: { info: CoinInfo; size: number }[];
  refresh?: RefreshChoice;
}

function getCoinsFilter(req: GetPlanForOperationRequest): CoinsFilter {
  switch (req.type) {
    case TransactionType.Withdrawal: {
      return {
        exchanges:
          req.exchangeUrl === undefined ? undefined : [req.exchangeUrl],
      };
    }
    case TransactionType.Deposit: {
      const payto = parsePaytoUri(req.account);
      if (!payto) {
        throw Error(`wrong payto ${req.account}`);
      }
      return {
        wireMethod: payto.targetType,
      };
    }
  }
}

interface RefreshChoice {
  /**
   * Amount that need to be covered
   */
  gap: AmountJson;
  totalFee: AmountJson;
  selected: CoinInfo;
  totalChangeValue: AmountJson;
  refreshEffective: AmountJson;
  coins: { info: CoinInfo; size: number }[];

  // totalValue: AmountJson;
  // totalDepositFee: AmountJson;
  // totalRefreshFee: AmountJson;
  // totalChangeContribution: AmountJson;
  // totalChangeWithdrawalFee: AmountJson;
}

interface CoinsFilter {
  shouldCalculatePurseFee?: boolean;
  exchanges?: string[];
  wireMethod?: string;
  ageRestricted?: number;
}

interface AvailableCoins {
  list: CoinInfo[];
  exchanges: Record<string, ExchangeInfo>;
}

/**
 * Get all the denoms that can be used for a operation that is limited
 * by the following restrictions.
 * This function is costly (by the database access) but with high chances
 * of being cached
 */
async function getAvailableDenoms(
  wex: WalletExecutionContext,
  op: TransactionType,
  currency: string,
  filters: CoinsFilter = {},
): Promise<AvailableCoins> {
  const operationType = getOperationType(TransactionType.Deposit);

  return await wex.db.runReadOnlyTx(
    {
      storeNames: [
        "exchanges",
        "exchangeDetails",
        "denominations",
        "coinAvailability",
      ],
    },
    async (tx) => {
      const list: CoinInfo[] = [];
      const exchanges: Record<string, ExchangeInfo> = {};

      const databaseExchanges = await tx.exchanges.iter().toArray();
      const filteredExchanges =
        filters.exchanges ?? databaseExchanges.map((e) => e.baseUrl);

      for (const exchangeBaseUrl of filteredExchanges) {
        const exchangeDetails = await getExchangeWireDetailsInTx(
          tx,
          exchangeBaseUrl,
        );
        // 1.- exchange has same currency
        if (exchangeDetails?.currency !== currency) {
          continue;
        }

        let deadline = AbsoluteTime.never();
        // 2.- exchange supports wire method
        let wireFee: AmountJson | undefined;
        if (filters.wireMethod) {
          const wireMethodWithDates =
            exchangeDetails.wireInfo.feesForType[filters.wireMethod];

          if (!wireMethodWithDates) {
            throw Error(
              `exchange ${exchangeBaseUrl} doesn't have wire method ${filters.wireMethod}`,
            );
          }
          const wireMethodFee = wireMethodWithDates.find((x) => {
            return AbsoluteTime.isBetween(
              AbsoluteTime.now(),
              AbsoluteTime.fromProtocolTimestamp(x.startStamp),
              AbsoluteTime.fromProtocolTimestamp(x.endStamp),
            );
          });

          if (!wireMethodFee) {
            throw Error(
              `exchange ${exchangeBaseUrl} doesn't have wire fee defined for this period`,
            );
          }
          wireFee = Amounts.parseOrThrow(wireMethodFee.wireFee);
          deadline = AbsoluteTime.min(
            deadline,
            AbsoluteTime.fromProtocolTimestamp(wireMethodFee.endStamp),
          );
        }
        // exchanges[exchangeBaseUrl].wireFee = wireMethodFee;

        // 3.- exchange supports wire method
        let purseFee: AmountJson | undefined;
        if (filters.shouldCalculatePurseFee) {
          const purseFeeFound = exchangeDetails.globalFees.find((x) => {
            return AbsoluteTime.isBetween(
              AbsoluteTime.now(),
              AbsoluteTime.fromProtocolTimestamp(x.startDate),
              AbsoluteTime.fromProtocolTimestamp(x.endDate),
            );
          });
          if (!purseFeeFound) {
            throw Error(
              `exchange ${exchangeBaseUrl} doesn't have purse fee defined for this period`,
            );
          }
          purseFee = Amounts.parseOrThrow(purseFeeFound.purseFee);
          deadline = AbsoluteTime.min(
            deadline,
            AbsoluteTime.fromProtocolTimestamp(purseFeeFound.endDate),
          );
        }

        let creditDeadline = AbsoluteTime.never();
        let debitDeadline = AbsoluteTime.never();
        //4.- filter coins restricted by age
        if (operationType === OperationType.Credit) {
          // FIXME: Use denom groups instead of querying all denominations!
          const ds =
            await tx.denominations.indexes.byExchangeBaseUrl.getAll(
              exchangeBaseUrl,
            );
          for (const denom of ds) {
            const expiresWithdraw = AbsoluteTime.fromProtocolTimestamp(
              timestampProtocolFromDb(denom.stampExpireWithdraw),
            );
            const expiresDeposit = AbsoluteTime.fromProtocolTimestamp(
              timestampProtocolFromDb(denom.stampExpireDeposit),
            );
            creditDeadline = AbsoluteTime.min(deadline, expiresWithdraw);
            debitDeadline = AbsoluteTime.min(deadline, expiresDeposit);
            list.push(
              buildCoinInfoFromDenom(
                denom,
                purseFee,
                wireFee,
                AgeRestriction.AGE_UNRESTRICTED,
                Number.MAX_SAFE_INTEGER, // Max withdrawable from single denom
              ),
            );
          }
        } else {
          const ageLower = filters.ageRestricted ?? 0;
          const ageUpper = AgeRestriction.AGE_UNRESTRICTED;

          const myExchangeCoins =
            await tx.coinAvailability.indexes.byExchangeAgeAvailability.getAll(
              GlobalIDB.KeyRange.bound(
                [exchangeDetails.exchangeBaseUrl, ageLower, 1],
                [
                  exchangeDetails.exchangeBaseUrl,
                  ageUpper,
                  Number.MAX_SAFE_INTEGER,
                ],
              ),
            );
          //5.- save denoms with how many coins are available
          // FIXME: Check that the individual denomination is audited!
          // FIXME: Should we exclude denominations that are
          // not spendable anymore?
          for (const coinAvail of myExchangeCoins) {
            const denom = await tx.denominations.get([
              coinAvail.exchangeBaseUrl,
              coinAvail.denomPubHash,
            ]);
            checkDbInvariant(!!denom);
            if (denom.isRevoked || !denom.isOffered) {
              continue;
            }
            const expiresWithdraw = AbsoluteTime.fromProtocolTimestamp(
              timestampProtocolFromDb(denom.stampExpireWithdraw),
            );
            const expiresDeposit = AbsoluteTime.fromProtocolTimestamp(
              timestampProtocolFromDb(denom.stampExpireDeposit),
            );
            creditDeadline = AbsoluteTime.min(deadline, expiresWithdraw);
            debitDeadline = AbsoluteTime.min(deadline, expiresDeposit);
            list.push(
              buildCoinInfoFromDenom(
                denom,
                purseFee,
                wireFee,
                coinAvail.maxAge,
                coinAvail.freshCoinCount,
              ),
            );
          }
        }

        exchanges[exchangeBaseUrl] = {
          purseFee,
          wireFee,
          debitDeadline,
          creditDeadline,
        };
      }

      return { list, exchanges };
    },
  );
}

function buildCoinInfoFromDenom(
  denom: DenominationRecord,
  purseFee: AmountJson | undefined,
  wireFee: AmountJson | undefined,
  maxAge: number,
  total: number,
): CoinInfo {
  return {
    id: denom.denomPubHash,
    denomWithdraw: Amounts.parseOrThrow(denom.fees.feeWithdraw),
    denomDeposit: Amounts.parseOrThrow(denom.fees.feeDeposit),
    denomRefresh: Amounts.parseOrThrow(denom.fees.feeRefresh),
    exchangePurse: purseFee,
    exchangeWire: wireFee,
    exchangeBaseUrl: denom.exchangeBaseUrl,
    duration: AbsoluteTime.difference(
      AbsoluteTime.now(),
      AbsoluteTime.fromProtocolTimestamp(
        timestampProtocolFromDb(denom.stampExpireDeposit),
      ),
    ),
    totalAvailable: total,
    value: Amounts.parseOrThrow(denom.value),
    maxAge,
  };
}

export async function convertDepositAmount(
  wex: WalletExecutionContext,
  req: ConvertAmountRequest,
): Promise<AmountResponse> {
  const amount = Amounts.parseOrThrow(req.amount);
  // const filter = getCoinsFilter(req);

  const denoms = await getAvailableDenoms(
    wex,
    TransactionType.Deposit,
    amount.currency,
    {},
  );
  const result = convertDepositAmountForAvailableCoins(
    denoms,
    amount,
    req.type,
  );

  return {
    effectiveAmount: Amounts.stringify(result.effective),
    rawAmount: Amounts.stringify(result.raw),
  };
}

const LOG_REFRESH = false;
const LOG_DEPOSIT = false;
export function convertDepositAmountForAvailableCoins(
  denoms: AvailableCoins,
  amount: AmountJson,
  mode: TransactionAmountMode,
): AmountAndRefresh {
  const zero = Amounts.zeroOfCurrency(amount.currency);
  if (!denoms.list.length) {
    // no coins in the database
    return { effective: zero, raw: zero };
  }
  const depositDenoms = rankDenominationForDeposit(denoms.list, mode);

  //FIXME: we are not taking into account
  // * exchanges with multiple accounts
  // * wallet with multiple exchanges
  const wireFee = Object.values(denoms.exchanges)[0]?.wireFee ?? zero;
  const adjustedAmount = Amounts.add(amount, wireFee).amount;

  const selected = selectGreedyCoins(depositDenoms, adjustedAmount);

  const gap = Amounts.sub(amount, selected.totalValue).amount;

  const result = getTotalEffectiveAndRawForDeposit(
    selected.coins,
    amount.currency,
  );
  result.raw = Amounts.sub(result.raw, wireFee).amount;

  if (Amounts.isZero(gap)) {
    // exact amount founds
    return result;
  }

  if (LOG_DEPOSIT) {
    const logInfo = selected.coins.map((c) => {
      return `${Amounts.stringifyValue(c.info.id)} x ${c.size}`;
    });
    console.log(
      "deposit used:",
      logInfo.join(", "),
      "gap:",
      Amounts.stringifyValue(gap),
    );
  }

  const refreshDenoms = rankDenominationForRefresh(denoms.list);
  /**
   * FIXME: looking for refresh AFTER selecting greedy is not optimal
   */
  const refreshCoin = searchBestRefreshCoin(
    depositDenoms,
    refreshDenoms,
    gap,
    mode,
  );

  if (refreshCoin) {
    const fee = Amounts.sub(result.effective, result.raw).amount;
    const effective = Amounts.add(
      result.effective,
      refreshCoin.refreshEffective,
    ).amount;
    const raw = Amounts.sub(effective, fee, refreshCoin.totalFee).amount;
    //found with change
    return {
      effective,
      raw,
      refresh: refreshCoin,
    };
  }

  // there is a gap, but no refresh coin was found
  return result;
}

export async function getMaxDepositAmount(
  wex: WalletExecutionContext,
  req: GetAmountRequest,
): Promise<AmountResponse> {
  // const filter = getCoinsFilter(req);

  const denoms = await getAvailableDenoms(
    wex,
    TransactionType.Deposit,
    req.currency,
    {},
  );

  const result = getMaxDepositAmountForAvailableCoins(denoms, req.currency);
  return {
    effectiveAmount: Amounts.stringify(result.effective),
    rawAmount: Amounts.stringify(result.raw),
  };
}

export function getMaxDepositAmountForAvailableCoins(
  denoms: AvailableCoins,
  currency: string,
) {
  const zero = Amounts.zeroOfCurrency(currency);
  if (!denoms.list.length) {
    // no coins in the database
    return { effective: zero, raw: zero };
  }

  const result = getTotalEffectiveAndRawForDeposit(
    denoms.list.map((info) => {
      return { info, size: info.totalAvailable ?? 0 };
    }),
    currency,
  );

  const wireFee = Object.values(denoms.exchanges)[0]?.wireFee ?? zero;
  result.raw = Amounts.sub(result.raw, wireFee).amount;

  return result;
}

export async function convertPeerPushAmount(
  wex: WalletExecutionContext,
  req: ConvertAmountRequest,
): Promise<AmountResponse> {
  throw Error("to be implemented after 1.0");
}

export async function getMaxPeerPushAmount(
  wex: WalletExecutionContext,
  req: GetAmountRequest,
): Promise<AmountResponse> {
  throw Error("to be implemented after 1.0");
}

export async function convertWithdrawalAmount(
  wex: WalletExecutionContext,
  req: ConvertAmountRequest,
): Promise<AmountResponse> {
  const amount = Amounts.parseOrThrow(req.amount);

  const denoms = await getAvailableDenoms(
    wex,
    TransactionType.Withdrawal,
    amount.currency,
    {},
  );

  const result = convertWithdrawalAmountFromAvailableCoins(
    denoms,
    amount,
    req.type,
  );

  return {
    effectiveAmount: Amounts.stringify(result.effective),
    rawAmount: Amounts.stringify(result.raw),
  };
}

export function convertWithdrawalAmountFromAvailableCoins(
  denoms: AvailableCoins,
  amount: AmountJson,
  mode: TransactionAmountMode,
) {
  const zero = Amounts.zeroOfCurrency(amount.currency);
  if (!denoms.list.length) {
    // no coins in the database
    return { effective: zero, raw: zero };
  }
  const withdrawDenoms = rankDenominationForWithdrawals(denoms.list, mode);

  const selected = selectGreedyCoins(withdrawDenoms, amount);

  return getTotalEffectiveAndRawForWithdrawal(selected.coins, amount.currency);
}

/** *****************************************************
 * HELPERS
 * *****************************************************
 */

/**
 *
 * @param depositDenoms
 * @param refreshDenoms
 * @param amount
 * @param mode
 * @returns
 */
function searchBestRefreshCoin(
  depositDenoms: SelectableElement[],
  refreshDenoms: Record<string, SelectableElement[]>,
  amount: AmountJson,
  mode: TransactionAmountMode,
): RefreshChoice | undefined {
  let choice: RefreshChoice | undefined = undefined;
  let refreshIdx = 0;
  refreshIteration: while (refreshIdx < depositDenoms.length) {
    const d = depositDenoms[refreshIdx];

    const denomContribution =
      mode === TransactionAmountMode.Effective
        ? d.value
        : Amounts.sub(d.value, d.info.denomRefresh, d.info.denomDeposit).amount;

    const changeAfterDeposit = Amounts.sub(denomContribution, amount).amount;
    if (Amounts.isZero(changeAfterDeposit)) {
      //this coin is not big enough to use for refresh
      //since the list is sorted, we can break here
      break refreshIteration;
    }

    const withdrawDenoms = refreshDenoms[d.info.exchangeBaseUrl];
    const change = selectGreedyCoins(withdrawDenoms, changeAfterDeposit);

    const zero = Amounts.zeroOfCurrency(amount.currency);
    const withdrawChangeFee = change.coins.reduce((cur, prev) => {
      return Amounts.add(
        cur,
        Amounts.mult(prev.info.denomWithdraw, prev.size).amount,
      ).amount;
    }, zero);

    const withdrawChangeValue = change.coins.reduce((cur, prev) => {
      return Amounts.add(cur, Amounts.mult(prev.info.value, prev.size).amount)
        .amount;
    }, zero);

    const totalFee = Amounts.add(
      d.info.denomDeposit,
      d.info.denomRefresh,
      withdrawChangeFee,
    ).amount;

    if (!choice || Amounts.cmp(totalFee, choice.totalFee) === -1) {
      //found cheaper change
      choice = {
        gap: amount,
        totalFee: totalFee,
        totalChangeValue: change.totalValue, //change after refresh
        refreshEffective: Amounts.sub(d.info.value, withdrawChangeValue).amount, // what of the denom used is not recovered
        selected: d.info,
        coins: change.coins,
      };
    }
    refreshIdx++;
  }
  if (choice) {
    if (LOG_REFRESH) {
      const logInfo = choice.coins.map((c) => {
        return `${Amounts.stringifyValue(c.info.id)} x ${c.size}`;
      });
      console.log(
        "refresh used:",
        Amounts.stringifyValue(choice.selected.value),
        "change:",
        logInfo.join(", "),
        "fee:",
        Amounts.stringifyValue(choice.totalFee),
        "refreshEffective:",
        Amounts.stringifyValue(choice.refreshEffective),
        "totalChangeValue:",
        Amounts.stringifyValue(choice.totalChangeValue),
      );
    }
  }
  return choice;
}

/**
 * Returns a copy of the list sorted for the best denom to withdraw first
 *
 * @param denoms
 * @returns
 */
function rankDenominationForWithdrawals(
  denoms: CoinInfo[],
  mode: TransactionAmountMode,
): SelectableElement[] {
  const copyList = [...denoms];
  /**
   * Rank coins
   */
  copyList.sort((d1, d2) => {
    // the best coin to use is
    // 1.- the one that contrib more and pay less fee
    // 2.- it takes more time before expires

    //different exchanges may have different wireFee
    //ranking should take the relative contribution in the exchange
    //which is (value - denomFee / fixedFee)
    const rate1 = Amounts.divmod(d1.value, d1.denomWithdraw).quotient;
    const rate2 = Amounts.divmod(d2.value, d2.denomWithdraw).quotient;
    const contribCmp = rate1 === rate2 ? 0 : rate1 < rate2 ? 1 : -1;
    return (
      contribCmp ||
      Duration.cmp(d1.duration, d2.duration) ||
      strcmp(d1.id, d2.id)
    );
  });

  return copyList.map((info) => {
    switch (mode) {
      case TransactionAmountMode.Effective: {
        //if the user instructed "effective" then we need to selected
        //greedy total coin value
        return {
          info,
          value: info.value,
          total: Number.MAX_SAFE_INTEGER,
        };
      }
      case TransactionAmountMode.Raw: {
        //if the user instructed "raw" then we need to selected
        //greedy total coin raw amount (without fee)
        return {
          info,
          value: Amounts.add(info.value, info.denomWithdraw).amount,
          total: Number.MAX_SAFE_INTEGER,
        };
      }
    }
  });
}

/**
 * Returns a copy of the list sorted for the best denom to deposit first
 *
 * @param denoms
 * @returns
 */
function rankDenominationForDeposit(
  denoms: CoinInfo[],
  mode: TransactionAmountMode,
): SelectableElement[] {
  const copyList = [...denoms];
  /**
   * Rank coins
   */
  copyList.sort((d1, d2) => {
    // the best coin to use is
    // 1.- the one that contrib more and pay less fee
    // 2.- it takes more time before expires

    //different exchanges may have different wireFee
    //ranking should take the relative contribution in the exchange
    //which is (value - denomFee / fixedFee)
    const rate1 = Amounts.divmod(d1.value, d1.denomDeposit).quotient;
    const rate2 = Amounts.divmod(d2.value, d2.denomDeposit).quotient;
    const contribCmp = rate1 === rate2 ? 0 : rate1 < rate2 ? 1 : -1;
    return (
      contribCmp ||
      Duration.cmp(d1.duration, d2.duration) ||
      strcmp(d1.id, d2.id)
    );
  });

  return copyList.map((info) => {
    switch (mode) {
      case TransactionAmountMode.Effective: {
        //if the user instructed "effective" then we need to selected
        //greedy total coin value
        return {
          info,
          value: info.value,
          total: info.totalAvailable ?? 0,
        };
      }
      case TransactionAmountMode.Raw: {
        //if the user instructed "raw" then we need to selected
        //greedy total coin raw amount (without fee)
        return {
          info,
          value: Amounts.sub(info.value, info.denomDeposit).amount,
          total: info.totalAvailable ?? 0,
        };
      }
    }
  });
}

/**
 * Returns a copy of the list sorted for the best denom to withdraw first
 *
 * @param denoms
 * @returns
 */
function rankDenominationForRefresh(
  denoms: CoinInfo[],
): Record<string, SelectableElement[]> {
  const groupByExchange: Record<string, CoinInfo[]> = {};
  for (const d of denoms) {
    if (!groupByExchange[d.exchangeBaseUrl]) {
      groupByExchange[d.exchangeBaseUrl] = [];
    }
    groupByExchange[d.exchangeBaseUrl].push(d);
  }

  const result: Record<string, SelectableElement[]> = {};
  for (const d of denoms) {
    result[d.exchangeBaseUrl] = rankDenominationForWithdrawals(
      groupByExchange[d.exchangeBaseUrl],
      TransactionAmountMode.Raw,
    );
  }
  return result;
}

interface SelectableElement {
  total: number;
  value: AmountJson;
  info: CoinInfo;
}

function selectGreedyCoins(
  coins: SelectableElement[],
  limit: AmountJson,
): SelectedCoins {
  const result: SelectedCoins = {
    totalValue: Amounts.zeroOfCurrency(limit.currency),
    coins: [],
  };
  if (!coins.length) return result;

  let denomIdx = 0;
  iterateDenoms: while (denomIdx < coins.length) {
    const denom = coins[denomIdx];
    // let total = denom.total;
    const left = Amounts.sub(limit, result.totalValue).amount;

    if (Amounts.isZero(denom.value)) {
      // 0 contribution denoms should be the last
      break iterateDenoms;
    }

    //use Amounts.divmod instead of iterate
    const div = Amounts.divmod(left, denom.value);
    const size = Math.min(div.quotient, denom.total);
    if (size > 0) {
      const mul = Amounts.mult(denom.value, size).amount;
      const progress = Amounts.add(result.totalValue, mul).amount;

      result.totalValue = progress;
      result.coins.push({ info: denom.info, size });
      denom.total = denom.total - size;
    }

    //go next denom
    denomIdx++;
  }

  return result;
}

type AmountWithFee = { raw: AmountJson; effective: AmountJson };
type AmountAndRefresh = AmountWithFee & { refresh?: RefreshChoice };

export function getTotalEffectiveAndRawForDeposit(
  list: { info: CoinInfo; size: number }[],
  currency: string,
): AmountWithFee {
  const init = {
    raw: Amounts.zeroOfCurrency(currency),
    effective: Amounts.zeroOfCurrency(currency),
  };
  return list.reduce((prev, cur) => {
    const ef = Amounts.mult(cur.info.value, cur.size).amount;
    const rw = Amounts.mult(
      Amounts.sub(cur.info.value, cur.info.denomDeposit).amount,
      cur.size,
    ).amount;

    prev.effective = Amounts.add(prev.effective, ef).amount;
    prev.raw = Amounts.add(prev.raw, rw).amount;
    return prev;
  }, init);
}

function getTotalEffectiveAndRawForWithdrawal(
  list: { info: CoinInfo; size: number }[],
  currency: string,
): AmountWithFee {
  const init = {
    raw: Amounts.zeroOfCurrency(currency),
    effective: Amounts.zeroOfCurrency(currency),
  };
  return list.reduce((prev, cur) => {
    const ef = Amounts.mult(cur.info.value, cur.size).amount;
    const rw = Amounts.mult(
      Amounts.add(cur.info.value, cur.info.denomWithdraw).amount,
      cur.size,
    ).amount;

    prev.effective = Amounts.add(prev.effective, ef).amount;
    prev.raw = Amounts.add(prev.raw, rw).amount;
    return prev;
  }, init);
}