re-add tests, more coin selection tests

This commit is contained in:
Florian Dold 2021-03-27 20:48:44 +01:00
parent 93128f9358
commit 2be1c3c8bd
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
5 changed files with 71 additions and 2 deletions

View File

@ -71,7 +71,7 @@
"esm"
],
"files": [
"src/**/*-test.*"
"src/**/*.test.*"
],
"typescript": {
"extensions": [

View File

@ -184,3 +184,71 @@ test("coin selection 6", (t) => {
t.true(!res);
t.pass();
});
test("coin selection 7", (t) => {
const acis: AvailableCoinInfo[] = [
fakeAci("EUR:1.0", "EUR:0.1"),
fakeAci("EUR:1.0", "EUR:0.1"),
];
const res = selectPayCoins({
candidates: {
candidateCoins: acis,
wireFeesPerExchange: {},
},
contractTermsAmount: a("EUR:2.0"),
depositFeeLimit: a("EUR:0.2"),
wireFeeLimit: a("EUR:0"),
wireFeeAmortization: 1,
});
t.truthy(res);
t.true(Amounts.cmp(res!.customerDepositFees, "EUR:0.0") === 0);
t.true(
Amounts.cmp(Amounts.sum(res!.coinContributions).amount, "EUR:2.0") === 0,
);
t.pass();
});
test("coin selection 8", (t) => {
const acis: AvailableCoinInfo[] = [
fakeAci("EUR:1.0", "EUR:0.2"),
fakeAci("EUR:0.1", "EUR:0.2"),
fakeAci("EUR:0.05", "EUR:0.05"),
fakeAci("EUR:0.05", "EUR:0.05"),
];
const res = selectPayCoins({
candidates: {
candidateCoins: acis,
wireFeesPerExchange: {},
},
contractTermsAmount: a("EUR:1.1"),
depositFeeLimit: a("EUR:0.4"),
wireFeeLimit: a("EUR:0"),
wireFeeAmortization: 1,
});
t.truthy(res);
t.true(res!.coinContributions.length === 3);
t.pass();
});
test("coin selection 9", (t) => {
const acis: AvailableCoinInfo[] = [
fakeAci("EUR:1.0", "EUR:0.2"),
fakeAci("EUR:0.2", "EUR:0.2"),
];
const res = selectPayCoins({
candidates: {
candidateCoins: acis,
wireFeesPerExchange: {},
},
contractTermsAmount: a("EUR:1.2"),
depositFeeLimit: a("EUR:0.4"),
wireFeeLimit: a("EUR:0"),
wireFeeAmortization: 1,
});
t.truthy(res);
t.true(res!.coinContributions.length === 2);
t.true(
Amounts.cmp(Amounts.sum(res!.coinContributions).amount, "EUR:1.2") === 0,
);
t.pass();
});

View File

@ -284,9 +284,10 @@ export function selectPayCoins(
for (const aci of candidateCoins) {
// Don't use this coin if depositing it is more expensive than
// the amount it would give the merchant.
if (Amounts.cmp(aci.feeDeposit, aci.availableAmount) >= 0) {
if (Amounts.cmp(aci.feeDeposit, aci.availableAmount) > 0) {
continue;
}
if (Amounts.isZero(tally.amountPayRemaining)) {
// We have spent enough!
break;