From 2eee87b29130a0d760f24de74d0fbd53197365dc Mon Sep 17 00:00:00 2001 From: pociej Date: Wed, 5 Jun 2024 06:37:44 +0200 Subject: [PATCH] fix: use placholder instead of 0 #62 --- backend/src/services/yagna/service.ts | 8 ++--- .../homePage/statusSections/allocation.tsx | 6 ---- .../homePage/statusSections/approve.tsx | 2 +- .../src/components/providers/userProvider.tsx | 4 +-- frontend/src/hooks/GLM/useAllowanceTx.tsx | 29 +++---------------- .../src/hooks/depositContract/useDeposit.ts | 16 ++++++++-- frontend/src/hooks/useCurrentAllocation.ts | 28 ++++++++++++------ frontend/src/types/zero.ts | 1 + frontend/src/utils/formatBalance.ts | 5 +++- 9 files changed, 46 insertions(+), 53 deletions(-) create mode 100644 frontend/src/types/zero.ts diff --git a/backend/src/services/yagna/service.ts b/backend/src/services/yagna/service.ts index 38b2999..7dcb62d 100644 --- a/backend/src/services/yagna/service.ts +++ b/backend/src/services/yagna/service.ts @@ -36,10 +36,8 @@ export class Yagna { getExecutor: async (userId: string) => { const existingExecutor = this.userContext.data.get(userId)?.executor; if (existingExecutor) { - console.log("existingExecutor", existingExecutor); return existingExecutor; } - console.log("creating executor"); return await this.createExecutor(userId); }, setWorker: async (userId: string, worker: Worker) => { @@ -84,10 +82,8 @@ export class Yagna { async makeAgreement(userId: string) { const executor = await this.userContext.getExecutor(userId); - console.log("executor", executor.agreementPoolService.isServiceRunning); - // //@ts-ignore - // console.log("executor", executor.agreementPoolService.isServiceRunning); - // return await executor.getAgreement(); + + const agreement = executor.getAgreement(); } async createUserAllocation(userId: string) { diff --git a/frontend/src/components/homePage/statusSections/allocation.tsx b/frontend/src/components/homePage/statusSections/allocation.tsx index 9613ae9..449922a 100644 --- a/frontend/src/components/homePage/statusSections/allocation.tsx +++ b/frontend/src/components/homePage/statusSections/allocation.tsx @@ -1,15 +1,11 @@ import { AllocationLink } from "components/alloctionLink"; import { GLMAmountStat } from "components/atoms/GLMAmount"; -import { useCreateAllocationEvents } from "hooks/events/useCreateAllocationEvents"; -import { useReleaseAllocationEvents } from "hooks/events/useReleaseAllocationEvents"; import { useCreateAllocation } from "hooks/useCreateAllocation"; import { useCurrentAllocation } from "hooks/useCurrentAllocation"; import { useReleaseAllocation } from "hooks/useReleaseAllocation"; import { useUser } from "hooks/useUser"; import { Loading } from "react-daisyui"; -import { Event } from "types/events"; import { formatBalance } from "utils/formatBalance"; -import { parseEther } from "viem"; export const Allocation = () => { const { isCreating: isCreatingAllocation, createAllocation } = @@ -18,8 +14,6 @@ export const Allocation = () => { const { releaseAllocation } = useReleaseAllocation(); const { user } = useUser(); - console.log("currentAllocation", currentAllocation); - return (
{
Given
- +
{txHash && ( diff --git a/frontend/src/components/providers/userProvider.tsx b/frontend/src/components/providers/userProvider.tsx index 3e9aac5..165ae00 100644 --- a/frontend/src/components/providers/userProvider.tsx +++ b/frontend/src/components/providers/userProvider.tsx @@ -198,11 +198,11 @@ export const UserProvider = ({ children }: PropsWithChildren<{}>) => { }, [isLoggingIn]); useEffect(() => { - if (tokens?.accessToken) { + if (address && tokens?.accessToken) { dispatch({ kind: UserAction.REGISTER }); setIsRegistered(true); } - }, [tokens]); + }, [tokens, address]); useEffect(() => { const currentDeposit = (userData?.deposits || []).find( diff --git a/frontend/src/hooks/GLM/useAllowanceTx.tsx b/frontend/src/hooks/GLM/useAllowanceTx.tsx index 12df011..84cff98 100644 --- a/frontend/src/hooks/GLM/useAllowanceTx.tsx +++ b/frontend/src/hooks/GLM/useAllowanceTx.tsx @@ -1,11 +1,12 @@ import { useEffect, useMemo, useState } from "react"; import { useAccount, useWatchContractEvent } from "wagmi"; -import { createPublicClient, http } from "viem"; +import { createPublicClient, getAddress, http } from "viem"; import { holesky } from "viem/chains"; import { abi } from "./abi"; import { config } from "config"; import { useRequestorWalletAddress } from "hooks/useRequestorWalletAddress"; +import { ZERO_ADDRESS } from "types/zero"; type WithBlockNumber = T & { blockNumber: bigint }; @@ -22,38 +23,16 @@ export const useAllowanceTx = () => { [] ); - useEffect(() => { - client.watchContractEvent({ - address: config.GLMContractAddress[holesky.id], - abi: abi, - eventName: "Approval", - args: { - owner: address, - spender: requestor?.wallet, - }, - onLogs: (logs) => { - const newApprove = logs.sort( - (a, b) => Number(a.blockNumber) - Number(b.blockNumber) - )[logs.length - 1]; - - if (newApprove.transactionHash) { - setTxHash(newApprove.transactionHash); - } - }, - }); - }, [address, requestor?.wallet]); useEffect(() => { const fetchLogs = async () => { const block = await client.getBlock(); - // if (!address) { - // return []; - // } const logs = await client.getContractEvents({ address: config.GLMContractAddress[holesky.id], abi: abi, eventName: "Approval", args: { - owner: address, + //with undefined address it will return all logs + owner: address || ZERO_ADDRESS, spender: requestor?.wallet, }, fromBlock: block.number - 1000n, diff --git a/frontend/src/hooks/depositContract/useDeposit.ts b/frontend/src/hooks/depositContract/useDeposit.ts index 9c20e41..64af0cb 100644 --- a/frontend/src/hooks/depositContract/useDeposit.ts +++ b/frontend/src/hooks/depositContract/useDeposit.ts @@ -19,6 +19,7 @@ import { Event } from "types/events"; import { useDepositCreatedEvents } from "hooks/events/useDepositCreatedEvents"; import { useDepositExtendedEvents } from "hooks/events/useDepositExtendedEvents"; import { use } from "i18next"; +import { ZERO_ADDRESS } from "types/zero"; export function useCreateDeposit() { const { @@ -140,10 +141,19 @@ export function useUserCurrentDeposit() { } }, [isSuccess, data]); + console.log("deposit data", data); return { - amount: data ? data[1] : 0n, - flatFeeAmount: data ? data[2] : 0n, - validToTimestamp: data ? data[3] : 0n, + ...(data?.[0] === ZERO_ADDRESS + ? { + amount: undefined, + flatFeeAmount: undefined, + validToTimestamp: undefined, + } + : { + amount: data?.[1], + flatFeeAmount: data?.[2], + validToTimestamp: data?.[3], + }), isError, isSuccess, isPending, diff --git a/frontend/src/hooks/useCurrentAllocation.ts b/frontend/src/hooks/useCurrentAllocation.ts index efc45ee..70d1a6c 100644 --- a/frontend/src/hooks/useCurrentAllocation.ts +++ b/frontend/src/hooks/useCurrentAllocation.ts @@ -18,13 +18,23 @@ export const useCurrentAllocation = () => { } ); - return { - currentAllocation: { - totalAmount: parseEther(data?.totalAmount || "0"), - spentAmount: parseEther(data?.spentAmount || "0"), - remainingAmount: parseEther(data?.remainingAmount || "0"), - }, - isLoading: !error && !data, - isError: error, - }; + return data + ? { + currentAllocation: { + totalAmount: parseEther(data?.totalAmount || "0"), + spentAmount: parseEther(data?.spentAmount || "0"), + remainingAmount: parseEther(data?.remainingAmount || "0"), + }, + isLoading: !error && !data, + isError: error, + } + : { + currentAllocation: { + totalAmount: undefined, + spentAmount: undefined, + remainingAmount: undefined, + }, + isLoading: !error && !data, + isError: error, + }; }; diff --git a/frontend/src/types/zero.ts b/frontend/src/types/zero.ts new file mode 100644 index 0000000..6cfbec2 --- /dev/null +++ b/frontend/src/types/zero.ts @@ -0,0 +1 @@ +export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; diff --git a/frontend/src/utils/formatBalance.ts b/frontend/src/utils/formatBalance.ts index 0b45355..f40e49b 100644 --- a/frontend/src/utils/formatBalance.ts +++ b/frontend/src/utils/formatBalance.ts @@ -1,5 +1,8 @@ import { formatEther } from "viem"; -export const formatBalance = (rawBalance: bigint | number) => { +export const formatBalance = (rawBalance?: bigint | number) => { + if (rawBalance === undefined || rawBalance === null) { + return ""; + } return rawBalance !== undefined && rawBalance !== null ? parseFloat(formatEther(BigInt(rawBalance))).toFixed(2) : "";