Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions covalentclient/covalent_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type CovalentClientSettings struct {
Debug *bool `json:"debug,omitempty"`
// The number of concurrent requests allowed.
ThreadCount *int `json:"thread_count,omitempty"`
// The base URL for the Covalent API
BaseURL string `json:"base_url,omitempty"`
}

type CovalentClientType struct {
Expand All @@ -31,6 +33,7 @@ func CovalentClient(apiKey string, settings ...CovalentClientSettings) *Covalent
client := &CovalentClientType{}
validator := utils.NewApiKeyValidator(apiKey)
isValidKey := validator.IsValidApiKey()
baseURL := "https://api.covalenthq.com"

if len(settings) == 0 {
client.Debug = defaultDebug
Expand All @@ -50,15 +53,19 @@ func CovalentClient(apiKey string, settings ...CovalentClientSettings) *Covalent
} else {
client.ThreadCount = *setting.ThreadCount
}

if setting.BaseURL != "" {
baseURL = setting.BaseURL
}
}

client.SecurityService = services.NewSecurityServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.BalanceService = services.NewBalanceServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.BaseService = services.NewBaseServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.NftService = services.NewNftServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.PricingService = services.NewPricingServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.TransactionService = services.NewTransactionServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.XykService = services.NewXykServiceImpl(apiKey, client.Debug, client.ThreadCount, isValidKey)
client.SecurityService = services.NewSecurityServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.BalanceService = services.NewBalanceServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.BaseService = services.NewBaseServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.NftService = services.NewNftServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.PricingService = services.NewPricingServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.TransactionService = services.NewTransactionServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)
client.XykService = services.NewXykServiceImpl(baseURL, apiKey, client.Debug, client.ThreadCount, isValidKey)

return client

Expand Down
21 changes: 11 additions & 10 deletions services/balance_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,9 @@ type GetNativeTokenBalanceQueryParamOpts struct {
BlockHeight *string `json:"blockHeight,omitempty"`
}

func NewBalanceServiceImpl(apiKey string, debug bool, threadCount int, isValidKey bool) BalanceService {
func NewBalanceServiceImpl(baseURL string, apiKey string, debug bool, threadCount int, isValidKey bool) BalanceService {

return &balanceServiceImpl{APIKey: apiKey, Debug: debug, ThreadCount: threadCount, IskeyValid: isValidKey}
return &balanceServiceImpl{BaseURL: baseURL, APIKey: apiKey, Debug: debug, ThreadCount: threadCount, IskeyValid: isValidKey}
}

type BalanceService interface {
Expand Down Expand Up @@ -538,6 +538,7 @@ type BalanceService interface {
}

type balanceServiceImpl struct {
BaseURL string
APIKey string
Debug bool
ThreadCount int
Expand All @@ -546,7 +547,7 @@ type balanceServiceImpl struct {

func (s *balanceServiceImpl) GetTokenBalancesForWalletAddress(chainName chains.Chain, walletAddress string, queryParamOpts ...GetTokenBalancesForWalletAddressQueryParamOpts) (*utils.Response[BalancesResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/balances_v2/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/balances_v2/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -657,7 +658,7 @@ func (s *balanceServiceImpl) GetTokenBalancesForWalletAddress(chainName chains.C

func (s *balanceServiceImpl) GetHistoricalPortfolioForWalletAddress(chainName chains.Chain, walletAddress string, queryParamOpts ...GetHistoricalPortfolioForWalletAddressQueryParamOpts) (*utils.Response[PortfolioResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/portfolio_v2/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/portfolio_v2/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -767,7 +768,7 @@ func (s *balanceServiceImpl) GetErc20TransfersForWalletAddress(chainName chains.
return
}

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/transfers_v2/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/transfers_v2/", s.BaseURL, chainName, walletAddress)

// Parse the formatted URL
parsedURL, err := url.Parse(apiURL)
Expand Down Expand Up @@ -861,7 +862,7 @@ func (s *balanceServiceImpl) GetErc20TransfersForWalletAddress(chainName chains.
}

func (s *balanceServiceImpl) GetErc20TransfersForWalletAddressByPage(chainName chains.Chain, walletAddress string, queryParamOpts ...GetErc20TransfersForWalletAddressQueryParamOpts) (*utils.Response[Erc20TransfersResponse], error) {
apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/transfers_v2/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/transfers_v2/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -987,7 +988,7 @@ func (s *balanceServiceImpl) GetTokenHoldersV2ForTokenAddress(chainName chains.C
return
}

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/tokens/%s/token_holders_v2/", chainName, tokenAddress)
apiURL := fmt.Sprintf("%s/v1/%v/tokens/%s/token_holders_v2/", s.BaseURL, chainName, tokenAddress)

// Parse the formatted URL
parsedURL, err := url.Parse(apiURL)
Expand Down Expand Up @@ -1073,7 +1074,7 @@ func (s *balanceServiceImpl) GetTokenHoldersV2ForTokenAddress(chainName chains.C
}

func (s *balanceServiceImpl) GetTokenHoldersV2ForTokenAddressByPage(chainName chains.Chain, tokenAddress string, queryParamOpts ...GetTokenHoldersV2ForTokenAddressQueryParamOpts) (*utils.Response[TokenHoldersResponse], error) {
apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/tokens/%s/token_holders_v2/", chainName, tokenAddress)
apiURL := fmt.Sprintf("%s/v1/%v/tokens/%s/token_holders_v2/", s.BaseURL, chainName, tokenAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1180,7 +1181,7 @@ func (s *balanceServiceImpl) GetTokenHoldersV2ForTokenAddressByPage(chainName ch

func (s *balanceServiceImpl) GetHistoricalTokenBalancesForWalletAddress(chainName chains.Chain, walletAddress string, queryParamOpts ...GetHistoricalTokenBalancesForWalletAddressQueryParamOpts) (*utils.Response[HistoricalBalancesResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/historical_balances/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/historical_balances/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1299,7 +1300,7 @@ func (s *balanceServiceImpl) GetHistoricalTokenBalancesForWalletAddress(chainNam

func (s *balanceServiceImpl) GetNativeTokenBalance(chainName chains.Chain, walletAddress string, queryParamOpts ...GetNativeTokenBalanceQueryParamOpts) (*utils.Response[TokenBalanceNativeResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/balances_native/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/balances_native/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down
31 changes: 16 additions & 15 deletions services/base_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ type GetGasPricesQueryParamOpts struct {
QuoteCurrency *quotes.Quote `json:"quoteCurrency,omitempty"`
}

func NewBaseServiceImpl(apiKey string, debug bool, threadCount int, isValidKey bool) BaseService {
func NewBaseServiceImpl(baseURL string, apiKey string, debug bool, threadCount int, isValidKey bool) BaseService {

return &baseServiceImpl{APIKey: apiKey, Debug: debug, ThreadCount: threadCount, IskeyValid: isValidKey}
return &baseServiceImpl{BaseURL: baseURL, APIKey: apiKey, Debug: debug, ThreadCount: threadCount, IskeyValid: isValidKey}
}

type BaseService interface {
Expand Down Expand Up @@ -437,6 +437,7 @@ type BaseService interface {
}

type baseServiceImpl struct {
BaseURL string
APIKey string
Debug bool
ThreadCount int
Expand All @@ -445,7 +446,7 @@ type baseServiceImpl struct {

func (s *baseServiceImpl) GetBlock(chainName chains.Chain, blockHeight string) (*utils.Response[BlockResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/block_v2/%s/", chainName, blockHeight)
apiURL := fmt.Sprintf("%s/v1/%v/block_v2/%s/", s.BaseURL, chainName, blockHeight)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -532,7 +533,7 @@ func (s *baseServiceImpl) GetBlock(chainName chains.Chain, blockHeight string) (

func (s *baseServiceImpl) GetResolvedAddress(chainName chains.Chain, walletAddress string) (*utils.Response[ResolvedAddress], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/address/%s/resolve_address/", chainName, walletAddress)
apiURL := fmt.Sprintf("%s/v1/%v/address/%s/resolve_address/", s.BaseURL, chainName, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -630,7 +631,7 @@ func (s *baseServiceImpl) GetBlockHeights(chainName chains.Chain, startDate stri
return
}

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/block_v2/%s/%s/", chainName, startDate, endDate)
apiURL := fmt.Sprintf("%s/v1/%v/block_v2/%s/%s/", s.BaseURL, chainName, startDate, endDate)

// Parse the formatted URL
parsedURL, err := url.Parse(apiURL)
Expand Down Expand Up @@ -708,7 +709,7 @@ func (s *baseServiceImpl) GetBlockHeights(chainName chains.Chain, startDate stri
}

func (s *baseServiceImpl) GetBlockHeightsByPage(chainName chains.Chain, startDate string, endDate string, queryParamOpts ...GetBlockHeightsQueryParamOpts) (*utils.Response[BlockHeightsResponse], error) {
apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/block_v2/%s/%s/", chainName, startDate, endDate)
apiURL := fmt.Sprintf("%s/v1/%v/block_v2/%s/%s/", s.BaseURL, chainName, startDate, endDate)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -807,7 +808,7 @@ func (s *baseServiceImpl) GetBlockHeightsByPage(chainName chains.Chain, startDat

func (s *baseServiceImpl) GetLogs(chainName chains.Chain, queryParamOpts ...GetLogsQueryParamOpts) (*utils.Response[GetLogsResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/events/", chainName)
apiURL := fmt.Sprintf("%s/v1/%v/events/", s.BaseURL, chainName)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -933,7 +934,7 @@ func (s *baseServiceImpl) GetLogEventsByAddress(chainName chains.Chain, contract
return
}

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/events/address/%s/", chainName, contractAddress)
apiURL := fmt.Sprintf("%s/v1/%v/events/address/%s/", s.BaseURL, chainName, contractAddress)

// Parse the formatted URL
parsedURL, err := url.Parse(apiURL)
Expand Down Expand Up @@ -1019,7 +1020,7 @@ func (s *baseServiceImpl) GetLogEventsByAddress(chainName chains.Chain, contract
}

func (s *baseServiceImpl) GetLogEventsByAddressByPage(chainName chains.Chain, contractAddress string, queryParamOpts ...GetLogEventsByAddressQueryParamOpts) (*utils.Response[LogEventsByAddressResponse], error) {
apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/events/address/%s/", chainName, contractAddress)
apiURL := fmt.Sprintf("%s/v1/%v/events/address/%s/", s.BaseURL, chainName, contractAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1137,7 +1138,7 @@ func (s *baseServiceImpl) GetLogEventsByTopicHash(chainName chains.Chain, topicH
return
}

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/events/topics/%s/", chainName, topicHash)
apiURL := fmt.Sprintf("%s/v1/%v/events/topics/%s/", s.BaseURL, chainName, topicHash)

// Parse the formatted URL
parsedURL, err := url.Parse(apiURL)
Expand Down Expand Up @@ -1227,7 +1228,7 @@ func (s *baseServiceImpl) GetLogEventsByTopicHash(chainName chains.Chain, topicH
}

func (s *baseServiceImpl) GetLogEventsByTopicHashByPage(chainName chains.Chain, topicHash string, queryParamOpts ...GetLogEventsByTopicHashQueryParamOpts) (*utils.Response[LogEventsByTopicHashResponse], error) {
apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/events/topics/%s/", chainName, topicHash)
apiURL := fmt.Sprintf("%s/v1/%v/events/topics/%s/", s.BaseURL, chainName, topicHash)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1338,7 +1339,7 @@ func (s *baseServiceImpl) GetLogEventsByTopicHashByPage(chainName chains.Chain,

func (s *baseServiceImpl) GetAllChains() (*utils.Response[AllChainsResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/chains/")
apiURL := fmt.Sprintf("%s/v1/chains/", s.BaseURL)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1425,7 +1426,7 @@ func (s *baseServiceImpl) GetAllChains() (*utils.Response[AllChainsResponse], er

func (s *baseServiceImpl) GetAllChainStatus() (*utils.Response[AllChainsStatusResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/chains/status/")
apiURL := fmt.Sprintf("%s/v1/chains/status/", s.BaseURL)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1512,7 +1513,7 @@ func (s *baseServiceImpl) GetAllChainStatus() (*utils.Response[AllChainsStatusRe

func (s *baseServiceImpl) GetAddressActivity(walletAddress string, queryParamOpts ...GetAddressActivityQueryParamOpts) (*utils.Response[ChainActivityResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/address/%s/activity/", walletAddress)
apiURL := fmt.Sprintf("%s/v1/address/%s/activity/", s.BaseURL, walletAddress)

if !s.IskeyValid {
errorCode := 401
Expand Down Expand Up @@ -1607,7 +1608,7 @@ func (s *baseServiceImpl) GetAddressActivity(walletAddress string, queryParamOpt

func (s *baseServiceImpl) GetGasPrices(chainName chains.Chain, eventType string, queryParamOpts ...GetGasPricesQueryParamOpts) (*utils.Response[GasPricesResponse], error) {

apiURL := fmt.Sprintf("https://api.covalenthq.com/v1/%v/event/%s/gas_prices/", chainName, eventType)
apiURL := fmt.Sprintf("%s/v1/%v/event/%s/gas_prices/", s.BaseURL, chainName, eventType)

if !s.IskeyValid {
errorCode := 401
Expand Down
Loading