From a842e8f8e686d20a98d908237973f9165e34fd62 Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Wed, 22 Dec 2021 10:56:26 +0400 Subject: [PATCH] adding username translation --- nft.go | 13 ++----------- profile.go | 17 ++++++++--------- transaction.go | 24 ++++-------------------- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/nft.go b/nft.go index d855944..33637aa 100644 --- a/nft.go +++ b/nft.go @@ -207,12 +207,7 @@ func (s *MarketService) GetNft(ctx context.Context, collection string, tokenId i res.IsRecent = time.Unix(res.UpdatedAt, 0).After(time.Now().Add(-1 * time.Hour)) res.IsVeryRecent = time.Unix(res.UpdatedAt, 0).After(time.Now().Add(-5 * time.Minute)) res.IsNotMine = res.CurrentSeller != s.bscaddress - - if n, err := s.profileService.GetUsername(res.CurrentSeller); err != nil { - res.CurrentSellerUsername = res.CurrentSeller - } else { - res.CurrentSellerUsername = n - } + res.CurrentSellerUsername = s.profileService.GetUsername(res.CurrentSeller) return res, nil } @@ -274,11 +269,7 @@ func (s *MarketService) getPage(ctx context.Context, collection string, pagenum floor = nft.CurrentAskPrice } nft.IsNotMine = nft.CurrentSeller != s.bscaddress - if n, err := s.profileService.GetUsername(nft.CurrentSeller); err != nil { - nft.CurrentSellerUsername = nft.CurrentSeller - } else { - nft.CurrentSellerUsername = n - } + nft.CurrentSellerUsername = s.profileService.GetUsername(nft.CurrentSeller) res = append(res, nft) } diff --git a/profile.go b/profile.go index 457e9ff..f4ae0dc 100644 --- a/profile.go +++ b/profile.go @@ -52,18 +52,17 @@ func NewProfileService() *ProfileService { } } -func (s *ProfileService) GetUsername(address string) (string, error) { +func (s *ProfileService) GetUsername(address string) string { s.lock.Lock() defer s.lock.Unlock() if name, ok := s.cache[address]; ok { - return name, nil + return name + } + if name, err := getUsernameFromAddress(address); err != nil { + s.cache[address] = address + } else { + s.cache[address] = name } - name, err := getUsernameFromAddress(address) - if err != nil { - return "", err - } - s.cache[address] = name - - return name, nil + return s.cache[address] } diff --git a/transaction.go b/transaction.go index 7a43c5f..3b973fe 100644 --- a/transaction.go +++ b/transaction.go @@ -110,16 +110,8 @@ func (s *TransactionService) GetLightPageByLimit(ctx context.Context, pagenumber return transaction.Nft.TransactionHistory[i].Timestamp > transaction.Nft.TransactionHistory[j].Timestamp }) transaction.IsMine = transaction.Buyer.Address == s.bscaddress || transaction.Seller.Address == s.bscaddress - if n, err := s.profileService.GetUsername(transaction.Seller.Address); err != nil { - transaction.SellerUsername = transaction.Seller.Address - } else { - transaction.SellerUsername = n - } - if n, err := s.profileService.GetUsername(transaction.Buyer.Address); err != nil { - transaction.BuyerUsername = transaction.Buyer.Address - } else { - transaction.BuyerUsername = n - } + transaction.SellerUsername = s.profileService.GetUsername(transaction.Seller.Address) + transaction.BuyerUsername = s.profileService.GetUsername(transaction.Buyer.Address) } var res []Transaction @@ -179,16 +171,8 @@ func (s *TransactionService) GetPageByLimit(ctx context.Context, pagenumber int, transaction.Time = time.Unix(transaction.Timestamp, 0) transaction.TimeDescription = humanize.Time(transaction.Time) - if n, err := s.profileService.GetUsername(transaction.Seller.Address); err != nil { - transaction.SellerUsername = transaction.Seller.Address - } else { - transaction.SellerUsername = n - } - if n, err := s.profileService.GetUsername(transaction.Buyer.Address); err != nil { - transaction.BuyerUsername = transaction.Buyer.Address - } else { - transaction.BuyerUsername = n - } + transaction.SellerUsername = s.profileService.GetUsername(transaction.Seller.Address) + transaction.BuyerUsername = s.profileService.GetUsername(transaction.Buyer.Address) } var res []Transaction