Skip to content

Commit

Permalink
Make base url configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ilamanov committed Jan 23, 2025
1 parent 5efc6d2 commit 01a4b23
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 33 deletions.
6 changes: 4 additions & 2 deletions __tests__/hooks/useEvmTokenBalances.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ describe("useTokenBalances", () => {
expect(mockFetchEvmBalances).toHaveBeenCalledWith(
walletAddress,
{},
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(result.current.isLoading).toBe(false);
expect(result.current.error).toBeNull();
Expand Down Expand Up @@ -106,7 +107,8 @@ describe("useTokenBalances", () => {
expect(mockFetchEvmBalances).toHaveBeenCalledWith(
walletAddress,
{},
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(result.current.isLoading).toBe(false);
expect(result.current.error).toEqual(mockError);
Expand Down
6 changes: 4 additions & 2 deletions __tests__/hooks/useEvmTransactions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ describe("useTransactions", () => {
expect(mockFetchEvmTransactions).toHaveBeenCalledWith(
walletAddress,
{ offset: undefined },
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(result.current.data).toEqual(mockResponse);
expect(result.current.nextOffset).toBe("offset1");
Expand Down Expand Up @@ -119,7 +120,8 @@ describe("useTransactions", () => {
expect(mockFetchEvmTransactions).toHaveBeenCalledWith(
walletAddress,
{ offset: undefined },
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(result.current.error).toEqual(mockError);
expect(result.current.data).toBeNull();
Expand Down
6 changes: 4 additions & 2 deletions __tests__/hooks/useSvmTokenBalances.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ describe("useTokenBalances", () => {
expect(mockFetchSvmBalances).toHaveBeenCalledWith(
walletAddress,
{},
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(svmResult.current.isLoading).toBe(false);
expect(svmResult.current.error).toBeNull();
Expand Down Expand Up @@ -97,7 +98,8 @@ describe("useTokenBalances", () => {
expect(mockFetchSvmBalances).toHaveBeenCalledWith(
walletAddress,
{},
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(svmResult.current.isLoading).toBe(false);
expect(svmResult.current.error).toEqual(mockError);
Expand Down
6 changes: 4 additions & 2 deletions __tests__/hooks/useSvmTransactions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ describe("useTransactions", () => {
expect(mockFetchSvmTransactions).toHaveBeenCalledWith(
walletAddress,
{ offset: undefined },
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(svmResult.current.data).toEqual(mockResponse);
expect(svmResult.current.nextOffset).toBe("offset1");
Expand Down Expand Up @@ -109,7 +110,8 @@ describe("useTransactions", () => {
expect(mockFetchSvmTransactions).toHaveBeenCalledWith(
walletAddress,
{ offset: undefined },
process.env.DUNE_API_KEY
process.env.DUNE_API_KEY,
"https://api.dune.com"
);
expect(svmResult.current.error).toEqual(mockError);
expect(svmResult.current.data).toBeNull();
Expand Down
15 changes: 8 additions & 7 deletions src/evm/duneApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
TransactionsParams,
} from "./types";

const BALANCE_API_BASE_URL = "https://api.dune.com/api/echo/v1/balances/evm";
const TRANSACTIONS_API_BASE_URL =
"https://api.dune.com/api/echo/v1/transactions/evm";
const BALANCES_PREFIX = "api/echo/v1/balances/evm";
const TRANSACTIONS_PREFIX = "api/echo/v1/transactions/evm";

const getBalanceQueryParams = (
params: TokenBalancesParams
Expand Down Expand Up @@ -40,10 +39,11 @@ const getTransactionsQueryParams = (
export async function fetchEvmBalances(
walletAddress: string,
params: TokenBalancesParams,
duneApiKey: string
duneApiKey: string,
baseUrl: string
): Promise<BalanceData> {
const queryParams = getBalanceQueryParams(params);
const apiUrl = `${BALANCE_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
const apiUrl = `${baseUrl}/${BALANCES_PREFIX}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: "GET",
Expand All @@ -65,10 +65,11 @@ export const fetchBalances = fetchEvmBalances;
export async function fetchEvmTransactions(
walletAddress: string,
params: TransactionsParams,
duneApiKey: string
duneApiKey: string,
baseUrl: string
): Promise<TransactionData> {
const queryParams = getTransactionsQueryParams(params);
const apiUrl = `${TRANSACTIONS_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
const apiUrl = `${baseUrl}/${TRANSACTIONS_PREFIX}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: "GET",
Expand Down
6 changes: 4 additions & 2 deletions src/evm/useEvmTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { TokenBalancesParams, BalanceData, FetchError } from "./types";
import { fetchEvmBalances } from "./duneApi";
import { useDeepMemo } from "../useDeepMemo";
import { useGetApiKey } from "../provider";
import { useGetApiKey, useGetBaseUrl } from "../provider";
import { isAddress } from "viem";

export const useEvmTokenBalances = (
Expand All @@ -28,6 +28,7 @@ export const useEvmTokenBalances = (

const memoizedParams = useDeepMemo(() => params, params);
const apiKey = useGetApiKey();
const baseUrl = useGetBaseUrl();

// Function to fetch data for a specific page
const fetchDataAsync = async (offset: string | null) => {
Expand All @@ -45,7 +46,8 @@ export const useEvmTokenBalances = (
const result = await fetchEvmBalances(
walletAddress,
updatedParams,
apiKey
apiKey,
baseUrl
);

setState((prevState) => ({
Expand Down
6 changes: 4 additions & 2 deletions src/evm/useEvmTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { TransactionsParams, TransactionData, FetchError } from "./types";
import { fetchEvmTransactions } from "./duneApi";
import { useDeepMemo } from "../useDeepMemo";
import { useGetApiKey } from "../provider";
import { useGetApiKey, useGetBaseUrl } from "../provider";
import { isAddress } from "viem";

export const useEvmTransactions = (
Expand All @@ -28,6 +28,7 @@ export const useEvmTransactions = (

const memoizedParams = useDeepMemo(() => params, [params]);
const apiKey = useGetApiKey();
const baseUrl = useGetBaseUrl();

// Function to fetch data for a specific page
const fetchDataAsync = async (offset: string | null) => {
Expand All @@ -45,7 +46,8 @@ export const useEvmTransactions = (
const result = await fetchEvmTransactions(
walletAddress,
updatedParams,
apiKey
apiKey,
baseUrl
);

setState((prevState) => ({
Expand Down
23 changes: 20 additions & 3 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import React, { createContext, useContext } from "react";

interface DuneContextType {
duneApiKey: string;
baseUrl?: string;
}

const DuneContext = createContext<DuneContextType>({ duneApiKey: "" });
const DuneContext = createContext<DuneContextType>({
duneApiKey: "",
baseUrl: "",
});

export const useDuneContext = () => {
const context = useContext(DuneContext);
Expand All @@ -19,14 +23,27 @@ export const useGetApiKey = () => {
return context.duneApiKey;
};

export const useGetBaseUrl = () => {
const context = useDuneContext();
if (!context.baseUrl) {
return "https://api.dune.com";
}
return context.baseUrl;
};

interface DuneProviderProps {
duneApiKey: string;
baseUrl?: string;
children: React.ReactNode;
}

export const DuneProvider = ({ duneApiKey, children }: DuneProviderProps) => {
export const DuneProvider = ({
duneApiKey,
baseUrl,
children,
}: DuneProviderProps) => {
return (
<DuneContext.Provider value={{ duneApiKey }}>
<DuneContext.Provider value={{ duneApiKey, baseUrl }}>
{children}
</DuneContext.Provider>
);
Expand Down
15 changes: 8 additions & 7 deletions src/svm/duneApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
TransactionsParams,
} from "./types";

const BALANCE_API_BASE_URL = "https://api.dune.com/api/echo/beta/balances/svm/";
const TRANSACTIONS_API_BASE_URL =
"https://api.dune.com/api/echo/beta/transactions/svm/";
const BALANCES_PREFIX = "api/echo/beta/balances/svm";
const TRANSACTIONS_PREFIX = "api/echo/beta/transactions/svm";

const getBalanceQueryParams = (
params: TokenBalancesParams
Expand All @@ -31,10 +30,11 @@ const getTransactionsQueryParams = (
export async function fetchSvmBalances(
walletAddress: string,
params: TokenBalancesParams,
duneApiKey: string
duneApiKey: string,
baseUrl: string
): Promise<BalanceData> {
const queryParams = getBalanceQueryParams(params);
const apiUrl = `${BALANCE_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
const apiUrl = `${baseUrl}/${BALANCES_PREFIX}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: "GET",
Expand All @@ -53,10 +53,11 @@ export async function fetchSvmBalances(
export async function fetchSvmTransactions(
walletAddress: string,
params: TransactionsParams,
duneApiKey: string
duneApiKey: string,
baseUrl: string
): Promise<TransactionData> {
const queryParams = getTransactionsQueryParams(params);
const apiUrl = `${TRANSACTIONS_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
const apiUrl = `${baseUrl}/${TRANSACTIONS_PREFIX}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: "GET",
Expand Down
6 changes: 4 additions & 2 deletions src/svm/useSvmTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { TokenBalancesParams, BalanceData, FetchError } from "./types";
import { fetchSvmBalances } from "./duneApi";
import { useDeepMemo } from "../useDeepMemo";
import { useGetApiKey } from "../provider";
import { useGetApiKey, useGetBaseUrl } from "../provider";

export const useSvmTokenBalances = (
walletAddress: string,
Expand All @@ -27,6 +27,7 @@ export const useSvmTokenBalances = (

const memoizedParams = useDeepMemo(() => params, params);
const apiKey = useGetApiKey();
const baseUrl = useGetBaseUrl();

// Function to fetch data for a specific page
const fetchDataAsync = async (offset: string | null) => {
Expand All @@ -44,7 +45,8 @@ export const useSvmTokenBalances = (
const result = await fetchSvmBalances(
walletAddress,
updatedParams,
apiKey
apiKey,
baseUrl
);

setState((prevState) => ({
Expand Down
6 changes: 4 additions & 2 deletions src/svm/useSvmTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { TransactionsParams, TransactionData, FetchError } from "./types";
import { fetchSvmTransactions } from "./duneApi";
import { useDeepMemo } from "../useDeepMemo";
import { useGetApiKey } from "../provider";
import { useGetApiKey, useGetBaseUrl } from "../provider";

export const useSvmTransactions = (
walletAddress: string,
Expand All @@ -27,6 +27,7 @@ export const useSvmTransactions = (

const memoizedParams = useDeepMemo(() => params, [params]);
const apiKey = useGetApiKey();
const baseUrl = useGetBaseUrl();

// Function to fetch data for a specific page
const fetchDataAsync = async (offset: string | null) => {
Expand All @@ -44,7 +45,8 @@ export const useSvmTransactions = (
const result = await fetchSvmTransactions(
walletAddress,
updatedParams,
apiKey
apiKey,
baseUrl
);

setState((prevState) => ({
Expand Down

0 comments on commit 01a4b23

Please sign in to comment.