Skip to content

Commit

Permalink
fix: agreement flow
Browse files Browse the repository at this point in the history
close #81
close #82
close #85
  • Loading branch information
pociej committed Jun 20, 2024
1 parent ca4d354 commit 667af8f
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 56 deletions.
51 changes: 29 additions & 22 deletions backend/src/services/yagna/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ export class Yagna {
});
}
container.cradle.userService.setCurrentAgreementId(userId, agreement.id);
agreement.events.on("terminated", (e: any) => {
this.agreementEvents.next({
agreement,
event: "terminated",
});
});
// agreement.events.on("terminated", (e: any) => {
// console.log("agreement terminated");
// this.agreementEvents.next({
// agreement,
// event: "terminated",
// });
// });
}

async getUserAgreement(userId: string) {
Expand Down Expand Up @@ -337,20 +338,24 @@ export class Yagna {
"creating worker, executor found getting agreement",
userId
);
executor
.run(async (ctx: WorkContext) => {
debugLog("yagna", "created worker", userId);
newWorker.context = ctx;

newWorker.setState("free");

resolve(newWorker);
})
.catch((e: any) => {
console.log("Error", e);
debugLog("yagna", "error creating worker", e);
reject(e);
});
try {
executor
.run(async (ctx: WorkContext) => {
debugLog("yagna", "created worker", userId);
newWorker.context = ctx;

newWorker.setState("free");

resolve(newWorker);
})
.catch((e: any) => {
console.log("Error", e);
debugLog("yagna", "error creating worker", e);
reject(e);
});
} catch (e) {
console.log("Error", e);
}
}
});
}
Expand All @@ -377,9 +382,10 @@ export class Yagna {
});

debitNoteEvents.forEach(async (event: any) => {
console.log("debit note event", event);
console.log("debit note event");
const debitNoteId = event.debitNoteId;
const debitNote = await this.paymentService.getDebitNote(debitNoteId);
console.log("debit note");
this.debitNoteEvents.next({
debitNote,
event,
Expand Down Expand Up @@ -410,6 +416,7 @@ export class Yagna {
console.log("invoice event", event);
const invoiceId = event.invoiceId;
const invoice = await this.paymentService.getInvoice(invoiceId);
console.log("invoice");
this.invoiceEvents.next({ invoice, event, id: uuidv4() });
});
}
Expand Down Expand Up @@ -439,7 +446,7 @@ export class Yagna {
if (event.eventType === "AgreementTerminatedEvent") {
container.cradle.userService.onAgreementTerminated(agreementId);
}

console.log("agreement event");
this.agreementEvents.next({
id: uuidv4(),
agreement,
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/components/homePage/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { useDepositPaymentEvents } from "hooks/events/usePaymentEvents";
import { useFlowEvents } from "components/providers/flowEventsProvider";
import { init, set } from "ramda";
import { AnimatePresence, Variants, motion } from "framer-motion";
console.log("Action", copy);
export const Action = () => {
const { user } = useUser();
//TODO : this logic should be move outside of this component
Expand Down Expand Up @@ -48,13 +47,9 @@ export const Action = () => {
}
}, [user.state]);

useEffect(() => {
console.log("user.state", state);
}, [state]);

const { events$: scanResults$ } = useScanResults();
const { events$: yagnaEvents$ } = useYagnaEvents();
const { events$: flowEvents$ } = useFlowEvents();
const { events$: flowEvents$, restartSession } = useFlowEvents();
const { events$: paymentEvents$ } = useDepositPaymentEvents();
useEffect(() => {
const scanResultSub = scanResults$.subscribe((event) => {
Expand All @@ -80,6 +75,9 @@ export const Action = () => {
if (event === "releaseAgreement") {
setState("WAITING_FOR_PROVIDER_PAYMENT");
}
if (event === "restartSession") {
setState(user.state);
}
});

const paymentsSub = paymentEvents$.subscribe((event) => {
Expand All @@ -92,7 +90,6 @@ export const Action = () => {
});
}
if (event.kind === Event.DEPOSIT_FEE_PAYMENT) {
console.log("DEPOSIT_FEE_PAYMENT");
setState("DEPOSIT_RELEASED");
}
});
Expand Down Expand Up @@ -143,7 +140,8 @@ export const Action = () => {
<button
className="btn"
onClick={() => {
flowEvents$.complete();
restartSession();
// flowEvents$.complete();
}}
>
Restart session
Expand Down
20 changes: 10 additions & 10 deletions frontend/src/components/homePage/events/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventCard } from "./event";
import { uniqBy, sortBy, reverse, prop } from "ramda";
import { useDepositEvents } from "hooks/events/useDepositEvents";
import { useYagnaEvents } from "hooks/events/useYagnaEvents";
import { finalize, merge } from "rxjs";
import { filter, finalize, merge } from "rxjs";
import { useDepositPaymentEvents } from "hooks/events/usePaymentEvents";
import { useScanResults } from "hooks/useScanResults";
import { useFlowEvents } from "components/providers/flowEventsProvider";
Expand Down Expand Up @@ -33,16 +33,16 @@ export const Events = () => {
useEffect(() => {
flowEvents$
.pipe(
finalize(() => {
cleanAllocationEvents();
cleanDepositEvents();
cleanYagnaEvents();
cleanPaymentEvents();
cleanFileEvents();
setEvents([]);
})
filter((event) => event === "restartSession") // Adjust the condition to match your event structure
)
.subscribe();
.subscribe(() => {
cleanAllocationEvents();
cleanDepositEvents();
cleanYagnaEvents();
cleanPaymentEvents();
cleanFileEvents();
setEvents([]);
});
}, []);

useEffect(() => {
Expand Down
40 changes: 39 additions & 1 deletion frontend/src/components/homePage/statusSections/agreement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,41 @@ import { parseEther } from "viem";
import { GLMAmountStat } from "components/atoms/GLMAmount";
import { Loading, Tooltip } from "react-daisyui";
import { filter } from "rxjs";
import { useFlowEvents } from "components/providers/flowEventsProvider";
import { Bip } from "components/atoms/bip";

export const Agreement = () => {
const { user } = useUser();
const { createAgreement, isCreating } = useCreateAgreement();
const { releaseAgreement, isReleasing } = useReleaseAgreement();
const { events$ } = useDebitNoteEvents();
const [totalAmount, setTotalAmount] = useState("-");

const { events$: flowEvents$ } = useFlowEvents();
const [isAgreementButtonActive, setIsAgreementButtonActive] = useState(false);

const [canButtonBeActive, setCanButtonBeActive] = useState(true);

useEffect(() => {
const sub = flowEvents$.subscribe((event) => {
if (event === "restartSession") {
setCanButtonBeActive(true);
}
if (event === "releaseAgreement") {
setCanButtonBeActive(false);
}
});
return () => {
sub.unsubscribe();
};
}, []);

useEffect(() => {
setIsAgreementButtonActive(
canButtonBeActive && user.hasAllocation() && !user.hasAgreement()
);
}, [user.hasAgreement(), user.hasAllocation(), canButtonBeActive]);

useEffect(() => {
if (user.currentAgreement?.id) {
events$
Expand Down Expand Up @@ -91,7 +119,17 @@ export const Agreement = () => {
createAgreement();
}}
>
{isCreating ? <Loading variant="infinity" /> : "Create"}
{isCreating ? (
<Loading variant="infinity" />
) : isAgreementButtonActive ? (
<>
{" "}
<Bip />
"Create"{" "}
</>
) : (
"Create"
)}
</button>
)}
</div>
Expand Down
22 changes: 20 additions & 2 deletions frontend/src/components/homePage/statusSections/allocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,37 @@ import { formatBalance } from "utils/formatBalance";
import { Tooltip } from "react-daisyui";
import { useEffect, useState } from "react";
import { Bip } from "components/atoms/bip";
import { useFlowEvents } from "components/providers/flowEventsProvider";
export const Allocation = () => {
const { isCreating, createAllocation } = useCreateAllocation();
const { currentAllocation } = useCurrentAllocation();
const { releaseAllocation, isReleasing } = useReleaseAllocation();
const { user } = useUser();
const [isCreateAllocationButtonActive, setIsCreateAllocationButtonActive] =
useState(false);
const [canButtonBeActive, setCanButtonBeActive] = useState(true);

const { events$: flowEvents$ } = useFlowEvents();

useEffect(() => {
setIsCreateAllocationButtonActive(
user.hasDeposit() && !user.hasAllocation()
user.hasDeposit() && !user.hasAllocation() && canButtonBeActive
);
}, [user.hasAllocation(), user.hasDeposit()]);
}, [user.hasAllocation(), user.hasDeposit(), canButtonBeActive]);

useEffect(() => {
const sub = flowEvents$.subscribe((event) => {
if (event === "restartSession") {
setCanButtonBeActive(true);
}
if (event === "releaseAllocation") {
setCanButtonBeActive(false);
}
});
return () => {
sub.unsubscribe();
};
}, []);

return (
<div className="stats shadow mt-2 pt-4 pb-4">
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/components/homePage/statusSections/approve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const Approve = () => {
const [isApproveButtonActive, setIsApproveButtonActive] = useState(false);

useEffect(() => {
console.log(user.hasEnoughAllowance());
console.log(user.isRegistered());
setIsApproveButtonActive(user.isRegistered() && !user.hasEnoughAllowance());
}, [user.hasEnoughAllowance(), user.isRegistered()]);
return (
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/components/homePage/statusSections/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ export const Register = () => {
const { user } = useUser();
const { events$: flowEvents$, releaseAgreement } = useFlowEvents();

useEffect(() => {
const subscription = flowEvents$.subscribe((event) => {
console.log("flow event", event);
});

return () => {
subscription.unsubscribe();
};
}, []);
return (
<div className="stats shadow pt-4 pb-4">
<div className="stat ">
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/providers/flowEventsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const FlowEventsContext = createContext({
releaseAllocation() {
this.events$.next("releaseAllocation");
},
restartSession() {
this.events$.next("restartSession");
},
});

export const FlowEventsProvider = ({ children }: PropsWithChildren) => {
Expand All @@ -29,6 +32,9 @@ export const FlowEventsProvider = ({ children }: PropsWithChildren) => {
releaseAllocation() {
events.current.next("releaseAllocation");
},
restartSession() {
events.current.next("restartSession");
},
}}
>
{children}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/hooks/events/usePaymentEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const useDepositEvent = ({

const onLogs = useCallback((logs: any) => {
logs.forEach((log: any) => {
console.log("got e", log);
const e = {
txHash: log.transactionHash,
amount: Number(log.args.amount),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/yagna/useCurrentAgreement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const useCurrentAgreement = () => {
setIsPaused(true);
}
if (event.kind === Event.AGREEMENT_SIGNED) {
setIsPaused(false);
mutate();
setIsPaused(false);
}
});
return () => {
Expand Down

0 comments on commit 667af8f

Please sign in to comment.