Skip to content

Commit

Permalink
chore: improve debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed Jun 18, 2024
1 parent 30a6014 commit d15e648
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 46 deletions.
3 changes: 3 additions & 0 deletions backend/src/errors/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ErrorCode {
ALLOCATION_NOT_FOUND = "ALLOCATION_NOT_FOUND",
AGREEMENT_NOT_FOUND = "AGREEMENT_NOT_FOUND",
ALLOCATION_TOP_UP_FAILED = "ALLOCATION_TOP_UP_FAILED",
UNABLE_TO_CREATE_AGREEMENT = "UNABLE_TO_CREATE_AGREEMENT",
}

export const errorMessages = {
Expand All @@ -26,6 +27,7 @@ export const errorMessages = {
}: {
allocationId: string;
}) => `Allocation top up failed: ${allocationId}`,
[ErrorCode.UNABLE_TO_CREATE_AGREEMENT]: () => "Unable to create agreement",
};

export type ErrorParams = {
Expand All @@ -34,6 +36,7 @@ export type ErrorParams = {
[ErrorCode.NO_ALLOCATION]: never;
[ErrorCode.NO_WORKER]: never;
[ErrorCode.USER_NOT_FOUND]: never;
[ErrorCode.UNABLE_TO_CREATE_AGREEMENT]: never;
[ErrorCode.ALLOCATION_NOT_FOUND]: {
allocationId: string;
};
Expand Down
1 change: 0 additions & 1 deletion backend/src/services/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const userService: IUserService = {
//@ts-ignore
getCurrentDeposit: async (userId: string): DepositData | null => {
const user = await userModel.findOne({ _id: userId });
console.log("user", user);
if (!user) {
throw new Error(`User not found with id ${userId}`);
}
Expand Down
7 changes: 6 additions & 1 deletion backend/src/services/yagna/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ export const Yagna = fastifyPlugin((fastify: FastifyInstance, opts, done) => {
const Yagna = container.cradle.Yagna;
try {
await Yagna.makeAgreement(requestUser._id);
reply.code(201).send({
message: "Agreement created",
});
} catch (e) {
console.log("error", e);
reply.code(500).send({
message: "Unable to create agreement",
});
}
},
});
Expand Down Expand Up @@ -143,7 +149,6 @@ export const Yagna = fastifyPlugin((fastify: FastifyInstance, opts, done) => {
const user = await container.cradle.userService.getUserById(
requestUser._id
);
console.log("user", user);
if (!user?.currentAllocationId) {
reply.code(500).send({
message: "No allocation found",
Expand Down
50 changes: 34 additions & 16 deletions backend/src/services/yagna/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { WorkContext } from "@golem-sdk/golem-js";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { formatEther, parseEther } from "viem";
import { UUID } from "mongodb";
import { debug } from "node:console";
export class Yagna {
public debitNoteEvents: Subject<any>;
public invoiceEvents: Subject<any>;
Expand Down Expand Up @@ -93,20 +94,27 @@ export class Yagna {

async makeAgreement(userId: string) {
//creating executor by task executor makes agreement
const executor = await this.userContext.getExecutor(userId);
//in order to make sure agreement wont be automatically closed after 90s
//which is HARDCODED in yagna we make worker which under the hood makes activity
// which prevents agreement from closing
const worker = await this.getUserWorker(userId);

const agreement = await worker.context?.activity.agreement;
debugLog("yagna", "making agreement", userId);
let executor, worker, agreement;
try {
executor = await this.userContext.getExecutor(userId);
debugLog("yagna", "making agreement, executor found", userId);

//in order to make sure agreement wont be automatically closed after 90s
//which is HARDCODED in yagna we make worker which under the hood makes activity
// which prevents agreement from closing
worker = await this.getUserWorker(userId);
debugLog("yagna", "making agreement, worker found", userId);
agreement = await worker.context?.activity.agreement;
debugLog("yagna", "making agreement, agreement found", agreement);
} catch (e) {
console.log("Error", e);
}

if (!agreement) {
debugLog("yagna", "Error making agreement, no agreement found", userId);
throw new Error({
code: ErrorCode.AGREEMENT_NOT_FOUND,
payload: {
agreementId: "",
},
code: ErrorCode.UNABLE_TO_CREATE_AGREEMENT,
});
}
container.cradle.userService.setCurrentAgreementId(userId, agreement.id);
Expand Down Expand Up @@ -234,7 +242,7 @@ export class Yagna {

//task executor creates allocation as well
async createExecutor(userId: string) {
debugLog("payments", "creating executor", userId);
debugLog("yagna", "creating executor", userId);
const userService = container.cradle.userService;
const userDeposit = await userService.getCurrentDeposit(userId);

Expand All @@ -254,7 +262,7 @@ export class Yagna {

//@ts-ignore
if (allocation?.id) {
debugLog("payments", "allocation already exists, reusing", allocation);
debugLog("yagna", "allocation already exists, reusing", allocation);
}

const executor = await TaskExecutor.create({
Expand All @@ -281,11 +289,11 @@ export class Yagna {

//task executor creates agreement and activity
async getUserWorker(userId: string): Promise<Worker> {
debugLog("payments", "getting user worker", userId);
debugLog("yagna", "getting user worker", userId);
return new Promise(async (resolve, reject) => {
const worker = this.userContext.getWorker(userId);
if (worker) {
debugLog("yagna", "worker found", userId);
debugLog("yagna", "getUSerWorker: worker found", userId);
const isConnected = await worker.isConnected();
if (isConnected) {
debugLog("yagna", "worker connected", userId);
Expand All @@ -308,18 +316,27 @@ export class Yagna {
resolve(newWorker);
return;
} else {
debugLog("yagna", "creating worker, not mock getting deposit", userId);
const userService = container.cradle.userService;
const userDeposit = await userService.getCurrentDeposit(userId);
debugLog("yagna", "creating worker deposit found", userDeposit);
if (!userDeposit) {
throw new Error({ code: ErrorCode.NO_DEPOSIT });
}
debugLog("yagna", "creating worker, getting executor", userId);
const executor = await this.userContext.getExecutor(userId);
if (!executor) {
throw new Error({ code: ErrorCode.NO_ALLOCATION });
}

debugLog(
"yagna",
"creating worker, executor found getting agreement",
userId
);
executor
.run(async (ctx: WorkContext) => {
debugLog("yagna", "created worker", userId);
newWorker.context = ctx;

newWorker.setState("free");
Expand All @@ -328,7 +345,8 @@ export class Yagna {
})
.catch((e: any) => {
console.log("Error", e);
reject();
debugLog("yagna", "error creating worker", e);
reject(e);
});
}
});
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/homePage/events/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const Events = () => {
const { events$: flowEvents$ } = useFlowEvents();

useEffect(() => {
console.log("events", events);
flowEvents$
.pipe(
finalize(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const ExtendDeposit = () => {
setNonce(user.currentDeposit?.nonce);
}
if (isSuccessTransaction && additionalAmount > 0) {
console.log("topping up", additionalAmount, isSuccessTransaction);
topUp(additionalAmount);
hideModal();
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/providers/blockchainProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const siweConfig = createSIWEConfig({
}
const responseData = await response.json();

console.log("nonce", responseData.nonce.toString());
return responseData.nonce.toString();
},
// @ts-ignore
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/providers/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export const UserProvider = ({ children }: PropsWithChildren<{}>) => {
}
}, [isUserLoading, userData, user.allowanceAmount, depositData]);

console.log("use allowance hook call");
const { isFetched: isAllowanceFetched, amount: allowanceAmount } =
useAllowance();

Expand Down
21 changes: 0 additions & 21 deletions frontend/src/hooks/depositContract/useWatchDepositPayments.ts

This file was deleted.

1 change: 0 additions & 1 deletion frontend/src/hooks/userUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const useUserData = () => {

const { data, error } = useSWRSubscription("userData", (key, { next }) => {
socket.on("user", (data: any) => {
console.log("user", data);
setIsLoading(false);
next(null, data);
});
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/hooks/yagna/useCreateAgreement.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import axios from "axios";
import useSWRMutation from "swr/mutation";

import { useSnackbar } from "notistack";
import { useActionDebounce } from "hooks/useActionDbounce";
import { useEffect } from "react";
export const useCreateAgreement = () => {
const { trigger, isMutating } = useSWRMutation(
const { enqueueSnackbar } = useSnackbar();
const { trigger, isMutating, error } = useSWRMutation(
`${import.meta.env.VITE_BACKEND_HTTP_URL}/me`,
function () {
return axios.post(
`${import.meta.env.VITE_BACKEND_HTTP_URL}/create-agreement`
);
}
);

useEffect(() => {
if (error) {
enqueueSnackbar("Error creating agreement", { variant: "error" });
console.error("Error creating agreement", error);
}
}, [error]);

const isCreating = useActionDebounce(isMutating, 1000);
return {
createAgreement: trigger,
Expand Down

0 comments on commit d15e648

Please sign in to comment.