Skip to content

Commit cfe596e

Browse files
dlipicarsaledjenic
authored andcommitted
fix(wallet)_: fixed pegged token currency format
1 parent 115c07b commit cfe596e

File tree

6 files changed

+125
-63
lines changed

6 files changed

+125
-63
lines changed

services/wallet/currency/currency.go

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package currency
22

33
import (
4-
"errors"
54
"math"
65
"strings"
76

7+
"go.uber.org/zap"
8+
89
iso4217 "github.com/ladydascalie/currency"
910

1011
"github.com/status-im/status-go/services/wallet/market"
11-
"github.com/status-im/status-go/services/wallet/token"
12+
tokentypes "github.com/status-im/status-go/services/wallet/token/types"
1213
)
1314

1415
const decimalsCalculationCurrency = "USD"
@@ -26,11 +27,13 @@ type FormatPerSymbol = map[string]Format
2627

2728
type Currency struct {
2829
marketManager *market.Manager
30+
logger *zap.Logger
2931
}
3032

31-
func NewCurrency(marketManager *market.Manager) *Currency {
33+
func NewCurrency(marketManager *market.Manager, logger *zap.Logger) *Currency {
3234
return &Currency{
3335
marketManager: marketManager,
36+
logger: logger,
3437
}
3538
}
3639

@@ -87,17 +90,6 @@ func calculateTokenDisplayDecimals(price float64) uint {
8790
}
8891

8992
func (cm *Currency) calculateTokenCurrencyFormat(symbol string, price float64) (*Format, error) {
90-
pegSymbol := token.GetTokenPegSymbol(symbol)
91-
92-
if pegSymbol != "" {
93-
var currencyFormat, err = calculateFiatCurrencyFormat(pegSymbol)
94-
if err != nil {
95-
return nil, err
96-
}
97-
currencyFormat.Symbol = symbol
98-
return currencyFormat, nil
99-
}
100-
10193
currencyFormat := &Format{
10294
Symbol: symbol,
10395
DisplayDecimals: calculateTokenDisplayDecimals(price),
@@ -122,29 +114,57 @@ func GetFiatCurrencyFormats(symbols []string) (FormatPerSymbol, error) {
122114
return formats, nil
123115
}
124116

125-
func (cm *Currency) FetchTokenCurrencyFormats(symbols []string) (FormatPerSymbol, error) {
117+
func (cm *Currency) FetchTokenCurrencyFormats(tokens []*tokentypes.Token) (FormatPerSymbol, error) {
126118
formats := make(FormatPerSymbol)
127119

128-
// Get latest cached price, fetch only if not available
129-
prices, err := cm.marketManager.GetOrFetchPrices(symbols, []string{decimalsCalculationCurrency}, math.MaxInt64)
130-
if err != nil {
131-
return nil, err
132-
}
120+
peggedTokens := make(map[string]*tokentypes.Token, 0)
121+
nonPeggedTokens := make(map[string]*tokentypes.Token, 0)
133122

134-
for _, symbol := range symbols {
135-
priceData, ok := prices[symbol][decimalsCalculationCurrency]
123+
for _, token := range tokens {
124+
if token.PegSymbol != "" {
125+
peggedTokens[token.Symbol] = token
126+
} else {
127+
nonPeggedTokens[token.Symbol] = token
128+
}
129+
}
136130

137-
if !ok {
138-
return nil, errors.New("Could not get price for: " + symbol)
131+
for _, token := range peggedTokens {
132+
var currencyFormat, err = calculateFiatCurrencyFormat(token.PegSymbol)
133+
if err != nil {
134+
cm.logger.Error("Failed to calculate fiat currency format for pegged token", zap.Error(err))
135+
continue
139136
}
137+
currencyFormat.Symbol = token.Symbol
138+
formats[token.Symbol] = *currencyFormat
139+
}
140140

141-
format, err := cm.calculateTokenCurrencyFormat(symbol, priceData.Price)
141+
if len(nonPeggedTokens) > 0 {
142+
symbols := make([]string, 0, len(nonPeggedTokens))
143+
for symbol := range nonPeggedTokens {
144+
symbols = append(symbols, symbol)
145+
}
142146

147+
// Get latest cached price, fetch only if not available
148+
prices, err := cm.marketManager.GetOrFetchPrices(symbols, []string{decimalsCalculationCurrency}, math.MaxInt64)
143149
if err != nil {
144150
return nil, err
145151
}
146152

147-
formats[symbol] = *format
153+
for _, symbol := range symbols {
154+
priceData, ok := prices[symbol][decimalsCalculationCurrency]
155+
156+
if !ok {
157+
cm.logger.Error("Could not get price for: " + symbol)
158+
continue
159+
}
160+
161+
format, err := cm.calculateTokenCurrencyFormat(symbol, priceData.Price)
162+
if err != nil {
163+
return nil, err
164+
}
165+
166+
formats[symbol] = *format
167+
}
148168
}
149169

150170
return formats, nil

services/wallet/currency/service.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import (
55
"database/sql"
66
"time"
77

8+
"go.uber.org/zap"
9+
810
"github.com/ethereum/go-ethereum/event"
911
gocommon "github.com/status-im/status-go/common"
12+
"github.com/status-im/status-go/logutils"
1013
"github.com/status-im/status-go/services/wallet/market"
1114
"github.com/status-im/status-go/services/wallet/token"
15+
tokentypes "github.com/status-im/status-go/services/wallet/token/types"
1216
"github.com/status-im/status-go/services/wallet/walletevent"
1317
)
1418

@@ -24,25 +28,33 @@ type Service struct {
2428

2529
tokenManager *token.Manager
2630
walletFeed *event.Feed
31+
32+
logger *zap.Logger
2733
}
2834

2935
func NewService(db *sql.DB, walletFeed *event.Feed, tokenManager *token.Manager, marketManager *market.Manager) *Service {
36+
logger := logutils.ZapLogger().Named("Currency")
3037
return &Service{
31-
currency: NewCurrency(marketManager),
38+
currency: NewCurrency(marketManager, logger),
3239
db: NewCurrencyDB(db),
3340
tokenManager: tokenManager,
3441
walletFeed: walletFeed,
42+
logger: logger,
3543
}
3644
}
3745

3846
func (s *Service) Start(ctx context.Context) {
3947
// Update all fiat currency formats in cache
4048
fiatFormats, err := s.getAllFiatCurrencyFormats()
41-
4249
if err == nil {
4350
_ = s.db.UpdateCachedFormats(fiatFormats)
4451
}
4552

53+
fixedTokenFormats, err := s.getAllFixedTokenCurrencyFormats()
54+
if err == nil {
55+
_ = s.db.UpdateCachedFormats(fixedTokenFormats)
56+
}
57+
4658
go func() {
4759
defer gocommon.LogOnPanic()
4860
ticker := time.NewTicker(currencyFormatUpdateInterval)
@@ -69,12 +81,14 @@ func (s *Service) FetchAllCurrencyFormats() (FormatPerSymbol, error) {
6981
tokenFormats, err := s.fetchAllTokenCurrencyFormats()
7082

7183
if err != nil {
84+
s.logger.Error("Failed to fetch all token currency formats", zap.Error(err))
7285
return nil, err
7386
}
7487

7588
err = s.db.UpdateCachedFormats(tokenFormats)
7689

7790
if err != nil {
91+
s.logger.Error("Failed to update cached currency formats", zap.Error(err))
7892
return nil, err
7993
}
8094

@@ -85,31 +99,44 @@ func (s *Service) getAllFiatCurrencyFormats() (FormatPerSymbol, error) {
8599
return GetFiatCurrencyFormats(GetAllFiatCurrencySymbols())
86100
}
87101

88-
func (s *Service) fetchAllTokenCurrencyFormats() (FormatPerSymbol, error) {
102+
func (s *Service) getAllFixedTokenCurrencyFormats() (FormatPerSymbol, error) {
89103
tokens, err := s.tokenManager.GetAllTokens()
90104
if err != nil {
91105
return nil, err
92106
}
93107

94-
tokenPerSymbolMap := make(map[string]bool)
95-
tokenSymbols := make([]string, 0)
96-
for _, t := range tokens {
97-
symbol := t.Symbol
98-
if !tokenPerSymbolMap[symbol] {
99-
tokenPerSymbolMap[symbol] = true
100-
tokenSymbols = append(tokenSymbols, symbol)
108+
peggedTokens := make([]*tokentypes.Token, 0, len(tokens))
109+
for _, token := range tokens {
110+
if token.PegSymbol != "" {
111+
peggedTokens = append(peggedTokens, token)
101112
}
102113
}
103114

104-
tokenFormats, err := s.currency.FetchTokenCurrencyFormats(tokenSymbols)
115+
tokenFormats, err := s.currency.FetchTokenCurrencyFormats(peggedTokens)
105116
if err != nil {
106117
return nil, err
107118
}
108-
gweiSymbol := "Gwei"
119+
120+
const gweiSymbol = "Gwei"
109121
tokenFormats[gweiSymbol] = Format{
110122
Symbol: gweiSymbol,
111123
DisplayDecimals: 9,
112124
StripTrailingZeroes: true,
113125
}
126+
127+
return tokenFormats, nil
128+
}
129+
130+
func (s *Service) fetchAllTokenCurrencyFormats() (FormatPerSymbol, error) {
131+
tokens, err := s.tokenManager.GetAllTokens()
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
tokenFormats, err := s.currency.FetchTokenCurrencyFormats(tokens)
137+
if err != nil {
138+
return nil, err
139+
}
140+
114141
return tokenFormats, err
115142
}

services/wallet/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (r *Reader) balancesToTokensByAddress(connectedPerChain map[uint64]bool, ad
415415
Name: tokens[0].Name,
416416
Symbol: symbol,
417417
Decimals: tokens[0].Decimals,
418-
PegSymbol: token.GetTokenPegSymbol(symbol),
418+
PegSymbol: tokenTypes.GetTokenPegSymbol(symbol),
419419
Verified: tokens[0].Verified,
420420
CommunityData: tokens[0].CommunityData,
421421
Image: tokens[0].Image,

services/wallet/token/token-lists/builder.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/status-im/status-go/multiaccounts/settings"
1212
defaulttokenlists "github.com/status-im/status-go/services/wallet/token/token-lists/default-lists"
1313
"github.com/status-im/status-go/services/wallet/token/token-lists/fetcher"
14+
tokenTypes "github.com/status-im/status-go/services/wallet/token/types"
1415
)
1516

1617
func (t *TokenLists) rebuildTokensMap(fetchedLists []fetcher.FetchedTokenList) error {
@@ -27,6 +28,8 @@ func (t *TokenLists) rebuildTokensMap(fetchedLists []fetcher.FetchedTokenList) e
2728
list.Source = fetchedTokenList.SourceURL
2829
list.FetchedTimestamp = fetchedTokenList.Fetched.Format(time.RFC3339)
2930

31+
processTokenPegs(list.Tokens)
32+
3033
t.tokensListsMu.Lock()
3134
t.tokensLists[fetchedTokenList.ID] = &list
3235
t.tokensListsMu.Unlock()
@@ -96,3 +99,9 @@ func (t *TokenLists) rebuildTokensListsMap() error {
9699

97100
return t.rebuildTokensMap(tokensListsForProcessing)
98101
}
102+
103+
func processTokenPegs(tokens []*tokenTypes.Token) {
104+
for _, token := range tokens {
105+
token.PegSymbol = tokenTypes.GetTokenPegSymbol(token.Symbol)
106+
}
107+
}

services/wallet/token/token_peg.go

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package tokentypes
2+
3+
func getTokenPegMap() map[string]string {
4+
return map[string]string{
5+
"aUSDC": "USD",
6+
"DAI": "USD",
7+
"EURC": "EUR",
8+
"SAI": "USD",
9+
"sUSD": "USD",
10+
"PAXG": "XAU",
11+
"TCAD": "CAD",
12+
"TUSD": "USD",
13+
"TGBP": "GBP",
14+
"TAUD": "AUD",
15+
"USDC": "USD",
16+
"USDD": "USD",
17+
"USDS": "USD",
18+
"USDT": "USD",
19+
"USDT(6)": "USD",
20+
"USDT(18)": "USD",
21+
"USDP": "USD",
22+
"USDSC": "USD",
23+
"USDSC(6)": "USD",
24+
"USDSC(18)": "USD",
25+
}
26+
}
27+
28+
func GetTokenPegSymbol(symbol string) string {
29+
return getTokenPegMap()[symbol]
30+
}

0 commit comments

Comments
 (0)