From 8b18c4ecaceed6a16c27f6bc7474591b78868ca8 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 17 Jan 2023 20:16:25 +0100 Subject: [PATCH] Optimize token exchange rates in explorer --- server/public.go | 29 +++++++++++++++++------------ server/public_ethereumtype_test.go | 4 ++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/server/public.go b/server/public.go index 9b704f03..36a3bc37 100644 --- a/server/public.go +++ b/server/public.go @@ -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 { primary := formatAmountWithDecimals(t.Value, t.Decimals) var rv strings.Builder - appendAmountWrapperSpan(&rv, primary, td.CoinShortcut, classes) + appendAmountWrapperSpan(&rv, primary, t.Symbol, classes) appendAmountSpan(&rv, "prim-amt", primary, t.Symbol, "") if td.SecondaryCoin != "" { var currentBase, currentSecondary, txBase, txSecondary string p, err := strconv.ParseFloat(primary, 64) if err == nil { - baseRate, found := s.api.GetContractBaseRate(td.CurrentTicker, t.Contract, 0) - if found { - base := p * baseRate - currentBase = strconv.FormatFloat(base, 'f', 6, 64) - currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td) - } - baseRate, found = s.api.GetContractBaseRate(td.TxTicker, t.Contract, td.Tx.Blocktime) - if found { - base := p * baseRate - txBase = strconv.FormatFloat(base, 'f', 6, 64) - txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td) + if td.CurrentTicker != nil { + // get rate from current ticker + baseRateCurrent, found := td.CurrentTicker.GetTokenRate(t.Contract) + if found { + base := p * float64(baseRateCurrent) + currentBase = strconv.FormatFloat(base, 'f', 6, 64) + currentSecondary = formatSecondaryAmount(base*td.CurrentSecondaryCoinRate, td) + // get the historical rate only if current rate exist + // 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 { + base := p * baseRate + txBase = strconv.FormatFloat(base, 'f', 6, 64) + txSecondary = formatSecondaryAmount(base*td.TxSecondaryCoinRate, td) + } + } } } if txBase != "" { diff --git a/server/public_ethereumtype_test.go b/server/public_ethereumtype_test.go index 6d87cf86..5c372b67 100644 --- a/server/public_ethereumtype_test.go +++ b/server/public_ethereumtype_test.go @@ -24,7 +24,7 @@ func httpTestsEthereumType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "text/html; charset=utf-8", body: []string{ - `Trezor Fake Coin Explorer

Address address7b.eth

0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b

0.000000000123450123 FAKE

Confirmed
Balance0.000000000123450123 FAKE
Transactions2
Non-contract Transactions0
Internal Transactions0
Nonce123
ContractQuantityValueTransfers#
Contract 130.000000001000123013 S131
Contract 740.001000123074 S741
ContractTokensTransfers#
Contract 20511

Transactions

ERC721 Token Transfers
`, + `Trezor Fake Coin Explorer

Address address7b.eth

0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b

0.000000000123450123 FAKE

Confirmed
Balance0.000000000123450123 FAKE
Transactions2
Non-contract Transactions0
Internal Transactions0
Nonce123
ContractQuantityValueTransfers#
Contract 130.000000001000123013 S131
Contract 740.001000123074 S741
ContractTokensTransfers#
Contract 20511

Transactions

ERC721 Token Transfers
`, }, }, { @@ -42,7 +42,7 @@ func httpTestsEthereumType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "text/html; charset=utf-8", body: []string{ - `Trezor Fake Coin Explorer

Transaction

0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101
In BlockUnconfirmed
StatusSuccess
Value0 FAKE
Gas Used / Limit52025 / 78037
Gas Price0.00000004 FAKE (40 Gwei)
Fees0.002081 FAKE
RBFON
ERC20 Token Transfers
Input Data

0xa9059cbb000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f00000000000000000000000000000000000000000000021e19e0c9bab2400000
transfer(address, uint256)
#TypeData
0address0x555Ee11FBDDc0E49A9bAB358A8941AD95fFDB48f
1uint25610000000000000000000000
Raw Transaction
`, + `Trezor Fake Coin Explorer

Transaction

0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101
In BlockUnconfirmed
StatusSuccess
Value0 FAKE
Gas Used / Limit52025 / 78037
Gas Price0.00000004 FAKE (40 Gwei)
Fees0.002081 FAKE
RBFON
ERC20 Token Transfers
Input Data

0xa9059cbb000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f00000000000000000000000000000000000000000000021e19e0c9bab2400000
transfer(address, uint256)
#TypeData
0address0x555Ee11FBDDc0E49A9bAB358A8941AD95fFDB48f
1uint25610000000000000000000000
Raw Transaction
`, }, }, { name: "explorerTokenDetail " + dbtestdata.EthAddr7b,