mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2025-04-27 18:41:01 +02:00
refactor: ge bootstrapper, cache price quoter
This commit is contained in:
parent
79ec07ef5d
commit
87e91907dd
71
internal/cache/bootstrap.go
vendored
Normal file
71
internal/cache/bootstrap.go
vendored
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/celo-org/celo-blockchain/common"
|
||||||
|
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
||||||
|
"github.com/grassrootseconomics/celoutils/v2"
|
||||||
|
"github.com/grassrootseconomics/w3-celo"
|
||||||
|
"github.com/grassrootseconomics/w3-celo/module/eth"
|
||||||
|
)
|
||||||
|
|
||||||
|
func bootstrapAllGESmartContracts(ctx context.Context, registries []string, chain *chain.Chain, cache Cache) error {
|
||||||
|
for _, registry := range registries {
|
||||||
|
registryMap, err := chain.Provider.RegistryMap(ctx, w3.A(registry))
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range registryMap {
|
||||||
|
cache.Add(v.Hex())
|
||||||
|
}
|
||||||
|
|
||||||
|
if registryMap[celoutils.TokenIndex] != common.ZeroAddress {
|
||||||
|
tokens, err := chain.GetAllTokensFromTokenIndex(ctx, registryMap[celoutils.TokenIndex])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, token := range tokens {
|
||||||
|
cache.Add(token.Hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if registryMap[celoutils.PoolIndex] != common.ZeroAddress {
|
||||||
|
pools, err := chain.GetAllTokensFromTokenIndex(ctx, registryMap[celoutils.PoolIndex])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pool := range pools {
|
||||||
|
cache.Add(pool.Hex())
|
||||||
|
|
||||||
|
var (
|
||||||
|
poolTokenRegistry common.Address
|
||||||
|
priceQuoter common.Address
|
||||||
|
)
|
||||||
|
err := chain.Provider.Client.CallCtx(
|
||||||
|
ctx,
|
||||||
|
eth.CallFunc(pool, tokenRegistryGetter).Returns(&poolTokenRegistry),
|
||||||
|
eth.CallFunc(pool, quoterGetter).Returns(&priceQuoter),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cache.Add(priceQuoter.Hex())
|
||||||
|
|
||||||
|
poolTokens, err := chain.GetAllTokensFromTokenIndex(ctx, poolTokenRegistry)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, token := range poolTokens {
|
||||||
|
cache.Add(token.Hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
65
internal/cache/cache.go
vendored
65
internal/cache/cache.go
vendored
@ -4,11 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"github.com/celo-org/celo-blockchain/common"
|
|
||||||
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
||||||
"github.com/grassrootseconomics/celoutils/v2"
|
|
||||||
"github.com/grassrootseconomics/w3-celo"
|
"github.com/grassrootseconomics/w3-celo"
|
||||||
"github.com/grassrootseconomics/w3-celo/module/eth"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -28,7 +25,8 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tokenRegistryFunc = w3.MustNewFunc("tokenRegistry()", "address")
|
tokenRegistryGetter = w3.MustNewFunc("tokenRegistry()", "address")
|
||||||
|
quoterGetter = w3.MustNewFunc("quoter()", "address")
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(o CacheOpts) (Cache, error) {
|
func New(o CacheOpts) (Cache, error) {
|
||||||
@ -43,58 +41,13 @@ func New(o CacheOpts) (Cache, error) {
|
|||||||
cache = NewMapCache()
|
cache = NewMapCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
if err := bootstrapAllGESmartContracts(
|
||||||
for _, registry := range o.Registries {
|
context.Background(),
|
||||||
registryMap, err := o.Chain.Provider.RegistryMap(ctx, w3.A(registry))
|
o.Registries,
|
||||||
if err != nil {
|
o.Chain,
|
||||||
return nil, err
|
cache,
|
||||||
}
|
); 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())
|
|
||||||
|
|
||||||
var (
|
|
||||||
poolTokenRegistry common.Address
|
|
||||||
)
|
|
||||||
err := o.Chain.Provider.Client.CallCtx(
|
|
||||||
ctx,
|
|
||||||
eth.CallFunc(pool, tokenRegistryFunc).Returns(&poolTokenRegistry),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
poolTokens, err := o.Chain.GetAllTokensFromTokenIndex(ctx, poolTokenRegistry)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, token := range poolTokens {
|
|
||||||
cache.Add(token.Hex())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
o.Logg.Debug("cache bootstrap complete", "cached_addresses", cache.Size())
|
o.Logg.Debug("cache bootstrap complete", "cached_addresses", cache.Size())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user