Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
  • Loading branch information
ukane-philemon committed Dec 12, 2023
1 parent 0008b30 commit 4db3d57
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 175 deletions.
39 changes: 39 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"path/filepath"
"sort"
"strconv"
"sync"
"time"

"decred.org/dcrwallet/v3/errors"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
"github.com/crypto-power/cryptopower/dexc"
"github.com/crypto-power/cryptopower/libwallet/ext"
"github.com/crypto-power/cryptopower/libwallet/instantswap"
"github.com/crypto-power/cryptopower/libwallet/internal/politeia"
Expand Down Expand Up @@ -61,6 +63,9 @@ type AssetsManager struct {
InstantSwap *instantswap.InstantSwap
ExternalService *ext.Service
RateSource ext.RateSource

dexcMtx sync.RWMutex
dexc *dexc.DEXClient
}

// initializeAssetsFields validate the network provided is valid for all assets before proceeding
Expand Down Expand Up @@ -882,3 +887,37 @@ func (mgr *AssetsManager) CalculateAssetsUSDBalance(balances map[utils.AssetType

return assetsTotalUSDBalance, nil
}

// DexClient returns a dexc client that MUST never be modified.
func (mgr *AssetsManager) DexClient() *dexc.DEXClient {
mgr.dexcMtx.RLock()
defer mgr.dexcMtx.RUnlock()
return mgr.dexc
}

func (mgr *AssetsManager) DexcReady() bool {
mgr.dexcMtx.RLock()
defer mgr.dexcMtx.RUnlock()
return mgr.dexc != nil
}

// InitializeDEX initializes mgr.dexc.
func (mgr *AssetsManager) InitializeDEX(ctx context.Context) {
logDir := filepath.Dir(mgr.LogFile())
dexc, err := dexc.Start(ctx, mgr.RootDir(), mgr.GetLanguagePreference(), logDir, mgr.GetLogLevels(), mgr.NetType(), 0 /* TODO: Make configurable */)
if err != nil {
log.Errorf("Error starting dex client: %v", err)
return
}

mgr.dexcMtx.Lock()
mgr.dexc = dexc
mgr.dexcMtx.Unlock()

go func() {
<-mgr.dexc.WaitForShutdown()
mgr.dexcMtx.Lock()
mgr.dexc = nil
mgr.dexcMtx.Unlock()
}()
}
16 changes: 5 additions & 11 deletions ui/page/dcrdex/dcrdex_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"gioui.org/layout"
"gioui.org/widget"
"github.com/crypto-power/cryptopower/app"
"github.com/crypto-power/cryptopower/dexc"
"github.com/crypto-power/cryptopower/libwallet/utils"
libutils "github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
Expand All @@ -26,18 +25,15 @@ type DEXPage struct {

*load.Load

// Might be nil but interaction with this page will be disabled if it is.
dexc *dexc.DEXClient
generalSettingsBtn cryptomaterial.Button
openTradeMainPage *cryptomaterial.Clickable
splashPageInfoButton cryptomaterial.IconButton
splashPageContainer *widget.List
startTradingBtn cryptomaterial.Button
isDexFirstVisit bool
inited bool
}

func NewDEXPage(l *load.Load, dexc *dexc.DEXClient) *DEXPage {
func NewDEXPage(l *load.Load) *DEXPage {
dp := &DEXPage{
MasterPage: app.NewMasterPage(DCRDEXPageID),
Load: l,
Expand All @@ -48,13 +44,11 @@ func NewDEXPage(l *load.Load, dexc *dexc.DEXClient) *DEXPage {
Axis: layout.Vertical,
}},
isDexFirstVisit: true,
dexc: dexc,
generalSettingsBtn: l.Theme.Button(values.StringF(values.StrEnableAPI, values.String(values.StrExchange))),
}

// Init splash page more info widget.
_, dp.splashPageInfoButton = components.SubpageHeaderButtons(l)
dp.inited = dexc != nil && len(dexc.Exchanges()) > 0
return dp
}

Expand All @@ -70,16 +64,16 @@ func (pg *DEXPage) ID() string {
// displayed.
// Part of the load.Page interface.
func (pg *DEXPage) OnNavigatedTo() {
if pg.dexc == nil {
if !pg.AssetsManager.DexcReady() {
return
}

if pg.CurrentPage() != nil {
pg.CurrentPage().OnNavigatedTo()
} else if pg.inited {
} else if len(pg.AssetsManager.DexClient().Exchanges()) > 0 {
pg.Display(NewDEXMarketPage(pg.Load))
} else {
pg.Display(NewDEXOnboarding(pg.Load, pg.dexc))
pg.Display(NewDEXOnboarding(pg.Load))
}
}

Expand All @@ -102,7 +96,7 @@ func (pg *DEXPage) Layout(gtx C) D {
msg = values.StringF(values.StrNotAllowed, values.String(values.StrExchange))
} else if !hasMultipleWallets {
msg = values.String(values.StrMultipleAssetRequiredMsg)
} else if pg.dexc == nil {
} else if pg.AssetsManager.DexClient() == nil {
msg = values.String(values.StrDEXInitErrorMsg)
}

Expand Down
2 changes: 1 addition & 1 deletion ui/page/dcrdex/dex_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"decred.org/dcrdex/client/core"
)

type clientCore interface {
type dexClient interface {
Ready() <-chan struct{}
WaitForShutdown() <-chan struct{}
IsDEXPasswordSet() bool
Expand Down

0 comments on commit 4db3d57

Please sign in to comment.