Skip to content

Commit 8bdb56c

Browse files
committed
feat: run the actual denomination
1 parent 7075f9f commit 8bdb56c

File tree

1 file changed

+99
-24
lines changed

1 file changed

+99
-24
lines changed

public/wallet-app.js

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@
250250
console.log('DEBUG Amount:', amount);
251251

252252
let output = { satoshis, address };
253+
let draft = await draftWalletTx(utxos, inputs, output);
254+
255+
amount = output.satoshis / SATS;
256+
$('[data-id=send-dust]').textContent = draft.tx.feeTarget;
257+
$('[data-id=send-amount]').textContent = amount.toFixed(8);
258+
259+
let signedTx = await dashTx.legacy.finalizePresorted(draft.tx);
260+
console.log('DEBUG signed tx', signedTx);
261+
{
262+
let amountStr = amount.toFixed(4);
263+
let confirmed = window.confirm(`Really send ${amountStr} to ${address}?`);
264+
if (!confirmed) {
265+
return;
266+
}
267+
}
268+
void (await rpc('sendrawtransaction', signedTx.transaction));
269+
void (await commitWalletTx(signedTx));
270+
};
271+
272+
async function draftWalletTx(utxos, inputs, output) {
253273
let draftTx = dashTx.legacy.draftSingleOutput({ utxos, inputs, output });
254274
console.log('DEBUG draftTx', draftTx);
255275

@@ -286,24 +306,16 @@
286306

287307
draftTx.inputs.sort(DashTx.sortInputs);
288308
draftTx.outputs.sort(DashTx.sortOutputs);
289-
amount = output.satoshis / SATS;
290-
291-
$('[data-id=send-dust]').textContent = draftTx.feeTarget;
292-
$('[data-id=send-amount]').textContent = amount.toFixed(8);
293309

294-
let tx = await dashTx.legacy.finalizePresorted(draftTx);
295-
console.log('DEBUG signed tx', tx);
296-
{
297-
let amountStr = amount.toFixed(4);
298-
let confirmed = window.confirm(`Really send ${amountStr} to ${address}?`);
299-
if (!confirmed) {
300-
return;
301-
}
302-
}
303-
void (await rpc('sendrawtransaction', tx.transaction));
310+
return {
311+
tx: draftTx,
312+
change: changeOutput,
313+
};
314+
}
304315

316+
async function commitWalletTx(signedTx) {
305317
let updatedAddrs = [];
306-
for (let input of tx.inputs) {
318+
for (let input of signedTx.inputs) {
307319
updatedAddrs.push(input.address);
308320
let knownSpent = spentAddrs.includes(input.address);
309321
if (!knownSpent) {
@@ -315,19 +327,46 @@
315327
delete deltasMap[input.address];
316328
dbSet(input.address, null);
317329
}
318-
for (let output of tx.outputs) {
330+
for (let output of signedTx.outputs) {
319331
updatedAddrs.push(output.address);
320332
removeElement(addresses, output.address);
321333
removeElement(receiveAddrs, output.address);
322334
removeElement(changeAddrs, output.address);
323335
delete deltasMap[output.address];
324336
dbSet(output.address, null);
325337
}
326-
327338
await updateDeltas(updatedAddrs);
339+
340+
let txid = await DashTx.getId(signedTx.transaction);
341+
for (let input of signedTx.inputs) {
342+
let coin = selectCoin(input.address, input.txid, input.outputIndex);
343+
if (!coin) {
344+
continue;
345+
}
346+
coin.reserved = true; // mark as spent-ish
347+
}
348+
for (let i = 0; i < signedTx.outputs.length; i += 1) {
349+
let output = signedTx.outputs[i];
350+
let info = deltasMap[output.address];
351+
if (!info) {
352+
info = { balance: 0, deltas: [] };
353+
deltasMap[output.address] = info;
354+
}
355+
let memCoin = selectCoin(output.address, txid, i);
356+
if (!memCoin) {
357+
memCoin = {
358+
address: output.address,
359+
satoshis: output.satoshis,
360+
txid: txid,
361+
index: i,
362+
};
363+
info.deltas.push(memCoin);
364+
}
365+
}
366+
328367
renderAddresses();
329368
renderCoins();
330-
};
369+
}
331370

332371
function renderAddresses() {
333372
$('[data-id=spent-count]').textContent = spentAddrs.length;
@@ -580,7 +619,7 @@
580619
$('[data-id=cj-balance]').textContent = cjAmount.toFixed(8);
581620
}
582621

583-
App.denominateCoins = function (event) {
622+
App.denominateCoins = async function (event) {
584623
console.log('DENOMINATE COINS');
585624
event.preventDefault();
586625

@@ -636,17 +675,51 @@
636675
}
637676
slot.need -= 1;
638677

639-
// TODO DashTx.
678+
// TODO DashTx.
640679
console.log('Found coins to make denom', slot.denom, coins);
680+
let roundRobiner = createRoundRobin(slots, slot);
681+
// roundRobiner();
641682

642-
if (slot.need >= 1) {
643-
// round-robin same priority
644-
slots.push(slot);
645-
}
683+
let address = receiveAddrs.shift();
684+
let satoshis = slot.denom;
685+
let output = { satoshis, address };
686+
687+
void (await confirmAndBroadcastAndCompleteTx(coins, output).then(
688+
roundRobiner,
689+
));
646690
}
647691
}
648692
};
649693

694+
function createRoundRobin(slots, slot) {
695+
return function () {
696+
if (slot.need >= 1) {
697+
// round-robin same priority
698+
slots.push(slot);
699+
}
700+
};
701+
}
702+
703+
async function confirmAndBroadcastAndCompleteTx(inputs, output) {
704+
let utxos = null;
705+
let draft = await draftWalletTx(utxos, inputs, output);
706+
707+
let signedTx = await dashTx.legacy.finalizePresorted(draft.tx);
708+
{
709+
console.log('DEBUG confirming signed tx', signedTx);
710+
let amount = output.satoshis / SATS;
711+
let amountStr = amount.toFixed(4);
712+
let confirmed = window.confirm(
713+
`Really send ${amountStr} to ${output.address}?`,
714+
);
715+
if (!confirmed) {
716+
return;
717+
}
718+
}
719+
void (await rpc('sendrawtransaction', signedTx.transaction));
720+
void (await commitWalletTx(signedTx));
721+
}
722+
650723
function groupSlotsByPriorityAndAmount(slots) {
651724
let priorityGroups = {};
652725
for (let slot of slots) {
@@ -796,6 +869,7 @@
796869
continue;
797870
}
798871

872+
console.log('DEBUG denom', denom, coin);
799873
denomsMap[denom][coin.address] = coin;
800874
}
801875
}
@@ -812,6 +886,7 @@
812886
await init();
813887
siftDenoms();
814888
renderCashDrawer();
889+
App.syncCashDrawer();
815890
}
816891

817892
main().catch(function (err) {

0 commit comments

Comments
 (0)