Optimize token exchange rates in explorer

This commit is contained in:
Martin Boehm 2023-01-17 20:16:25 +01:00 committed by Martin
parent 0d9d09b755
commit 8b18c4ecac
2 changed files with 19 additions and 14 deletions

View File

@ -808,23 +808,28 @@ func (s *PublicServer) amountSatsSpan(a *api.Amount, td *TemplateData, classes s
func (s *PublicServer) tokenAmountSpan(t *api.TokenTransfer, td *TemplateData, classes string) template.HTML { func (s *PublicServer) tokenAmountSpan(t *api.TokenTransfer, td *TemplateData, classes string) template.HTML {
primary := formatAmountWithDecimals(t.Value, t.Decimals) primary := formatAmountWithDecimals(t.Value, t.Decimals)
var rv strings.Builder var rv strings.Builder
appendAmountWrapperSpan(&rv, primary, td.CoinShortcut, classes) appendAmountWrapperSpan(&rv, primary, t.Symbol, classes)
appendAmountSpan(&rv, "prim-amt", primary, t.Symbol, "") appendAmountSpan(&rv, "prim-amt", primary, t.Symbol, "")
if td.SecondaryCoin != "" { if td.SecondaryCoin != "" {
var currentBase, currentSecondary, txBase, txSecondary string var currentBase, currentSecondary, txBase, txSecondary string
p, err := strconv.ParseFloat(primary, 64) p, err := strconv.ParseFloat(primary, 64)
if err == nil { if err == nil {
baseRate, found := s.api.GetContractBaseRate(td.CurrentTicker, t.Contract, 0) if td.CurrentTicker != nil {
if found { // get rate from current ticker
base := p * baseRate baseRateCurrent, found := td.CurrentTicker.GetTokenRate(t.Contract)
currentBase = strconv.FormatFloat(base, 'f', 6, 64) if found {
currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td) base := p * float64(baseRateCurrent)
} currentBase = strconv.FormatFloat(base, 'f', 6, 64)
baseRate, found = s.api.GetContractBaseRate(td.TxTicker, t.Contract, td.Tx.Blocktime) currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td)
if found { // get the historical rate only if current rate exist
base := p * baseRate // it is very costly to search in DB in vain for a rate for token for which there are no exchange rates
txBase = strconv.FormatFloat(base, 'f', 6, 64) baseRate, found := s.api.GetContractBaseRate(td.TxTicker, t.Contract, td.Tx.Blocktime)
txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td) if found {
base := p * baseRate
txBase = strconv.FormatFloat(base, 'f', 6, 64)
txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td)
}
}
} }
} }
if txBase != "" { if txBase != "" {

File diff suppressed because one or more lines are too long