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

Poidh v2 dev #4

Merged
merged 2 commits into from
Apr 10, 2024
Merged
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
75 changes: 72 additions & 3 deletions src/app/context/web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Contract, ethers } from "ethers";
import abi from './abi';
import abiNFT from './abiNFT'
import chains from './config';
import { CreateBountyFunction,withdrawFromOpenBountyFunction ,SubmitClaimForVoteFunction, GetParticipants, CreateClaimFunction, AcceptClaimFunction, CancelBountyFunction, FetchBountiesFunction, FetchBountyByIdFunction, GetBountiesByUserFunction, Bounty , GetClaimsByUserFunction, GetClaimsByBountyIdFunction, GetURIFunction, Claim, GetAllBountiesFunction, JoinOpenBountyFunction, BountyCurrentVotingClaimFunction, BountyVotingTrackerFunction, VoteClaimFunction, ResolveVoteFunction } from '../../types/web3';
import { CreateBountyFunction,withdrawFromOpenBountyFunction ,SubmitClaimForVoteFunction, GetParticipants, CreateClaimFunction, AcceptClaimFunction, CancelBountyFunction, FetchBountiesFunction, FetchBountyByIdFunction, GetBountiesByUserFunction, Bounty , GetClaimsByUserFunction, GetClaimsByBountyIdFunction, GetURIFunction, Claim, GetAllBountiesFunction, JoinOpenBountyFunction, BountyCurrentVotingClaimFunction, BountyVotingTrackerFunction, VoteClaimFunction, ResolveVoteFunction, GetClaimByIdFunction } from '../../types/web3';


const currentChain = chains.sepolia;
Expand Down Expand Up @@ -33,6 +33,35 @@ export const getNFTContractRead = async () => {
};


export const getNftsOfOwner = async () => {
const contract = getNFTContractRead()

// console.log(contract)
// console.log(balance)
return contract
}

// async function getNFTsOfOwner() {
// const contract = new ethers.Contract(contractAddress, erc721ABI, provider);
// const balance = await contract.balanceOf(ownerAddress); //Returns the number of tokens owned by the address

// let tokenIds = [];

// for(let i = 0; i < balance.toNumber(); i++) {
// const tokenId = await contract.tokenOfOwnerByIndex(ownerAddress, i); //Get the token ID based on the index from the balanceOf call
// tokenIds.push(tokenId.toString());
// }

// console.log(`Token IDs owned by ${ownerAddress}: ${tokenIds}`);
// }










// WRITE Functions
Expand Down Expand Up @@ -119,7 +148,6 @@ export const submitClaimForVote: SubmitClaimForVoteFunction = async (
}
};


export const cancelOpenBounty: CancelBountyFunction = async (
primaryWallet, id
) => {
Expand Down Expand Up @@ -283,10 +311,13 @@ export const getBountiesByUser: GetBountiesByUserFunction = async (


export const fetchAllBounties: GetAllBountiesFunction = async () => {

const contractRead = await getContractRead();
const bountyCounter = await contractRead.bountyCounter();

let allBounties: Bounty[] = [];


const totalBounties = Number(bountyCounter.toString());

for (let offset = Math.floor(totalBounties / 10) * 10; offset >= 0; offset -= 10) {
Expand All @@ -308,7 +339,6 @@ export const fetchAllBounties: GetAllBountiesFunction = async () => {

allBounties.sort((a, b) => Number(b.createdAt) - Number(a.createdAt));

console.log(allBounties)
return allBounties;
};

Expand Down Expand Up @@ -405,6 +435,45 @@ export const getClaimsByBountyId: GetClaimsByBountyIdFunction = async (id) => {
return formattedClaims;
};


export const getClaimById: GetClaimByIdFunction = async (claimId) => {
const contractRead = await getContractRead();
const claimById = await contractRead.claims(claimId);

const formattedClaim: Claim[] = [{
id: claimById[0].toString(),
issuer: claimById[1],
bountyId: claimById[2].toString(),
bountyIssuer: claimById[3],
name: claimById[4],
description: claimById[5],
createdAt: claimById[6].toString(),
accepted: claimById[7]
}];

return formattedClaim;
};





// export const getClaimById: GetClaimByIdFunction = async (claimId) => {
// const contractRead = await getContractRead();
// const claimById = await contractRead.claims(claimId);

// const names = claimById['#names'];
// const data = claimById.filter((_, index) => index !== '#names');

// const formattedClaim = {};
// names.forEach((name, index) => {
// formattedClaim[name] = data[index];
// });

// return formattedClaim;
// };


export const getURI: GetURIFunction = async (claimId) => {
const contractNFT = await getNFTContractRead();
const uri = await contractNFT.tokenURI(claimId);
Expand Down
102 changes: 90 additions & 12 deletions src/components/account/AccountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { ethers } from "ethers";
import {
useDynamicContext,
} from '@dynamic-labs/sdk-react-core'
import { getSigner, getContract, getBountiesByUser, getProvider, getClaimsByUser } from '@/app/context/web3';
import { getSigner, getContract, getBountiesByUser, getProvider, getClaimsByUser, getNftsOfOwner , getClaimById, getURI} from '@/app/context/web3';
import Button from '@/components/ui/Button';
import BountyList from '@/components/ui/BountyList';
import { BountiesData, ClaimsData } from '@/types/web3';
import FilterButton from '@/components/ui/FilterButton';
import ProofList from '@/components/bounty/ProofList';
import ProofListAccount from '@/components/bounty/ProofListAccount';



Expand All @@ -19,10 +22,13 @@ const AccountInfo = () => {
const [bountiesData, setBountiesData] = useState<BountiesData[]>([]);
const [claimsData, setClaimsData] = useState<ClaimsData[]>([]);

const [completedBounties, setCompletedBounties] = useState("");
const [completedBounties, setCompletedBounties] = useState<BountiesData[]>([]);
const [inProgressBounties, setInProgressBounties] = useState("");
const [ETHinContract, setETHinContract] = useState("");
const [completedClaims, setCompletedClaims] = useState("");
const [submitedClaims, setSubmitedClaims] = useState<ClaimsData[]>([]);
const [currentSection, setCurrentSection] = useState<string>('a');




Expand All @@ -41,23 +47,48 @@ const AccountInfo = () => {
const contractBalance = await provider.getBalance(contract.getAddress())
const balanceETH = ethers.formatEther(contractBalance)
setETHinContract(balanceETH)


// console.log("getting nft...")

// const getNfts = await getNftsOfOwner()

// console.log(getNfts)

// const balance = await getNfts.balanceOf(primaryWallet?.address);


// const tid = await getNfts.tokenOfOwnerByIndex(primaryWallet?.address, 0)

// console.log(tid)

// console.log("got nft...")


getBountiesByUser(address, 0, [])
.then((data: any) => {
setBountiesData(data)
const completedBounties = data.filter((bounty:any) => bounty.claimer !== '0x0000000000000000000000000000000000000000' && bounty.claimer.toLowerCase() !== address.toLowerCase()).length;
const inProgressBounties = data.filter((bounty:any) => bounty.claimer === '0x0000000000000000000000000000000000000000').length;
const completedBounties = data.filter((bounty:any) => bounty.claimer !== '0x0000000000000000000000000000000000000000' && bounty.claimer.toLowerCase() !== address.toLowerCase());
const inProgressBounties = data.filter((bounty:any) => bounty.claimer === '0x0000000000000000000000000000000000000000');
setInProgressBounties(inProgressBounties);
setCompletedBounties(completedBounties);


})





getClaimsByUser(address)
.then((data: any) => {
setClaimsData(data);
const completedClaims = data.filter((claim: any) => claim.accepted !== true).length;
const completedClaims = data.filter((claim: any) => claim.accepted === true);
const submitedClaims = data.filter((claim: any) => claim.accepted === false);

setCompletedClaims(completedClaims);
console.log(claimsData);
setSubmitedClaims(submitedClaims);

});


Expand All @@ -68,6 +99,29 @@ const AccountInfo = () => {
userInformation().catch(console.error);
}
}, [primaryWallet]);

useEffect(() => {
const fetchClaimInformation = async () => {
const claimIds = completedBounties.map(bounty => bounty.claimId);
const claimInformationPromises = claimIds.map(async (claimId) => {
const uri = await getURI(claimId);
return {
claimId: claimId,
claimURI: uri
};
});
const claimInformation = await Promise.all(claimInformationPromises);
console.log("Claim Information:", claimInformation);
};

fetchClaimInformation();
}, [completedBounties]);

const handleFilterButtonClick = (section: string) => {
setCurrentSection(section);
};




return (
Expand All @@ -82,11 +136,11 @@ const AccountInfo = () => {
</div>

<div className='flex flex-col'>
<div>completed bounties: <span className='font-bold' >{completedBounties}</span></div>
<div>completed bounties: <span className='font-bold' >{completedBounties.length}</span></div>
<div>total eth paid: <span className='font-bold' >0.0144</span> </div>
<div>in progress bounties: <span className='font-bold' >{inProgressBounties}</span> </div>
<div>in progress bounties: <span className='font-bold' >{inProgressBounties.length}</span> </div>
<div>total eth in contract: <span className='font-bold' >{ETHinContract}</span> </div>
<div>completed claims: <span className='font-bold' >{completedClaims}</span></div>
<div>completed claims: <span className='font-bold' >{completedClaims.length}</span></div>
<div>total eth earned: <span className='font-bold' >0.0109</span> </div>


Expand All @@ -100,9 +154,33 @@ const AccountInfo = () => {
</div>
</div>

<div>
<BountyList bountiesData={bountiesData} />
</div>

<div className='flex flex-row overflow-x-scroll items-center py-12 border-b border-white lg:justify-center gap-x-5 '>
<FilterButton onClick={() => handleFilterButtonClick('a')} >nft's (3)</FilterButton>
<FilterButton onClick={() => handleFilterButtonClick('b')} >your bounties ({inProgressBounties.length})</FilterButton>
<FilterButton onClick={() => handleFilterButtonClick('c')} >submitted claims ({submitedClaims.length})</FilterButton>
<FilterButton>collab bounties (0)</FilterButton>
</div>

<div>
{currentSection === 'a' && (
<div>
<BountyList bountiesData={completedBounties} />
</div>
)}
{currentSection === 'b' && (
<div>
<BountyList bountiesData={bountiesData} />
</div>
)}
{currentSection === 'c' && (
<div>
<ProofListAccount youOwner={true} data={submitedClaims} />
</div>
)}
</div>


</div>
) : (
<div className='h-screen w-full flex items-center justify-center flex-col' >
Expand Down
27 changes: 20 additions & 7 deletions src/components/bounty/BountyInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function weiToEther(weiValue: string | number): string {
const BountyInfo = ({ bountyId }: { bountyId: string }) => {


const { isMultiplayer, isOwner, bountyData, isBountyClaimed} = useBountyContext()!;
const { isMultiplayer, isOwner, bountyData, isBountyClaimed, isBountyCanceled} = useBountyContext()!;


console.log("Is multiplayer:", isMultiplayer)
Expand All @@ -29,20 +29,33 @@ const BountyInfo = ({ bountyId }: { bountyId: string }) => {

return (
<>
<div className="flex pt-20 justify-between">
<div className="flex flex-col lg:max-w-[50%]">
<p className="text-4xl text-bold">{bountyData?.name}</p>
<div className="flex pt-20 flex-col justify-between lg:flex-row">
<div className="flex flex-col lg:max-w-[50%]">
<p className=" text-2xl lg:text-4xl text-bold">{bountyData?.name}</p>
<p className="mt-5">{bountyData?.description}</p>
<p>Bounty issuer: {bountyData?.issuer}</p>

<div>
<p>Debug:</p>

<p>is Owner: {isOwner ? "true" : "false"}</p>
<p>isBountyClaimed: {isBountyClaimed ? "true" : "false"}</p>
<p>isMultiplayer: {isMultiplayer ? "true" : "false"}</p>
<p>isBountyCanceled: {isBountyCanceled ? "true" : "false"}</p>


</div>


</div>

<div className='flex flex-col'>
<div className="flex gap-x-2 flex-row">
<div className="flex mt-5 lg:mt-0 gap-x-2 flex-row">
<span>{bountyData ? weiToEther(bountyData.amount) : "Loading..."}</span>
<span>eth</span>
</div>
<div>{isMultiplayer? "this is multiplayer" : "no this is solo"}</div>
<div> {!isBountyClaimed && !isOwner ? <CreateProof bountyId={bountyId} /> : ""}</div>
{/* <div >{isMultiplayer? "this is multiplayer" : "no this is solo"}</div> */}
<div className='' > {!isBountyClaimed && !isOwner ? <CreateProof bountyId={bountyId} /> : ""}</div>


</div>
Expand Down
21 changes: 15 additions & 6 deletions src/components/bounty/BountyMultiplayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import Withdraw from '@/components/ui/Withdraw';
import CancelOpenBounty from '@/components/ui/CancelOpenBounty';
import { useBountyContext } from '@/components/bounty/BountyProvider';

function weiToEther(weiValue: string | number): string {
const etherValue = Number(weiValue) / 1e18;
Expand Down Expand Up @@ -43,6 +44,7 @@ const BountyMultiplayer = ({ bountyId }: { bountyId: string }) => {

const isCurrentUserAParticipant = currentUser ? participants?.addresses.includes(currentUser) : false;

const { isMultiplayer, isOwner, bountyData, isBountyClaimed} = useBountyContext()!;


return (
Expand All @@ -53,19 +55,19 @@ const BountyMultiplayer = ({ bountyId }: { bountyId: string }) => {
</div>
<button
onClick={toggleParticipants}
className='border border-white rounded-full px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'
className='border border-white rounded-full mt-5 px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'
>
{participants ? `${participants.addresses.length} contributors` : 'Loading contributors...'}
<span className={`${showParticipants ? '-rotate-180' : '' } animation-all duration-300 `} ><ExpandMoreIcon /></span>
</button>

{showParticipants && (
<div className='border mt-5 border-white rounded-full px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'>
<div className='border mt-5 border-white rounded-[8px] px-10 lg:px-5 py-2 flex justify-between items-center backdrop-blur-sm bg-[#D1ECFF]/20 w-fit'>
<div className='flex flex-col'>
{participants ? (
participants.addresses.map((address, index) => (
<div key={index}>
{address} - {weiToEther(participants.amounts[index])} ETH
<div className="py-2" key={index}>
{address.substring(0, 6)}...{address.substring(address.length - 3)} - {weiToEther(participants.amounts[index])} ETH
</div>
))
) : (
Expand All @@ -77,11 +79,18 @@ const BountyMultiplayer = ({ bountyId }: { bountyId: string }) => {
</div>
<div>

<JoinBounty bountyId={bountyId} />

{isOwner ?
<CancelOpenBounty bountyId={bountyId} />
: null
}
</div>
<div>
{isCurrentUserAParticipant ? <Withdraw bountyId={bountyId}/> : <JoinBounty bountyId={bountyId} /> }
{isCurrentUserAParticipant && !isBountyClaimed ? <Withdraw bountyId={bountyId}/> : null}
</div>

<div>
{!isCurrentUserAParticipant && !isBountyClaimed ? <JoinBounty bountyId={bountyId} /> : null }
</div>

</>
Expand Down
Loading
Loading