Skip to content

Commit

Permalink
feat: prepare denoms for send
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 15, 2024
1 parent eec662a commit e302ccb
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 45 deletions.
79 changes: 79 additions & 0 deletions public/dashwallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var DashWallet = ('object' === typeof module && exports) || {};
(function (window, DashWallet) {
'use strict';

let DashTx = window.DashTx;

/**
* @template {Pick<CoreUtxo, "satoshis">} T
* @param {Array<T>} utxos
* @param {Number} output - including fee estimate
* @return {Array<T>}
*/
DashWallet.selectOptimalUtxos = function (utxos, satsOut) {
let balance = DashTx.sum(utxos);
let fees = DashTx.appraise({
//@ts-ignore
inputs: [{}],
outputs: [{}],
});

let fullSats = satsOut + fees.min;

if (balance < fullSats) {
return [];
}

// from largest to smallest
utxos.sort(function (a, b) {
return b.satoshis - a.satoshis;
});

/** @type Array<T> */
let included = [];
let total = 0;

// try to get just one
utxos.every(function (utxo) {
if (utxo.satoshis > fullSats) {
included[0] = utxo;
total = utxo.satoshis;
return true;
}
return false;
});
if (total) {
return included;
}

// try to use as few coins as possible
utxos.some(function (utxo, i) {
included.push(utxo);
total += utxo.satoshis;
if (total >= fullSats) {
return true;
}

// it quickly becomes astronomically unlikely to hit the one
// exact possibility that least to paying the absolute minimum,
// but remains about 75% likely to hit any of the mid value
// possibilities
if (i < 2) {
// 1 input 25% chance of minimum (needs ~2 tries)
// 2 inputs 6.25% chance of minimum (needs ~8 tries)
fullSats = fullSats + DashTx.MIN_INPUT_SIZE;
return false;
}
// but by 3 inputs... 1.56% chance of minimum (needs ~32 tries)
// by 10 inputs... 0.00953674316% chance (needs ~524288 tries)
fullSats = fullSats + DashTx.MIN_INPUT_SIZE + 1;
});
return included;
};

//@ts-ignore
window.DashWallet = DashWallet;
})(globalThis.window || {}, DashWallet);
if ('object' === typeof module) {
module.exports = DashWallet;
}
55 changes: 31 additions & 24 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="./node_modules/dashhd/dashhd.js"></script>
<script src="./node_modules/dashtx/dashtx.js"></script>
<script src="./dashjoin.js"></script>
<script src="./dashwallet.js"></script>
<style>
nav {
margin-bottom: 0.3rem;
Expand Down Expand Up @@ -158,7 +159,9 @@ <h1>Digital Cash Wallet</h1>
<table>
<thead>
<tr>
<th><input onchange="toggleAll(event)" type="checkbox" /></th>
<th>
<input onchange="App.toggleAll(event)" type="checkbox" />
</th>
<th>Amount</th>
<th>Address</th>
<th>TXID</th>
Expand All @@ -173,7 +176,9 @@ <h1>Digital Cash Wallet</h1>
<th></th>
<th>
<small
><button type="button">Denominate Selected</button></small
><button type="button" onclick="App.denominateCoins(event)">
Denominate Selected
</button></small
>
</th>
<th></th>
Expand Down Expand Up @@ -214,7 +219,9 @@ <h1>Digital Cash Wallet</h1>
/>
</label>
<small
><button type="button" onclick="setMax(event)">Max</button></small
><button type="button" onclick="App.setMax(event)">
Max
</button></small
>
<small><span data-id="send-dust">200</span> dust</small>
<!-- <label -->
Expand All @@ -239,7 +246,7 @@ <h1>Digital Cash Wallet</h1>
/>
</label>

<button type="submit" onclick="sendDash(event)">Send DASH</button>
<button type="submit" onclick="App.sendDash(event)">Send DASH</button>

<hr />
<label
Expand All @@ -262,8 +269,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -274,8 +281,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -289,8 +296,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -301,8 +308,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -316,8 +323,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -328,8 +335,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -343,8 +350,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -355,8 +362,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -370,8 +377,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand All @@ -382,8 +389,8 @@ <h1>Digital Cash Wallet</h1>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
onkeyup="App.syncCashDrawer(event)"
onchange="App.syncCashDrawer(event)"
type="number"
step="1"
min="0"
Expand Down
Loading

0 comments on commit e302ccb

Please sign in to comment.