Skip to content
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

flows: refresh balance after successful tx #235

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion anchor/src/react/glam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ interface GlamProviderContext {
fund?: PublicKey;
treasury?: FundCacheTreasury;
fundsList: FundCache[];
//@ts-ignore
allFunds: FundModel[];
walletBalances: any;
walletBalancesQueryKey: any[];
refresh?: () => void;
setActiveFund: any;
tokenList?: TokenListItem[];
Expand Down Expand Up @@ -175,10 +177,13 @@ export function GlamProvider({
);
}, [allFundsData]);

const walletBalancesQueryKey = ["balances", wallet?.publicKey];
const { data: walletBalances } = useQuery({
queryKey: ["balances", wallet?.publicKey],
queryKey: walletBalancesQueryKey,
enabled: !!wallet?.publicKey,
// refetchInterval: 2000, // 2s
queryFn: async () => {
console.log("fetching walletBalances");
const balanceLamports = await glamClient.provider.connection.getBalance(
wallet?.publicKey || new PublicKey(0)
);
Expand Down Expand Up @@ -228,6 +233,7 @@ export function GlamProvider({
fundsList: useAtomValue(fundsListAtom),
allFunds,
walletBalances,
walletBalancesQueryKey,
tokenList,
setActiveFund,
};
Expand Down
8 changes: 6 additions & 2 deletions playground/src/app/flows/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useMemo } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm, SubmitHandler, FormProvider } from "react-hook-form";
import { z } from "zod";
import { useQuery } from "@tanstack/react-query";
import { useQuery, useQueryClient } from "@tanstack/react-query";

import { Button } from "@/components/ui/button";
import {
Expand Down Expand Up @@ -372,7 +372,10 @@ function InvestorDisclaimers({
}

function InvestorWidget({ fundId }: { fundId: string }) {
const { glamClient, allFunds, walletBalances } = useGlam();
const { glamClient, allFunds, walletBalances, walletBalancesQueryKey } =
useGlam();
const queryClient = useQueryClient();

const fund: any = fundId
? (allFunds || []).find((f: any) => f.idStr === fundId)
: undefined;
Expand Down Expand Up @@ -508,6 +511,7 @@ function InvestorWidget({ fundId }: { fundId: string }) {
txId = await glamClient.investor.subscribe(fund.id, asset, amount);
}

queryClient.invalidateQueries({ queryKey: walletBalancesQueryKey });
toast({
title: `Successful ${direction}`,
description: <ExplorerLink path={`tx/${txId}`} label={txId} />,
Expand Down
6 changes: 4 additions & 2 deletions playground/src/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const AssetInput: React.FC<AssetInputProps> = ({
if (!isNaN(numericValue)) {
setValue(name, numericValue);
} else {
setValue(name, 0);
setValue(name, "");
}
}
};
Expand Down Expand Up @@ -145,10 +145,12 @@ export const AssetInput: React.FC<AssetInputProps> = ({
<div className="relative">
<Input
{...field}
type="number"
step="any"
ref={inputRef}
value={getValues()[name]}
className="pr-20"
placeholder="0"
placeholder=""
onChange={(e) => handleInputChange(e.target.value)}
disabled={disableAmountInput}
/>
Expand Down