Skip to content

Commit

Permalink
Merge pull request #1 from MarcoMandar/web3-stuff
Browse files Browse the repository at this point in the history
wallet and prices
  • Loading branch information
lalalune authored Oct 22, 2024
2 parents a32b1c0 + a6aa182 commit 41323c6
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/providers/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
// TokenBalanceProvider.ts
import { Connection, PublicKey } from "@solana/web3.js";
import { getTokenBalances, getTokenPriceInSol } from "./tokenUtils";
import fetch from "cross-fetch";

interface Item {
name: string;
symbol: string;
decimals: number;
balance: string;
uiAmount: string;
priceUSD: string;
valueUSD: string;
}
interface walletPortfolio {
totalUsd: string;
items: Array<Item>;
}
interface price {
usd: string;
}
interface Prices {
solana: price;
bitcoin: price;
ethereum: price;

}
const API_Key = ""
export class WalletProvider {
private connection: Connection;
private walletPublicKey: PublicKey;
Expand Down Expand Up @@ -29,4 +53,50 @@ export class WalletProvider {

return formattedBalances;
}

async fetchPortfolioValue(walletPublicKey:string): Promise<walletPortfolio> {
try {
const options = {
method: 'GET',
headers: {
accept: 'application/json',
'x-chain': 'solana',
'X-API-KEY': API_Key
}
};
const walletPortfolio = await fetch(`https://public-api.birdeye.so/v1/wallet/token_list?wallet=${walletPublicKey}`,
options
);
const walletPortfolioJson = await walletPortfolio.json();
const data = walletPortfolioJson.data;
const totalUsd = data.totalUsd;
const items = data.items;
const walletPortfolioFormatted = {
totalUsd,
items
};
return walletPortfolioFormatted;
} catch (error) {
console.log(error);
}
}

async fetchPrices(): Promise<Prices> {
const apiUrl = 'https://api.coingecko.com/api/v3/simple/price';
const ids = 'solana,bitcoin,ethereum';
const vsCurrencies = 'usd';

try {
const response = await fetch(`${apiUrl}?ids=${ids}&vs_currencies=${vsCurrencies}`);

if (!response.ok) {
throw new Error('Failed to fetch prices');
}

const data = await response.json();
return data;
} catch (error) {
console.log('Error fetching prices:', error);
}
}
}

0 comments on commit 41323c6

Please sign in to comment.