-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
multi: add support for DCR-USDT pair on /markets
view
#2002
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -784,21 +784,28 @@ func (exp *explorerUI) watchExchanges() { | |
} | ||
xcChans := exp.xcBot.UpdateChannels() | ||
|
||
sendXcUpdate := func(isFiat bool, token string, updater *exchanges.ExchangeState) { | ||
sendXcUpdate := func(isFiat bool, token, pair string, updater *exchanges.ExchangeState) { | ||
xcState := exp.xcBot.State() | ||
update := &WebsocketExchangeUpdate{ | ||
Updater: WebsocketMiniExchange{ | ||
Token: token, | ||
Price: updater.Price, | ||
Volume: updater.Volume, | ||
Change: updater.Change, | ||
Token: token, | ||
CurrencyPair: pair, | ||
Price: updater.Price, | ||
Volume: updater.Volume, | ||
Change: updater.Change, | ||
}, | ||
IsFiatIndex: isFiat, | ||
BtcIndex: exp.xcBot.BtcIndex, | ||
Index: exp.xcBot.Index, | ||
Price: xcState.Price, | ||
BtcPrice: xcState.BtcPrice, | ||
Volume: xcState.Volume, | ||
Indices: make(map[string]float64), | ||
} | ||
|
||
// Other DCR pairs should also provide an index price for the quote | ||
// asset. | ||
update.Indices[exchanges.BTCIndex.String()] = xcState.BtcPrice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a personal preference thing, but I'd rather cases like this just cast it directly to string since it makes the performance implications much more clear. As a case in point, I had to go look up what the On the other hand I understand the thought process is likely to make it so the type could theoretically be changed and the |
||
update.Indices[exchanges.USDTIndex.String()] = indexPrice(exchanges.USDTIndex, xcState.FiatIndices) | ||
|
||
select { | ||
case exp.wsHub.xcChan <- update: | ||
default: | ||
|
@@ -809,14 +816,22 @@ func (exp *explorerUI) watchExchanges() { | |
for { | ||
select { | ||
case update := <-xcChans.Exchange: | ||
sendXcUpdate(false, update.Token, update.State) | ||
sendXcUpdate(false, update.Token, update.CurrencyPair.String(), update.State) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same type of thing here regarding the I won't comment on all the rest of the cases, but the same holds for them too. |
||
case update := <-xcChans.Index: | ||
indexState, found := exp.xcBot.State().FiatIndices[update.Token] | ||
currencyIndices, found := exp.xcBot.State().FiatIndices[update.Token] | ||
if !found { | ||
log.Errorf("Index state not found when preparing websocket update") | ||
log.Error("Index state not found when preparing websocket update") | ||
continue | ||
} | ||
sendXcUpdate(true, update.Token, indexState) | ||
|
||
indexState, found := currencyIndices[update.CurrencyPair] | ||
if !found { | ||
log.Errorf("Index state not found for %s when preparing websocket update", update.CurrencyPair) | ||
continue | ||
} | ||
|
||
sendXcUpdate(true, update.Token, update.CurrencyPair.String(), indexState) | ||
|
||
case <-xcChans.Quit: | ||
log.Warnf("ExchangeBot has quit.") | ||
return | ||
|
@@ -844,3 +859,20 @@ func (exp *explorerUI) mempoolTime(txid string) types.TimeDef { | |
} | ||
return types.NewTimeDefFromUNIX(tx.Time) | ||
} | ||
|
||
// indexPrice is calculates the aggregate index price across all exchanges. | ||
func indexPrice(index exchanges.CurrencyPair, indices map[string]map[exchanges.CurrencyPair]*exchanges.ExchangeState) float64 { | ||
var price, nSources float64 | ||
for _, currecncyIndices := range indices { | ||
for pair, state := range currecncyIndices { | ||
if pair == index { | ||
price += state.Price | ||
nSources++ | ||
} | ||
} | ||
} | ||
if price == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really matter since price will be zero when there are no sources due to above logic, but this really should be checking |
||
return 0 | ||
} | ||
return price / nSources | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like
error
shold be consistently namederr
like the rest.