func (a *Anchor) updateUTXO(base btcutil.Amount) error { anchorLog.Info("updateUTXO: base=", base.ToBTC()) if a.wclient == nil { anchorLog.Info("updateUTXO: wclient is nil") return nil } a.balances = make([]balance, 0, 200) err := a.unlockWallet(int64(6)) //600 if err != nil { return fmt.Errorf("%s", err) } unspentResults, err := a.wclient.ListUnspentMin(a.confirmationsNeeded) //minConf=1 if err != nil { return fmt.Errorf("cannot list unspent. %s", err) } anchorLog.Info("updateUTXO: unspentResults.len=", len(unspentResults)) if len(unspentResults) > 0 { var i int for _, b := range unspentResults { if b.Amount > base.ToBTC() { //fee.ToBTC() a.balances = append(a.balances, balance{unspentResult: b}) i++ } } } anchorLog.Info("updateUTXO: balances.len=", len(a.balances)) // Sort eligible balances so that we first pick the ones with highest one sort.Sort(sort.Reverse(ByAmount(a.balances))) for i, b := range a.balances { addr, err := btcutil.DecodeAddress(b.unspentResult.Address, &chaincfg.TestNet3Params) if err != nil { return fmt.Errorf("cannot decode address: %s", err) } a.balances[i].address = addr wif, err := a.wclient.DumpPrivKey(addr) if err != nil { return fmt.Errorf("cannot get WIF: %s", err) } a.balances[i].wif = wif //anchorLog.Infof("balance[%d]=%s \n", i, spew.Sdump(balances[i])) } if len(a.balances) > 0 { a.defaultAddress = a.balances[0].address } return nil }
func updateUTXO() error { anchorLog.Info("updateUTXO: walletLocked=", walletLocked) balances = make([]balance, 0, 200) //if walletLocked { err := unlockWallet(int64(6)) //600 if err != nil { return fmt.Errorf("%s", err) } //} unspentResults, err := wclient.ListUnspentMin(confirmationsNeeded) //minConf=1 if err != nil { return fmt.Errorf("cannot list unspent. %s", err) } anchorLog.Info("updateUTXO: unspentResults.len=", len(unspentResults)) if len(unspentResults) > 0 { var i int for _, b := range unspentResults { if b.Amount > fee.ToBTC() { balances = append(balances, balance{unspentResult: b}) i++ } } } anchorLog.Info("updateUTXO: balances.len=", len(balances)) for i, b := range balances { addr, err := btcutil.DecodeAddress(b.unspentResult.Address, &chaincfg.TestNet3Params) if err != nil { return fmt.Errorf("cannot decode address: %s", err) } balances[i].address = addr wif, err := wclient.DumpPrivKey(addr) if err != nil { return fmt.Errorf("cannot get WIF: %s", err) } balances[i].wif = wif //anchorLog.Infof("balance[%d]=%s \n", i, spew.Sdump(balances[i])) } //time.Sleep(1 * time.Second) return nil }
func initWallet() error { balances = make([]balance, 0, 100) fee, _ = btcutil.NewAmount(cfg.Btc.BtcTransFee) err := unlockWallet(int64(600)) if err != nil { return fmt.Errorf("%s", err) } unspentResults, err := wclient.ListUnspent() //minConf=1 if err != nil { return fmt.Errorf("cannot list unspent. %s", err) } anchorLog.Info("unspentResults.len=", len(unspentResults)) if len(unspentResults) > 0 { var i int for _, b := range unspentResults { if b.Amount > fee.ToBTC() { balances = append(balances, balance{unspentResult: b}) i++ } } } anchorLog.Info("balances.len=", len(balances)) for i, b := range balances { addr, err := btcutil.DecodeAddress(b.unspentResult.Address, &chaincfg.TestNet3Params) if err != nil { return fmt.Errorf("cannot decode address: %s", err) } balances[i].address = addr wif, err := wclient.DumpPrivKey(addr) if err != nil { return fmt.Errorf("cannot get WIF: %s", err) } balances[i].wif = wif anchorLog.Info("balance[%d]=%s", i, spew.Sdump(balances[i])) } time.Sleep(1 * time.Second) return nil }