-
Notifications
You must be signed in to change notification settings - Fork 619
Add Solana wallet balance endpoint and UI integration #8338
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
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@thirdweb-dev/api": patch | ||
| --- | ||
|
|
||
| added solana token balances endpoint | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ import { useV5DashboardChain } from "@/hooks/chains/v5-adapter"; | |
| import { WalletProductIcon } from "@/icons/WalletProductIcon"; | ||
| import { useDashboardRouter } from "@/lib/DashboardRouter"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { fetchSolanaBalance } from "../lib/getSolanaBalance"; | ||
| import { updateDefaultProjectWallet } from "../lib/vault.client"; | ||
| import { CreateServerWallet } from "../server-wallets/components/create-server-wallet.client"; | ||
| import type { Wallet as EVMWallet } from "../server-wallets/wallet-table/types"; | ||
|
|
@@ -79,6 +80,7 @@ interface ServerWalletsTableProps { | |
| teamSlug: string; | ||
| client: ThirdwebClient; | ||
| solanaPermissionError?: boolean; | ||
| authToken: string; | ||
|
Contributor
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. Critical: Auth token exposed to browser violates security guidelines. The Recommended architecture: Create an internal API route that fetches Solana balance server-side: // app/api/solana-balance/route.ts
import "server-only";
import { getAuthToken } from "@/lib/auth";
import { fetchSolanaBalance } from "...";
export async function GET(request: Request): Promise<Response> {
const authToken = await getAuthToken();
const { searchParams } = new URL(request.url);
const publicKey = searchParams.get("publicKey");
const clientId = searchParams.get("clientId");
const chainId = searchParams.get("chainId") as "solana:mainnet" | "solana:devnet";
const balance = await fetchSolanaBalance({
publicKey,
authToken,
clientId,
chainId
});
return Response.json(balance);
}Then update the client component to call the internal route: function SolanaWalletBalance({
publicKey,
- authToken,
clientId,
chainId,
}: {
publicKey: string;
- authToken: string;
clientId: string;
chainId: "solana:mainnet" | "solana:devnet";
}) {
const balance = useQuery({
queryFn: async () => {
- return await fetchSolanaBalance({
- publicKey,
- authToken,
- clientId,
- chainId,
- });
+ const params = new URLSearchParams({ publicKey, clientId, chainId });
+ const response = await fetch(`/api/solana-balance?${params}`);
+ if (!response.ok) throw new Error("Failed to fetch balance");
+ return response.json();
},
queryKey: ["solanaWalletBalance", publicKey, chainId],
});As per coding guidelines. Also applies to: 107-107, 316-317, 547-548, 554-555, 591-596, 801-806 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| export function ServerWalletsTable(props: ServerWalletsTableProps) { | ||
|
|
@@ -95,6 +97,7 @@ export function ServerWalletsTable(props: ServerWalletsTableProps) { | |
| solanaTotalPages, | ||
| client, | ||
| solanaPermissionError, | ||
| authToken, | ||
| } = props; | ||
|
|
||
| const [activeChain, setActiveChain] = useState<WalletChain>("evm"); | ||
|
|
@@ -278,6 +281,7 @@ export function ServerWalletsTable(props: ServerWalletsTableProps) { | |
| project={project} | ||
| teamSlug={teamSlug} | ||
| client={client} | ||
| authToken={authToken} | ||
| /> | ||
| ))} | ||
| </TableBody> | ||
|
|
@@ -507,11 +511,13 @@ function SolanaWalletRow({ | |
| project, | ||
| teamSlug, | ||
| client, | ||
| authToken, | ||
| }: { | ||
| wallet: SolanaWallet; | ||
| project: Project; | ||
| teamSlug: string; | ||
| client: ThirdwebClient; | ||
| authToken: string; | ||
| }) { | ||
| const engineService = project.services.find( | ||
| (s) => s.name === "engineCloud", | ||
|
|
@@ -547,7 +553,11 @@ function SolanaWalletRow({ | |
| </TableCell> | ||
|
|
||
| <TableCell> | ||
| <SolanaWalletBalance publicKey={wallet.publicKey} /> | ||
| <SolanaWalletBalance | ||
| publicKey={wallet.publicKey} | ||
| authToken={authToken} | ||
| clientId={project.publishableKey} | ||
| /> | ||
| </TableCell> | ||
|
|
||
| <TableCell> | ||
|
|
@@ -739,14 +749,22 @@ function WalletBalance({ | |
| ); | ||
| } | ||
|
|
||
| function SolanaWalletBalance({ publicKey }: { publicKey: string }) { | ||
| function SolanaWalletBalance({ | ||
| publicKey, | ||
| authToken, | ||
| clientId, | ||
| }: { | ||
| publicKey: string; | ||
| authToken: string; | ||
| clientId: string; | ||
| }) { | ||
| const balance = useQuery({ | ||
| queryFn: async () => { | ||
| // TODO: Implement actual Solana balance fetching | ||
| return { | ||
| displayValue: "0", | ||
| symbol: "SOL", | ||
| }; | ||
| return await fetchSolanaBalance({ | ||
| publicKey, | ||
| authToken, | ||
| clientId, | ||
| }); | ||
| }, | ||
| queryKey: ["solanaWalletBalance", publicKey], | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { configure, getSolanaWalletBalance } from "@thirdweb-dev/api"; | ||
| import { THIRDWEB_API_HOST } from "@/constants/urls"; | ||
|
|
||
| // Configure the API client to use the correct base URL | ||
| configure({ | ||
| override: { | ||
| baseUrl: THIRDWEB_API_HOST, | ||
| }, | ||
| }); | ||
0xFirekeeper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export async function fetchSolanaBalance({ | ||
| publicKey, | ||
| authToken, | ||
| clientId, | ||
| }: { | ||
| publicKey: string; | ||
| authToken: string; | ||
| clientId: string; | ||
| }): Promise<{ | ||
| displayValue: string; | ||
| symbol: string; | ||
| value: string; | ||
| decimals: number; | ||
| } | null> { | ||
| try { | ||
| const response = await getSolanaWalletBalance({ | ||
| path: { | ||
| address: publicKey, | ||
| }, | ||
| query: { | ||
| chainId: "solana:mainnet", | ||
| }, | ||
| headers: { | ||
| Authorization: `Bearer ${authToken}`, | ||
| "Content-Type": "application/json", | ||
| "x-client-id": clientId, | ||
| }, | ||
| }); | ||
|
|
||
| if (response.error || !response.data) { | ||
| console.error( | ||
| "Error fetching Solana balance:", | ||
| response.error || "No data returned", | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| displayValue: response.data.result.displayValue, | ||
| symbol: "SOL", | ||
| value: response.data.result.value, | ||
| decimals: response.data.result.decimals, | ||
| }; | ||
| } catch (error) { | ||
| console.error("Error fetching Solana balance:", error); | ||
| return null; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.