diff options
Diffstat (limited to 'packages/taler-wallet-core/src/coinSelection.test.ts')
-rw-r--r-- | packages/taler-wallet-core/src/coinSelection.test.ts | 101 |
1 files changed, 38 insertions, 63 deletions
diff --git a/packages/taler-wallet-core/src/coinSelection.test.ts b/packages/taler-wallet-core/src/coinSelection.test.ts index 4fac244fc..6eae9deaa 100644 --- a/packages/taler-wallet-core/src/coinSelection.test.ts +++ b/packages/taler-wallet-core/src/coinSelection.test.ts @@ -24,8 +24,8 @@ import { import test from "ava"; import { AvailableDenom, - PeerCoinSelectionTally, - testing_greedySelectPeer, + CoinSelectionTally, + emptyTallyForPeerPayment, testing_selectGreedy, } from "./coinSelection.js"; @@ -42,12 +42,13 @@ const inThePast = AbsoluteTime.toProtocolTimestamp( test("p2p: should select the coin", (t) => { const instructedAmount = Amounts.parseOrThrow("LOCAL:2"); - const tally = { - amountRemaining: instructedAmount, - depositFeesAcc: Amounts.zeroOfCurrency(instructedAmount.currency), - lastDepositFee: Amounts.zeroOfCurrency(instructedAmount.currency), - } satisfies PeerCoinSelectionTally; - const coins = testing_greedySelectPeer( + const tally = emptyTallyForPeerPayment(instructedAmount); + t.log(`tally before: ${j2s(tally)}`); + const coins = testing_selectGreedy( + { + wireFeeAmortization: 1, + wireFeesPerExchange: {}, + }, createCandidates([ { amount: "LOCAL:10" as AmountString, @@ -59,7 +60,8 @@ test("p2p: should select the coin", (t) => { tally, ); - t.log(j2s(coins)); + t.log(`coins: ${j2s(coins)}`); + t.log(`tally: ${j2s(tally)}`); t.assert(coins != null); @@ -73,22 +75,16 @@ test("p2p: should select the coin", (t) => { expireWithdraw: inTheDistantFuture, }, }); - - t.deepEqual(tally, { - amountRemaining: Amounts.parseOrThrow("LOCAL:0"), - depositFeesAcc: Amounts.parseOrThrow("LOCAL:0.1"), - lastDepositFee: Amounts.parseOrThrow("LOCAL:0.1"), - }); }); test("p2p: should select 3 coins", (t) => { const instructedAmount = Amounts.parseOrThrow("LOCAL:20"); - const tally = { - amountRemaining: instructedAmount, - depositFeesAcc: Amounts.zeroOfCurrency(instructedAmount.currency), - lastDepositFee: Amounts.zeroOfCurrency(instructedAmount.currency), - } satisfies PeerCoinSelectionTally; - const coins = testing_greedySelectPeer( + const tally = emptyTallyForPeerPayment(instructedAmount); + const coins = testing_selectGreedy( + { + wireFeeAmortization: 1, + wireFeesPerExchange: {}, + }, createCandidates([ { amount: "LOCAL:10" as AmountString, @@ -114,22 +110,16 @@ test("p2p: should select 3 coins", (t) => { expireWithdraw: inTheDistantFuture, }, }); - - t.deepEqual(tally, { - amountRemaining: Amounts.parseOrThrow("LOCAL:0"), - depositFeesAcc: Amounts.parseOrThrow("LOCAL:0.3"), - lastDepositFee: Amounts.parseOrThrow("LOCAL:0.1"), - }); }); test("p2p: can't select since the instructed amount is too high", (t) => { const instructedAmount = Amounts.parseOrThrow("LOCAL:60"); - const tally = { - amountRemaining: instructedAmount, - depositFeesAcc: Amounts.zeroOfCurrency(instructedAmount.currency), - lastDepositFee: Amounts.zeroOfCurrency(instructedAmount.currency), - } satisfies PeerCoinSelectionTally; - const coins = testing_greedySelectPeer( + const tally = emptyTallyForPeerPayment(instructedAmount); + const coins = testing_selectGreedy( + { + wireFeeAmortization: 1, + wireFeesPerExchange: {}, + }, createCandidates([ { amount: "LOCAL:10" as AmountString, @@ -142,12 +132,6 @@ test("p2p: can't select since the instructed amount is too high", (t) => { ); t.is(coins, undefined); - - t.deepEqual(tally, { - amountRemaining: Amounts.parseOrThrow("LOCAL:10.5"), - depositFeesAcc: Amounts.parseOrThrow("LOCAL:0.5"), - lastDepositFee: Amounts.parseOrThrow("LOCAL:0.1"), - }); }); test("pay: select one coin to pay with fee", (t) => { @@ -162,22 +146,11 @@ test("pay: select one coin to pay with fee", (t) => { customerWireFees: zero, wireFeeCoveredForExchange: new Set<string>(), lastDepositFee: zero, - }; + } satisfies CoinSelectionTally; const coins = testing_selectGreedy( { - auditors: [], - exchanges: [ - { - exchangeBaseUrl: "http://exchange.localhost/", - exchangePub: "E5M8CGRDHXF1RCVP3B8TQCTDYNQ7T4XHWR5SVEQRGVVMVME41VJ0", - }, - ], - contractTermsAmount: payment, - depositFeeLimit: zero, wireFeeAmortization: 1, - wireFeeLimit: zero, - prevPayCoins: [], - wireMethod: "x-taler-bank", + wireFeesPerExchange: { "http://exchange.localhost/": exchangeWireFee }, }, createCandidates([ { @@ -187,7 +160,6 @@ test("pay: select one coin to pay with fee", (t) => { fromExchange: "http://exchange.localhost/", }, ]), - { "http://exchange.localhost/": exchangeWireFee }, tally, ); @@ -203,13 +175,13 @@ test("pay: select one coin to pay with fee", (t) => { }); t.deepEqual(tally, { - amountPayRemaining: Amounts.parseOrThrow("LOCAL:2"), + amountPayRemaining: Amounts.parseOrThrow("LOCAL:0"), amountWireFeeLimitRemaining: zero, amountDepositFeeLimitRemaining: zero, - customerDepositFees: zero, + customerDepositFees: Amounts.parse("LOCAL:0.1"), customerWireFees: zero, - wireFeeCoveredForExchange: new Set(), - lastDepositFee: zero, + wireFeeCoveredForExchange: new Set(["http://exchange.localhost/"]), + lastDepositFee: Amounts.parse("LOCAL:0.1"), }); }); @@ -309,11 +281,14 @@ test("p2p: regression STATER", (t) => { }, ]; const instructedAmount = Amounts.parseOrThrow("STATER:1"); - const tally = { - amountRemaining: instructedAmount, - depositFeesAcc: Amounts.parseOrThrow("STATER:0"), - lastDepositFee: Amounts.parseOrThrow("STATER:0"), - } satisfies PeerCoinSelectionTally; - const res = testing_greedySelectPeer(candidates as any, tally); + const tally = emptyTallyForPeerPayment(instructedAmount); + const res = testing_selectGreedy( + { + wireFeeAmortization: 1, + wireFeesPerExchange: {}, + }, + candidates as any, + tally, + ); t.assert(!!res); }); |