Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovespectra authored Dec 7, 2023
1 parent 5abcb5d commit 40ac047
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/util/calculate-total-tokens.ts
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;
38 changes: 38 additions & 0 deletions src/lib/util/get-assets-with-native-balances.ts
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;
}
};

0 comments on commit 40ac047

Please sign in to comment.