Skip to content

Commit

Permalink
Add Manage Team feature (#3446)
Browse files Browse the repository at this point in the history
* feat: update copy in manage team depending on user role

- update operator copy
- add edit button

* wire in contract calls

* wip

* update allo functions, add context

* working transactions but buggy modal

* add error handling

* cleanup

* fix

* fix

* fic

* lint fix

* fix

* Apply suggestions from code review

* pointer

---------

Co-authored-by: Aditya Anand M C <[email protected]>
Co-authored-by: 0xKurt <[email protected]>
  • Loading branch information
3 people authored May 27, 2024
1 parent f6b3ab7 commit 49e0632
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 22 deletions.
13 changes: 13 additions & 0 deletions packages/common/src/allo/allo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ export interface Allo {
indexingStatus: Result<void>;
}
>;

managePoolManager: (args: {
poolId: string;
manager: Address;
addOrRemove: "add" | "remove";
}) => AlloOperation<
Result<null>,
{
transaction: Result<Hex>;
transactionStatus: Result<TransactionReceipt>;
indexingStatus: Result<null>;
}
>;
}

export { AlloOperation };
Expand Down
19 changes: 18 additions & 1 deletion packages/common/src/allo/backends/allo-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,25 @@ export class AlloV1 implements Allo {
});
});
}
}

managePoolManager(args: {
poolId: string;
manager: Address;
addOrRemove: "add" | "remove";
}): AlloOperation<
Result<null>,
{
transaction: Result<Hex>;
transactionStatus: Result<TransactionReceipt>;
indexingStatus: Result<null>;
}
> {
return new AlloOperation(async () => {
const result = new AlloError(`Unsupported on v1 ${args}`);
return error(result);
});
}
}
// todo: move this out?
export type CreateRoundArgs = {
roundMetadata: { protocol: bigint; pointer: string };
Expand Down
52 changes: 52 additions & 0 deletions packages/common/src/allo/backends/allo-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,58 @@ export class AlloV2 implements Allo {
});
});
}

managePoolManager(args: {
poolId: string;
manager: Address;
addOrRemove: "add" | "remove";
}): AlloOperation<
Result<null>,
{
transaction: Result<Hex>;
transactionStatus: Result<TransactionReceipt>;
indexingStatus: Result<null>;
}
> {
return new AlloOperation(async ({ emit }) => {
const txData =
args.addOrRemove === "add"
? this.allo.addPoolManager(BigInt(args.poolId), args.manager)
: this.allo.removePoolManager(BigInt(args.poolId), args.manager);

const txResult = await sendRawTransaction(this.transactionSender, {
to: txData.to,
data: txData.data,
value: BigInt(txData.value),
});

emit("transaction", txResult);

if (txResult.type === "error") {
return error(txResult.error);
}

let receipt: TransactionReceipt;
try {
receipt = await this.transactionSender.wait(txResult.value);
emit("transactionStatus", success(receipt));
} catch (err) {
console.log(err);
const result = new AlloError("Failed to add pool manager");
emit("transactionStatus", error(result));
return error(result);
}

await this.waitUntilIndexerSynced({
chainId: this.chainId,
blockNumber: receipt.blockNumber,
});

emit("indexingStatus", success(null));

return success(null);
});
}
}

export function serializeProject(project: ProjectWithMerkleProof) {
Expand Down
132 changes: 132 additions & 0 deletions packages/round-manager/src/context/round/UpdateRolesContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import React, { SetStateAction, createContext, useContext } from "react";
import { ProgressStatus } from "../../features/api/types";
import { Allo } from "common";
import { Hex } from "viem";
import { datadogLogs } from "@datadog/browser-logs";

type SetStatusFn = React.Dispatch<SetStateAction<ProgressStatus>>;

export enum AddOrRemove {
ADD = "add",
REMOVE = "remove",
}

export type UpdateRolesData = {
roundId: string;
manager: Hex;
addOrRemove: AddOrRemove;
allo: Allo;
};

export interface UpdateRolesState {
contractUpdatingStatus: ProgressStatus;
setContractUpdatingStatus: SetStatusFn;
indexingStatus: ProgressStatus;
setIndexingStatus: SetStatusFn;
}

export const initialUpdateRolesState: UpdateRolesState = {
contractUpdatingStatus: ProgressStatus.IN_PROGRESS,
setContractUpdatingStatus: () => {
/* empty */
},
indexingStatus: ProgressStatus.NOT_STARTED,
setIndexingStatus: () => {
/* empty */
},
};

export const UpdateRolesContext = createContext<UpdateRolesState>(
initialUpdateRolesState
);

export const UpdateRolesProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [contractUpdatingStatus, setContractUpdatingStatus] =
React.useState<ProgressStatus>(
initialUpdateRolesState.contractUpdatingStatus
);

const [indexingStatus, setIndexingStatus] = React.useState<ProgressStatus>(
initialUpdateRolesState.indexingStatus
);

const providerProps: UpdateRolesState = {
contractUpdatingStatus,
setContractUpdatingStatus,
indexingStatus,
setIndexingStatus,
};

return (
<UpdateRolesContext.Provider value={providerProps}>
{children}
</UpdateRolesContext.Provider>
);
};

interface _updateRolesParams {
context: UpdateRolesState;
UpdateRolesData: UpdateRolesData;
}

const _updateRoles = async ({
context,
UpdateRolesData,
}: _updateRolesParams) => {
const { roundId, manager, addOrRemove, allo } = UpdateRolesData;
const { setContractUpdatingStatus, setIndexingStatus } = context;

await allo
.managePoolManager({
poolId: roundId,
manager,
addOrRemove,
})
.on("transactionStatus", (res) => {
if (res.type === "success") {
setContractUpdatingStatus(ProgressStatus.IS_SUCCESS);
setIndexingStatus(ProgressStatus.IN_PROGRESS);
} else {
console.error("Transaction Status Error", res.error);
datadogLogs.logger.error(`_updateRoles: ${res.error}`);
setContractUpdatingStatus(ProgressStatus.IS_ERROR);
}
})
.on("indexingStatus", (res) => {
if (res.type === "success") {
setIndexingStatus(ProgressStatus.IS_SUCCESS);
} else {
console.error("Indexing Status Error", res.error);
datadogLogs.logger.error(`_updateRoles: ${res.error}`);
setIndexingStatus(ProgressStatus.IS_ERROR);
}
})
.execute();
};

export const useUpdateRoles = () => {
const context = useContext(UpdateRolesContext);
if (!context) throw new Error("Missing UpdateRolesContext");

const { setContractUpdatingStatus, setIndexingStatus } = context;

const updateRoles = async (UpdateRolesData: UpdateRolesData) => {
setContractUpdatingStatus(initialUpdateRolesState.contractUpdatingStatus);
setIndexingStatus(initialUpdateRolesState.indexingStatus);

return _updateRoles({
context,
UpdateRolesData,
});
};

return {
updateRoles,
contractUpdatingStatus: context.contractUpdatingStatus,
indexingStatus: context.indexingStatus,
};
};
Loading

0 comments on commit 49e0632

Please sign in to comment.