mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2026-09-13 04:07:46 +02:00
feat: add cache bootstrapper
This commit is contained in:
52
internal/cache/cache.go
vendored
52
internal/cache/cache.go
vendored
@@ -1,9 +1,13 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
||||
"github.com/grassrootseconomics/celoutils/v2"
|
||||
"github.com/grassrootseconomics/w3-celo"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -15,13 +19,14 @@ type (
|
||||
}
|
||||
|
||||
CacheOpts struct {
|
||||
Logg *slog.Logger
|
||||
Chain *chain.Chain
|
||||
CacheType string
|
||||
Logg *slog.Logger
|
||||
Chain *chain.Chain
|
||||
CacheType string
|
||||
Registries []string
|
||||
}
|
||||
)
|
||||
|
||||
func New(o CacheOpts) Cache {
|
||||
func New(o CacheOpts) (Cache, error) {
|
||||
var (
|
||||
cache Cache
|
||||
)
|
||||
@@ -32,7 +37,42 @@ func New(o CacheOpts) Cache {
|
||||
default:
|
||||
cache = NewMapCache()
|
||||
}
|
||||
o.Logg.Debug("bootstrapping cache")
|
||||
|
||||
return cache
|
||||
ctx := context.Background()
|
||||
for _, registry := range o.Registries {
|
||||
registryMap, err := o.Chain.Provider.RegistryMap(ctx, w3.A(registry))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, v := range registryMap {
|
||||
cache.Add(v.Hex())
|
||||
}
|
||||
|
||||
if registryMap[celoutils.TokenIndex] != common.ZeroAddress {
|
||||
tokens, err := o.Chain.GetAllTokensFromTokenIndex(ctx, registryMap[celoutils.TokenIndex])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
cache.Add(token.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
if registryMap[celoutils.PoolIndex] != common.ZeroAddress {
|
||||
pools, err := o.Chain.GetAllTokensFromTokenIndex(ctx, registryMap[celoutils.PoolIndex])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, pool := range pools {
|
||||
cache.Add(pool.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
o.Logg.Debug("cache bootstrap complete", "cached_addresses", cache.Size())
|
||||
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user