From ea704b82ed43923ec8dfedf6261e8fab16f41a63 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 20 Apr 2021 20:28:16 +0300 Subject: [PATCH] Fix: transfers loading --- cmd/api/handlers/transfers.go | 59 +++++++++------------ configs/development.yml | 2 +- internal/elastic/tokenmetadata/storage.go | 27 +++++++++- internal/models/tokenmetadata/repository.go | 1 + internal/reindexer/tokenmetadata/storage.go | 5 ++ 5 files changed, 57 insertions(+), 37 deletions(-) diff --git a/cmd/api/handlers/transfers.go b/cmd/api/handlers/transfers.go index b3658141c..8eb4309ef 100644 --- a/cmd/api/handlers/transfers.go +++ b/cmd/api/handlers/transfers.go @@ -1,7 +1,9 @@ package handlers import ( + "fmt" "net/http" + "time" "github.com/baking-bad/bcdhub/internal/models/tokenmetadata" "github.com/baking-bad/bcdhub/internal/models/transfer" @@ -56,10 +58,15 @@ func (ctx *Context) GetContractTransfers(c *gin.Context) { c.JSON(http.StatusOK, response) } -type tokenKey struct { - Network string - Contract string - TokenID int64 +func (ctx *Context) getTokenMetadata(network, contract string, tokenID int64) (tokenmetadata.TokenMetadata, error) { + key := fmt.Sprintf("%s:%s:%d", network, contract, tokenID) + item, err := ctx.Cache.Fetch(key, time.Minute*5, func() (interface{}, error) { + return ctx.TokenMetadata.GetOne(network, contract, tokenID) + }) + if err != nil { + return tokenmetadata.TokenMetadata{}, err + } + return item.Value().(tokenmetadata.TokenMetadata), nil } func (ctx *Context) transfersPostprocessing(transfers transfer.Pageable, withLastID bool) (response TransferResponse, err error) { @@ -69,40 +76,22 @@ func (ctx *Context) transfersPostprocessing(transfers transfer.Pageable, withLas response.LastID = transfers.LastID } - mapTokens := make(map[tokenKey]*TokenMetadata) - tokens, err := ctx.TokenMetadata.GetAll(tokenmetadata.GetContext{ - TokenID: -1, - }) - if err != nil { - if !ctx.Storage.IsRecordNotFound(err) { - return - } - } else { - for i := range tokens { - mapTokens[tokenKey{ - Network: tokens[i].Network, - Contract: tokens[i].Contract, - TokenID: tokens[i].TokenID, - }] = &TokenMetadata{ - Contract: tokens[i].Contract, - TokenID: tokens[i].TokenID, - Symbol: tokens[i].Symbol, - Name: tokens[i].Name, - Decimals: tokens[i].Decimals, - Network: tokens[i].Network, + for i := range transfers.Transfers { + response.Transfers[i] = TransferFromElasticModel(transfers.Transfers[i]) + + token, err := ctx.getTokenMetadata(response.Transfers[i].Network, response.Transfers[i].Contract, response.Transfers[i].TokenID) + if err == nil { + tm := TokenMetadataFromElasticModel(token, false) + response.Transfers[i].Token = &TokenMetadata{ + Contract: tm.Contract, + TokenID: tm.TokenID, + Symbol: tm.Symbol, + Name: tm.Name, + Decimals: tm.Decimals, + Network: tm.Network, } } - } - for i := range transfers.Transfers { - token := mapTokens[tokenKey{ - Network: transfers.Transfers[i].Network, - Contract: transfers.Transfers[i].Contract, - TokenID: transfers.Transfers[i].TokenID, - }] - - response.Transfers[i] = TransferFromElasticModel(transfers.Transfers[i]) - response.Transfers[i].Token = token response.Transfers[i].Alias = ctx.getAlias(transfers.Transfers[i].Network, transfers.Transfers[i].Contract) response.Transfers[i].InitiatorAlias = ctx.getAlias(transfers.Transfers[i].Network, transfers.Transfers[i].Initiator) response.Transfers[i].FromAlias = ctx.getAlias(transfers.Transfers[i].Network, transfers.Transfers[i].From) diff --git a/configs/development.yml b/configs/development.yml index ea2fbf9fc..d0eb8ba20 100644 --- a/configs/development.yml +++ b/configs/development.yml @@ -23,7 +23,7 @@ tzkt: storage: uri: - - http://${ELASTIC_HOSTNAME:-127.0.0.1}:9200 + - http://${ELASTIC_HOSTNAME:-127.0.0.1}:9201 timeout: 10 rabbitmq: diff --git a/internal/elastic/tokenmetadata/storage.go b/internal/elastic/tokenmetadata/storage.go index e85595b48..638e93156 100644 --- a/internal/elastic/tokenmetadata/storage.go +++ b/internal/elastic/tokenmetadata/storage.go @@ -1,6 +1,8 @@ package tokenmetadata import ( + "encoding/json" + "github.com/baking-bad/bcdhub/internal/elastic/consts" "github.com/baking-bad/bcdhub/internal/models" @@ -28,7 +30,7 @@ func (storage *Storage) Get(ctx []tokenmetadata.GetContext, size, offset int64) return } -// Get - +// GetAll - func (storage *Storage) GetAll(ctx ...tokenmetadata.GetContext) (tokens []tokenmetadata.TokenMetadata, err error) { query := buildGetTokenMetadataContext(ctx, true) err = storage.es.GetAllByQuery(query, &tokens) @@ -56,3 +58,26 @@ func (storage *Storage) GetWithExtras() ([]tokenmetadata.TokenMetadata, error) { func (storage *Storage) Count(ctx []tokenmetadata.GetContext) (int64, error) { return storage.es.CountItems([]string{models.DocTokenMetadata}, buildGetTokenMetadataContext(ctx, false)) } + +// GetOne - +func (storage *Storage) GetOne(network, contract string, tokenID int64) (token tokenmetadata.TokenMetadata, err error) { + query := core.NewQuery().Query( + core.Bool( + core.Filter( + core.Match("network", network), + core.MatchPhrase("contract", contract), + core.Term("token_id", tokenID), + ), + ), + ).One() + + var response core.SearchResponse + if err = storage.es.Query([]string{models.DocTokenMetadata}, query, &response); err != nil { + return + } + if response.Hits.Total.Value == 0 { + return token, core.NewRecordNotFoundError(models.DocTokenMetadata, "") + } + err = json.Unmarshal(response.Hits.Hits[0].Source, &token) + return +} diff --git a/internal/models/tokenmetadata/repository.go b/internal/models/tokenmetadata/repository.go index e40c5c614..e75af5e33 100644 --- a/internal/models/tokenmetadata/repository.go +++ b/internal/models/tokenmetadata/repository.go @@ -4,6 +4,7 @@ package tokenmetadata type Repository interface { Get(ctx []GetContext, size, offset int64) ([]TokenMetadata, error) GetAll(ctx ...GetContext) ([]TokenMetadata, error) + GetOne(network, contract string, tokenID int64) (token TokenMetadata, err error) GetWithExtras() ([]TokenMetadata, error) Count(ctx []GetContext) (int64, error) } diff --git a/internal/reindexer/tokenmetadata/storage.go b/internal/reindexer/tokenmetadata/storage.go index 54ac88fb8..6dc101d76 100644 --- a/internal/reindexer/tokenmetadata/storage.go +++ b/internal/reindexer/tokenmetadata/storage.go @@ -41,3 +41,8 @@ func (storage *Storage) GetWithExtras() ([]tokenmetadata.TokenMetadata, error) { func (storage *Storage) Count(ctx []tokenmetadata.GetContext) (int64, error) { return 0, nil } + +// GetOne - +func (storage *Storage) GetOne(network, contract string, tokenID int64) (token tokenmetadata.TokenMetadata, err error) { + return +}