-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
] | ||
} |
This file was deleted.
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, | ||
}); | ||
} |
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"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
export type TokenBalancesParams = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect, useState } from "react"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.