mirror of
				https://github.com/grassrootseconomics/eth-tracker.git
				synced 2025-10-31 08:44:12 +01: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 | ||||
| } | ||||
							
								
								
									
										63
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										63
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
								
							| @ -4,11 +4,8 @@ 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" | ||||
| 	"github.com/grassrootseconomics/w3-celo/module/eth" | ||||
| ) | ||||
| 
 | ||||
| type ( | ||||
| @ -28,7 +25,8 @@ type ( | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	tokenRegistryFunc = w3.MustNewFunc("tokenRegistry()", "address") | ||||
| 	tokenRegistryGetter = w3.MustNewFunc("tokenRegistry()", "address") | ||||
| 	quoterGetter        = w3.MustNewFunc("quoter()", "address") | ||||
| ) | ||||
| 
 | ||||
| func New(o CacheOpts) (Cache, error) { | ||||
| @ -43,59 +41,14 @@ func New(o CacheOpts) (Cache, error) { | ||||
| 		cache = NewMapCache() | ||||
| 	} | ||||
| 
 | ||||
| 	ctx := context.Background() | ||||
| 	for _, registry := range o.Registries { | ||||
| 		registryMap, err := o.Chain.Provider.RegistryMap(ctx, w3.A(registry)) | ||||
| 		if err != nil { | ||||
| 	if err := bootstrapAllGESmartContracts( | ||||
| 		context.Background(), | ||||
| 		o.Registries, | ||||
| 		o.Chain, | ||||
| 		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()) | ||||
| 
 | ||||
| 	return cache, nil | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user