-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5abcb5d
commit 40ac047
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const calculateTotalTokens = (pricedAssets: any[]) => { | ||
if (Array.isArray(pricedAssets) && pricedAssets.length > 0) { | ||
return pricedAssets.reduce((total: number, asset: any) => { | ||
const { token_info } = asset; | ||
const { price_info } = token_info || {}; | ||
const { total_price } = price_info || {}; | ||
|
||
return total + (total_price || 0); | ||
}, 0); | ||
} | ||
return 0; | ||
}; | ||
|
||
export default calculateTotalTokens; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import calculateTotalTokens from "./calculate-total-tokens"; | ||
const url = import.meta.env.VITE_HELIUS_URL; | ||
|
||
export const searchAssets = async (account: string) => { | ||
try { | ||
const response = await fetch(url, { | ||
body: JSON.stringify({ | ||
id: "my-id", | ||
jsonrpc: "2.0", | ||
method: "searchAssets", | ||
params: { | ||
displayOptions: { | ||
showNativeBalance: true, | ||
}, | ||
ownerAddress: account, | ||
tokenType: "fungible", | ||
}, | ||
}), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
}); | ||
const { result } = await response.json(); | ||
|
||
if (result && result.items && Array.isArray(result.items)) { | ||
const pricedAssets = result.items.filter( | ||
(asset: any) => asset.token_info && asset.token_info.price_info | ||
); | ||
|
||
const tokenPrices = calculateTotalTokens(pricedAssets); | ||
|
||
return { pricedAssets, tokenPrices }; | ||
} | ||
} catch (error) { | ||
return null; | ||
} | ||
}; |