Skip to content

Commit

Permalink
Add prettier formatting (#9)
Browse files Browse the repository at this point in the history
Add prettier formatting following the same configuration as in dune.com repo.
Fixes https://linear.app/dune/issue/DX-689/create-standard-hard-linting-rules-for-pr-approvals

The PR:
• Installs prettier and adds prettier config
• Runs prettier on the entire repo
  • Loading branch information
ilamanov authored Jan 13, 2025
2 parents ea28e95 + c556e17 commit 2f1b8cb
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 184 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: React Hooks for Dune Echo

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "npm"

- name: Install dependencies
run: npm install --no-audit --no-fund

- name: "Test: prettier"
run: npm run test:prettier
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm install @duneanalytics/hooks
To use the Dune API, wrap your application in the DuneProvider and provide your Dune API key:

```javascript
import { DuneProvider } from '@duneanalytics/hooks';
import { DuneProvider } from "@duneanalytics/hooks";

const App = () => (
<DuneProvider duneApiKey={YOUR_API_KEY}>
Expand All @@ -26,7 +26,7 @@ const App = () => (

# Props

• duneApiKey: Required. The API key to authenticate your requests with Dune Analytics.
• duneApiKey: Required. The API key to authenticate your requests with Dune Analytics.

# Hooks

Expand All @@ -37,7 +37,7 @@ Fetches token balances for a specific wallet address.
### Example Usage

```javascript
import { useTokenBalances } from '@duneanalytics/hooks';
import { useTokenBalances } from "@duneanalytics/hooks";

const MyComponent = ({ account }) => {
const { data, isLoading, error } = useTokenBalances(account.address, {});
Expand All @@ -59,14 +59,14 @@ const MyComponent = ({ account }) => {

### Parameters

• walletAddress: Required. The Ethereum wallet address for which to fetch token balances.
• params: Optional. Additional parameters for the request (e.g., chain or token filters).
• walletAddress: Required. The Ethereum wallet address for which to fetch token balances.
• params: Optional. Additional parameters for the request (e.g., chain or token filters).

### Returns

• data: Contains the list of token balances.
• isLoading: A boolean indicating whether the request is in progress.
• error: Contains any error that occurred during the request.
• data: Contains the list of token balances.
• isLoading: A boolean indicating whether the request is in progress.
• error: Contains any error that occurred during the request.

## useTransactions

Expand All @@ -75,10 +75,17 @@ Fetches paginated transaction data for a specific wallet address.
### Example Usage

```javascript
import { useTransactions } from '@duneanalytics/hooks';
import { useTransactions } from "@duneanalytics/hooks";

const TransactionHistory = ({ account }) => {
const { data: transactionData, isLoading, error, nextPage, previousPage, currentPage } = useTransactions(account.address, {});
const {
data: transactionData,
isLoading,
error,
nextPage,
previousPage,
currentPage,
} = useTransactions(account.address, {});

if (isLoading) return <p>Loading transactions...</p>;
if (error) return <p>Error: {error.message}</p>;
Expand Down Expand Up @@ -106,37 +113,39 @@ const TransactionHistory = ({ account }) => {

### Parameters

• walletAddress: Required. The Ethereum wallet address for which to fetch transactions.
• params: Optional. Additional parameters for the request (e.g., chain or block filters).
• walletAddress: Required. The Ethereum wallet address for which to fetch transactions.
• params: Optional. Additional parameters for the request (e.g., chain or block filters).

### Returns

• data: Contains the transaction data for the current page.
• isLoading: A boolean indicating whether the request is in progress.
• error: Contains any error that occurred during the request.
• nextPage: Function to fetch the next page of transactions.
• previousPage: Function to fetch the previous page of transactions.
• currentPage: The current page number (0-based index).
• data: Contains the transaction data for the current page.
• isLoading: A boolean indicating whether the request is in progress.
• error: Contains any error that occurred during the request.
• nextPage: Function to fetch the next page of transactions.
• previousPage: Function to fetch the previous page of transactions.
• currentPage: The current page number (0-based index).

### Error Handling

Both hooks return an error object that you can use to handle and display errors.

```javascript
```javascript
if (error) {
console.error("Error fetching data:", error.message);
return <p>Error: {error.message}</p>;
}
```
```

# Development

Please use node v20 for development.

# Deploying

If you have the power to deploy, you can run the following commands to deploy the package to the npm registry:

First tag a new version of the package:
*note you can also use `minor` or `major` instead of `patch` to bump the version number accordingly*
_note you can also use `minor` or `major` instead of `patch` to bump the version number accordingly_

```bash
npm version patch
Expand Down
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"
]
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"watch": "rollup -c rollup.config.mjs -w",
"prepare": "npm run build",
"clean": "rm -rf node_modules package-lock.json dist",
"local-setup": "npm run clean && npm i && npm run build && npm pack"
"local-setup": "npm run clean && npm i && npm run build && npm pack",
"test:prettier": "prettier --check .",
"fix:prettier": "prettier --write ."
},
"prettier": {
"trailingComma": "es5"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0"
Expand All @@ -28,6 +33,7 @@
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-typescript": "^11.0.0",
"@types/react": "18.2.25",
"prettier": "^3.4.2",
"rollup": "^3.0.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"typescript": "^5.0.0"
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 || {}),
};
101 changes: 59 additions & 42 deletions src/duneApi.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,82 @@

import {BalanceData, TokenBalancesParams, TransactionData, TransactionsParams} from "./types";
import {
BalanceData,
TokenBalancesParams,
TransactionData,
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 getBalanceQueryParams = (params: TokenBalancesParams): URLSearchParams => {
const queryParams = new URLSearchParams();
if (params.allChains) queryParams.append("all_chains", "true");
if (params.chainIds) queryParams.append("chain_ids", params.chainIds);
if (params.excludeSpamTokens) queryParams.append("exclude_spam_tokens", "true");
if (params.filters) queryParams.append("filters", params.filters);
if (params.offset) queryParams.append("offset", params.offset.toString());
if (params.limit) queryParams.append("limit", params.limit.toString());
if (params.metadata) queryParams.append("metadata", "logo,url");
return queryParams;
};
const TRANSACTIONS_API_BASE_URL =
"https://api.dune.com/api/echo/v1/transactions/evm/";

const getTransactionsQueryParams = (params: TransactionsParams): URLSearchParams => {
const queryParams = new URLSearchParams();
if (params.chainIds) queryParams.append("chain_ids", params.chainIds);
if (params.offset) queryParams.append("offset", params.offset.toString());
if (params.limit) queryParams.append("limit", params.limit.toString());
if (params.method_id) queryParams.append("method_id", params.method_id);
if (params.to) queryParams.append("to", params.to);
if (params.decode) queryParams.append("decode", params.decode.toString());
return queryParams;
};
const getBalanceQueryParams = (
params: TokenBalancesParams
): URLSearchParams => {
const queryParams = new URLSearchParams();
if (params.allChains) queryParams.append("all_chains", "true");
if (params.chainIds) queryParams.append("chain_ids", params.chainIds);
if (params.excludeSpamTokens)
queryParams.append("exclude_spam_tokens", "true");
if (params.filters) queryParams.append("filters", params.filters);
if (params.offset) queryParams.append("offset", params.offset.toString());
if (params.limit) queryParams.append("limit", params.limit.toString());
if (params.metadata) queryParams.append("metadata", "logo,url");
return queryParams;
};

const getTransactionsQueryParams = (
params: TransactionsParams
): URLSearchParams => {
const queryParams = new URLSearchParams();
if (params.chainIds) queryParams.append("chain_ids", params.chainIds);
if (params.offset) queryParams.append("offset", params.offset.toString());
if (params.limit) queryParams.append("limit", params.limit.toString());
if (params.method_id) queryParams.append("method_id", params.method_id);
if (params.to) queryParams.append("to", params.to);
if (params.decode) queryParams.append("decode", params.decode.toString());
return queryParams;
};

export async function fetchBalances(walletAddress: string, params: TokenBalancesParams, duneApiKey: string): Promise<BalanceData> {
const queryParams = getBalanceQueryParams(params)
const apiUrl = `${BALANCE_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
export async function fetchBalances(
walletAddress: string,
params: TokenBalancesParams,
duneApiKey: string
): Promise<BalanceData> {
const queryParams = getBalanceQueryParams(params);
const apiUrl = `${BALANCE_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: 'GET',
headers: {
"x-dune-api-key": duneApiKey
method: "GET",
headers: {
"x-dune-api-key": duneApiKey,
},
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
}

export async function fetchTransactions(walletAddress: string, params: TransactionsParams, duneApiKey: string): Promise<TransactionData> {
const queryParams = getTransactionsQueryParams(params)
const apiUrl = `${TRANSACTIONS_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;
export async function fetchTransactions(
walletAddress: string,
params: TransactionsParams,
duneApiKey: string
): Promise<TransactionData> {
const queryParams = getTransactionsQueryParams(params);
const apiUrl = `${TRANSACTIONS_API_BASE_URL}/${walletAddress}?${queryParams.toString()}`;

const response = await fetch(apiUrl, {
method: 'GET',
headers: {
"x-dune-api-key": duneApiKey
method: "GET",
headers: {
"x-dune-api-key": duneApiKey,
},
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
}
}
Loading

0 comments on commit 2f1b8cb

Please sign in to comment.