Skip to content

Commit

Permalink
fix: use placholder instead of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed Jun 5, 2024
1 parent 7bb2efa commit 2eee87b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 53 deletions.
8 changes: 2 additions & 6 deletions backend/src/services/yagna/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 } =
Expand All @@ -18,8 +14,6 @@ export const Allocation = () => {
const { releaseAllocation } = useReleaseAllocation();
const { user } = useUser();

console.log("currentAllocation", currentAllocation);

return (
<div className="stats shadow mt-2 ">
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const Approve = () => {
</div>
<div className="stat">
<div className="stat-title">Given</div>
<GLMAmountStat amount={formatBalance(user.allowanceAmount || 0n)} />
<GLMAmountStat amount={formatBalance(user.allowanceAmount)} />
</div>
<div className="stat ">
{txHash && (
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/providers/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 4 additions & 25 deletions frontend/src/hooks/GLM/useAllowanceTx.tsx
Original file line number Diff line number Diff line change
@@ -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> = T & { blockNumber: bigint };

Expand All @@ -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,
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/hooks/depositContract/useDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 19 additions & 9 deletions frontend/src/hooks/useCurrentAllocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
1 change: 1 addition & 0 deletions frontend/src/types/zero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
5 changes: 4 additions & 1 deletion frontend/src/utils/formatBalance.ts
Original file line number Diff line number Diff line change
@@ -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)
: "";
Expand Down

0 comments on commit 2eee87b

Please sign in to comment.