Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benleb committed Aug 14, 2022
1 parent fb3a807 commit c2127c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 59 deletions.
10 changes: 4 additions & 6 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@ func formatEvent(ctx context.Context, g *gocui.Gui, event *collections.Event, no

cache := cache.New(ctx)

if viper.GetBool("redis.enabled") {
// check if the ENS name is already in the cache
if name, err := cache.GetENSName(event.To.Address); err == nil && name != "" {
gbl.Log.Infof("cache | cached ENS name: %s", name)
// check if the ENS name is already in the cache
if name, err := cache.GetENSName(event.To.Address); err == nil && name != "" {
gbl.Log.Infof("cache | cached ENS name: %s", name)

ensName = name
}
ensName = name
}

if ensName == "" && viper.IsSet("api_keys.etherscan") {
Expand Down
4 changes: 3 additions & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func New(ctx context.Context) *GbCache {
}

if viper.GetBool("redis.enabled") {
if client := NewRCache(ctx); client != nil {
if client := NewRedisClient(ctx); client != nil {
gCache.rdb = client
} else {
viper.Set("redis.enabled", false)
}
}

Expand Down
20 changes: 6 additions & 14 deletions internal/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,27 @@ import (
"context"
"fmt"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
)

// cache = &redisCache{
// rdb: nil,
// }
var mu = &sync.RWMutex{}

type NamespaceKey string

const (
PrefixContractName NamespaceKey = "contractName"
PrefixENS NamespaceKey = "ensDomain"
KeyDelimiter string = ":"
prefixContractName string = "contractName"
prefixENS string = "ensDomain"
keyDelimiter string = ":"
)

func keyContract(contractAddress common.Address) string {
return fmt.Sprint(PrefixContractName, KeyDelimiter, contractAddress.Hex())
return fmt.Sprint(prefixContractName, keyDelimiter, contractAddress.Hex())
}

func keyENS(address common.Address) string {
return fmt.Sprint(PrefixENS, KeyDelimiter, address.Hex())
return fmt.Sprint(prefixENS, keyDelimiter, address.Hex())
}

func NewRCache(ctx context.Context) *redis.Client {
func NewRedisClient(ctx context.Context) *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: strings.Join([]string{
viper.GetString("redis.host"),
Expand Down
38 changes: 6 additions & 32 deletions internal/gbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/benleb/gloomberg/internal/abis"
"github.com/benleb/gloomberg/internal/cache"
"github.com/benleb/gloomberg/internal/gbl"
"github.com/benleb/gloomberg/internal/style"
"github.com/ethereum/go-ethereum"
Expand All @@ -25,7 +24,6 @@ import (
)

type CollectionMetadata struct {
Name string `json:"name"`
Symbol string `mapstructure:"symbol"`
TotalSupply uint64 `mapstructure:"total_supply"`
TokenURI string `mapstructure:"token_uri"`
Expand Down Expand Up @@ -183,46 +181,22 @@ func (p ChainNode) subscribeTo(ctx context.Context, queueLogs chan types.Log, to
return p.Client.SubscribeFilterLogs(ctx, filterQuery, queueLogs)
}

func (p ChainNode) GetCollectionName(contractAddress common.Address) string {
func (p ChainNode) GetCollectionName(contractAddress common.Address) (string, error) {
// get the contractERC721 ABIs
contractERC721, err := abis.NewERC721v3(contractAddress, p.Client)
if err != nil {
gbl.Log.Error(err)
}

// collection name
collectionName := ""

cache := cache.New(ctx)

// check if the collection is already in the redis cache
if viper.GetBool("redis.enabled") {
if name, err := cache.GetCollectionName(contractAddress); err == nil && name != "" {
gbl.Log.Infof("cache | cached collection name: %s", name)

collectionName = name
}
return "", err
}

// if not found in redis, we call the contract method to get the name
if collectionName == "" {
if name, err := contractERC721.Name(&bind.CallOpts{}); err == nil {
collectionName = name

if viper.GetBool("redis.enabled") {
if collectionName != "" {
// cache collection name
gbl.Log.Infof("cache | caching collection name: %s", collectionName)
if name, err := contractERC721.Name(&bind.CallOpts{}); err == nil {
gbl.Log.Infof("found collection name via chain call: %s", name)

cache.CacheCollectionName(contractAddress, collectionName)
}
}
} else {
collectionName = style.ShortenAddress(&contractAddress)
}
return name, nil
}

return collectionName
return "", nil
}

func (p ChainNode) GetCollectionMetadata(contractAddress common.Address) *CollectionMetadata {
Expand Down
10 changes: 4 additions & 6 deletions internal/wwatcher/ens.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/benleb/gloomberg/internal/gbl"
"github.com/benleb/gloomberg/internal/gbnode"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/viper"
"github.com/wealdtech/go-ens/v3"
)

Expand Down Expand Up @@ -94,11 +93,10 @@ func ReverseLookupAndValidate(ctx context.Context, address common.Address, nodes

cache := cache.New(ctx)

if viper.GetBool("redis.enabled") {
// cache collection name
gbl.Log.Infof("cache | caching ENS name: %s", ensName)
cache.CacheCollectionName(address, ensName)
}
// cache collection name
gbl.Log.Infof("cache | caching ENS name: %s", ensName)
cache.CacheCollectionName(address, ensName)

}

return ensName
Expand Down

0 comments on commit c2127c8

Please sign in to comment.