Skip to content

Commit

Permalink
fixed up imports, replaced proof with claims, created getChain Hook c…
Browse files Browse the repository at this point in the history
…leaned up types
  • Loading branch information
ie2173 committed Aug 22, 2024
1 parent 4f4d410 commit d0293d6
Show file tree
Hide file tree
Showing 40 changed files with 305 additions and 272 deletions.
39 changes: 30 additions & 9 deletions src/app/context/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { ethers } from 'ethers';
import { useRouter } from 'next/navigation';
import React, { createContext, ReactNode, useEffect, useState } from 'react';
import { EIP1193Provider } from 'viem';

interface WalletContextType {
walletAddress: string | null;
Expand All @@ -21,19 +22,39 @@ const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
const { primaryWallet, network } = useDynamicContext();
const router = useRouter();

// const walletConnection = async (): Promise<void> => {
// if ((window as any).ethereum === undefined) {
// setWalletAddress('No wallet');
// return;
// }
// try {
// const provider = new ethers.BrowserProvider((window as any).ethereum);
// const accounts = await provider.send('eth_requestAccounts', []);
// const account = accounts[0];
// setWalletAddress(account);
// } catch (error) {
// console.log(error);
// }
// };
// !-Check for breaking changes
const walletConnection = async (): Promise<void> => {
if ((window as any).ethereum === undefined) {
const ethereum = window.ethereum as EIP1193Provider | undefined;
// Teat to make sure non-breaking changes
if (ethereum) {
try {
const provider = new ethers.BrowserProvider(
window.ethereum as EIP1193Provider
);
const accounts = await provider.send('eth_requestAccounts', []);
const account = accounts[0];
setWalletAddress(account);
} catch (error) {
console.log(error);
}
} else {
setWalletAddress('No wallet');
return;
}
try {
const provider = new ethers.BrowserProvider((window as any).ethereum);
const accounts = await provider.send('eth_requestAccounts', []);
const account = accounts[0];
setWalletAddress(account);
} catch (error) {
console.log(error);
}
};

// const connectedWallet = () => {
Expand Down
1 change: 0 additions & 1 deletion src/app/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { walletClient as WalletClient } from './client';
export { chains as chains, networks as Networks } from './config';
export { default as ContextProvider } from './ContextProvider';
export {
Expand Down
134 changes: 65 additions & 69 deletions src/app/context/web3.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable no-console */
/* eslint-disable unused-imports/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Contract, ethers } from 'ethers';
import { Address, isAddress } from 'viem';
import { isAddress } from 'viem';

import { publicClient } from '@/lib/publicClient';
import { publicClient } from '@/lib';
import chainStatusStore from '@/store/chainStatus.store';
import { ABI, DEGENNAMERESABI, NFTABI } from '@/constant';

Expand Down Expand Up @@ -157,7 +154,7 @@ export const createOpenBounty: CreateBountyFunction = async (
const contract = await getContract(signer);

const options = {
value: ethers.parseEther(value.toString()),
value: ethers.parseEther(value),
};

const transaction = await contract.createOpenBounty(
Expand Down Expand Up @@ -345,23 +342,23 @@ export const fetchBounties: FetchBountiesFunction = async (
}

while (bountiesLength < count && offset < totalBounties) {
const rawBounties = (await contractRead.getBounties(offset)) as Bounty[];
const rawBounties = await contractRead.getBounties(offset);
const bountiesPromise = rawBounties
.map((bounty) => ({
id: bounty.id,
issuer: bounty.issuer,
name: bounty.name,
description: bounty.description,
amount: bounty.amount,
claimer: bounty.claimer,
createdAt: bounty.createdAt,
claimId: bounty.claimId,
.map((bounty: string[] | bigint[]) => ({
id: bounty[0].toString(),
issuer: bounty[1].toString(),
name: bounty[2].toString(),
description: bounty[3].toString(),
amount: bounty[4].toString(),
claimer: bounty[5].toString(),
createdAt: BigInt(bounty[6]),
claimId: bounty[7].toString(),
}))
.filter(
(bounty) =>
(bounty: Bounty) =>
bounty.issuer !== '0x0000000000000000000000000000000000000000'
)
.map(async (bounty) => {
.map(async (bounty: Bounty) => {
const claim = await contractRead.bountyCurrentVotingClaim(bounty.id);
const participants = await contractRead.getParticipants(bounty.id);
const isMultiplayer = participants[0].length > 0;
Expand Down Expand Up @@ -461,23 +458,23 @@ export const fetchAllBounties: GetAllBountiesFunction = async () => {
offset >= 0;
offset -= 10
) {
const rawBounties = (await contractRead.getBounties(offset)) as Bounty[];
const rawBounties = await contractRead.getBounties(offset);
const bountiesPromise = rawBounties
.map((bounty) => ({
id: bounty.id,
issuer: bounty.issuer,
name: bounty.name,
description: bounty.description,
amount: bounty.amount,
claimer: bounty.claimer,
createdAt: bounty.createdAt,
claimId: bounty.claimId,
.map((bounty: (string | bigint)[]) => ({
id: bounty[0].toString(),
issuer: bounty[1].toString(),
name: bounty[2].toString(),
description: bounty[3].toString(),
amount: bounty[4].toString(),
claimer: bounty[5].toString(),
createdAt: BigInt(bounty[6]),
claimId: bounty[7].toString(),
}))
.filter(
(bounty) =>
(bounty: Bounty) =>
bounty.issuer !== '0x0000000000000000000000000000000000000000'
)
.map(async (bounty) => {
.map(async (bounty: Bounty) => {
const claim = await contractRead.bountyCurrentVotingClaim(bounty.id);
const participants = await contractRead.getParticipants(bounty.id);
const isMultiplayer = participants[0].length > 0;
Expand Down Expand Up @@ -553,16 +550,18 @@ export const bountyVotingTracker: BountyVotingTrackerFunction = async (id) => {
export const getClaimsByUser: GetClaimsByUserFunction = async (user) => {
const contractRead = await getContractRead();
const claimsRaw = await contractRead.getClaimsByUser(user);
const formattedClaims: Claim[] = claimsRaw.map((claim: any) => ({
id: claim[0].toString(),
issuer: claim[1],
bountyId: claim[2].toString(),
bountyIssuer: claim[3],
name: claim[4],
description: claim[5],
createdAt: claim[6],
accepted: claim[7],
}));
const formattedClaims: Claim[] = claimsRaw.map(
(claim: (string | bigint)[]) => ({
id: claim[0].toString(),
issuer: claim[1],
bountyId: claim[2].toString(),
bountyIssuer: claim[3],
name: claim[4],
description: claim[5],
createdAt: claim[6],
accepted: claim[7],
})
);

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

Expand All @@ -589,19 +588,16 @@ 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],
},
];

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;
};

Expand Down Expand Up @@ -656,20 +652,20 @@ export const getDegenOrEnsName = async (

/* Uses Multicall vis-a-vis Multicall3 to bunch contract calls */

export const getClaimsByMulticall: GetClaimsByMulticall = async (
bountyData
) => {
const MultiCallObejcts: MultiCallInput[] = bountyData.map((bounty) => {
return {
address: chainStatusStore.currentChain.contracts.mainContract as Address,
abi: ABI,
functionName: 'getClaimsByBountyId',
args: [bounty.id],
};
});
const results = (
await publicClient.multicall({ contracts: MultiCallObejcts })
).map((v) => v.result);
// export const getClaimsByMulticall: GetClaimsByMulticall = async (
// bountyData
// ) => {
// const MultiCallObejcts: MultiCallInput[] = bountyData.map((bounty) => {
// return {
// address: chainStatusStore.currentChain.contracts.mainContract as Address,
// abi: ABI,
// functionName: 'getClaimsByBountyId',
// args: [bounty.id],
// };
// });
// const results = (
// await publicClient.multicall({ contracts: MultiCallObejcts })
// ).map((v) => v.result);

return results;
};
// return results;
// };
2 changes: 1 addition & 1 deletion src/components/NextImage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Image, { ImageProps } from 'next/image';
import * as React from 'react';

import { cn } from '@/lib/utils';
import { cn } from '@/lib';

type NextImageProps = {
useSkeleton?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';

import { cn } from '@/lib/utils';
import { cn } from '@/lib';

type SkeletonProps = React.ComponentPropsWithoutRef<'div'>;

Expand Down
53 changes: 28 additions & 25 deletions src/components/account/AccountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ethers } from 'ethers';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';

import { NftList, ProofListAccount } from '@/components/bounty';
import { ClaimsListAccount, NftList } from '@/components/bounty';
import { FilterButton } from '@/components/ui';
import { BountyList } from '@/components/ui';
import {
Expand All @@ -23,7 +23,13 @@ import {
getSigner,
getURI,
} from '@/app/context/web3';
import { BountiesData, ClaimsData, NFTDetails } from '@/types/web3';
import {
BountiesData,
Bounty,
Claim,
ClaimsData,
NFTDetails,
} from '@/types/web3';

const AccountInfo = () => {
const { isAuthenticated, primaryWallet, network } = useDynamicContext();
Expand Down Expand Up @@ -84,23 +90,20 @@ const AccountInfo = () => {
if ((pathname.split('/').pop() || '') !== '') {
const userInformation2 = async () => {
const address = pathname.split('/').pop() || '';

// !- Check for breaking change here with type enforcement
const balanceNFT = await getNftsOfOwner(address);
const nftDetailsPromises = balanceNFT.map(async (nftId) => {
const uri = await getURI(nftId);
const response = await fetch(uri);
const data = await response.json();
const claims = await getClaimById(nftId);
if (claims.length > 0) {
const { name, description } = claims[0];
return {
name: data?.name,
description: data?.description,
nftId: claims[0].id,
uri: data?.image,
} as NFTDetails;
}
return null;

return {
name: claims.name,
description: claims.description,
nftId: claims.id,
uri: data?.image,
} as NFTDetails;
});

const completedNFTs = (await Promise.all(nftDetailsPromises)).filter(
Expand All @@ -116,29 +119,29 @@ const AccountInfo = () => {
// console.log('degenOrEnsName', degenOrEnsName);

setUserAddress(formattedAddress);

getBountiesByUser(address, 0, []).then((data: any) => {
// !- Check for Breaking Changes Here with type enforcement
getBountiesByUser(address, 0, []).then((data: Bounty[]) => {
setBountiesData(data);
const completedBounties = data.filter(
(bounty: any) =>
bounty.claimer !== '0x0000000000000000000000000000000000000000' &&
bounty.claimer.toLowerCase() !== address.toLowerCase()
(bounty: Bounty) =>
bounty.issuer !== '0x0000000000000000000000000000000000000000' &&
bounty.issuer.toLowerCase() !== address.toLowerCase()
);
const inProgressBounties = data.filter(
(bounty: any) =>
bounty.claimer === '0x0000000000000000000000000000000000000000'
(bounty: Bounty) =>
bounty.issuer === '0x0000000000000000000000000000000000000000'
);
setInProgressBounties(inProgressBounties);
console.log('in progress bounties:');
console.log(inProgressBounties);

setCompletedBounties(completedBounties);
});

getClaimsByUser(address).then((data: any) => {
// !- Check for Breaking Changes Here with type enforcement
getClaimsByUser(address).then((data) => {
setClaimsData(data);
const completedClaims = data.filter(
(claim: any) => claim.accepted === true
(claim: Claim) => claim.accepted === true
);
const submitedClaims = data;
setCompletedClaims(completedClaims);
Expand Down Expand Up @@ -318,7 +321,7 @@ const AccountInfo = () => {
)}
{currentSection === 'c' && (
<div>
<ProofListAccount youOwner={true} data={submitedClaims} />
<ClaimsListAccount youOwner={true} data={submitedClaims} />
</div>
)}
</div>
Expand Down Expand Up @@ -407,7 +410,7 @@ const AccountInfo = () => {
)}
{currentSection === 'c' && (
<div>
<ProofListAccount youOwner={true} data={submitedClaims} />
<ClaimsListAccount youOwner={true} data={submitedClaims} />
</div>
)}
</div>
Expand Down
Loading

0 comments on commit d0293d6

Please sign in to comment.