Skip to content

Commit ee98a2b

Browse files
committed
test: createLegacyTx() is no longer async
1 parent 8f09241 commit ee98a2b

File tree

2 files changed

+49
-75
lines changed

2 files changed

+49
-75
lines changed

tests/legacy-tx.js

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@ let outputs = [
1313

1414
let changeOutput = {};
1515

16-
Zora.test("too few sats", async function (t) {
16+
Zora.test("too few sats", function (t) {
1717
let inputs = [
1818
{
1919
satoshis: 20000 + 190,
2020
},
2121
];
22-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
23-
.then(function () {
24-
t.ok(false, "should throw when there aren't enough sats");
25-
})
26-
.catch(function (e) {
27-
let msg = e.message;
28-
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
29-
if (isAboutMemo) {
30-
msg = "throws when there are too few sats";
31-
}
32-
t.ok(isAboutMemo, msg);
33-
});
22+
try {
23+
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
24+
t.ok(false, "should throw when there aren't enough sats");
25+
} catch (e) {
26+
let msg = e.message;
27+
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
28+
if (isAboutMemo) {
29+
msg = "throws when there are too few sats";
30+
}
31+
t.ok(isAboutMemo, msg);
32+
}
3433
});
3534

36-
Zora.test("exactly enough sats", async function (t) {
35+
Zora.test("exactly enough sats", function (t) {
3736
let inputs = [
3837
{
3938
satoshis: 20000 + 190,
@@ -43,52 +42,37 @@ Zora.test("exactly enough sats", async function (t) {
4342
},
4443
];
4544

46-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
47-
.then(function () {
48-
t.ok(true, "sats match exactly");
49-
})
50-
.catch(function (e) {
51-
t.ok(false, e.message);
52-
});
45+
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
46+
t.ok(true, "sats match exactly");
5347
});
5448

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

59-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
60-
.then(function (txInfo) {
61-
if (txInfo.length > 1) {
62-
throw new Error("created return change for dust");
63-
}
64-
t.ok(true, "has no return change");
65-
})
66-
.catch(function (e) {
67-
t.ok(false, e.message);
68-
});
53+
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
54+
if (txInfo.length > 1) {
55+
throw new Error("created return change for dust");
56+
}
57+
t.ok(true, "has no return change");
6958
});
7059

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

75-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
76-
.then(function (txInfo) {
77-
let hasChange = txInfo.changeIndex >= 0;
78-
if (!hasChange) {
79-
throw new Error("did not create return change");
80-
}
81-
82-
let change = txInfo.outputs[txInfo.changeIndex];
83-
if (!change) {
84-
throw new Error("did not add change to outputs");
85-
}
86-
87-
t.ok(true, "returned change >= dust");
88-
})
89-
.catch(function (e) {
90-
t.ok(false, e.message);
91-
});
64+
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
65+
let hasChange = txInfo.changeIndex >= 0;
66+
if (!hasChange) {
67+
throw new Error("did not create return change");
68+
}
69+
70+
let change = txInfo.outputs[txInfo.changeIndex];
71+
if (!change) {
72+
throw new Error("did not add change to outputs");
73+
}
74+
75+
t.ok(true, "returned change >= dust");
9276
});
9377

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

103-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
104-
.then(function (txInfo) {
105-
let tooManyInputs = txInfo.inputs.length >= 0;
106-
if (!tooManyInputs) {
107-
throw new Error("selected more inputs than necessary");
108-
}
109-
110-
let isOptimalInput = txInfo.inputs[0].satoshis === exact;
111-
if (!isOptimalInput) {
112-
throw new Error("did not select clearly optimal input");
113-
}
114-
115-
t.ok(true, "selected closest input");
116-
})
117-
.catch(function (e) {
118-
t.ok(false, e.message);
119-
});
87+
let txInfo = await DashTx.legacyCreateTx(inputs, outputs, changeOutput);
88+
let tooManyInputs = txInfo.inputs.length >= 0;
89+
if (!tooManyInputs) {
90+
throw new Error("selected more inputs than necessary");
91+
}
92+
93+
let isOptimalInput = txInfo.inputs[0].satoshis === exact;
94+
if (!isOptimalInput) {
95+
throw new Error("did not select clearly optimal input");
96+
}
97+
98+
t.ok(true, "selected closest input");
12099
});

tests/lex-sort.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,7 @@ Zora.test("legacyCreateTx uses lex sort functions", async function (t) {
179179

180180
let changeOutput = {};
181181

182-
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
183-
.then(function (txInfo) {
184-
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
185-
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
186-
})
187-
.catch(function (e) {
188-
t.ok(false, e.message);
189-
});
182+
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
183+
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
184+
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
190185
});

0 commit comments

Comments
 (0)