Skip to content

Commit

Permalink
test: createLegacyTx() is no longer async
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 17, 2024
1 parent 8f09241 commit ee98a2b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 75 deletions.
113 changes: 46 additions & 67 deletions tests/legacy-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ let outputs = [

let changeOutput = {};

Zora.test("too few sats", async function (t) {
Zora.test("too few sats", function (t) {
let inputs = [
{
satoshis: 20000 + 190,
},
];
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function () {
t.ok(false, "should throw when there aren't enough sats");
})
.catch(function (e) {
let msg = e.message;
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
if (isAboutMemo) {
msg = "throws when there are too few sats";
}
t.ok(isAboutMemo, msg);
});
try {
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.ok(false, "should throw when there aren't enough sats");
} catch (e) {
let msg = e.message;
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
if (isAboutMemo) {
msg = "throws when there are too few sats";
}
t.ok(isAboutMemo, msg);
}
});

Zora.test("exactly enough sats", async function (t) {
Zora.test("exactly enough sats", function (t) {
let inputs = [
{
satoshis: 20000 + 190,
Expand All @@ -43,52 +42,37 @@ Zora.test("exactly enough sats", async function (t) {
},
];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function () {
t.ok(true, "sats match exactly");
})
.catch(function (e) {
t.ok(false, e.message);
});
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.ok(true, "sats match exactly");
});

Zora.test("donates dust", async function (t) {
Zora.test("donates dust", function (t) {
let satoshis = 20000 + 193 + DashTx.LEGACY_DUST + DashTx.OUTPUT_SIZE + -1;
let inputs = [{ satoshis }];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
if (txInfo.length > 1) {
throw new Error("created return change for dust");
}
t.ok(true, "has no return change");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
if (txInfo.length > 1) {
throw new Error("created return change for dust");
}
t.ok(true, "has no return change");
});

Zora.test("returns change", async function (t) {
Zora.test("returns change", function (t) {
let satoshis = 20000 + 193 + DashTx.LEGACY_DUST + DashTx.OUTPUT_SIZE;
let inputs = [{ satoshis }];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
let hasChange = txInfo.changeIndex >= 0;
if (!hasChange) {
throw new Error("did not create return change");
}

let change = txInfo.outputs[txInfo.changeIndex];
if (!change) {
throw new Error("did not add change to outputs");
}

t.ok(true, "returned change >= dust");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
let hasChange = txInfo.changeIndex >= 0;
if (!hasChange) {
throw new Error("did not create return change");
}

let change = txInfo.outputs[txInfo.changeIndex];
if (!change) {
throw new Error("did not add change to outputs");
}

t.ok(true, "returned change >= dust");
});

Zora.test("coins selection is better than random", async function (t) {
Expand All @@ -100,21 +84,16 @@ Zora.test("coins selection is better than random", async function (t) {
{ satoshis: 500000 },
];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
let tooManyInputs = txInfo.inputs.length >= 0;
if (!tooManyInputs) {
throw new Error("selected more inputs than necessary");
}

let isOptimalInput = txInfo.inputs[0].satoshis === exact;
if (!isOptimalInput) {
throw new Error("did not select clearly optimal input");
}

t.ok(true, "selected closest input");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = await DashTx.legacyCreateTx(inputs, outputs, changeOutput);
let tooManyInputs = txInfo.inputs.length >= 0;
if (!tooManyInputs) {
throw new Error("selected more inputs than necessary");
}

let isOptimalInput = txInfo.inputs[0].satoshis === exact;
if (!isOptimalInput) {
throw new Error("did not select clearly optimal input");
}

t.ok(true, "selected closest input");
});
11 changes: 3 additions & 8 deletions tests/lex-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,7 @@ Zora.test("legacyCreateTx uses lex sort functions", async function (t) {

let changeOutput = {};

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
});

0 comments on commit ee98a2b

Please sign in to comment.