Skip to content

Commit

Permalink
feat: find denominated coins in wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 14, 2024
1 parent 4df3a17 commit 79b2aa4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
23 changes: 12 additions & 11 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="./node_modules/dashkeys/dashkeys.js"></script>
<script src="./node_modules/dashhd/dashhd.js"></script>
<script src="./node_modules/dashtx/dashtx.js"></script>
<script src="./dashjoin.js"></script>
<style>
nav {
margin-bottom: 0.3rem;
Expand Down Expand Up @@ -242,7 +243,7 @@ <h1>Digital Cash Wallet</h1>

<hr />
<label
>Cash Drawer (<code data-id="cash-balance">0.0000</code>)
>Cash Drawer (<code data-id="cj-balance">0.0000</code>)
<small>denominated coins</small>
</label>
<table>
Expand All @@ -268,7 +269,7 @@ <h1>Digital Cash Wallet</h1>
value="1"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
Expand All @@ -279,7 +280,7 @@ <h1>Digital Cash Wallet</h1>
value="2"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
<tr data-denom="100001000">
<th>1.000<span hidden>01000</span></th>
Expand All @@ -293,7 +294,7 @@ <h1>Digital Cash Wallet</h1>
value="10"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
Expand All @@ -304,7 +305,7 @@ <h1>Digital Cash Wallet</h1>
value="10"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
<tr data-denom="10000100">
<th>0.100<span hidden>00100</span></th>
Expand All @@ -318,7 +319,7 @@ <h1>Digital Cash Wallet</h1>
value="10"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
Expand All @@ -329,7 +330,7 @@ <h1>Digital Cash Wallet</h1>
value="50"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
<tr data-denom="1000010">
<th>0.010<span hidden>00010</span></th>
Expand All @@ -343,7 +344,7 @@ <h1>Digital Cash Wallet</h1>
value="1"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
Expand All @@ -354,7 +355,7 @@ <h1>Digital Cash Wallet</h1>
value="20"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
<tr data-denom="100001">
<th>0.001<span hidden>00001</span></th>
Expand All @@ -368,7 +369,7 @@ <h1>Digital Cash Wallet</h1>
value="0"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
Expand All @@ -379,7 +380,7 @@ <h1>Digital Cash Wallet</h1>
value="5"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
</tbody>
</table>
Expand Down
50 changes: 45 additions & 5 deletions public/wallet-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
let DashTx = window.DashTx;
let Secp256k1 = window.nobleSecp256k1;

let DashJoin = window.DashJoin;

const SATS = 100000000;
const MIN_BALANCE = 100001 * 1000;

Expand All @@ -28,6 +30,7 @@
let spentAddrs = [];
let deltasMap = {};
let keysMap = {};
let denomsMap = {};

let keyUtils = {
getPrivateKey: async function (txInput, i) {
Expand Down Expand Up @@ -440,7 +443,7 @@

let defaultCjSlots = [
{
denom: 10000100000,
denom: 1000010000,
priority: 1,
have: 0,
want: 2,
Expand Down Expand Up @@ -484,19 +487,16 @@
return slots;
}
window.syncCashDrawer = function (event) {
console.log('DEBUG syncCashDrawer');
let isDirty = false;

let slots = getCashDrawer();
for (let slot of slots) {
let $row = $(`[data-denom="${slot.denom}"]`);
console.log('DEBUG syncCashDrawer slot', slot, $row);

let priorityStr = $('[name=priority]', $row).value;
if (priorityStr) {
let priority = parseFloat(priorityStr);
if (slot.priority !== priority) {
console.log('DEBUG update priority', slot.priority, priority);
isDirty = true;
slot.priority = priority;
}
Expand All @@ -506,7 +506,6 @@
if (wantStr) {
let want = parseFloat(wantStr);
if (slot.want !== want) {
console.log('DEBUG update priority', slot.want, want);
isDirty = true;
slot.want = want;
}
Expand All @@ -520,12 +519,27 @@
return true;
};
function renderCashDrawer() {
let cjBalance = 0;
let slots = getCashDrawer();
for (let slot of slots) {
let $row = $(`[data-denom="${slot.denom}"]`);
let addrs = Object.keys(denomsMap[slot.denom]);
let have = addrs.length;
let need = slot.want - have;
need = Math.max(0, need);

$('[name=priority]', $row).value = slot.priority;
$('[name=want]', $row).value = slot.want;
$('[data-name=have]', $row).textContent = have;
$('[data-name=need]', $row).textContent = need;

for (let addr of addrs) {
cjBalance += denomsMap[slot.denom][addr].satoshis;
}
}

let cjAmount = cjBalance / SATS;
$('[data-id=cj-balance]').textContent = cjAmount.toFixed(8);
}

async function updateDeltas(addrs) {
Expand Down Expand Up @@ -625,6 +639,31 @@
}
}

function siftDenoms() {
for (let denom of DashJoin.DENOMS) {
if (!denomsMap[denom]) {
denomsMap[denom] = {};
}
}

let addrs = Object.keys(deltasMap);
for (let addr of addrs) {
let info = deltasMap[addr];
if (info.balance === 0) {
continue;
}

for (let coin of info.deltas) {
let denom = DashJoin.getDenom(coin.satoshis);
if (!denom) {
continue;
}

denomsMap[denom][coin.address] = coin;
}
}
}

async function main() {
if (network === `testnet`) {
let $testnets = $$('[data-network=testnet]');
Expand All @@ -634,6 +673,7 @@
}

await init();
siftDenoms();
renderCashDrawer();
}

Expand Down

0 comments on commit 79b2aa4

Please sign in to comment.