Skip to content

Commit e302ccb

Browse files
committed
feat: prepare denoms for send
1 parent eec662a commit e302ccb

File tree

3 files changed

+255
-45
lines changed

3 files changed

+255
-45
lines changed

public/dashwallet.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var DashWallet = ('object' === typeof module && exports) || {};
2+
(function (window, DashWallet) {
3+
'use strict';
4+
5+
let DashTx = window.DashTx;
6+
7+
/**
8+
* @template {Pick<CoreUtxo, "satoshis">} T
9+
* @param {Array<T>} utxos
10+
* @param {Number} output - including fee estimate
11+
* @return {Array<T>}
12+
*/
13+
DashWallet.selectOptimalUtxos = function (utxos, satsOut) {
14+
let balance = DashTx.sum(utxos);
15+
let fees = DashTx.appraise({
16+
//@ts-ignore
17+
inputs: [{}],
18+
outputs: [{}],
19+
});
20+
21+
let fullSats = satsOut + fees.min;
22+
23+
if (balance < fullSats) {
24+
return [];
25+
}
26+
27+
// from largest to smallest
28+
utxos.sort(function (a, b) {
29+
return b.satoshis - a.satoshis;
30+
});
31+
32+
/** @type Array<T> */
33+
let included = [];
34+
let total = 0;
35+
36+
// try to get just one
37+
utxos.every(function (utxo) {
38+
if (utxo.satoshis > fullSats) {
39+
included[0] = utxo;
40+
total = utxo.satoshis;
41+
return true;
42+
}
43+
return false;
44+
});
45+
if (total) {
46+
return included;
47+
}
48+
49+
// try to use as few coins as possible
50+
utxos.some(function (utxo, i) {
51+
included.push(utxo);
52+
total += utxo.satoshis;
53+
if (total >= fullSats) {
54+
return true;
55+
}
56+
57+
// it quickly becomes astronomically unlikely to hit the one
58+
// exact possibility that least to paying the absolute minimum,
59+
// but remains about 75% likely to hit any of the mid value
60+
// possibilities
61+
if (i < 2) {
62+
// 1 input 25% chance of minimum (needs ~2 tries)
63+
// 2 inputs 6.25% chance of minimum (needs ~8 tries)
64+
fullSats = fullSats + DashTx.MIN_INPUT_SIZE;
65+
return false;
66+
}
67+
// but by 3 inputs... 1.56% chance of minimum (needs ~32 tries)
68+
// by 10 inputs... 0.00953674316% chance (needs ~524288 tries)
69+
fullSats = fullSats + DashTx.MIN_INPUT_SIZE + 1;
70+
});
71+
return included;
72+
};
73+
74+
//@ts-ignore
75+
window.DashWallet = DashWallet;
76+
})(globalThis.window || {}, DashWallet);
77+
if ('object' === typeof module) {
78+
module.exports = DashWallet;
79+
}

public/index.html

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="./node_modules/dashhd/dashhd.js"></script>
1212
<script src="./node_modules/dashtx/dashtx.js"></script>
1313
<script src="./dashjoin.js"></script>
14+
<script src="./dashwallet.js"></script>
1415
<style>
1516
nav {
1617
margin-bottom: 0.3rem;
@@ -158,7 +159,9 @@ <h1>Digital Cash Wallet</h1>
158159
<table>
159160
<thead>
160161
<tr>
161-
<th><input onchange="toggleAll(event)" type="checkbox" /></th>
162+
<th>
163+
<input onchange="App.toggleAll(event)" type="checkbox" />
164+
</th>
162165
<th>Amount</th>
163166
<th>Address</th>
164167
<th>TXID</th>
@@ -173,7 +176,9 @@ <h1>Digital Cash Wallet</h1>
173176
<th></th>
174177
<th>
175178
<small
176-
><button type="button">Denominate Selected</button></small
179+
><button type="button" onclick="App.denominateCoins(event)">
180+
Denominate Selected
181+
</button></small
177182
>
178183
</th>
179184
<th></th>
@@ -214,7 +219,9 @@ <h1>Digital Cash Wallet</h1>
214219
/>
215220
</label>
216221
<small
217-
><button type="button" onclick="setMax(event)">Max</button></small
222+
><button type="button" onclick="App.setMax(event)">
223+
Max
224+
</button></small
218225
>
219226
<small><span data-id="send-dust">200</span> dust</small>
220227
<!-- <label -->
@@ -239,7 +246,7 @@ <h1>Digital Cash Wallet</h1>
239246
/>
240247
</label>
241248

242-
<button type="submit" onclick="sendDash(event)">Send DASH</button>
249+
<button type="submit" onclick="App.sendDash(event)">Send DASH</button>
243250

244251
<hr />
245252
<label
@@ -262,8 +269,8 @@ <h1>Digital Cash Wallet</h1>
262269
<th>
263270
<input
264271
name="priority"
265-
onkeyup="syncCashDrawer(event)"
266-
onchange="syncCashDrawer(event)"
272+
onkeyup="App.syncCashDrawer(event)"
273+
onchange="App.syncCashDrawer(event)"
267274
type="number"
268275
step="1"
269276
min="0"
@@ -274,8 +281,8 @@ <h1>Digital Cash Wallet</h1>
274281
<th>
275282
<input
276283
name="want"
277-
onkeyup="syncCashDrawer(event)"
278-
onchange="syncCashDrawer(event)"
284+
onkeyup="App.syncCashDrawer(event)"
285+
onchange="App.syncCashDrawer(event)"
279286
type="number"
280287
step="1"
281288
min="0"
@@ -289,8 +296,8 @@ <h1>Digital Cash Wallet</h1>
289296
<th>
290297
<input
291298
name="priority"
292-
onkeyup="syncCashDrawer(event)"
293-
onchange="syncCashDrawer(event)"
299+
onkeyup="App.syncCashDrawer(event)"
300+
onchange="App.syncCashDrawer(event)"
294301
type="number"
295302
step="1"
296303
min="0"
@@ -301,8 +308,8 @@ <h1>Digital Cash Wallet</h1>
301308
<th>
302309
<input
303310
name="want"
304-
onkeyup="syncCashDrawer(event)"
305-
onchange="syncCashDrawer(event)"
311+
onkeyup="App.syncCashDrawer(event)"
312+
onchange="App.syncCashDrawer(event)"
306313
type="number"
307314
step="1"
308315
min="0"
@@ -316,8 +323,8 @@ <h1>Digital Cash Wallet</h1>
316323
<th>
317324
<input
318325
name="priority"
319-
onkeyup="syncCashDrawer(event)"
320-
onchange="syncCashDrawer(event)"
326+
onkeyup="App.syncCashDrawer(event)"
327+
onchange="App.syncCashDrawer(event)"
321328
type="number"
322329
step="1"
323330
min="0"
@@ -328,8 +335,8 @@ <h1>Digital Cash Wallet</h1>
328335
<th>
329336
<input
330337
name="want"
331-
onkeyup="syncCashDrawer(event)"
332-
onchange="syncCashDrawer(event)"
338+
onkeyup="App.syncCashDrawer(event)"
339+
onchange="App.syncCashDrawer(event)"
333340
type="number"
334341
step="1"
335342
min="0"
@@ -343,8 +350,8 @@ <h1>Digital Cash Wallet</h1>
343350
<th>
344351
<input
345352
name="priority"
346-
onkeyup="syncCashDrawer(event)"
347-
onchange="syncCashDrawer(event)"
353+
onkeyup="App.syncCashDrawer(event)"
354+
onchange="App.syncCashDrawer(event)"
348355
type="number"
349356
step="1"
350357
min="0"
@@ -355,8 +362,8 @@ <h1>Digital Cash Wallet</h1>
355362
<th>
356363
<input
357364
name="want"
358-
onkeyup="syncCashDrawer(event)"
359-
onchange="syncCashDrawer(event)"
365+
onkeyup="App.syncCashDrawer(event)"
366+
onchange="App.syncCashDrawer(event)"
360367
type="number"
361368
step="1"
362369
min="0"
@@ -370,8 +377,8 @@ <h1>Digital Cash Wallet</h1>
370377
<th>
371378
<input
372379
name="priority"
373-
onkeyup="syncCashDrawer(event)"
374-
onchange="syncCashDrawer(event)"
380+
onkeyup="App.syncCashDrawer(event)"
381+
onchange="App.syncCashDrawer(event)"
375382
type="number"
376383
step="1"
377384
min="0"
@@ -382,8 +389,8 @@ <h1>Digital Cash Wallet</h1>
382389
<th>
383390
<input
384391
name="want"
385-
onkeyup="syncCashDrawer(event)"
386-
onchange="syncCashDrawer(event)"
392+
onkeyup="App.syncCashDrawer(event)"
393+
onchange="App.syncCashDrawer(event)"
387394
type="number"
388395
step="1"
389396
min="0"

0 commit comments

Comments
 (0)