redgold 0.1.12

p2p database / compute engine & portfolio target models
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use log::{error, info};

use redgold_schema::{bytes_data, error_info, ErrorInfoContext, from_hex, from_hex_ref, RgResult, SafeBytesAccess, SafeOption, structs, WithMetadataHashable};
use redgold_schema::structs::{Address, BytesData, ErrorInfo, ExternalCurrency, Hash, InitiateMultipartyKeygenRequest, InitiateMultipartySigningRequest, MultipartyIdentifier, NetworkEnvironment, PublicKey, StandardContractType, SubmitTransactionResponse, Transaction, TransactionAmount};
use crate::core::relay::Relay;
use crate::core::stream_handlers::IntervalFold;
use crate::multiparty::initiate_mp;

use serde::{Serialize, Deserialize};
use redgold_keys::transaction_support::{TransactionBuilderSupport, TransactionSupport};
use redgold_schema::transaction_builder::TransactionBuilder;
use redgold_keys::util::btc_wallet::{ExternalTimedTransaction, SingleKeyBitcoinWallet};
use crate::multiparty::initiate_mp::{default_room_id, initiate_mp_keysign};
use crate::node::Node;
use crate::util::address_external::ToBitcoinAddress;
use crate::util::logging::Loggable;
use redgold_schema::EasyJson;
use redgold_schema::errors::EnhanceErrorInfo;
use crate::node_config::NodeConfig;
use crate::util::cli::arg_parse_config::ArgTranslate;
use crate::util::cli::args::RgArgs;

#[derive(Serialize, Deserialize, Clone)]
pub struct DepositKeyAllocation {
    pub key: PublicKey,
    pub allocation: f64,
    pub initiate: InitiateMultipartyKeygenRequest,
    pub balance_btc: u64,
    pub balance_rdg: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PriceVolume {
    pub price: f64, // RDG/BTC (in satoshis for both) for now
    pub volume: u64, // Volume of RDG available
}

impl PriceVolume {
    pub fn generate(
        available_volume: u64,
        center_price: f64,
        divisions: i32,
        price_width: f64,
        scale: f64
    ) -> Vec<PriceVolume> {
        let divisions_f64 = divisions as f64;

        // Calculate the common ratio
        let ratio = (1.0 / scale).powf(1.0 / (divisions_f64 - 1.0));

        // Calculate the first term
        let first_term = available_volume as f64 * scale / (1.0 - ratio.powf(divisions_f64));

        let mut price_volumes = Vec::new();
        for i in 0..divisions {
            let price_offset = (i+1) as f64;
            let price = center_price + (price_offset * (price_width/divisions_f64));
            let volume = (first_term * ratio.powi(divisions-i)) as u64;
            price_volumes.push(PriceVolume { price, volume });
        }
        let total_volume = price_volumes.iter().map(|v| v.volume).sum::<u64>();

        // Normalize the volumes so their sum equals available_volume
        for pv in &mut price_volumes {
            pv.volume = ((pv.volume as f64 / total_volume as f64) * available_volume as f64) as u64;
        }

        if total_volume != available_volume {
            let delta = total_volume as i64 - available_volume as i64;
            let last = price_volumes.last_mut().unwrap();
            if delta > 0 && (last.volume as u64) > delta as u64 {
                last.volume = ((last.volume as i64) - delta) as u64;
            } else if delta < 0 {
                last.volume = ((last.volume as i64) - delta) as u64;
            }
        }
        price_volumes
    }
}

#[test]
fn inspect_price_volume() {
    let pv = PriceVolume::generate(1_000_000, 1., 25, -0.5, 10.0);
    for p in pv.iter() {
        println!("{}, {}", p.price, p.volume);
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct BidAsk{
    pub bids: Vec<PriceVolume>,
    pub asks: Vec<PriceVolume>,
    pub center_price: f64
}

impl BidAsk {

    pub fn asking_price(&self) -> f64 {
        self.asks.get(0).map(|v| v.price).unwrap_or(0.)
    }

    pub fn sum_bid_volume(&self) -> u64 {
        self.bids.iter().map(|v| v.volume).sum::<u64>()
    }

    pub fn sum_ask_volume(&self) -> u64 {
        self.asks.iter().map(|v| v.volume).sum::<u64>()
    }

    pub fn volume_empty(&self) -> bool {
        self.bids.iter().find(|v| v.volume == 0).is_some() ||
        self.asks.iter().find(|v| v.volume == 0).is_some()
    }

    pub fn regenerate(&self, price: f64) -> BidAsk {
        BidAsk::generate_default(
            self.sum_bid_volume() as i64,
            self.sum_ask_volume(),
            price
        )
    }

    pub fn generate_default(
        available_balance: i64,
        pair_balance: u64,
        last_exchange_price: f64, // this is for available type / pair type
    ) -> BidAsk {
        BidAsk::generate(
            available_balance,
            pair_balance,
            last_exchange_price,
            100,
            100.
        )
    }

    pub fn generate(
        available_balance: i64,
        pair_balance: u64,
        last_exchange_price: f64, // this is for available type / pair type
        divisions: i32,
        scale: f64
    ) -> BidAsk {

        let bids = PriceVolume::generate(
            available_balance as u64,
            last_exchange_price,
            divisions,
            -(last_exchange_price*0.9),
            scale
        );
        let asks = PriceVolume::generate(
            pair_balance,
            last_exchange_price,
            divisions,
            last_exchange_price*100.0,
            scale
        );
        BidAsk {
            bids,
            asks,
            center_price: last_exchange_price,
        }
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct OrderFulfillment {
    pub order_amount: u64,
    pub fulfilled_amount: u64,
    pub updated_curve: Vec<PriceVolume>
}

impl OrderFulfillment {
    pub fn fulfillment_price(&self) -> f64 {
        self.fulfilled_amount as f64 / self.order_amount as f64
    }
}


#[derive(Clone, Serialize, Deserialize)]
pub struct WithdrawalBitcoin {
    outputs: Vec<(String, u64)>,
    updated_bidask: BidAsk,
    used_tx: Vec<Transaction>
}


impl BidAsk {
    pub fn fulfill_taker_order(&self, order_amount: u64, is_ask: bool) -> OrderFulfillment {
        let mut remaining_order_amount = order_amount.clone();
        let mut fulfilled_amount: u64 = 0;
        let mut updated_curve = if is_ask {
            self.asks.clone()
        } else {
            // Reverse the bids and invert the price so we can pop them off in order
            let mut b = self.bids.clone();
            b.reverse();
            for b in b.iter_mut() {
                b.price = 1.0 / b.price;
            }
            b
        };
        for b in updated_curve.iter_mut() {
            // Price is in RDG / BTC (satoshi)
            // Amount is in BTC satoshis, this gives RDG
            let rdg_amount = (b.price * (remaining_order_amount as f64)) as u64;
            if rdg_amount > b.volume {
                // We have more RDG than this ask can fulfill, so we take it all and move on.
                fulfilled_amount += b.volume;
                remaining_order_amount -= (b.volume as f64 / b.price) as u64;
                b.volume = 0;
            } else {
                // We have less RDG than this ask can fulfill, so we take it and stop
                b.volume -= rdg_amount;
                remaining_order_amount = 0;
                break
            }
        };
        // Return bids to the original order
        if !is_ask {
            updated_curve.reverse();
            for b in updated_curve.iter_mut() {
                b.price = 1.0 / b.price;
            }
        }
        OrderFulfillment {
            order_amount,
            fulfilled_amount,
            updated_curve,
        }
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct DepositWatcherConfig {
    pub deposit_allocations: Vec<DepositKeyAllocation>,
    // TODO: Make this a map over currency type
    pub bid_ask: BidAsk,
    pub last_btc_timestamp: u64
}

#[derive(Clone)]
pub struct Watcher {
    relay: Relay,
    wallet: Vec<Arc<Mutex<SingleKeyBitcoinWallet>>>
}

impl Watcher {
    pub async fn genesis_funding(&self, destination: &Address) -> RgResult<()> {
        let (_, utxos) = Node::genesis_from(self.relay.node_config.clone());
        let u = utxos.get(15).safe_get_msg("Missing utxo")?.clone();
        let a = u.key_pair.address_typed().render_string().expect("a");
        let res = self.relay.ds.transaction_store.query_utxo_id_valid(
            &u.utxo_entry.transaction_hash.clone().expect("hash"), u.utxo_entry.output_index.clone()
        ).await?;
        let uu = u.utxo_entry.clone().json_or();
        if res {
            info!("Sending genesis funding to multiparty address from origin {a} using utxo {uu}");
            let mut tb = TransactionBuilder::new();
            tb.with_utxo(&u.utxo_entry)?;
            tb.with_output(&destination, &TransactionAmount::from(u.utxo_entry.amount() as i64));
            let mut tx = tb.build()?;
            tx.sign(&u.key_pair)?;
            self.relay.submit_transaction_sync(&tx).await?;
        } else {
            info!("No genesis funding possible to send");
        }
        Ok(())
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct CurveUpdateResult {
    updated_bid_ask: BidAsk,
    updated_btc_timestamp: u64,
    updated_allocation: DepositKeyAllocation
}

impl Watcher {
    pub fn new(relay: Relay) -> Self {
        Self {
            relay,
            wallet: vec![],
        }
    }
    // pub fn establish_first_allocation(&self) -> RgResult<()> {
    //
    // }

    // TODO: From oracle or api
    pub async fn convert_btc_amount_usd(_timestamp: u64, _amount: u64) -> f64 {
        0.
    }

    pub async fn get_btc_deposits(&mut self, last_timestamp: u64, w: &Arc<Mutex<SingleKeyBitcoinWallet>>) -> Result<(u64, Vec<ExternalTimedTransaction>), ErrorInfo>{
        let pk_hex = w.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
            .public_key.hex_or();

        let mut sourced_tx = w.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
            .get_sourced_tx()?;

        info!("public key: {} Got {} sourced tx raw: {}", pk_hex, sourced_tx.len(), sourced_tx.json_or());

        let mut max_ts: u64 = last_timestamp;
        sourced_tx.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
        let mut res = vec![];
        for tx in sourced_tx.iter() {
            if tx.timestamp <= last_timestamp {
                continue;
            }
            if tx.timestamp > max_ts {
                max_ts = tx.timestamp;
            }
            let used = self.relay.ds.multiparty_store.check_bridge_txid_used(&from_hex(tx.tx_id.clone())?).await?;
            if used {
                continue
            }
            res.push(tx.clone())
        }
        Ok((max_ts, res.clone()))
    }

    pub async fn build_rdg_ask_swap_tx(&self, btc_deposits: Vec<ExternalTimedTransaction>, bid_ask: BidAsk, key_address: &structs::Address)
        -> RgResult<(Option<Transaction>, BidAsk)> {

        let mut bid_ask_latest = bid_ask.clone();

        let utxos =
            self.relay.ds.transaction_store.query_utxo_address(&key_address)
                .await?;
        // We're building a transaction FROM some stored input balance we have
        // for our pubkey multisig address
        let mut tb = TransactionBuilder::new();
        for u in &utxos {
            // Check contract type here
            // let o = u.output.safe_get_msg("Missing output on UTXO")?;
            // if let Some(o) = &o.contract.as_ref().and_then(|c| c.standard_contract_type) {
            //     if o == StandardContractType::Swap as i32
            // }
            tb.with_maybe_currency_utxo(u)?;
        }

        for tx in btc_deposits.iter() {
            let destination = tx.other_address.clone();
            let destination_address = structs::Address::from_bitcoin(&destination);
            let ask_fulfillment = bid_ask_latest.fulfill_taker_order(tx.amount, true);
            let destination_amount = ask_fulfillment.fulfilled_amount;

            tb.with_output(&destination_address,
                           &TransactionAmount::from(destination_amount as i64)
            );
            tb.with_last_output_deposit_swap(tx.tx_id.clone());

            let price = ask_fulfillment.fulfillment_price() * 1.05;
            bid_ask_latest = bid_ask_latest.regenerate(price)

        }
        let mut tx_ret = None;
        if !btc_deposits.is_empty() {
            tx_ret = Some(tb.build()?);
        }
        Ok((tx_ret, bid_ask_latest))
    }

    pub async fn send_ask_fulfillment_transaction(&self, tx: &mut Transaction, identifier: MultipartyIdentifier) -> RgResult<SubmitTransactionResponse> {

        let hash = tx.signable_hash();
        let result = initiate_mp_keysign(self.relay.clone(), identifier.clone(),
                                         hash.bytes.safe_get()?.clone(), identifier.party_keys.clone(), None
        ).await?;
        tx.add_proof_per_input(&result.proof);
        self.relay.submit_transaction_sync(tx).await
    }

    pub async fn fulfill_btc_bids(&self, w_arc: &Arc<Mutex<SingleKeyBitcoinWallet>>,
                                  identifier: MultipartyIdentifier, outputs: Vec<(String, u64)>) -> RgResult<String> {
        w_arc.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
            .create_transaction_output_batch(outputs)?;
        let hashes = w_arc.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
            .signable_hashes()?.clone();
        for (i, (hash, hash_type)) in hashes.iter().enumerate() {
            let result = initiate_mp_keysign(self.relay.clone(), identifier.clone(),
                                             BytesData::from(hash.clone()),
                                             identifier.party_keys.clone(), None
            ).await?;
            w_arc.lock()
                .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
                .affix_input_signature(i, &result.proof, hash_type);
        }
        let mut w = w_arc.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?;
        w.sign()?;
        w.broadcast_tx()?;
        Ok(w.txid()?)
    }

    pub async fn update_withdrawal_datastore(&self, withdrawals: WithdrawalBitcoin, txid: String, key_address: &structs::Address) -> RgResult<()> {
        for t in withdrawals.used_tx.iter() {
            let h = t.hash_or();
            let first_input_addr = t.first_input_address();
            let source_address = first_input_addr.safe_get_msg("Missing address")?;
            let input_pk_btc_addr = t.first_input_proof_public_key().as_ref()
                .and_then(|&pk| pk.to_bitcoin_address_network(self.relay.node_config.network.clone()).ok());
            let opt_btc_addr = t.output_bitcoin_address_of(key_address).cloned()
                .or(input_pk_btc_addr);
            let destination_address_string_btc = opt_btc_addr.safe_get_msg("Missing destination address")?.clone();
            let dest = structs::Address::from_bitcoin(&destination_address_string_btc);
            let amount_rdg = t.output_swap_amount_of(key_address);

            self.relay.ds.multiparty_store.insert_bridge_tx(
                &h.safe_bytes()?.clone(),
                &from_hex_ref(&txid)?,
                // Since the origin of this is on the network through a Redgold transaction,
                // and we're generating a bitcoin transaction from that, then its outgoing
                // To an external network
                true,
                ExternalCurrency::Bitcoin,
                source_address,
                &dest,
                t.time()?.clone(),
                amount_rdg
            ).await?;

        }
        Ok(())
    }

    pub async fn get_rdg_withdrawals_bids(&self, bid_ask: BidAsk, key_address: &structs::Address) -> RgResult<WithdrawalBitcoin> {
        let mut bid_ask_latest = bid_ask.clone();
        // These are all transactions that have been sent as RDG to this deposit address,
        // We need to filter out the ones that have already been paid.
        let tx: Vec<structs::Transaction> = self.relay.ds.transaction_store
            .get_filter_tx_for_address(&key_address, 10000, 0, true).await?;

        let mut btc_outputs: Vec<(String, u64)> = vec![];
        let mut tx_res: Vec<Transaction> = vec![];

        for t in tx.iter() {
            let h = t.hash_or();
            let used = self.relay.ds.multiparty_store.check_bridge_txid_used(&h.safe_bytes()?.clone()).await?;
            if !used {
                let input_pk_btc_addr = t.first_input_proof_public_key().as_ref()
                    .and_then(|&pk| pk.to_bitcoin_address_network(self.relay.node_config.network.clone()).ok());
                let opt_btc_addr = t.output_bitcoin_address_of(&key_address).cloned()
                    .or(input_pk_btc_addr);
                let amount_rdg = t.output_swap_amount_of(&key_address);
                let destination_address_string_btc = opt_btc_addr.safe_get_msg("Missing destination address")?.clone();

                if amount_rdg > 0 && opt_btc_addr.is_some() {

                    let fulfillment = bid_ask_latest.fulfill_taker_order(amount_rdg as u64, false);
                    btc_outputs.push((destination_address_string_btc.clone(), fulfillment.fulfilled_amount));
                    tx_res.push(t.clone());
                    // In case of failure or error, we need to keep track of the last price that was used so
                    // we can recover the partial state that was updated instead of the full.
                    let price = fulfillment.fulfillment_price() * 0.95;
                    bid_ask_latest = bid_ask_latest.regenerate(price);

                }

            }
        }

        Ok(WithdrawalBitcoin {
            outputs: btc_outputs,
            updated_bidask: bid_ask_latest,
            used_tx: tx_res,
        })
    }


    pub async fn process_requests(
        &mut self,
        alloc: &DepositKeyAllocation,
        bid_ask_original: BidAsk,
        last_timestamp: u64,
        w: &Arc<Mutex<SingleKeyBitcoinWallet>>,
    ) -> Result<CurveUpdateResult, ErrorInfo> {
        let key = &alloc.key;
        let key_address = key.address()?;
        let identifier = alloc.initiate.identifier.safe_get().cloned()?;

        let btc_starting_balance = w.lock()
            .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
            .get_wallet_balance()?.confirmed;
        let balance = self.relay.ds.transaction_store.get_balance(&key_address).await?;
        let rdg_starting_balance: i64 = balance.safe_get_msg("Missing balance")?.clone();


        let bid_ask = BidAsk::generate_default(rdg_starting_balance, btc_starting_balance, bid_ask_original.center_price);

        info!("Starting watcher process request with balances: RDG:{}, BTC:{} bid_ask: {}", rdg_starting_balance, btc_starting_balance, bid_ask.json_or());


        let mut bid_ask_latest = bid_ask.clone();

        // Prepare Fulfill Asks RDG Transaction from BTC deposits to this multiparty address,
        // but don't yet broadcast the transaction.
        let (updated_last_ts, deposit_txs) = self.get_btc_deposits(last_timestamp, w).await?;

        info!("Found {} new deposits last_updated {} updated_last_ts {} deposit_txs {}",
            deposit_txs.len(), last_timestamp, updated_last_ts, deposit_txs.json_or());

        let (tx, bid_ask_updated_ask_side) = self.build_rdg_ask_swap_tx(deposit_txs, bid_ask_latest, &key_address.clone()).await?;
        // info!("Built RDG ask swap tx: {} bid_ask_updated {}", tx.json_or(), bid_ask_updated_ask_side.json_or());
        if let Some(tx) = tx {
            info!("Sending RDG ask swap tx: {}", tx.json_or());
            self.send_ask_fulfillment_transaction(&mut tx.clone(), identifier.clone()).await?;
        }

        bid_ask_latest = bid_ask_updated_ask_side;

        let withdrawals = self.get_rdg_withdrawals_bids(bid_ask_latest, &key_address).await?;
        bid_ask_latest = withdrawals.updated_bidask.clone();

        info!("Found {} new withdrawals {}",
            withdrawals.outputs.len(), withdrawals.json_or());

        if withdrawals.outputs.len() > 0 {
            let txid = self.fulfill_btc_bids(w, identifier, withdrawals.outputs.clone()).await?;
            info!("Fullfilled btc Txid: {}", txid);
            // On failure here really need to handle this somehow?
            self.update_withdrawal_datastore(withdrawals, txid, &key_address).await?;
        }

        let mut updated_allocation = alloc.clone();
        updated_allocation.balance_btc = bid_ask_latest.sum_bid_volume();
        updated_allocation.balance_rdg = bid_ask_latest.sum_ask_volume();
        let update = CurveUpdateResult{
            updated_bid_ask: bid_ask_latest,
            updated_btc_timestamp: updated_last_ts,
            updated_allocation,
        };

        info!("Updated Curve Result {}", update.json_or());

        Ok(update)
    }

    pub async fn get_starting_center_price(&self) -> f64 {
        // RDG / BTC
        30_000.0
    }
}


#[async_trait]
impl IntervalFold for Watcher {

    #[tracing::instrument(skip(self))]
    async fn interval_fold(&mut self) -> RgResult<()> {

        info!("Deposit watcher interval fold complete");

        if self.relay.node_config.is_local_debug() {
            return Ok(())
        }

        let ds = self.relay.ds.clone();
        // TODO: Change to query to include trust information re: deposit score
        // How best to represent this to user? As trustData?
        let _nodes = ds.peer_store.active_nodes(None).await?;

        // Fund from genesis for test purposes
        // self.genesis_funding().await?;

        // let kp = initiate_mp::find_multiparty_key_pairs(self.relay.clone()).await;
        // match kp {
        //     Ok(_) => {}
        //     Err(_) => {}
        // }
        let cfg = ds.config_store.get_json::<DepositWatcherConfig>("deposit_watcher_config").await?;
        //.ok.andthen?
        if let Some(mut cfg) = cfg {
            if cfg.bid_ask.center_price == 1f64 {
                info!("Regenerating starting price due to zero stored in config.");
                cfg.bid_ask = cfg.bid_ask.regenerate(self.get_starting_center_price().await);
                ds.config_store.insert_update_json("deposit_watcher_config", cfg.clone()).await?;
            }

            // Check to see if other nodes are dead / not responding, if so, move the thing.
            // Also check bitcoin transaction balances? Find the address they came from.
            // we'll need a guide saying to send from a single account
            if let Some(d) = cfg.deposit_allocations.get(0) {
                info!("Watcher checking deposit allocation pubkey hex: {}", d.key.hex()?);
                if self.wallet.get(0).is_none() {
                    let key = &d.key;
                    let w = SingleKeyBitcoinWallet::new_wallet(key.clone(), self.relay.node_config.network, true)?;
                    self.wallet.push(Arc::new(Mutex::new(w)));
                }
                let w = self.wallet.get(0).cloned();
                if let Some(w) = w {
                    let btc_starting_balance = w.lock()
                        .map_err(|e| error_info(format!("Failed to lock wallet: {}", e).as_str()))?
                        .get_wallet_balance()?.confirmed;

                    let balance = self.relay.ds.transaction_store.get_balance(&d.key.address()?).await?;
                    if balance.map(|x| x > 0).unwrap_or(false) { // && btc_starting_balance > 3500 {
                        let update_result = self.process_requests(
                            d, cfg.bid_ask.clone(), cfg.last_btc_timestamp, &w
                        ).await;
                        if let Ok(update_result) = &update_result {
                            let mut cfg2 = cfg.clone();
                            cfg2.last_btc_timestamp = update_result.updated_btc_timestamp;
                            cfg2.bid_ask = update_result.updated_bid_ask.clone();
                            cfg2.deposit_allocations = vec![update_result.updated_allocation.clone()];
                            ds.config_store.insert_update_json("deposit_watcher_config", cfg2).await?;
                        } else if let Err(e) = update_result {
                            error!("Error processing requests: {}", e.json_or());
                        }
                    } else {
                        info!("No balance found for key: {} or insufficient bitcoin balance of {}", d.key.address()?.render_string()?, btc_starting_balance);
                    }
                }
            }
        } else {
            info!("Attempting to start MP watcher keygen round");
            // Initiate MP keysign etc. gather public key and original proof and params
            let res = initiate_mp::initiate_mp_keygen(self.relay.clone(), None, true).await.log_error();
            // TODO: Get this from local share instead of from a second keysign round.
            if let Ok(r) = res {
                let test_sign = r.identifier.uuid.clone();
                let h = Hash::from_string_calculate(&test_sign);
                let bd = h.bytes.safe_get_msg("Missing bytes in immediate hash calculation")?;
                let ksr = initiate_mp::initiate_mp_keysign(
                    self.relay.clone(), r.identifier.clone(),
                    bd.clone(),
                    r.identifier.party_keys.clone(),
                    None
                ).await.log_error();
                if let Ok(ksr) = ksr {
                    // TODO: if not successful, attempt some retries and then delete the operation
                    // and begin again from keygen.
                    // or just delete it immediately.
                    let pk = ksr.proof.public_key.safe_get_msg("Missing public key on key sign result")?;
                    let cfg = DepositWatcherConfig {
                        deposit_allocations: vec![DepositKeyAllocation{
                            key: pk.clone(),
                            allocation: 1.0,
                            initiate: r.request.clone(),
                            balance_btc: 0,
                            balance_rdg: 0,
                        }],
                        bid_ask: BidAsk { bids: vec![], asks: vec![], center_price: self.get_starting_center_price().await },
                        last_btc_timestamp: 0,
                    };
                    self.genesis_funding(&pk.address()?)
                        .await.add("Genesis watcher funding error").log_error().ok();
                    ds.config_store.insert_update_json("deposit_watcher_config", cfg).await?;
                }
            }
            // self.relay.broadcast_async(nodes, req)
        }

        Ok(())
    }
}

#[ignore]
#[tokio::test]
async fn debug_local_ds_utxo_balance() {
    let mut opts = RgArgs::default();
    opts.network = Some("dev".to_string());
    let node_config = NodeConfig::default();
    let mut arg_translate = ArgTranslate::new(&opts, &node_config.clone());
    arg_translate.translate_args().await.unwrap();
    let nc = arg_translate.node_config;
    let r = Relay::new(nc.clone()).await;
    let a = Address::parse("cf4989701946ae307efdb902efd73c13d933efda0ef04bcbc3eef2146850534a").expect("");
    let utxos = r.ds.transaction_store.query_utxo_address(&a).await.unwrap();
    println!("UTXOS: {}", utxos.json_or());
    println!("{}", nc.mnemonic_words.clone());
    let (tx, _gutxos) = Node::genesis_from(nc.clone());
    // let res = r.ds.transaction_store.query_utxo_output_index(&tx.hash_or()).await.unwrap();
    // println!("UTXO: {}", res.json_or());
    println!("Genesis hash {}", tx.hash_or().hex());
    //
    // // Node::prelim_setup(r);
    // for (i,utxo) in gutxos.iter().enumerate() {
    //     let res = r.ds.transaction_store.query_utxo_id_valid(&tx.hash_or(), i as i64).await.unwrap();
    //     if res {
    //         println!("UTXO {i}: {}", utxo.utxo_entry.json_or());
    //     }
    // }
}