Skip to content

Commit

Permalink
feat: handle deposit extension event
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed Jun 4, 2024
1 parent a394214 commit 7bb2efa
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 24 deletions.
42 changes: 41 additions & 1 deletion frontend/src/components/homePage/events/event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,45 @@ const DepositCreatedEvent = (event: {
</div>
<div className="flex gap-2">
<div className="stat-title"> Valid to: </div>
{dayjs(event.payload.validityTimestamp).format("YYYY-MM-DD HH:mm")}
{dayjs(event.payload.validityTimestamp * 1000).format("YYYY-MM-DD")}
</div>
</div>
</Card.Body>
</Card>
);
};

const DepositExtendedEvent = (event: {
kind: Event.DEPOSIT_EXTENDED;
payload: Payload[Event.DEPOSIT_EXTENDED];
}) => {
return (
<Card bordered={true}>
<Card.Body>
<Card.Title>Deposit Extended</Card.Title>
<div>
<div className="flex gap-2">
<div className="stat-title"> TX Hash: </div>

<EtherScanLink hash={event.payload.txHash}></EtherScanLink>
</div>
<div className="flex gap-2">
<div className="stat-title"> Extra Amount: </div>
<GLMAmountStat
amount={formatBalance(
parseEther(event.payload.amount.toString())
)}
></GLMAmountStat>{" "}
</div>
<div className="flex gap-2">
<div className="stat-title"> Extra Fee: </div>
<GLMAmountStat
amount={formatBalance(parseEther(event.payload.fee.toString()))}
></GLMAmountStat>{" "}
</div>
<div className="flex gap-2">
<div className="stat-title"> New Valid to: </div>
{dayjs(event.payload.validityTimestamp * 1000).format("YYYY-MM-DD")}
</div>
</div>
</Card.Body>
Expand All @@ -96,6 +134,8 @@ export const EventCard = (event: EventType) => {
return <AllocationReleasedEvent {...event} />;
case Event.DEPOSIT_CREATED:
return <DepositCreatedEvent {...event} />;
case Event.DEPOSIT_EXTENDED:
return <DepositExtendedEvent {...event} />;
}
})()}
</motion.div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import dayjs from "dayjs";
import { useCreateDeposit } from "hooks/depositContract/useDeposit";
import { useSaveDeposit } from "hooks/useSaveDeposit";

import { useEffect, useState } from "react";
import { useAccount, useWaitForTransactionReceipt } from "wagmi";
import { useEffect } from "react";
import { useAccount } from "wagmi";
import { UpsertDepositPresentational } from "./upsertDepositPresentational";
import debug from "debug";
import { useLayout } from "components/providers/layoutProvider";
const log = debug("CreateDeposit");

export const CreateDeposit = () => {
const { hideModal } = useLayout();

const {
createDeposit,
setFee,
Expand Down Expand Up @@ -43,6 +45,8 @@ export const CreateDeposit = () => {
id: depositId,
funder: address,
});

hideModal();
}, 1000);
}
}, [isSuccess, isError, nonce, depositId]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import { useUser } from "hooks/useUser";
import { useEffect } from "react";
import { useWaitForTransactionReceipt } from "wagmi";
import { UpsertDepositPresentational } from "./upsertDepositPresentational";
import { useLayout } from "components/providers/layoutProvider";

export const ExtendDeposit = () => {
const { hideModal } = useLayout();

const {
additionalAmount,
data,
Expand Down Expand Up @@ -44,17 +47,10 @@ export const ExtendDeposit = () => {
}
if (isSuccessTransaction && additionalAmount > 0) {
topUp(additionalAmount);
hideModal();
}
}, [isSuccessTransaction]);

useEffect(() => {
console.log("newValidToTimeStamp", newValidToTimeStamp);
console.log(
"dd",
dayjs(Number(newValidToTimeStamp) * 1000).format("YYYY-MM-DD")
);
}, [newValidToTimeStamp]);

return (
<UpsertDepositPresentational
amount={additionalAmount}
Expand Down
34 changes: 27 additions & 7 deletions frontend/src/hooks/depositContract/useDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import { useEffect, useRef, useState } from "react";
import { config } from "config";
import { useChainId } from "hooks/useChainId";
import { useUser } from "hooks/useUser";
import { formatEther, parseEther } from "viem";
import { formatEther, parseEther, toHex } from "viem";
import { useRequestorWalletAddress } from "hooks/useRequestorWalletAddress";
import { useHandleRpcError } from "hooks/useHandleRpcError";
import dayjs from "dayjs";
import { useEvents } from "hooks/events/useEvents";
import { Event } from "types/events";
import { useDepositCreatedEvents } from "hooks/events/useDepositCreatedEvents";
import { useDepositExtendedEvents } from "hooks/events/useDepositExtendedEvents";
import { use } from "i18next";

export function useCreateDeposit() {
const {
Expand Down Expand Up @@ -45,14 +48,10 @@ export function useCreateDeposit() {
hash: data,
});

const { emit } = useEvents({
eventKind: Event.DEPOSIT_CREATED,
key: "depositCreatedEvents",
});
const { emit } = useDepositCreatedEvents();

useEffect(() => {
if (isSuccess && data) {
console.log("emit", data);
emit({
txHash: data,
amount: amount,
Expand Down Expand Up @@ -152,10 +151,31 @@ export function useUserCurrentDeposit() {
}

export function useExtendDeposit() {
const { emit } = useDepositExtendedEvents();
const { showNotification, errorContext } = useHandleRpcError();

const { data, isError, isSuccess, writeContractAsync, isPending } =
useWriteContract();

const {
isSuccess: isSuccessTransaction,
isError: isErrorTransaction,
isLoading: isLoadingTransaction,
} = useWaitForTransactionReceipt({
hash: data,
});

useEffect(() => {
if (isSuccessTransaction && data) {
emit({
txHash: data,
amount: additionalAmount,
fee: additionalFee,
validityTimestamp: validToTimestamp,
});
}
}, [isSuccessTransaction]);

const chainId = useChainId();
const [validToTimestamp, setNewValidToTimestamp] = useState(
dayjs().add(1, "day").unix()
Expand Down Expand Up @@ -183,7 +203,7 @@ export function useExtendDeposit() {
},
data,
isError,
isSuccess,
isSuccess: isSuccessTransaction,
isPending,
newValidToTimeStamp: validToTimestamp,
setNewValidToTimestamp,
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/hooks/events/useDepositEvents.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { merge } from "rxjs";
import { useDepositReleasedEvents } from "./useDepositReleasedEvents";
import { useDepositCreatedEvents } from "./useDepositCreatedEvents";
import { useDepositExtendedEvents } from "./useDepositExtendedEvents";

export const useDepositEvents = () => {
const { events$: depositCreatedEvents$ } = useDepositCreatedEvents();
const { events$: depositReleasedEvents$ } = useDepositReleasedEvents();

const { events$: depositExtendedEvents$ } = useDepositExtendedEvents();
return {
events$: merge(depositReleasedEvents$, depositCreatedEvents$),
events$: merge(
depositReleasedEvents$,
depositCreatedEvents$,
depositExtendedEvents$
),
};
};
8 changes: 8 additions & 0 deletions frontend/src/hooks/events/useDepositExtendedEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Event } from "types/events";
import { useEvents } from "./useEvents";
export const useDepositExtendedEvents = () => {
return useEvents({
eventKind: Event.DEPOSIT_EXTENDED,
key: "depositExtendedEvents",
});
};
15 changes: 12 additions & 3 deletions frontend/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum Event {
ALLOCATION_RELEASED = "ALLOCATION_RELEASED",
PAYMENT_FOR_GAS = "PAYMENT_FOR_GAS",
DEPOSIT_RELEASED = "DEPOSIT_RELEASED",
DEPOSIT_EXTENDED = "DEPOSIT_EXTENDED",
}

export type Payload = {
Expand All @@ -21,6 +22,17 @@ export type Payload = {
fee: number;
validityTimestamp: number;
};

[Event.DEPOSIT_RELEASED]: {
agreementId: string;
};

[Event.DEPOSIT_EXTENDED]: {
txHash: `0x${string}`;
amount: number;
fee: number;
validityTimestamp: number;
};
[Event.ALLOCATION_CREATED]: {
allocationId: string;
amount: number;
Expand Down Expand Up @@ -53,9 +65,6 @@ export type Payload = {
[Event.PAYMENT_FOR_GAS]: {
agreementId: string;
};
[Event.DEPOSIT_RELEASED]: {
agreementId: string;
};
};

export type EventType = EventWithPayload<Payload>;
Expand Down

0 comments on commit 7bb2efa

Please sign in to comment.