Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ jobs:
- name: npm run build
working-directory: ./client/webserver/site
run: npm run build

- name: npm clean-install
working-directory: ./client/webserver/newui
run: npm ci
- name: npm run lint
working-directory: ./client/webserver/newui
run: npm run lint
- name: npm run build
working-directory: ./client/webserver/newui
run: npm run build

lint-docs:
name: Lint Markdown
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dex/testing/loadbot/loadbot
bin/
bin-v*/
client/webserver/site/template-builder/template-builder
client/webserver/newui/locales/cmd/importlocales/importlocales
dex/testing/btc/harnesschain.tar.gz
client/asset/btc/electrum/example/server/server
client/asset/btc/electrum/example/wallet/wallet
Expand Down
44 changes: 28 additions & 16 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1896,13 +1896,13 @@ func (c *Core) Run(ctx context.Context) {

// Construct enabled fiat rate sources.
fetchers:
for token, rateFetcher := range fiatRateFetchers {
for src, f := range fiatRateFetchers {
for _, v := range disabledSources {
if token == v {
if src == v {
continue fetchers
}
}
c.fiatRateSources[token] = newCommonRateSource(rateFetcher)
c.fiatRateSources[src] = newCommonRateSource(src, f)
}
c.fetchFiatExchangeRates(ctx)

Expand Down Expand Up @@ -2712,12 +2712,14 @@ func (c *Core) assetMap() map[uint32]*SupportedAsset {
// User is a thread-safe getter for the User.
func (c *Core) User() *User {
m := c.coreMesh()
idRates, tickerRates := c.fiatConversions()
return &User{
Assets: c.assetMap(),
Exchanges: c.Exchanges(),
Initialized: c.IsInitialized(),
SeedGenerationTime: c.seedGenerationTime,
FiatRates: c.fiatConversions(),
FiatRates: idRates,
TickerRates: tickerRates,
Net: c.net,
ExtensionConfig: c.extensionModeConfig,
Actions: c.requestedActionsList(),
Expand Down Expand Up @@ -11925,9 +11927,9 @@ func (c *Core) refreshFiatRates(ctx context.Context) {
// Remove expired rate source if any.
c.removeExpiredRateSources()

fiatRatesMap := c.fiatConversions()
if len(fiatRatesMap) != 0 {
c.notify(newFiatRatesUpdate(fiatRatesMap))
idRates, tickerRates := c.fiatConversions()
if len(idRates) != 0 {
c.notify(newFiatRatesUpdate(idRates, tickerRates))
}
}

Expand All @@ -11946,12 +11948,13 @@ func (c *Core) FiatRateSources() map[string]bool {
// FiatConversionRates are the currently cached fiat conversion rates. Must have
// 1 or more fiat rate sources enabled.
func (c *Core) FiatConversionRates() map[uint32]float64 {
return c.fiatConversions()
idRates, _ := c.fiatConversions()
return idRates
}

// fiatConversions returns fiat rate for all supported assets that have a
// wallet.
func (c *Core) fiatConversions() map[uint32]float64 {
func (c *Core) fiatConversions() (map[uint32]float64, map[string]float64) {
assetIDs := make(map[uint32]struct{})
supportedAssets := asset.Assets()
for assetID, asset := range supportedAssets {
Expand All @@ -11962,6 +11965,7 @@ func (c *Core) fiatConversions() map[uint32]float64 {
}

fiatRatesMap := make(map[uint32]float64, len(supportedAssets))
tickerRatesMap := make(map[string]float64)
for assetID := range assetIDs {
var rateSum float64
var sources int
Expand All @@ -11980,19 +11984,22 @@ func (c *Core) fiatConversions() map[uint32]float64 {
}
}
if rateSum != 0 {
fiatRatesMap[assetID] = rateSum / float64(sources) // get average rate.
r := rateSum / float64(sources) // get average rate.
fiatRatesMap[assetID] = r
ui, _ := asset.UnitInfo(assetID)
tickerRatesMap[ui.Conventional.Unit] = r
}
}
return fiatRatesMap
return fiatRatesMap, tickerRatesMap
}

// ToggleRateSourceStatus toggles a fiat rate source status. If disable is true,
// the fiat rate source is disabled, otherwise the rate source is enabled.
func (c *Core) ToggleRateSourceStatus(source string, disable bool) error {
if disable {
return c.disableRateSource(source)
func (c *Core) ToggleRateSourceStatus(source string, enable bool) error {
if enable {
return c.enableRateSource(source)
}
return c.enableRateSource(source)
return c.disableRateSource(source)
}

// enableRateSource enables a fiat rate source.
Expand All @@ -12010,7 +12017,7 @@ func (c *Core) enableRateSource(source string) error {
}

// Build fiat rate source.
rateSource := newCommonRateSource(rateFetcher)
rateSource := newCommonRateSource(source, rateFetcher)
c.fiatRateSources[source] = rateSource

select {
Expand Down Expand Up @@ -12521,6 +12528,11 @@ func (c *Core) ExtensionModeConfig() *ExtensionModeConfig {
return c.extensionModeConfig
}

func (c *Core) ValidateSeed(seed string) (bool, error) {
_, _, err := decodeSeedString(seed)
return err == nil, nil
}

// calcParcelLimit computes the users score-scaled user parcel limit.
func calcParcelLimit(tier int64, score, maxScore int32) uint32 {
// Users limit starts at 2 parcels per tier.
Expand Down
67 changes: 3 additions & 64 deletions client/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10895,7 +10895,7 @@ func TestToggleRateSourceStatus(t *testing.T) {

// Test disabling fiat rate source.
for _, test := range tests {
err := tCore.ToggleRateSourceStatus(test.source, true)
err := tCore.ToggleRateSourceStatus(test.source, false)
if test.wantErr != (err != nil) {
t.Fatalf("%s: wantErr = %t, err = %v", test.name, test.wantErr, err)
}
Expand All @@ -10904,76 +10904,15 @@ func TestToggleRateSourceStatus(t *testing.T) {
// Test enabling fiat rate source.
for _, test := range tests {
if test.init {
tCore.fiatRateSources[test.source] = newCommonRateSource(tFetcher)
tCore.fiatRateSources[test.source] = newCommonRateSource(test.source, tFetcher)
}
err := tCore.ToggleRateSourceStatus(test.source, false)
err := tCore.ToggleRateSourceStatus(test.source, true)
if test.wantErr != (err != nil) {
t.Fatalf("%s: wantErr = %t, err = %v", test.name, test.wantErr, err)
}
}
}

func TestFiatRateSources(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason test was removed?

rig := newTestRig()
defer rig.shutdown()
tCore := rig.core
supportedFetchers := len(fiatRateFetchers)
rateSources := tCore.FiatRateSources()
if len(rateSources) != supportedFetchers {
t.Fatalf("Expected %d number of fiat rate source/fetchers", supportedFetchers)
}
}

func TestFiatConversions(t *testing.T) {
rig := newTestRig()
defer rig.shutdown()
tCore := rig.core

// No fiat rate source initialized
fiatRates := tCore.fiatConversions()
if len(fiatRates) != 0 {
t.Fatal("Unexpected asset rate values.")
}

// Initialize fiat rate sources.
for token := range fiatRateFetchers {
tCore.fiatRateSources[token] = newCommonRateSource(tFetcher)
}

// Fetch fiat rates.
tCore.wg.Add(1)
go func() {
defer tCore.wg.Done()
tCore.refreshFiatRates(tCtx)
}()
tCore.wg.Wait()

// Expects assets fiat rate values.
fiatRates = tCore.fiatConversions()
if len(fiatRates) != 2 {
t.Fatal("Expected assets fiat rate for two assets")
}

// fiat rates for assets can expire, and fiat rate fetchers can be
// removed if expired.
for token, source := range tCore.fiatRateSources {
source.fiatRates[tUTXOAssetA.ID].lastUpdate = time.Now().Add(-time.Minute)
source.fiatRates[tUTXOAssetB.ID].lastUpdate = time.Now().Add(-time.Minute)
if source.isExpired(55 * time.Second) {
delete(tCore.fiatRateSources, token)
}
}

fiatRates = tCore.fiatConversions()
if len(fiatRates) != 0 {
t.Fatal("Unexpected assets fiat rate values, expected to ignore expired fiat rates.")
}

if len(tCore.fiatRateSources) != 0 {
t.Fatal("Expected fiat conversion to be disabled, all rate source data has expired.")
}
}

func TestValidateAddress(t *testing.T) {
rig := newTestRig()
defer rig.shutdown()
Expand Down
4 changes: 2 additions & 2 deletions client/core/exchangeratefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (

// Tokens. Used to identify fiat rate source, source name must not contain a
// comma.
messari = "Messari"
coinpaprika = "Coinpaprika"
dcrdataDotOrg = "dcrdata"
)
Expand Down Expand Up @@ -64,6 +63,7 @@ type fiatRateInfo struct {
type rateFetcher func(context context.Context, logger dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64

type commonRateSource struct {
source string
fetchRates rateFetcher

mtx sync.RWMutex
Expand Down Expand Up @@ -114,7 +114,7 @@ func (source *commonRateSource) refreshRates(ctx context.Context, logger dex.Log
}

// Used to initialize a fiat rate source.
func newCommonRateSource(fetcher rateFetcher) *commonRateSource {
func newCommonRateSource(name string, fetcher rateFetcher) *commonRateSource {
return &commonRateSource{
fetchRates: fetcher,
fiatRates: make(map[uint32]*fiatRateInfo),
Expand Down
8 changes: 5 additions & 3 deletions client/core/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,17 @@ func newConnEventNote(topic Topic, subject, host string, status comms.Connection
// FiatRatesNote is an update of fiat rate data for assets.
type FiatRatesNote struct {
db.Notification
FiatRates map[uint32]float64 `json:"fiatRates"`
FiatRates map[uint32]float64 `json:"fiatRates"`
TickerRates map[string]float64 `json:"tickerRates"`
}

const TopicFiatRatesUpdate Topic = "fiatrateupdate"

func newFiatRatesUpdate(rates map[uint32]float64) *FiatRatesNote {
func newFiatRatesUpdate(idRates map[uint32]float64, tickerRates map[string]float64) *FiatRatesNote {
return &FiatRatesNote{
Notification: db.NewNotification(NoteTypeFiatRates, TopicFiatRatesUpdate, "", "", db.Data),
FiatRates: rates,
FiatRates: idRates,
TickerRates: tickerRates,
}
}

Expand Down
1 change: 1 addition & 0 deletions client/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ type User struct {
SeedGenerationTime uint64 `json:"seedgentime"`
Assets map[uint32]*SupportedAsset `json:"assets"`
FiatRates map[uint32]float64 `json:"fiatRates"`
TickerRates map[string]float64 `json:"tickerRates"`
Net dex.Network `json:"net"`
ExtensionConfig *ExtensionModeConfig `json:"extensionModeConfig,omitempty"`
Actions []*asset.ActionRequiredNote `json:"actions,omitempty"`
Expand Down
Loading
Loading