Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/api calls contract #22

Open
wants to merge 6 commits into
base: feat/milestone-trustful-zuzalu-dApp-interface
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/spaces/[spaceid]/trustful/components/GiveBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export const GiveBadge = () => {
refUID:
inputBadge.title === 'Check-out'
? (commentBadge as `0x${string}`)
: zeroAddress,
: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: data,
value: BigInt(0),
};
Expand Down
10 changes: 5 additions & 5 deletions app/spaces/[spaceid]/trustful/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const EAS_CONTRACT_SCROLL = isDev
? EAS_CONTRACT_SCROLL_SEPOLIA
: EAS_CONTRACT_SCROLL_MAINET;

const RESOLVER_CONTRACT_SCROLL = '0x0A16dD89b4F06F111977D0F3bBab6165F08A5362';
const RESOLVER_CONTRACT_SCROLL = '0x0a830dd5C0c56752F5f1846F1a5cEA32bCf5Bc56';

const RESOLVER_CONTRACT_SCROLL_SEPOLIA =
'0x882977D73d7B2E67F088853B9065d4FD887e6bA0';
Expand All @@ -68,16 +68,16 @@ export const RESOLVER_CONTRACT_SCROLL_TRUSTFUL = isDev
export const SCHEMA_UIDS = {
ATTEST_MANAGER: isDev
? '0xf2f1d17213d5d5bed91abafecfc9862e857573d821ce0eeb36d8fe9cfebfc562'
: '0x6dc178b7782b9bd47ceab9e98aef9e9656a5b27722d384e74a3c654131657951',
: '0x7bbb090951d344668e6ba068d8382835460bcc21097f75b0821ba5695df5d274',
ATTEST_VILLAGER: isDev
? '0xfd6e98168eff2ca555ccf9cae1cf43198202f5495134b752b7a4cc0bc480c99d'
: '0x2b781f38ef65bdba6f7c70df32f90e53e4f9fc9f497e0324419de097cece3f3d',
: '0x50f27f787d0d055d5eca23bb55e256cfb729ba6c5eb3773c617fa03bb3ba3cd3',
ATTEST_EVENT: isDev
? '0x9b55966a9b4a7676a329f026bff451265f83fdc46eef72dc8b252a1121fc7ecc'
: '0x50a3ad32ee748ad7758f7429d8b30543dd6a60542e519ee0c85d7897144b0be3',
: '0xa34db27c36d14edb7ca5169307c3db8672e921040afcb7d47a3ed0fe4ae250ab',
ATTEST_RESPONSE: isDev
? '0x4a84dcaa2a5fffeb8dd99bfb2d12ef6fa0e233e9cfb11f2c8e08ad9714938ac8'
: '0x3a12210b37dd53be53954015c7e163b9be5445f7f7d397b5d7cb14f77a5c5510',
: '0xda260d1881382274f5d5101fe3a45b2c06e33bd6f1263ab3b5468d738dbb76ae',
};

export enum ROLES {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ import {
Box,
} from '@chakra-ui/react';
import { BeatLoader } from 'react-spinners';
import { Address, isAddress } from 'viem';
import {
Address,
encodeAbiParameters,
isAddress,
keccak256,
zeroAddress,
} from 'viem';
import { useAccount } from 'wagmi';
import {
Event,
Role,
ROLES,
} from '@/app/spaces/[spaceid]/trustful/constants/constants';
import { removeSession } from '@/app/spaces/[spaceid]/trustful/service/smart-contract';
import {
closeSession,
removeSession,
} from '@/app/spaces/[spaceid]/trustful/service/smart-contract';
import { EthereumAddress } from '@/app/spaces/[spaceid]/trustful/utils/types';
import { useTrustful } from '@/context/TrustfulContext';
import toast from 'react-hot-toast';
Expand All @@ -33,8 +42,11 @@ import {
deleteSession,
getAllEventsBySpaceId,
wrapSession,
getSession,
Session,
} from '@/app/spaces/[spaceid]/trustful/service/backend/';
import { useParams } from 'next/navigation';
import { useParams, useRouter } from 'next/navigation';
import { solidityPackedKeccak256 } from 'ethers';

export const DropdownSessionDetails = () => {
const { address } = useAccount();
Expand All @@ -51,13 +63,16 @@ export const DropdownSessionDetails = () => {
const [inputValuesTextArea, setInputValuesTextArea] = useState<{
[key: string]: string;
}>({});
const [inputValuesChange, setInputValuesChange] = useState<{
[key: string]: string;
}>({});
const [events, setEvents] = useState<Event[] | undefined>([]);
// const [inputValuesChange, setInputValuesChange] = useState<{
// [key: string]: string;
// }>({});
const [session, setSession] = useState<Session | undefined>(undefined);
const params = useParams();
const spaceId = params.spaceid.toString();
console.log('params', params);
const sessionId = params.sessionid.toString();
const spaceId = params.spaceid.toString();
const eventId = params.eventid.toString();
const router = useRouter();

// Updates the validAddress when the inputAddress changes
useEffect(() => {
Expand All @@ -71,46 +86,91 @@ export const DropdownSessionDetails = () => {
}, [sessionAction]);

useEffect(() => {
const fetchAllEvents = async () => {
const fetchSession = async () => {
try {
const eventsData = await getAllEventsBySpaceId({
spaceId: Number(spaceId),
userAddress: address as Address,
const sessionsData = await getSession({
eventid: Number(params.eventid),
});
setEvents(eventsData);
console.log('da um session aqui po', sessionsData);

if (sessionsData && sessionsData.sessions.length > 0) {
const sessionById = sessionsData.sessions.find(
(session) => session.sessionId === Number(params.sessionid),
);

if (sessionById) {
console.log('sessinById po ', sessionById);
setSession(sessionById);
}
}
} catch (error) {
console.log('error', error);
}
};
fetchAllEvents();

fetchSession();
}, []);

useEffect(() => {
const fetchSession = async () => {
try {
const sessionsData = await getSession({
eventid: Number(params.eventid),
});

if (sessionsData && sessionsData.sessions.length > 0) {
const sessionById = sessionsData.sessions.find(
(session) => session.sessionId === Number(params.sessionid),
);

if (sessionById) {
setSession(sessionById);
}
}
} catch (error) {
console.log('error', error);
}
};

fetchSession();
}, [address, params.eventid, params.sessionid]);

/**Root */
const handleRemoveSession = async () => {
if (!address || !userRole) {
setIsLoading(false);
toast.error('Please connect first. No address found.');
return;
}
console.log('session', session);

if (!validAddress) {
setIsLoading(false);
toast.error('Please enter a valid address.');
if (!session) {
return;
}

const responseSmartContract = await removeSession({
from: address,
sessionTitle: inputValuesTextArea['removeSessionTitle'],
sessionOwner: validAddress.address as Address,
msgValue: BigInt(0),
});
// if (address !== session.hostAddress) {
// return;
// }

const responseBackend = await deleteSession({
role: userRole.role,
sessionId: Number(sessionId),
userAddress: validAddress.address as Address,
userAddress: address as Address,
});
if (responseBackend) {
const responseSmartContract = await removeSession({
from: address,
sessionTitle: session.sessionTitle,
sessionOwner: address as Address,
msgValue: BigInt(0),
});
if (!responseSmartContract) {
setIsLoading(false);
toast.error('Transaction Rejected');
return;
}
router.push(`/spaces/${spaceId}/trustful/events/${eventId}`);
}

setIsLoading(false);
// toast.success(
Expand Down Expand Up @@ -154,11 +214,42 @@ export const DropdownSessionDetails = () => {
return;
}

if (!session) {
return;
}

// console.log('session', session.name + '_' + session.title);
// const mockedSession = 'TKDWClass_1731430187447';

const sessionIdSC = solidityPackedKeccak256(
['address', 'string'],
[session.hostAddress, session.sessionTitle],
);
console.log('sessionId', sessionId);

const response = await wrapSession({
role: userRole.role,
sessionId: Number(sessionId),
userAddress: address as Address,
});
console.log('wrapa a session', response);

if (response) {
console.log('response entrou', response);
const responseSC = await closeSession({
from: address,
sessionId: sessionIdSC as Address,
msgValue: BigInt(0),
});
console.log('closeSessionResponseSC', responseSC);

if (responseSC instanceof Error) {
setIsLoading(false);
toast.error(`Transaction Rejected: ${responseSC.message}`);
return;
}
console.log('responseSC', responseSC);
}

if (response instanceof Error) {
setIsLoading(false);
Expand Down Expand Up @@ -212,7 +303,7 @@ export const DropdownSessionDetails = () => {
>
<Flex className="w-full flex-col">
<Flex className="gap-4 pb-4 justify-start items-center">
<Textarea
{/* <Textarea
style={{ color: 'black' }}
className="text-black text-base font-normal leading-snug"
color="white"
Expand All @@ -231,13 +322,13 @@ export const DropdownSessionDetails = () => {
}
minH="unset"
resize="none"
/>
/> */}
</Flex>
<InputAddressUser
{/* <InputAddressUser
label="Address to Session Owner"
onInputChange={(value: string) => setInputAddress(value)}
inputAddress={String(inputAddress)}
/>
/> */}
<Box>
<Flex className="pb-4 gap-4 items-center">
<Text className="flex min-w-[80px] text-white opacity-70 text-sm font-normal leading-tight">
Expand All @@ -250,21 +341,21 @@ export const DropdownSessionDetails = () => {
</Flex>
</Box>
<Button
className={`w-full justify-center items-center gap-2 px-6 bg-[#B1EF42] text-[#161617] rounded-lg ${!isAddress(inputAddress.toString()) || !inputValuesTextArea['removeSession'] ? 'cursor-not-allowed opacity-10' : ''}`}
className={`w-full justify-center items-center gap-2 px-6 bg-[#B1EF42] text-[#161617] rounded-lg ${!session ? 'cursor-not-allowed opacity-10' : ''}`}
_hover={{ bg: '#B1EF42' }}
_active={{ bg: '#B1EF42' }}
isLoading={isloading}
isDisabled={
!isAddress(inputAddress.toString()) ||
!inputValuesTextArea['removeSessionTitle']
}
// isDisabled={
// // !isAddress(inputAddress.toString()) ||
// // !session.
// }
spinner={<BeatLoader size={8} color="white" />}
onClick={() => {
!isAddress(inputAddress.toString()) ||
(!inputValuesTextArea['removeSessionTitle'] &&
toast.error(
'Please enter a valid address and set the session title to remove',
));
// !isAddress(inputAddress.toString()) ||
// (!session. &&
// toast.error(
// 'Please enter a valid address and set the session title to remove',
// ));
setIsLoading(true);
handleRemoveSession();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
CYPHERHOUSE_SPACEID,
Role,
ROLES,
spaceIdValue,
} from '@/app/spaces/[spaceid]/trustful/constants/constants';
import { EthereumAddress } from '@/app/spaces/[spaceid]/trustful/utils/types';
import { useTrustful } from '@/context/TrustfulContext';
Expand All @@ -29,6 +30,7 @@ import {
import { createSessionSC } from '@/app/spaces/[spaceid]/trustful/service/smart-contract';
import {
createSession,
deleteSession,
getAllEventsBySpaceId,
} from '@/app/spaces/[spaceid]/trustful/service';
import { Event } from '@/app/spaces/[spaceid]/trustful/constants/constants';
Expand Down Expand Up @@ -72,7 +74,7 @@ export const DropdownEventSelected = () => {
const fetchAllEvents = async () => {
try {
const eventsData = await getAllEventsBySpaceId({
spaceId: Number(spaceId),
spaceId: spaceIdValue,
userAddress: address as Address,
});
setEvents(eventsData);
Expand Down Expand Up @@ -102,22 +104,45 @@ export const DropdownEventSelected = () => {

const oneDayInSeconds = BigInt(86400);

/*Create Session in Smart Contract */
const smartContractCreateSession = await createSessionSC({
from: address as Address,
sessionTitle: sessionTitle,
duration: oneDayInSeconds,
msgValue: BigInt(0),
});

/*Create Session in Backend */
const response = await createSession({
name: inputValuesTextArea['createSessionName'],
eventId: Number(eventId),
zucityId: CYPHERHOUSE_SPACEID,
hostAddress: address as Address,
sessionTitle: sessionTitle,
});

//TODO: Check the error here
if (response) {
console.log('entrou no create-session-response');
console.log('address', address);
console.log('sessionTitle', sessionTitle);
console.log('duration', oneDayInSeconds);
console.log('msgValue', BigInt(0));
/*Create Session in Smart Contract */
const smartContractCreateSession = await createSessionSC({
from: address as Address,
sessionTitle: sessionTitle,
duration: oneDayInSeconds,
msgValue: BigInt(0),
});
if (!smartContractCreateSession) {
const responseBackend = await deleteSession({
role: userRole.role,
sessionId: response.sessionId,
userAddress: address as Address,
});
if (responseBackend) {
setIsLoading(false);
toast.error(
'Transaction Rejected: Error creating session in Smart Contract',
);
return;
}
}
}

if (response instanceof Error) {
setIsLoading(false);
toast.error(`Transaction Rejected: ${response.message}`);
Expand Down
Loading