mirror of
				https://github.com/grassrootseconomics/eth-tracker.git
				synced 2025-11-04 10:28:22 +01:00 
			
		
		
		
	feat: add token index fetcher
This commit is contained in:
		
							parent
							
								
									343f304eaf
								
							
						
					
					
						commit
						7263fc1950
					
				
							
								
								
									
										42
									
								
								internal/chain/token_index.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								internal/chain/token_index.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
package chain
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"math/big"
 | 
			
		||||
 | 
			
		||||
	"github.com/celo-org/celo-blockchain/common"
 | 
			
		||||
	"github.com/grassrootseconomics/w3-celo"
 | 
			
		||||
	"github.com/grassrootseconomics/w3-celo/module/eth"
 | 
			
		||||
	"github.com/grassrootseconomics/w3-celo/w3types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	entryCountFunc = w3.MustNewFunc("entryCount()", "uint256")
 | 
			
		||||
	entrySig       = w3.MustNewFunc("entry(uint256 _idx)", "address")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (c *Chain) GetAllTokensFromTokenIndex(ctx context.Context, tokenIndex common.Address) ([]common.Address, error) {
 | 
			
		||||
	var (
 | 
			
		||||
		tokenIndexEntryCount big.Int
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	if err := c.provider.Client.CallCtx(
 | 
			
		||||
		ctx,
 | 
			
		||||
		eth.CallFunc(tokenIndex, entryCountFunc).Returns(&tokenIndexEntryCount),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	calls := make([]w3types.RPCCaller, tokenIndexEntryCount.Int64())
 | 
			
		||||
	tokenAddresses := make([]common.Address, tokenIndexEntryCount.Int64())
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < int(tokenIndexEntryCount.Int64()); i++ {
 | 
			
		||||
		calls[i] = eth.CallFunc(tokenIndex, entrySig, new(big.Int).SetInt64(int64(i))).Returns(&tokenAddresses[i])
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := c.provider.Client.CallCtx(ctx, calls...); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return tokenAddresses, nil
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user