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 77de585
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 18 deletions.
33 changes: 22 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 @@ -262,124 +263,134 @@ <h1>Digital Cash Wallet</h1>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="1"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
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>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="10"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
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>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="10"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
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>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="1"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
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>
<th>
<input
name="priority"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="0"
/>
</th>
<th>0</th>
<th data-name="have">0</th>
<th>
<input
name="want"
onkeyup="syncCashDrawer(event)"
onchange="syncCashDrawer(event)"
type="number"
step="1"
min="0"
value="5"
/>
</th>
<th>0</th>
<th data-name="need">0</th>
</tr>
</tbody>
</table>
Expand Down
67 changes: 60 additions & 7 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 @@ -517,15 +516,43 @@
dbSet('cash-drawer-control', slots);
}

renderCashDrawer();
return true;
};

function renderCashDrawer() {
let cjBalance = 0;
let slots = getCashDrawer();
for (let slot of slots) {
let $row = $(`[data-denom="${slot.denom}"]`);
$('[name=priority]', $row).value = slot.priority;
$('[name=want]', $row).value = slot.want;
let addrs = Object.keys(denomsMap[slot.denom]);
let have = addrs.length;
let need = slot.want - have;
need = Math.max(0, need);

let priority = $('[name=priority]', $row).value;
if (priority) {
if (priority !== slot.priority.toString()) {
$('[name=priority]', $row).value = slot.priority;
}
}
let want = $('[name=want]', $row).value;
if (want) {
if (want !== slot.want.toString()) {
$('[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 +652,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 +686,7 @@
}

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

Expand Down

0 comments on commit 77de585

Please sign in to comment.