33import assert from "node:assert" ;
44import type { TokenDetails } from "../hooks/useGetERC20Tokens" ;
55
6+ // "native_tokens": [
7+ // {
8+ // "token_id": "ethereum.native",
9+ // "name": "Ether",
10+ // "symbol": "ETH",
11+ // "decimals": 18,
12+ // "chain": "ethereum",
13+ // "total_quantity": 1527391387004726000,
14+ // "total_quantity_string": "1527391387004725927",
15+ // "total_value_usd_cents": 477180,
16+ // "queried_wallet_balances": [
17+ // {
18+ // "address": "0xa5B8492D8223D255dB279C7c3ebdA34Be5eC9D85",
19+ // "quantity": 1527391387004726000,
20+ // "quantity_string": "1527391387004725927",
21+ // "value_usd_cents": 477180
22+ // }
23+ // ]
24+ // }
25+ // ]
26+
27+ interface QueriedWalletBalance {
28+ address : string ;
29+ quantity_string : string ;
30+ value_usd_cents : number ;
31+ first_transferred_date : string ;
32+ last_transferred_date : string ;
33+ }
34+
35+ interface FungibleToken {
36+ fungible_id : string ;
37+ name : string ;
38+ symbol : string ;
39+ decimals : number ;
40+ total_quantity_string : string ;
41+ total_value_usd_cents : number ;
42+ queried_wallet_balances : QueriedWalletBalance [ ] ;
43+ }
44+
45+ interface NativeToken {
46+ token_id : string ;
47+ name : string ;
48+ symbol : string ;
49+ decimals : number ;
50+ total_quantity_string : string ;
51+ total_value_usd_cents : number ;
52+ queried_wallet_balances : QueriedWalletBalance [ ] ;
53+ }
54+
655interface SimpleHashResponse {
7- fungibles : {
8- name : string ;
9- symbol : string ;
10- decimals : number ;
11- total_quantity_string : string ;
12- total_value_usd_string : string ;
13- } [ ] ;
56+ fungibles : FungibleToken [ ] ;
57+ native_tokens : NativeToken [ ] ;
1458 next_cursor : string | null ;
1559}
1660
@@ -24,7 +68,7 @@ export async function fetchERC20Tokens(args: {
2468 const { chainId, address } = args ;
2569
2670 const response = await fetch (
27- `https://api.simplehash.com/api/v0/fungibles/balances?chains=eip155:${ chainId } &wallet_addresses=${ address } &include_fungible_details=1 &include_prices=1&count=0&limit=50&order_by=last_transferred_date__desc&include_native_tokens=0 ` ,
71+ `https://api.simplehash.com/api/v0/fungibles/balances?chains=eip155:${ chainId } &wallet_addresses=${ address } &include_fungible_details=0 &include_prices=1&count=0&limit=50&order_by=last_transferred_date__desc&include_native_tokens=1 ` ,
2872 {
2973 headers : { "X-API-KEY" : SIMPLEHASH_API_KEY } ,
3074 } ,
@@ -36,13 +80,31 @@ export async function fetchERC20Tokens(args: {
3680 }
3781
3882 const data : SimpleHashResponse = await response . json ( ) ;
39- return data . fungibles . map ( ( token ) => ( {
83+ const nativeTokens = data . native_tokens . map ( ( token ) => ( {
84+ name : token . name ,
85+ symbol : token . symbol ,
86+ contractAddress : "Native" ,
87+ decimals : token . decimals ,
88+ balance : BigInt ( token . total_quantity_string ?? 0n ) ,
89+ totalValueUsdCents : token . total_value_usd_cents ,
90+ firstTransferredDate :
91+ token . queried_wallet_balances [ 0 ] ?. first_transferred_date ,
92+ lastTransferredDate :
93+ token . queried_wallet_balances [ 0 ] ?. last_transferred_date ,
94+ } ) ) ;
95+ const fungibleTokens = data . fungibles . map ( ( token ) => ( {
4096 name : token . name ,
4197 symbol : token . symbol ,
98+ contractAddress : token . fungible_id . split ( "." ) [ 1 ] ?? "--" ,
4299 decimals : token . decimals ,
43- balance : token . total_quantity_string ,
44- totalValueUsdString : token . total_value_usd_string ,
100+ balance : BigInt ( token . total_quantity_string ?? 0n ) ,
101+ totalValueUsdCents : token . total_value_usd_cents ,
102+ firstTransferredDate :
103+ token . queried_wallet_balances [ 0 ] ?. first_transferred_date ,
104+ lastTransferredDate :
105+ token . queried_wallet_balances [ 0 ] ?. last_transferred_date ,
45106 } ) ) ;
107+ return [ ...nativeTokens , ...fungibleTokens ] ;
46108 } catch ( error ) {
47109 console . error ( "Error fetching tokens:" , error ) ;
48110 return [ ] ;
0 commit comments