Skip to content

Commit

Permalink
Merge pull request #81 from Guboehm/feat/adminOptions
Browse files Browse the repository at this point in the history
Feat: implementation of setQuorum and deposit on the adminOptions
  • Loading branch information
heronlancellot authored Oct 14, 2023
2 parents 0f657b5 + 0562f7b commit c7488c8
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 49 deletions.
128 changes: 93 additions & 35 deletions src/content/applications/Tasks/settings/AdminOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CoverAdminOptions from 'src/components/Cover/CoverAdminOptions';
const AdminOptions = () => {

const taskService = useTaskService();
const { handleRole, handleOperator } = useTaskServiceHook(taskService);
const { handleRole, handleOperator, handleQuorum, handleDeposit } = useTaskServiceHook(taskService);

const [role, setRole] = useState({
roleId: "",
Expand All @@ -22,37 +22,90 @@ const AdminOptions = () => {
isAuthorized: false
});

const handleInputChange = (action: string, event: { target: { name: any; value: any; }; }) => {
if (action == 'role') {
setRole((prevRole) => ({
...prevRole,
[event.target.name]: event.target.value
}));
} else {
setOperator((prevOperator) => ({
...prevOperator,
[event.target.name]: event.target.value
}));
const [quorum, setQuorum] = useState({
quorum: ""
});

const [deposit, setDeposit] = useState({
roleId: "",
amount: ""
});

const handleInputChange = (action: string, event: { target: { name: any; value: any; }; }) => {
switch (action) {
case "role": {
setRole((prevRole) => ({
...prevRole,
[event.target.name]: event.target.value
}));
break;
}
case "operator": {
setOperator((prevOperator) => ({
...prevOperator,
[event.target.name]: event.target.value
}));
break;
}
case "quorum": {
setQuorum((prevOperator) => ({
...prevOperator,
[event.target.name]: event.target.value
}));
break;
}
case "deposit": {
setDeposit((prevOperator) => ({
...prevOperator,
[event.target.name]: event.target.value
}));
break;
}
default: {
console.log('default');
}
}
}

const handleSubmit = async (action: string, event: { preventDefault: () => void; }) => {
event.preventDefault();

if (action == 'setRole') {
try {
await handleRole(role.roleId, role.authorizedAddress, role.isAuthorized);

} catch (error) {
console.error('Erro ao enviar o formulário:', error);
switch (action) {
case "setRole": {
try {
await handleRole(role.roleId, role.authorizedAddress, role.isAuthorized);

} catch (error) {
console.error('Erro ao enviar o formulário:', error);
}
break;
}
} else {
try {

await handleOperator(operator.interfaceId, operator.roleId, operator.isAuthorized)
} catch (error) {
console.error('Erro ao enviar o formulário:', error);
case "setOperator": {
try {
await handleOperator(operator.interfaceId, operator.roleId, operator.isAuthorized)
} catch (error) {
console.error('Erro ao enviar o formulário:', error);
}
break;
}
case "setQuorum": {
try {
await handleQuorum(BigInt(quorum.quorum))
} catch (error) {
console.error('Erro ao enviar o formulário:', error);
}
break;
}
case "setDeposit": {
try {
await handleDeposit(deposit.roleId, deposit.amount)
} catch (error) {
console.error('Erro ao enviar o formulário:', error);
}
break;
}
default: {
break;
}
}
};
Expand All @@ -61,31 +114,36 @@ const AdminOptions = () => {
<>
<Box>
<CoverAdminOptions />

<Box display={'flex'} justifyContent={'center'} alignItems={'center'} flexDirection={'column'}>

<Stack>

<Box m={2} >
<Box m={2}>
<Typography sx={{ alignItems: 'left' }} fontWeight={'bold'} fontSize={'24px'} mb={2}>Deposit</Typography>
<TextField label={'Role ID'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('deposit', event))} name='roleId' />
<TextField label={'Amount'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('deposit', event))} name='amount' />
<Button variant='contained' onClick={(event) => (handleSubmit('setDeposit', event))}>Enviar</Button>
</Box>
<Box m={2}>
<Typography sx={{ alignItems: 'left' }} fontWeight={'bold'} fontSize={'24px'} mb={2}>setRole</Typography>
<TextField label={'Role ID'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('role', event))} name='roleId' />
<TextField label={'Address'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('role', event))} name='authorizedAddress' />
<TextField label={'isAuthorized'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('role', event))} name='isAuthorized' />
<Button variant='contained' onClick={(event) => (handleSubmit('setRole', event))} > Enviar </Button>
<Button variant='contained' onClick={(event) => (handleSubmit('setRole', event))} >Enviar</Button>
</Box>

<Box m={2} >
<Box m={2}>
<Typography sx={{ alignItems: 'left' }} fontWeight={'bold'} fontSize={'24px'} mb={2}>setOperator</Typography>
<TextField label={'InterfaceId'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('operator', event))} name='interfaceId' />
<TextField label={'Role ID'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('operator', event))} name='roleId' />
<TextField label={'Bool'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('operator', event))} name='isAuthorized' />
<Button variant='contained' onClick={(event) => (handleSubmit('setOperator', event))}> Enviar </Button>
<Button variant='contained' onClick={(event) => (handleSubmit('setOperator', event))}>Enviar</Button>
</Box>
<Box m={2}>
<Typography sx={{ alignItems: 'left' }} fontWeight={'bold'} fontSize={'24px'} mb={2}>setQuorum</Typography>
<TextField label={'Value'} sx={{ mr: 2 }} onChange={(event) => (handleInputChange('quorum', event))} name='Value' />
<Button variant='contained' onClick={(event) => (handleSubmit('setQuorum', event))}>Enviar</Button>
</Box>

</Stack>

</Box>
</Box >
</Box>
</>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/31337/TasksManager.json

Large diffs are not rendered by default.

29 changes: 23 additions & 6 deletions src/hooks/TaskServiceHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface TaskService {
getMultiTasks: (start: number, end: number, isUserProfile: boolean) => Promise<Task[]>;
setRole: (roleId: any, authorizedAddress: any, isAuthorized: boolean) => Promise<any>
setOperator: (interfaceId: any, roleId: any, isAuthorized: boolean) => Promise<any>
setMinQuorum: (quorum: any) => Promise<any>
deposit: (roleId: any, amount: any) => Promise<any>
}

/**
Expand Down Expand Up @@ -47,19 +49,14 @@ export const useTaskServiceHook = (task: TaskService) => {
switch (status) {
case TaskStatus.Created:
return "Created"
break;
case TaskStatus.Canceled:
return "Canceled"
break;
case TaskStatus.Review:
return "In Review"
break;
case TaskStatus.Progress:
return "In Progress"
break;
case TaskStatus.Completed:
return "Completed"
break;
default:
break;
}
Expand Down Expand Up @@ -192,6 +189,24 @@ export const useTaskServiceHook = (task: TaskService) => {

};

const handleQuorum = async (quorum: any) => {
try {
handleSnackbar('Set Quorum process initiated with success!', 'info')
return await task.setMinQuorum(quorum);
} catch (error) {
handleSnackbar('Error Set Quorum!', 'error')
}
};

const handleDeposit = async (roleId: any, amount: any) => {
try {
handleSnackbar('Set Deposit process initiated with success!', 'info')
return await task.deposit(roleId, amount);
} catch (error) {
handleSnackbar('Error Set Deposit!', 'error')
}
};

return {
taskData,
multiTasksData,
Expand All @@ -200,6 +215,8 @@ export const useTaskServiceHook = (task: TaskService) => {
handleTask,
handleMultiTask,
handleRole,
handleOperator
handleOperator,
handleQuorum,
handleDeposit
};
};
7 changes: 6 additions & 1 deletion src/hooks/Web3UtilsHook.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAccount, useEnsName } from 'wagmi';
import { BigNumber, ethers } from "ethers";

export function useWeb3Utils() {

Expand Down Expand Up @@ -30,8 +31,12 @@ export function useWeb3Utils() {
function userAddress(): string {
return accountData?.address;
}

function parseUnits(amount: string): BigNumber {
return ethers.utils.parseUnits(amount, "ether");
}

return { shortenAddressOrEnsName, shortenAddressFromAddress, shortenAddressFromUser, userAddress };
return { shortenAddressOrEnsName, shortenAddressFromAddress, shortenAddressFromUser, userAddress, parseUnits };
}


28 changes: 23 additions & 5 deletions src/services/tasks-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AlertColor } from '@mui/material/Alert';


export function useTaskService() {
const { userAddress } = useWeb3Utils();
const { userAddress, parseUnits } = useWeb3Utils();
const { showSnackBar } = useSnackBar();

const handleSnackbar = (message: string, color: AlertColor) => {
Expand Down Expand Up @@ -117,7 +117,7 @@ export function useTaskService() {
try {
return await tasksManagerContract.getTask(taskId);
} catch (error) {
handleSnackbar('Erro ao buscar tarefa', 'error')
handleSnackbar('Error searching Task', 'error')
}
}

Expand Down Expand Up @@ -168,15 +168,31 @@ export function useTaskService() {
try {
return await tasksManagerContract.setRole(roleId, authorizedAddress, isAuthorized);
} catch (error) {
handleSnackbar('Erro ao setar Role', 'error')
handleSnackbar('Error setting Role', 'error')
}
}

async function setOperator(interfaceId: any, roleId: any, isAuthorized: boolean) {
try {
return await tasksManagerContract.setOperator(interfaceId, roleId, isAuthorized);
} catch (error) {
handleSnackbar('Erro ao setar Operador', 'error')
handleSnackbar('Error setting Operator', 'error')
}
}

async function setMinQuorum(quorum: any) {
try {
return await tasksManagerContract.setMinQuorum(quorum);
} catch (error) {
handleSnackbar('Error setting Quorum', 'error')
}
}

async function deposit(roleId: any, amount: any) {
try {
return await tasksManagerContract.deposit(roleId, { value: parseUnits(amount) });
} catch (error) {
handleSnackbar('Error setting deposit', 'error')
}
}

Expand All @@ -199,7 +215,9 @@ export function useTaskService() {
setRole,
setOperator,
hasLeaderRole,
hasMemberRole
hasMemberRole,
setMinQuorum,
deposit
};

}

0 comments on commit c7488c8

Please sign in to comment.