Skip to content

Commit

Permalink
multi: retire load.WalletLoad (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain authored Dec 1, 2023
1 parent 6c223dc commit 9aa7f67
Show file tree
Hide file tree
Showing 81 changed files with 1,118 additions and 1,476 deletions.
16 changes: 16 additions & 0 deletions libwallet/assets/wallet/wallet_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,19 @@ func backupFile(fileName string, suffix int) (newName string, err error) {

return newName, nil
}

// Balances returns the spendable balance and total balance of the wallet.
func Balances(w Asset) (AssetAmount, AssetAmount, error) {
accountsResult, err := w.GetAccountsRaw()
if err != nil {
return w.ToAmount(0), w.ToAmount(0), err
}

var totalSpendable, totalBalance int64
for _, account := range accountsResult.Accounts {
totalSpendable += account.Balance.Spendable.ToInt()
totalBalance += account.Balance.Total.ToInt()
}

return w.ToAmount(totalSpendable), w.ToAmount(totalBalance), nil
}
10 changes: 10 additions & 0 deletions libwallet/assets_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func (mgr *AssetsManager) SetCurrencyConversionExchange(xc string) {
}()
}

// ExchangeRateFetchingEnabled returns true if privacy mode isn't turned on and
// a valid exchange rate source is configured.
func (mgr *AssetsManager) ExchangeRateFetchingEnabled() bool {
if mgr.IsPrivacyModeOn() {
return false
}
xc := mgr.GetCurrencyConversionExchange()
return xc != "" && xc != values.DefaultExchangeValue
}

// GetLanguagePreference returns the language preference.
func (mgr *AssetsManager) GetLanguagePreference() string {
var lang string
Expand Down
103 changes: 103 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"time"

Expand Down Expand Up @@ -490,6 +491,15 @@ func (mgr *AssetsManager) sortWallets(assetType utils.AssetType) []sharedW.Asset
normalWallets = append(normalWallets, wallet)
}
}

// Sort both lists by wallet ID.
sort.Slice(normalWallets, func(i, j int) bool {
return normalWallets[i].GetWalletID() < normalWallets[j].GetWalletID()
})
sort.Slice(watchOnlyWallets, func(i, j int) bool {
return watchOnlyWallets[i].GetWalletID() < watchOnlyWallets[j].GetWalletID()
})

return append(normalWallets, watchOnlyWallets...)
}

Expand Down Expand Up @@ -780,3 +790,96 @@ func (mgr *AssetsManager) BlockExplorerURLForTx(assetType utils.AssetType, txHas
func (mgr *AssetsManager) LogFile() string {
return filepath.Join(mgr.params.LogDir, LogFilename)
}

func (mgr *AssetsManager) DCRHDPrefix() string {
switch mgr.NetType() {
case utils.Testnet:
return dcr.TestnetHDPath
case utils.Mainnet:
return dcr.MainnetHDPath
default:
return ""
}
}

func (mgr *AssetsManager) BTCHDPrefix() string {
switch mgr.NetType() {
case utils.Testnet:
return btc.TestnetHDPath
case utils.Mainnet:
return btc.MainnetHDPath
default:
return ""
}
}

// LTC HDPrefix returns the HD path prefix for the Litecoin wallet network.
func (mgr *AssetsManager) LTCHDPrefix() string {
switch mgr.NetType() {
case utils.Testnet:
return ltc.TestnetHDPath
case utils.Mainnet:
return ltc.MainnetHDPath
default:
return ""
}
}

func (mgr *AssetsManager) CalculateTotalAssetsBalance() (map[utils.AssetType]sharedW.AssetAmount, error) {
assetsTotalBalance := make(map[utils.AssetType]sharedW.AssetAmount)

wallets := mgr.AllWallets()
for _, wal := range wallets {
if wal.IsWatchingOnlyWallet() {
continue
}

accountsResult, err := wal.GetAccountsRaw()
if err != nil {
return nil, err
}

assetType := wal.GetAssetType()
for _, account := range accountsResult.Accounts {
assetTotal, ok := assetsTotalBalance[assetType]
if ok {
assetTotal = wal.ToAmount(assetTotal.ToInt() + account.Balance.Total.ToInt())
} else {
assetTotal = account.Balance.Total
}
assetsTotalBalance[assetType] = assetTotal
}
}

return assetsTotalBalance, nil
}

func (mgr *AssetsManager) CalculateAssetsUSDBalance(balances map[utils.AssetType]sharedW.AssetAmount) (map[utils.AssetType]float64, error) {
if !mgr.ExchangeRateFetchingEnabled() {
return nil, fmt.Errorf("USD exchange rate is disabled")
}

usdBalance := func(bal sharedW.AssetAmount, market string) (float64, error) {
rate := mgr.RateSource.GetTicker(market)
if rate == nil || rate.LastTradePrice <= 0 {
return 0, fmt.Errorf("No rate information available")
}

return bal.MulF64(rate.LastTradePrice).ToCoin(), nil
}

assetsTotalUSDBalance := make(map[utils.AssetType]float64)
for assetType, balance := range balances {
marketValue, exist := values.AssetExchangeMarketValue[assetType]
if !exist {
return nil, fmt.Errorf("Unsupported asset type: %s", assetType)
}
usdBal, err := usdBalance(balance, marketValue)
if err != nil {
return nil, err
}
assetsTotalUSDBalance[assetType] = usdBal
}

return assetsTotalUSDBalance, nil
}
12 changes: 8 additions & 4 deletions ui/load/appinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"gioui.org/unit"
"github.com/crypto-power/cryptopower/libwallet"
"github.com/crypto-power/cryptopower/ui/values"
)

Expand All @@ -17,14 +18,17 @@ type AppInfo struct {

currentAppWidth unit.Dp
isMobileView bool

AssetsManager *libwallet.AssetsManager
}

// StartApp returns an instance of AppInfo with the startUpTime set to the current time.
func StartApp(version string, buildDate time.Time) *AppInfo {
func StartApp(version string, buildDate time.Time, assetsManager *libwallet.AssetsManager) *AppInfo {
return &AppInfo{
version: version,
buildDate: buildDate,
startUpTime: time.Now(),
version: version,
buildDate: buildDate,
startUpTime: time.Now(),
AssetsManager: assetsManager,
}
}

Expand Down
8 changes: 5 additions & 3 deletions ui/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"golang.org/x/text/message"

"github.com/crypto-power/cryptopower/app"
sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet"
"github.com/crypto-power/cryptopower/ui/assets"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/notification"
Expand All @@ -29,7 +30,6 @@ type Load struct {

Theme *cryptomaterial.Theme

WL *WalletLoad
Printer *message.Printer
Network string

Expand All @@ -38,11 +38,13 @@ type Load struct {
DarkModeSettingChanged func(bool)
LanguageSettingChanged func()
CurrencySettingChanged func()
ToggleSync func(NeedUnlockRestore)

// TODO: Kill this property!
ToggleSync func(sharedW.Asset, NeedUnlockRestore)
}

func (l *Load) RefreshTheme(window app.WindowNavigator) {
isDarkModeOn := l.WL.AssetsManager.IsDarkModeOn()
isDarkModeOn := l.AssetsManager.IsDarkModeOn()
l.Theme.SwitchDarkMode(isDarkModeOn, assets.DecredIcons)
l.DarkModeSettingChanged(isDarkModeOn)
l.LanguageSettingChanged()
Expand Down
Loading

0 comments on commit 9aa7f67

Please sign in to comment.