Skip to content

Commit

Permalink
Fix: transfers loading
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Apr 20, 2021
1 parent 91046a6 commit ea704b8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
59 changes: 24 additions & 35 deletions cmd/api/handlers/transfers.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion configs/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 26 additions & 1 deletion internal/elastic/tokenmetadata/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tokenmetadata

import (
"encoding/json"

"github.com/baking-bad/bcdhub/internal/elastic/consts"
"github.com/baking-bad/bcdhub/internal/models"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/models/tokenmetadata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions internal/reindexer/tokenmetadata/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ea704b8

Please sign in to comment.