Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and add SVM support #4

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 6 additions & 7 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}

"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@duneanalytics/hooks",
"version": "1.0.8",
"version": "2.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we want to bump to 2? Another approach is keep the useTransactions and pass an object config for vm: [evm|svm] with evm as the default.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've included some breaking changes, hence the major bump, but I could revert those. There are some inconsistencies in the API I'd like us to fix, but it's not necessary.

"description": "A collection of React hooks for Dune Analytics.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
26 changes: 13 additions & 13 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import babel from "@rollup/plugin-babel";
import typescript from "@rollup/plugin-typescript";
import pkg from './package.json' assert { type: 'json' };
import pkg from "./package.json" assert { type: "json" };

export default {
input: "src/index.ts",
output: [
{
file: pkg.main,
format: "cjs",
sourcemap: true
sourcemap: true,
},
{
file: pkg.module,
format: "esm",
sourcemap: true
}
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
resolve({
extensions: ['.mjs', '.js', '.jsx', '.json', '.ts', '.tsx']
extensions: [".mjs", ".js", ".jsx", ".json", ".ts", ".tsx"],
}),
commonjs(),
typescript({
tsconfig: './tsconfig.json',
tsconfig: "./tsconfig.json",
declaration: true,
declarationDir: './dist/types',
declarationDir: "./dist/types",
}),
babel({
babelHelpers: "bundled",
exclude: "node_modules/**",
extensions: [".js", ".jsx", ".ts", ".tsx"],
}),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.ts', '.tsx']
})
],
external: Object.keys(pkg.peerDependencies || {})
external: Object.keys(pkg.peerDependencies || {}),
};
65 changes: 0 additions & 65 deletions src/duneApi.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/evm/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpClient } from "../http";
import {
TokenBalancesData,
TokenBalancesParams,
TransactionsData,
TransactionsParams,
} from "./types";

export function fetchTokenBalances(
client: HttpClient,
walletAddress: string,
params?: TokenBalancesParams
): Promise<TokenBalancesData> {
return client.get<TokenBalancesData>(`/balances/evm/${walletAddress}`, {
query: params,
});
}

export function fetchTransactions(
client: HttpClient,
walletAddress: string,
params?: TransactionsParams
): Promise<TransactionsData> {
return client.get<TransactionsData>(`/transactions/evm/${walletAddress}`, {
query: params,
});
}
3 changes: 3 additions & 0 deletions src/evm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { useTokenBalances } from "./useTokenBalances";
export { useTransactions } from "./useTransactions";
export { fetchTokenBalances, fetchTransactions } from "./api";
85 changes: 85 additions & 0 deletions src/evm/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
export type TokenBalancesParams = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should double check these params. I think all is right, but something to keep an eye on as we continue to evolve.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should generate this from OpenAPI

/** Comma separated list of chain ids to get balances for */
chain_ids?: "all" | number[];

/** Specify this to exclude spam tokens from the response */
exclude_spam_tokens?: string;

/** Specify `erc20` or `native` to get only ERC20 tokens or native assets, respectively */
filters?: "erc20" | "native";

/** Maximum number of transactions to return */
limit?: number;

/** The offset to paginate through result sets. This is a cursor being passed from the previous response, only use what the backend returns here. */
offset?: string;

/** A comma separated list of additional metadata fields to include for each token. Supported values: logo, url */
metadata?: ("logo" | "url")[];
};

export type TokenBalance = {
chain: string;
chain_id: number;
address: string;
amount: string;
symbol?: string;
decimals?: number;
price_usd?: number;
value_usd?: number;
};

export type TokenBalancesData = {
request_time: string;
response_time: string;
wallet_address: string;
balances: TokenBalance[];
};

export type TransactionsParams = {
/** The offset to paginate through result sets. This is a cursor being passed from the previous response, only use what the backend has returned on previous responses. */
offset?: string;

/** Maximum number of transactions to return */
limit?: number;

/** Comma separated list of chain ids to get transactions for */
chain_ids?: "all" | number[];

/** Return only transactions with this method id */
method_id?: string;

/** Filter transactions to a given address */
to?: string;

/** Return abi decoded transactions and logs */
decode?: boolean;
};

export type Transaction = {
address: string;
block_hash: string;
block_number: string;
block_time: string;
block_version: number;
chain: string;
from: string;
to: string;
data: string;
gas_price: string;
hash: string;
index: string;
max_fee_per_gas: string;
max_priority_fee_per_gas: string;
nonce: string;
transaction_type: string;
value: string;
};

export type TransactionsData = {
request_time: string;
response_time: string;
wallet_address: string;
transactions: Transaction[];
next_offset?: string | null;
};
57 changes: 57 additions & 0 deletions src/evm/useTokenBalances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useEffect, useState } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these all copy pasta over from the previous files? No changes?

import { useDuneClient } from "../provider";
import { useDeepMemo } from "../useDeepMemo";
import { fetchTokenBalances } from "./api";
import { TokenBalancesData, TokenBalancesParams } from "./types";

export const useTokenBalances = (
walletAddress: string,
params?: TokenBalancesParams
) => {
const [state, setState] = useState<{
isLoading: boolean;
data: TokenBalancesData | null;
error: Error | null;
}>({
isLoading: true,
data: null,
error: null,
});

const memoizedParams = useDeepMemo(() => params, params);
const client = useDuneClient();

useEffect(() => {
const fetchDataAsync = async () => {
setState((prevState) => ({ ...prevState, isLoading: true }));

try {
const result = await fetchTokenBalances(
client,
walletAddress,
memoizedParams
);

setState({
isLoading: false,
data: result,
error: null,
});
} catch (error) {
if (!(error instanceof Error)) {
throw error;
}

setState({
isLoading: false,
data: null,
error,
});
}
};

fetchDataAsync();
}, [client, walletAddress, memoizedParams]);

return state;
};
Loading