lbrynet/lbrycrd wallet management #34

Merged
nikooo777 merged 4 commits from wallets-refactor into master 2018-08-20 14:09:06 +02:00
4 changed files with 31 additions and 7 deletions
Showing only changes of commit f434232cae - Show all commits

View file

@ -94,6 +94,7 @@ func ytSync(cmd *cobra.Command, args []string) {
apiToken := os.Getenv("LBRY_API_TOKEN")
youtubeAPIKey := os.Getenv("YOUTUBE_API_KEY")
blobsDir := os.Getenv("BLOBS_DIRECTORY")
lbrycrdString := os.Getenv("LBRYCRD_STRING")
if apiURL == "" {
log.Errorln("An API URL was not defined. Please set the environment variable LBRY_API")
return
@ -106,6 +107,9 @@ func ytSync(cmd *cobra.Command, args []string) {
log.Errorln("A Youtube API key was not defined. Please set the environment variable YOUTUBE_API_KEY")
return
}
if lbrycrdString == "" {
log.Infoln("Using default (local) lbrycrd instance. Set LBRYCRD_STRING if you want to use something else")
}
if blobsDir == "" {
usr, err := user.Current()
if err != nil {
@ -136,6 +140,7 @@ func ytSync(cmd *cobra.Command, args []string) {
BlobsDir: blobsDir,
VideosLimit: videosLimit,
MaxVideoSize: maxVideoSize,
LbrycrdString: lbrycrdString,
}
err := sm.Start()

View file

@ -39,6 +39,7 @@ type SyncManager struct {
BlobsDir string
VideosLimit int
MaxVideoSize int
LbrycrdString string
}
const (
@ -212,6 +213,7 @@ func (s SyncManager) Start() error {
TakeOverExistingChannel: s.TakeOverExistingChannel,
Refill: s.Refill,
Manager: &s,
LbrycrdString: s.LbrycrdString,
}
shouldInterruptLoop = true
} else {
@ -242,6 +244,7 @@ func (s SyncManager) Start() error {
TakeOverExistingChannel: s.TakeOverExistingChannel,
Refill: s.Refill,
Manager: &s,
LbrycrdString: s.LbrycrdString,
})
}
}

View file

@ -104,6 +104,7 @@ func (s *Sync) ensureEnoughUTXOs() error {
}
target := 40
slack := target - int(float32(0.05)*float32(target))
count := 0
for _, utxo := range *utxolist {
@ -112,7 +113,7 @@ func (s *Sync) ensureEnoughUTXOs() error {
}
}
if count < target {
if count < target-slack {
newAddresses := target - count
balance, err := s.daemon.WalletBalance()
@ -257,10 +258,19 @@ func allUTXOsConfirmed(utxolist *jsonrpc.UTXOListResponse) bool {
func (s *Sync) addCredits(amountToAdd float64) error {
log.Printf("Adding %f credits", amountToAdd)
lbrycrdd, err := lbrycrd.NewWithDefaultURL()
var lbrycrdd *lbrycrd.Client
var err error
if s.LbrycrdString == "" {
lbrycrdd, err = lbrycrd.NewWithDefaultURL()
if err != nil {
return err
}
} else {
lbrycrdd, err = lbrycrd.New(s.LbrycrdString)
if err != nil {
return err
}
}
addressResp, err := s.daemon.WalletUnusedAddress()
if err != nil {
@ -280,6 +290,4 @@ func (s *Sync) addCredits(amountToAdd float64) error {
time.Sleep(wait)
return nil
//log.Println("Waiting for transaction to be confirmed")
//return s.waitUntilUTXOsConfirmed()
}

View file

@ -60,6 +60,7 @@ type Sync struct {
TakeOverExistingChannel bool
Refill int
Manager *SyncManager
LbrycrdString string
daemon *jsonrpc.Client
claimAddress string
@ -169,6 +170,7 @@ func (s *Sync) FullCycle() (e error) {
defaultWalletDir = os.Getenv("HOME") + "/.lbryum_regtest/wallets/default_wallet"
}
walletBackupDir := os.Getenv("HOME") + "/wallets/" + strings.Replace(s.LbryChannelName, "@", "", 1)
newWalletBackupDir := os.Getenv("HOME") + "/renamed_wallets/" + s.YoutubeChannelID
if _, err := os.Stat(defaultWalletDir); !os.IsNotExist(err) {
return errors.Err("default_wallet already exists")
@ -180,6 +182,12 @@ func (s *Sync) FullCycle() (e error) {
return errors.Wrap(err, 0)
}
log.Println("Continuing previous upload")
} else if _, err = os.Stat(newWalletBackupDir); !os.IsNotExist(err) {
err = os.Rename(newWalletBackupDir, defaultWalletDir)
if err != nil {
return errors.Wrap(err, 0)
}
log.Println("Continuing previous upload")
}
defer func() {
@ -195,7 +203,7 @@ func (s *Sync) FullCycle() (e error) {
if processDeathError != nil {
logShutdownError(processDeathError)
} else {
walletErr := os.Rename(defaultWalletDir, walletBackupDir)
walletErr := os.Rename(defaultWalletDir, newWalletBackupDir)
if walletErr != nil {
log.Errorf("error moving wallet to backup dir: %v", walletErr)
}