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,25 +808,30 @@ 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 {
// get rate from current ticker
baseRateCurrent, found := td.CurrentTicker.GetTokenRate(t.Contract)
if found { if found {
base := p * baseRate base := p * float64(baseRateCurrent)
currentBase = strconv.FormatFloat(base, 'f', 6, 64) currentBase = strconv.FormatFloat(base, 'f', 6, 64)
currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td) currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td)
} // get the historical rate only if current rate exist
baseRate, found = s.api.GetContractBaseRate(td.TxTicker, t.Contract, td.Tx.Blocktime) // it is very costly to search in DB in vain for a rate for token for which there are no exchange rates
baseRate, found := s.api.GetContractBaseRate(td.TxTicker, t.Contract, td.Tx.Blocktime)
if found { if found {
base := p * baseRate base := p * baseRate
txBase = strconv.FormatFloat(base, 'f', 6, 64) txBase = strconv.FormatFloat(base, 'f', 6, 64)
txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td) txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td)
} }
} }
}
}
if txBase != "" { if txBase != "" {
appendAmountSpan(&rv, "base-amt", txBase, td.CoinShortcut, td.TxDate) appendAmountSpan(&rv, "base-amt", txBase, td.CoinShortcut, td.TxDate)
if currentBase != "" { if currentBase != "" {

File diff suppressed because one or more lines are too long