Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/react-app/src/hooks/queries/useGenericQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useQuery } from "@tanstack/react-query";

export const useGenericQuery = <T>(
queryKey: string[],
queryFn: () => Promise<T>
queryFn: () => Promise<T>,
enabled: boolean = true
) => {
const {
data,
Expand All @@ -12,6 +13,7 @@ export const useGenericQuery = <T>(
} = useQuery({
queryKey: queryKey,
queryFn: queryFn,
enabled: enabled,
});

const refetch = async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/react-app/src/hooks/queries/useGetMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ export const useGetMemberTrusters = (memberId: string) => {
};

export const useGetMember = (memberId: string) => {

const queryKey = ["member", memberId];
const queryFn = () => fetchMemberData(memberId);
return useGenericQuery(queryKey, queryFn);
return useGenericQuery(queryKey, queryFn, !!memberId && memberId.length > 0);
};
30 changes: 15 additions & 15 deletions packages/react-app/src/screens/TrustAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast";

import { useWriteContract, useAccount } from "wagmi";
import { useWriteContract, useAccount, useReadContract } from "wagmi";
import ABI from "../abis/CFAv1Forwarder.json";
import { GOODDOLLAR, SF_FORWARDER, POOL_CONTRACT } from "@/env";
import {
Expand All @@ -16,29 +16,32 @@ import {
import { Loader2 } from "lucide-react";
import { useNavigate } from "react-router-dom";
import { PasteInput } from "@/components/PasteInput";
import { useGetMember } from "@/hooks/queries/useGetMember";
import { truncateAddress } from "@/utils";

// @ts-expect-error
const isMiniPay = window?.ethereum?.isMiniPay;
const gasOpts = isMiniPay ? {} : {
maxFeePerGas: BigInt(5e9),
maxPriorityFeePerGas: BigInt(0)
}
maxFeePerGas: BigInt(25.1e9),
maxPriorityFeePerGas: BigInt(1e8)
};

const useGetFlowRate = (sender: string | undefined) => {
if (!sender) return undefined;
const memberData = useGetMember(sender);
// @ts-ignore
return memberData.status === "success" ? BigInt(memberData.data?.data?.outFlowRate || 0) : undefined
const result = useReadContract({
address: SF_FORWARDER,
abi: [{ name: "getFlowrate", type: "function", stateMutability: "view", inputs: [{ name: "token", type: "address" }, { name: "sender", type: "address" }, { name: "receiver", type: "address" }], outputs: [{ name: "", type: "int96" }] }],
functionName: "getFlowrate",
args: [GOODDOLLAR as `0x${string}`, (sender || "0x0000000000000000000000000000000000000000") as `0x${string}`, POOL_CONTRACT as `0x${string}`],
query: { enabled: !!sender },
});
if (!sender || !result.data) return 0n;
return BigInt(result.data);
};

export const QrScan = () => {
const navigation = useNavigate();
const account = useAccount();

const existingFlowRate = useGetFlowRate(account.address);
console.log({ existingFlowRate });
const { writeContractAsync } = useWriteContract();

const [result, setResult] = useState<string | undefined>(undefined);
Expand All @@ -50,17 +53,15 @@ export const QrScan = () => {
const validRecipient = isAddress(result || "");

const handleScan = (result: IDetectedBarcode[]) => {
console.log(result);
if (result.length > 0) {
setResult(result[0].rawValue);
}
};

const trust = async () => {
if (existingFlowRate !== undefined && result) {
if (result) {
const monthlyTrustRate =
parseEther(amount.toString()) / BigInt(60 * 60 * 24 * 30);
console.log("Trusting:", { existingFlowRate, result, monthlyTrustRate });
const newFlowRate = existingFlowRate + monthlyTrustRate;

const userData = encodeAbiParameters(
Expand Down Expand Up @@ -90,7 +91,6 @@ export const QrScan = () => {
});
navigation("/");
} catch (e: any) {
console.log({ e })
setLoading(false);
toast({
title: "Transaction failed",
Expand Down